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);
}
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;
}
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);
}
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);
}
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;
}
Aggregations