Search in sources :

Example 1 with MultipartRequest

use of org.springframework.web.multipart.MultipartRequest in project com.revolsys.open by revolsys.

the class WebMethodHandler method requestParameter.

public static WebParameterHandler requestParameter(final WebAnnotationMethodHandlerAdapter adapter, final Parameter parameter, final Annotation annotation) {
    final RequestParam requestParam = (RequestParam) annotation;
    final String name = getName(parameter, requestParam.value());
    final boolean required = requestParam.required();
    final String defaultValueString = requestParam.defaultValue();
    final Class<?> parameterClass = parameter.getType();
    final DataType dataType = DataTypes.getDataType(parameterClass);
    BiFunction<HttpServletRequest, HttpServletResponse, Object> function;
    Object defaultValue = null;
    if (List.class.equals(parameterClass)) {
        if (ValueConstants.DEFAULT_NONE.equals(defaultValueString)) {
            final ParameterizedType parameterizedType = (ParameterizedType) parameter.getParameterizedType();
            final Type[] typeParameters = parameterizedType.getActualTypeArguments();
            final Type elementType = typeParameters[0];
            if (MultipartFile.class.equals(elementType)) {
                function = (request, response) -> {
                    if (request instanceof MultipartRequest) {
                        final MultipartRequest multipartRequest = (MultipartRequest) request;
                        return multipartRequest.getFiles(name);
                    } else {
                        return Collections.emptyList();
                    }
                };
            } else {
                final DataType elementDataType = DataTypes.getDataType(elementType);
                function = (request, response) -> {
                    final List<Object> list = new ArrayList<>();
                    final String[] parameterValues = request.getParameterValues(name);
                    if (parameterValues != null) {
                        for (final String stringValue : parameterValues) {
                            final Object value = elementDataType.toObject(stringValue);
                            list.add(value);
                        }
                    }
                    return list;
                };
            }
        } else {
            throw new IllegalArgumentException("RequestParam.defaultValue not allowed for " + name);
        }
    } else if (parameterClass.isArray()) {
        if (ValueConstants.DEFAULT_NONE.equals(defaultValueString)) {
            final Class<?> elementClass = parameterClass.getComponentType();
            if (MultipartFile.class.equals(elementClass)) {
                function = (request, response) -> {
                    if (request instanceof MultipartRequest) {
                        final MultipartRequest multipartRequest = (MultipartRequest) request;
                        final List<MultipartFile> files = multipartRequest.getFiles(name);
                        return files.toArray();
                    } else {
                        return new MultipartFile[0];
                    }
                };
            } else {
                final DataType elementDataType = DataTypes.getDataType(elementClass);
                function = (request, response) -> {
                    final String[] parameterValues = request.getParameterValues(name);
                    int length;
                    if (parameterValues == null) {
                        length = 0;
                    } else {
                        length = parameterValues.length;
                    }
                    final Object array = Array.newInstance(elementClass, length);
                    for (int i = 0; i < length; i++) {
                        final String stringValue = parameterValues[i];
                        final Object value = elementDataType.toObject(stringValue);
                        Array.set(array, i, value);
                    }
                    return array;
                };
            }
        } else {
            throw new IllegalArgumentException("RequestParam.defaultValue not allowed for " + name);
        }
    } else {
        defaultValue = parseDefaultValueAttribute(dataType, defaultValueString);
        if (MultipartFile.class.equals(parameterClass)) {
            function = (request, response) -> {
                if (request instanceof MultipartRequest) {
                    final MultipartRequest multipartRequest = (MultipartRequest) request;
                    return multipartRequest.getFile(name);
                } else {
                    return null;
                }
            };
        } else {
            function = (request, response) -> {
                return request.getParameter(name);
            };
        }
    }
    return // 
    WebParameterHandler.function(// 
    name, // 
    function, // 
    dataType, // 
    required, // 
    defaultValue);
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) WrappedException(com.revolsys.util.WrappedException) RequestParam(org.springframework.web.bind.annotation.RequestParam) Array(java.lang.reflect.Array) URLDecoder(java.net.URLDecoder) BiFunction(java.util.function.BiFunction) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) Property(com.revolsys.util.Property) CookieValue(org.springframework.web.bind.annotation.CookieValue) RequestContextUtils(org.springframework.web.servlet.support.RequestContextUtils) ByteArrayInputStream(java.io.ByteArrayInputStream) Locale(java.util.Locale) Map(java.util.Map) MultipartRequest(org.springframework.web.multipart.MultipartRequest) Method(java.lang.reflect.Method) HttpSession(javax.servlet.http.HttpSession) WebUtils(org.springframework.web.util.WebUtils) ClassUtils(org.springframework.util.ClassUtils) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) StandardCharsets(java.nio.charset.StandardCharsets) Logs(com.revolsys.logging.Logs) List(java.util.List) Principal(java.security.Principal) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) HandlerMapping(org.springframework.web.servlet.HandlerMapping) Type(java.lang.reflect.Type) ServletResponse(javax.servlet.ServletResponse) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Annotation(java.lang.annotation.Annotation) RequestHeader(org.springframework.web.bind.annotation.RequestHeader) FileCopyUtils(org.springframework.util.FileCopyUtils) Exceptions(com.revolsys.util.Exceptions) HashMap(java.util.HashMap) HttpServletUtils(com.revolsys.ui.web.utils.HttpServletUtils) ArrayList(java.util.ArrayList) RequestBody(org.springframework.web.bind.annotation.RequestBody) HttpServletRequest(javax.servlet.http.HttpServletRequest) Charset(java.nio.charset.Charset) Parameter(java.lang.reflect.Parameter) Cookie(javax.servlet.http.Cookie) OutputStream(java.io.OutputStream) ServletRequest(javax.servlet.ServletRequest) RequestAttribute(com.revolsys.ui.web.annotation.RequestAttribute) HttpServletResponse(javax.servlet.http.HttpServletResponse) MultiValueMap(org.springframework.util.MultiValueMap) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) DataTypes(com.revolsys.datatype.DataTypes) Reader(com.revolsys.io.Reader) HttpMediaTypeNotSupportedException(org.springframework.web.HttpMediaTypeNotSupportedException) Function3(com.revolsys.util.function.Function3) HttpInputMessage(org.springframework.http.HttpInputMessage) ParameterizedType(java.lang.reflect.ParameterizedType) MultipartFile(org.springframework.web.multipart.MultipartFile) Writer(com.revolsys.io.Writer) ValueConstants(org.springframework.web.bind.annotation.ValueConstants) DataType(com.revolsys.datatype.DataType) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) InputStream(java.io.InputStream) StringUtils(org.springframework.util.StringUtils) RequestParam(org.springframework.web.bind.annotation.RequestParam) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ParameterizedType(java.lang.reflect.ParameterizedType) MediaType(org.springframework.http.MediaType) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) DataType(com.revolsys.datatype.DataType) MultipartFile(org.springframework.web.multipart.MultipartFile) DataType(com.revolsys.datatype.DataType) List(java.util.List) ArrayList(java.util.ArrayList) MultipartRequest(org.springframework.web.multipart.MultipartRequest)

Example 2 with MultipartRequest

use of org.springframework.web.multipart.MultipartRequest in project spring-framework by spring-projects.

the class MultipartResolutionDelegate method resolveMultipartRequest.

@Nullable
public static MultipartRequest resolveMultipartRequest(NativeWebRequest webRequest) {
    MultipartRequest multipartRequest = webRequest.getNativeRequest(MultipartRequest.class);
    if (multipartRequest != null) {
        return multipartRequest;
    }
    HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
    if (servletRequest != null && isMultipartContent(servletRequest)) {
        return new StandardMultipartHttpServletRequest(servletRequest);
    }
    return null;
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) MultipartRequest(org.springframework.web.multipart.MultipartRequest) Nullable(org.springframework.lang.Nullable)

Example 3 with MultipartRequest

use of org.springframework.web.multipart.MultipartRequest in project spring-framework by spring-projects.

the class WebRequestDataBinder method bind.

/**
	 * Bind the parameters of the given request to this binder's target,
	 * also binding multipart files in case of a multipart request.
	 * <p>This call can create field errors, representing basic binding
	 * errors like a required field (code "required"), or type mismatch
	 * between value and bean property (code "typeMismatch").
	 * <p>Multipart files are bound via their parameter name, just like normal
	 * HTTP parameters: i.e. "uploadedFile" to an "uploadedFile" bean property,
	 * invoking a "setUploadedFile" setter method.
	 * <p>The type of the target property for a multipart file can be Part, MultipartFile,
	 * byte[], or String. The latter two receive the contents of the uploaded file;
	 * all metadata like original file name, content type, etc are lost in those cases.
	 * @param request request with parameters to bind (can be multipart)
	 * @see org.springframework.web.multipart.MultipartRequest
	 * @see org.springframework.web.multipart.MultipartFile
	 * @see javax.servlet.http.Part
	 * @see #bind(org.springframework.beans.PropertyValues)
	 */
public void bind(WebRequest request) {
    MutablePropertyValues mpvs = new MutablePropertyValues(request.getParameterMap());
    if (isMultipartRequest(request) && request instanceof NativeWebRequest) {
        MultipartRequest multipartRequest = ((NativeWebRequest) request).getNativeRequest(MultipartRequest.class);
        if (multipartRequest != null) {
            bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
        } else {
            HttpServletRequest servletRequest = ((NativeWebRequest) request).getNativeRequest(HttpServletRequest.class);
            bindParts(servletRequest, mpvs);
        }
    }
    doBind(mpvs);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) MultipartRequest(org.springframework.web.multipart.MultipartRequest)

Example 4 with MultipartRequest

use of org.springframework.web.multipart.MultipartRequest in project spring-data-document-examples by spring-projects.

the class HandlerMethodInvoker method resolveRequestParam.

@SuppressWarnings("unchecked")
private Object resolveRequestParam(String paramName, boolean required, String defaultValue, MethodParameter methodParam, NativeWebRequest webRequest, Object handlerForInitBinderCall) throws Exception {
    Class<?> paramType = methodParam.getParameterType();
    if (Map.class.isAssignableFrom(paramType) && paramName.length() == 0) {
        return resolveRequestParamMap((Class<? extends Map>) paramType, webRequest);
    }
    if (paramName.length() == 0) {
        paramName = getRequiredParameterName(methodParam);
    }
    Object paramValue = null;
    MultipartRequest multipartRequest = webRequest.getNativeRequest(MultipartRequest.class);
    if (multipartRequest != null) {
        List<MultipartFile> files = multipartRequest.getFiles(paramName);
        if (!files.isEmpty()) {
            if (files.size() == 1 && !paramType.isArray() && !Collection.class.isAssignableFrom(paramType)) {
                paramValue = files.get(0);
            } else {
                paramValue = files;
            }
        }
    }
    if (paramValue == null) {
        String[] paramValues = webRequest.getParameterValues(paramName);
        if (paramValues != null) {
            if (paramValues.length == 1 && !paramType.isArray() && !Collection.class.isAssignableFrom(paramType)) {
                paramValue = paramValues[0];
            } else {
                paramValue = paramValues;
            }
        }
    }
    if (paramValue == null) {
        if (defaultValue != null) {
            paramValue = resolveDefaultValue(defaultValue);
        } else if (required) {
            raiseMissingParameterException(paramName, paramType);
        }
        paramValue = checkValue(paramName, paramValue, paramType);
    }
    WebDataBinder binder = createBinder(webRequest, null, paramName);
    initBinder(handlerForInitBinderCall, paramName, binder, webRequest);
    return binder.convertIfNecessary(paramValue, paramType, methodParam);
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) MultipartFile(org.springframework.web.multipart.MultipartFile) MultipartRequest(org.springframework.web.multipart.MultipartRequest) ExtendedModelMap(org.springframework.ui.ExtendedModelMap)

Example 5 with MultipartRequest

use of org.springframework.web.multipart.MultipartRequest in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolver method resolveName.

@Override
@Nullable
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
    HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class);
    if (servletRequest != null) {
        Object mpArg = MultipartResolutionDelegate.resolveMultipartArgument(name, parameter, servletRequest);
        if (mpArg != MultipartResolutionDelegate.UNRESOLVABLE) {
            return mpArg;
        }
    }
    Object arg = null;
    MultipartRequest multipartRequest = request.getNativeRequest(MultipartRequest.class);
    if (multipartRequest != null) {
        List<MultipartFile> files = multipartRequest.getFiles(name);
        if (!files.isEmpty()) {
            arg = (files.size() == 1 ? files.get(0) : files);
        }
    }
    if (arg == null) {
        String[] paramValues = request.getParameterValues(name);
        if (paramValues != null) {
            arg = (paramValues.length == 1 ? paramValues[0] : paramValues);
        }
    }
    return arg;
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MultipartFile(org.springframework.web.multipart.MultipartFile) MultipartRequest(org.springframework.web.multipart.MultipartRequest) Nullable(org.springframework.lang.Nullable)

Aggregations

MultipartRequest (org.springframework.web.multipart.MultipartRequest)6 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 MultipartFile (org.springframework.web.multipart.MultipartFile)2 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)2 DataType (com.revolsys.datatype.DataType)1 DataTypes (com.revolsys.datatype.DataTypes)1 Reader (com.revolsys.io.Reader)1 Writer (com.revolsys.io.Writer)1 Logs (com.revolsys.logging.Logs)1 RequestAttribute (com.revolsys.ui.web.annotation.RequestAttribute)1 HttpServletUtils (com.revolsys.ui.web.utils.HttpServletUtils)1 Exceptions (com.revolsys.util.Exceptions)1 Property (com.revolsys.util.Property)1 WrappedException (com.revolsys.util.WrappedException)1 Function3 (com.revolsys.util.function.Function3)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1