@Test public void testNativeRequest() { MockRenderRequest portletRequest = new MockRenderRequest(); MockRenderResponse portletResponse = new MockRenderResponse(); PortletWebRequest request = new PortletWebRequest(portletRequest, portletResponse); assertSame(portletRequest, request.getNativeRequest()); assertSame(portletRequest, request.getNativeRequest(PortletRequest.class)); assertSame(portletRequest, request.getNativeRequest(RenderRequest.class)); assertSame(portletRequest, request.getNativeRequest(MockRenderRequest.class)); assertNull(request.getNativeRequest(MultipartRequest.class)); assertSame(portletResponse, request.getNativeResponse()); assertSame(portletResponse, request.getNativeResponse(PortletResponse.class)); assertSame(portletResponse, request.getNativeResponse(RenderResponse.class)); assertSame(portletResponse, request.getNativeResponse(MockRenderResponse.class)); assertNull(request.getNativeResponse(MultipartRequest.class)); }
@Test public void testDecoratedNativeRequest() { MockRenderRequest portletRequest = new MockRenderRequest(); MockRenderResponse portletResponse = new MockRenderResponse(); PortletRequest decoratedRequest = new PortletRequestWrapper(portletRequest); PortletResponse decoratedResponse = new PortletResponseWrapper(portletResponse); PortletWebRequest request = new PortletWebRequest(decoratedRequest, decoratedResponse); assertSame(decoratedRequest, request.getNativeRequest()); assertSame(decoratedRequest, request.getNativeRequest(PortletRequest.class)); assertSame(portletRequest, request.getNativeRequest(RenderRequest.class)); assertSame(portletRequest, request.getNativeRequest(MockRenderRequest.class)); assertNull(request.getNativeRequest(MultipartRequest.class)); assertSame(decoratedResponse, request.getNativeResponse()); assertSame(decoratedResponse, request.getNativeResponse(PortletResponse.class)); assertSame(portletResponse, request.getNativeResponse(RenderResponse.class)); assertSame(portletResponse, request.getNativeResponse(MockRenderResponse.class)); assertNull(request.getNativeResponse(MultipartRequest.class)); }
@Test public void decoratedNativeRequest() { HttpServletRequest decoratedRequest = new HttpServletRequestWrapper(servletRequest); HttpServletResponse decoratedResponse = new HttpServletResponseWrapper(servletResponse); ServletWebRequest request = new ServletWebRequest(decoratedRequest, decoratedResponse); assertSame(decoratedRequest, request.getNativeRequest()); assertSame(decoratedRequest, request.getNativeRequest(ServletRequest.class)); assertSame(decoratedRequest, request.getNativeRequest(HttpServletRequest.class)); assertSame(servletRequest, request.getNativeRequest(MockHttpServletRequest.class)); assertNull(request.getNativeRequest(MultipartRequest.class)); assertSame(decoratedResponse, request.getNativeResponse()); assertSame(decoratedResponse, request.getNativeResponse(ServletResponse.class)); assertSame(decoratedResponse, request.getNativeResponse(HttpServletResponse.class)); assertSame(servletResponse, request.getNativeResponse(MockHttpServletResponse.class)); assertNull(request.getNativeResponse(MultipartRequest.class)); }
@PostMapping("file") public String upload(@RequestParam("file") MultipartFile file, MultipartRequest multipartRequest) { System.out.println(file); DefaultMultipartHttpServletRequest request = (DefaultMultipartHttpServletRequest) multipartRequest; String fileName = request.getParameter("fileName"); StringBuilder stringBuilder = new StringBuilder("fileName" + " : " + fileName); try { InputStream inputStream = file.getInputStream(); InputStreamReader in = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(in); bufferedReader.lines().forEach(stringBuilder::append); IOUtils.closeQuietly(inputStream, in, bufferedReader); } catch (IOException e) { e.printStackTrace(); } return stringBuilder.toString(); }
private void bind(ServletRequest request, ServletRequestDataBinder dataBinder) { MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request); MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class); if (multipartRequest != null) { bindMultipart(multipartRequest.getMultiFileMap(), mpvs); } String attr = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE; Map<String, String> uriVars = (Map<String, String>) request.getAttribute(attr); if (uriVars != null) { for (Map.Entry<String, String> entry : uriVars.entrySet()) { if (mpvs.contains(entry.getKey())) { logger.warn("Skipping URI variable '" + entry.getKey() + "' since the request contains a bind value with the same name."); } else { mpvs.addPropertyValue(entry.getKey(), entry.getValue()); } } } this.extendDataBinder.doExtendBind(mpvs, dataBinder); dataBinder.bind(mpvs); }
/** * @see org.openmrs.web.attribute.handler.WebDatatypeHandler#getValue(org.openmrs.customdatatype.CustomDatatype, javax.servlet.http.HttpServletRequest, java.lang.String) */ @Override public String getValue(LongFreeTextDatatype datatype, HttpServletRequest request, String formFieldName) throws InvalidCustomValueException { if (request instanceof MultipartRequest) { MultipartFile file = ((MultipartRequest) request).getFile(formFieldName); try { return new String(file.getBytes()); } catch (IOException e) { throw new InvalidCustomValueException("Error handling file upload as a String", e); } } else { throw new IllegalArgumentException( "Programming error: file upload handler can only be used in a form with enctype='multipart/form-data'"); } }
public void doValidate(HttpServletRequest request, HttpServletResponse response, HandlerMethod handler) { UploadFileValidator validator = handler.getMethodAnnotation(UploadFileValidator.class); MultipartRequest mrequest = (MultipartRequest) request; // Collection<List<MultipartFile>> files = mrequest.getMultiFileMap().values(); /*for(List<MultipartFile> mfiles : files){ checkFileTypes(mfiles, allowed, validator.errorPostfixMessage()); }*/ List<MultipartFile> files = mrequest.getMultiFileMap().values() .stream() .flatMap(list->list.stream()) .filter(f->StringUtils.isNotBlank(f.getOriginalFilename())) .collect(Collectors.toList()); if(files.isEmpty() && validator!=null && !validator.nullable()){ throw new ServiceException(validator.nullableErrorMessage()); } this.checkFileTypes(files, validator); }
@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()) { paramValue = (files.size() == 1 ? files.get(0) : files); } } if (paramValue == null) { String[] paramValues = webRequest.getParameterValues(paramName); if (paramValues != null) { paramValue = (paramValues.length == 1 ? paramValues[0] : 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); }
@Override public boolean supportsParameter(MethodParameter parameter) { Class<?> paramType = parameter.getParameterType(); return (WebRequest.class.isAssignableFrom(paramType) || ServletRequest.class.isAssignableFrom(paramType) || MultipartRequest.class.isAssignableFrom(paramType) || HttpSession.class.isAssignableFrom(paramType) || Principal.class.isAssignableFrom(paramType) || Locale.class == paramType || TimeZone.class == paramType || "java.time.ZoneId".equals(paramType.getName()) || InputStream.class.isAssignableFrom(paramType) || Reader.class.isAssignableFrom(paramType) || HttpMethod.class == paramType); }
@Before public void setUp() throws Exception { method = getClass().getMethod("supportedParams", ServletRequest.class, MultipartRequest.class, HttpSession.class, Principal.class, Locale.class, InputStream.class, Reader.class, WebRequest.class, TimeZone.class, ZoneId.class, HttpMethod.class); mavContainer = new ModelAndViewContainer(); servletRequest = new MockHttpServletRequest("GET", ""); webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse()); }
@SuppressWarnings("unused") public void supportedParams(ServletRequest p0, MultipartRequest p1, HttpSession p2, Principal p3, Locale p4, InputStream p5, Reader p6, WebRequest p7, TimeZone p8, ZoneId p9, HttpMethod p10) { }
@Test public void nativeRequest() { assertSame(servletRequest, request.getNativeRequest()); assertSame(servletRequest, request.getNativeRequest(ServletRequest.class)); assertSame(servletRequest, request.getNativeRequest(HttpServletRequest.class)); assertSame(servletRequest, request.getNativeRequest(MockHttpServletRequest.class)); assertNull(request.getNativeRequest(MultipartRequest.class)); assertSame(servletResponse, request.getNativeResponse()); assertSame(servletResponse, request.getNativeResponse(ServletResponse.class)); assertSame(servletResponse, request.getNativeResponse(HttpServletResponse.class)); assertSame(servletResponse, request.getNativeResponse(MockHttpServletResponse.class)); assertNull(request.getNativeResponse(MultipartRequest.class)); }
@Override public void decode(FacesContext context, UIComponent component) { rendererParamsNotNull(context, component); if (!shouldDecode(component)) { return; } String clientId = decodeBehaviors(context, component); if (null == clientId) { clientId = component.getClientId(context); } MultipartRequest multiReq = (MultipartRequest) context.getExternalContext().getRequest(); MultipartFile file = multiReq.getFile(clientId); Object submittedValue = (null != file) ? file : EMPTY_VALUE; ((UIInput) component).setSubmittedValue(submittedValue); }
/** * 保存multipartRequest包含的全部文件 * @param multipartRequest MultipartRequest请求对象 * @param strategy 文件保存使用的策略对象 * @throws IOException */ public static void saveAllFiles(MultipartRequest multipartRequest, SavingStrategy strategy) throws IOException { MultiValueMap<String, MultipartFile> fileMap = multipartRequest.getMultiFileMap(); for (Iterator<List<MultipartFile>> it = fileMap.values().iterator(); it.hasNext(); ) { List<MultipartFile> fileList = it.next(); for (MultipartFile file : fileList) { saveFile(file, strategy.getTargetFile(file)); } } }
public void bind(ServletRequest request, ServletRequestDataBinder binder) { Map<String, ?> values = parseValues(request, binder); MutablePropertyValues mpvs = new MutablePropertyValues(values); MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class); if (multipartRequest != null) { bindMultipart(multipartRequest.getMultiFileMap(), mpvs); } // two lines copied from ExtendedServletRequestDataBinder String attr = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE; mpvs.addPropertyValues((Map<String, String>) request.getAttribute(attr)); binder.bind(mpvs); }
public boolean supportsParameter(MethodParameter parameter) { Class<?> paramType = parameter.getParameterType(); return WebRequest.class.isAssignableFrom(paramType) || ServletRequest.class.isAssignableFrom(paramType) || MultipartRequest.class.isAssignableFrom(paramType) || HttpSession.class.isAssignableFrom(paramType) || Principal.class.isAssignableFrom(paramType) || Locale.class.equals(paramType) || InputStream.class.isAssignableFrom(paramType) || Reader.class.isAssignableFrom(paramType); }
public Object resolveArgument( MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws IOException { Class<?> paramType = parameter.getParameterType(); if (WebRequest.class.isAssignableFrom(paramType)) { return webRequest; } HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); if (ServletRequest.class.isAssignableFrom(paramType) || MultipartRequest.class.isAssignableFrom(paramType)) { Object nativeRequest = webRequest.getNativeRequest(paramType); if (nativeRequest == null) { throw new IllegalStateException( "Current request is not of type [" + paramType.getName() + "]: " + request); } return nativeRequest; } else if (HttpSession.class.isAssignableFrom(paramType)) { return request.getSession(); } else if (Principal.class.isAssignableFrom(paramType)) { return request.getUserPrincipal(); } else if (Locale.class.equals(paramType)) { return RequestContextUtils.getLocale(request); } else if (InputStream.class.isAssignableFrom(paramType)) { return request.getInputStream(); } else if (Reader.class.isAssignableFrom(paramType)) { return request.getReader(); } else { // should never happen... throw new UnsupportedOperationException( "Unknown parameter type: " + paramType + " in method: " + parameter.getMethod()); } }
@Before public void setUp() throws Exception { method = getClass().getMethod("supportedParams", ServletRequest.class, MultipartRequest.class, HttpSession.class, Principal.class, Locale.class, InputStream.class, Reader.class, WebRequest.class); mavContainer = new ModelAndViewContainer(); servletRequest = new MockHttpServletRequest(); webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse()); }
public void supportedParams(ServletRequest p0, MultipartRequest p1, HttpSession p2, Principal p3, Locale p4, InputStream p5, Reader p6, WebRequest p7) { }
@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()) { paramValue = (files.size() == 1 ? files.get(0) : files); } } if (paramValue == null) { String[] paramValues = webRequest.getParameterValues(paramName); if (paramValues != null) { paramValue = (paramValues.length == 1 ? paramValues[0] : 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); }
public MultipartFile getMultipartFile(HttpServletRequest request, String fileInputName){ MultipartRequest multipartRequest = (MultipartRequest) request; if(Strings.isNullOrEmpty(fileInputName)){ Map<String, MultipartFile> map = multipartRequest.getFileMap(); for(String k: map.keySet()){ return map.get(k); } return null; }else{ MultipartFile file = multipartRequest.getFile(fileInputName); return file; } }
/** * 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 if (ClassUtils.hasMethod(HttpServletRequest.class, "getParts")) { HttpServletRequest serlvetRequest = ((NativeWebRequest) request).getNativeRequest(HttpServletRequest.class); new Servlet3MultipartHelper(isBindEmptyMultipartFiles()).bindParts(serlvetRequest, mpvs); } } doBind(mpvs); }
@Override protected Object resolveStandardArgument(Class<?> parameterType, NativeWebRequest webRequest) throws Exception { PortletRequest request = webRequest.getNativeRequest(PortletRequest.class); PortletResponse response = webRequest.getNativeResponse(PortletResponse.class); if (PortletRequest.class.isAssignableFrom(parameterType) || MultipartRequest.class.isAssignableFrom(parameterType)) { Object nativeRequest = webRequest.getNativeRequest(parameterType); if (nativeRequest == null) { throw new IllegalStateException( "Current request is not of type [" + parameterType.getName() + "]: " + request); } return nativeRequest; } else if (PortletResponse.class.isAssignableFrom(parameterType)) { Object nativeResponse = webRequest.getNativeResponse(parameterType); if (nativeResponse == null) { throw new IllegalStateException( "Current response is not of type [" + parameterType.getName() + "]: " + response); } return nativeResponse; } else if (PortletSession.class.isAssignableFrom(parameterType)) { return request.getPortletSession(); } else if (PortletPreferences.class.isAssignableFrom(parameterType)) { return request.getPreferences(); } else if (PortletMode.class.isAssignableFrom(parameterType)) { return request.getPortletMode(); } else if (WindowState.class.isAssignableFrom(parameterType)) { return request.getWindowState(); } else if (PortalContext.class.isAssignableFrom(parameterType)) { return request.getPortalContext(); } else if (Principal.class.isAssignableFrom(parameterType)) { return request.getUserPrincipal(); } else if (Locale.class == parameterType) { return request.getLocale(); } else if (InputStream.class.isAssignableFrom(parameterType)) { if (!(request instanceof ClientDataRequest)) { throw new IllegalStateException("InputStream can only get obtained for Action/ResourceRequest"); } return ((ClientDataRequest) request).getPortletInputStream(); } else if (Reader.class.isAssignableFrom(parameterType)) { if (!(request instanceof ClientDataRequest)) { throw new IllegalStateException("Reader can only get obtained for Action/ResourceRequest"); } return ((ClientDataRequest) request).getReader(); } else if (OutputStream.class.isAssignableFrom(parameterType)) { if (!(response instanceof MimeResponse)) { throw new IllegalStateException("OutputStream can only get obtained for Render/ResourceResponse"); } return ((MimeResponse) response).getPortletOutputStream(); } else if (Writer.class.isAssignableFrom(parameterType)) { if (!(response instanceof MimeResponse)) { throw new IllegalStateException("Writer can only get obtained for Render/ResourceResponse"); } return ((MimeResponse) response).getWriter(); } else if (Event.class == parameterType) { if (!(request instanceof EventRequest)) { throw new IllegalStateException("Event can only get obtained from EventRequest"); } return ((EventRequest) request).getEvent(); } return super.resolveStandardArgument(parameterType, webRequest); }
@Override protected Object resolveStandardArgument(Class<?> parameterType, NativeWebRequest webRequest) throws Exception { HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class); if (ServletRequest.class.isAssignableFrom(parameterType) || MultipartRequest.class.isAssignableFrom(parameterType)) { Object nativeRequest = webRequest.getNativeRequest(parameterType); if (nativeRequest == null) { throw new IllegalStateException( "Current request is not of type [" + parameterType.getName() + "]: " + request); } return nativeRequest; } else if (ServletResponse.class.isAssignableFrom(parameterType)) { this.responseArgumentUsed = true; Object nativeResponse = webRequest.getNativeResponse(parameterType); if (nativeResponse == null) { throw new IllegalStateException( "Current response is not of type [" + parameterType.getName() + "]: " + response); } return nativeResponse; } else if (HttpSession.class.isAssignableFrom(parameterType)) { return request.getSession(); } else if (Principal.class.isAssignableFrom(parameterType)) { return request.getUserPrincipal(); } else if (Locale.class == parameterType) { return RequestContextUtils.getLocale(request); } else if (InputStream.class.isAssignableFrom(parameterType)) { return request.getInputStream(); } else if (Reader.class.isAssignableFrom(parameterType)) { return request.getReader(); } else if (OutputStream.class.isAssignableFrom(parameterType)) { this.responseArgumentUsed = true; return response.getOutputStream(); } else if (Writer.class.isAssignableFrom(parameterType)) { this.responseArgumentUsed = true; return response.getWriter(); } return super.resolveStandardArgument(parameterType, webRequest); }
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { Class<?> paramType = parameter.getParameterType(); if (WebRequest.class.isAssignableFrom(paramType)) { return webRequest; } HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); if (ServletRequest.class.isAssignableFrom(paramType) || MultipartRequest.class.isAssignableFrom(paramType)) { Object nativeRequest = webRequest.getNativeRequest(paramType); if (nativeRequest == null) { throw new IllegalStateException( "Current request is not of type [" + paramType.getName() + "]: " + request); } return nativeRequest; } else if (HttpSession.class.isAssignableFrom(paramType)) { return request.getSession(); } else if (HttpMethod.class == paramType) { return ((ServletWebRequest) webRequest).getHttpMethod(); } else if (Principal.class.isAssignableFrom(paramType)) { return request.getUserPrincipal(); } else if (Locale.class == paramType) { return RequestContextUtils.getLocale(request); } else if (TimeZone.class == paramType) { TimeZone timeZone = RequestContextUtils.getTimeZone(request); return (timeZone != null ? timeZone : TimeZone.getDefault()); } else if ("java.time.ZoneId".equals(paramType.getName())) { return ZoneIdResolver.resolveZoneId(request); } else if (InputStream.class.isAssignableFrom(paramType)) { return request.getInputStream(); } else if (Reader.class.isAssignableFrom(paramType)) { return request.getReader(); } else { // should never happen... throw new UnsupportedOperationException( "Unknown parameter type: " + paramType + " in method: " + parameter.getMethod()); } }
private synchronized void buildContentData() { try { MultiValueMap<String, Object> builder = new LinkedMultiValueMap<String, Object>(); Set<String> queryParams = findQueryParams(); for (Map.Entry<String, String[]> entry : this.request.getParameterMap() .entrySet()) { if (!queryParams.contains(entry.getKey())) { for (String value : entry.getValue()) { builder.add(entry.getKey(), value); } } } if (this.request instanceof MultipartRequest) { MultipartRequest multi = (MultipartRequest) this.request; for (Map.Entry<String, List<MultipartFile>> parts : multi .getMultiFileMap().entrySet()) { for (Part file : this.request.getParts()) { HttpHeaders headers = new HttpHeaders(); headers.setContentDispositionFormData(file.getName(), file.getSubmittedFileName()); if (file.getContentType() != null) { headers.setContentType( MediaType.valueOf(file.getContentType())); } HttpEntity<Resource> entity = new HttpEntity<Resource>( new InputStreamResource(file.getInputStream()), headers); builder.add(parts.getKey(), entity); } } } FormHttpOutputMessage data = new FormHttpOutputMessage(); this.contentType = MediaType.valueOf(this.request.getContentType()); data.getHeaders().setContentType(this.contentType); this.converter.write(builder, this.contentType, data); // copy new content type including multipart boundary this.contentType = data.getHeaders().getContentType(); this.contentData = data.getInput(); this.contentLength = this.contentData.length; } catch (Exception e) { throw new IllegalStateException("Cannot convert form data", e); } }
public boolean isSupport(HttpServletRequest request, HandlerMethod handler) { return MultipartRequest.class.isInstance(request); // return MultipartRequest.class.isInstance(request) && handler.getMethodAnnotation(UploadFileValidator.class)!=null; }
@Override protected Object resolveStandardArgument(Class<?> parameterType, NativeWebRequest webRequest) throws Exception { PortletRequest request = webRequest.getNativeRequest(PortletRequest.class); PortletResponse response = webRequest.getNativeResponse(PortletResponse.class); if (PortletRequest.class.isAssignableFrom(parameterType) || MultipartRequest.class.isAssignableFrom(parameterType)) { Object nativeRequest = webRequest.getNativeRequest(parameterType); if (nativeRequest == null) { throw new IllegalStateException( "Current request is not of type [" + parameterType.getName() + "]: " + request); } return nativeRequest; } else if (PortletResponse.class.isAssignableFrom(parameterType)) { Object nativeResponse = webRequest.getNativeResponse(parameterType); if (nativeResponse == null) { throw new IllegalStateException( "Current response is not of type [" + parameterType.getName() + "]: " + response); } return nativeResponse; } else if (PortletSession.class.isAssignableFrom(parameterType)) { return request.getPortletSession(); } else if (PortletPreferences.class.isAssignableFrom(parameterType)) { return request.getPreferences(); } else if (PortletMode.class.isAssignableFrom(parameterType)) { return request.getPortletMode(); } else if (WindowState.class.isAssignableFrom(parameterType)) { return request.getWindowState(); } else if (PortalContext.class.isAssignableFrom(parameterType)) { return request.getPortalContext(); } else if (Principal.class.isAssignableFrom(parameterType)) { return request.getUserPrincipal(); } else if (Locale.class.equals(parameterType)) { return request.getLocale(); } else if (InputStream.class.isAssignableFrom(parameterType)) { if (!(request instanceof ClientDataRequest)) { throw new IllegalStateException("InputStream can only get obtained for Action/ResourceRequest"); } return ((ClientDataRequest) request).getPortletInputStream(); } else if (Reader.class.isAssignableFrom(parameterType)) { if (!(request instanceof ClientDataRequest)) { throw new IllegalStateException("Reader can only get obtained for Action/ResourceRequest"); } return ((ClientDataRequest) request).getReader(); } else if (OutputStream.class.isAssignableFrom(parameterType)) { if (!(response instanceof MimeResponse)) { throw new IllegalStateException("OutputStream can only get obtained for Render/ResourceResponse"); } return ((MimeResponse) response).getPortletOutputStream(); } else if (Writer.class.isAssignableFrom(parameterType)) { if (!(response instanceof MimeResponse)) { throw new IllegalStateException("Writer can only get obtained for Render/ResourceResponse"); } return ((MimeResponse) response).getWriter(); } else if (Event.class.equals(parameterType)) { if (!(request instanceof EventRequest)) { throw new IllegalStateException("Event can only get obtained from EventRequest"); } return ((EventRequest) request).getEvent(); } return super.resolveStandardArgument(parameterType, webRequest); }
@Override protected Object resolveStandardArgument(Class<?> parameterType, NativeWebRequest webRequest) throws Exception { HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class); if (ServletRequest.class.isAssignableFrom(parameterType) || MultipartRequest.class.isAssignableFrom(parameterType)) { Object nativeRequest = webRequest.getNativeRequest(parameterType); if (nativeRequest == null) { throw new IllegalStateException( "Current request is not of type [" + parameterType.getName() + "]: " + request); } return nativeRequest; } else if (ServletResponse.class.isAssignableFrom(parameterType)) { this.responseArgumentUsed = true; Object nativeResponse = webRequest.getNativeResponse(parameterType); if (nativeResponse == null) { throw new IllegalStateException( "Current response is not of type [" + parameterType.getName() + "]: " + response); } return nativeResponse; } else if (HttpSession.class.isAssignableFrom(parameterType)) { return request.getSession(); } else if (Principal.class.isAssignableFrom(parameterType)) { return request.getUserPrincipal(); } else if (Locale.class.equals(parameterType)) { return RequestContextUtils.getLocale(request); } else if (InputStream.class.isAssignableFrom(parameterType)) { return request.getInputStream(); } else if (Reader.class.isAssignableFrom(parameterType)) { return request.getReader(); } else if (OutputStream.class.isAssignableFrom(parameterType)) { this.responseArgumentUsed = true; return response.getOutputStream(); } else if (Writer.class.isAssignableFrom(parameterType)) { this.responseArgumentUsed = true; return response.getWriter(); } return super.resolveStandardArgument(parameterType, webRequest); }
public static MultipartFile getMultipartFile(HttpServletRequest request, String fileInputName){ MultipartRequest multipartRequest = (MultipartRequest) request; MultipartFile file = multipartRequest.getFile(fileInputName); return file; }
@Override protected Object resolveStandardArgument(Class<?> parameterType, NativeWebRequest webRequest) throws Exception { HttpServletRequest request = webRequest .getNativeRequest(HttpServletRequest.class); HttpServletResponse response = webRequest .getNativeResponse(HttpServletResponse.class); if (ServletRequest.class.isAssignableFrom(parameterType) || MultipartRequest.class.isAssignableFrom(parameterType)) { Object nativeRequest = webRequest .getNativeRequest(parameterType); if (nativeRequest == null) { throw new IllegalStateException( "Current request is not of type [" + parameterType.getName() + "]: " + request); } return nativeRequest; } else if (ServletResponse.class.isAssignableFrom(parameterType)) { this.responseArgumentUsed = true; Object nativeResponse = webRequest .getNativeResponse(parameterType); if (nativeResponse == null) { throw new IllegalStateException( "Current response is not of type [" + parameterType.getName() + "]: " + response); } return nativeResponse; } else if (HttpSession.class.isAssignableFrom(parameterType)) { return request.getSession(); } else if (Principal.class.isAssignableFrom(parameterType)) { return request.getUserPrincipal(); } else if (Locale.class.equals(parameterType)) { return RequestContextUtils.getLocale(request); } else if (InputStream.class.isAssignableFrom(parameterType)) { return request.getInputStream(); } else if (Reader.class.isAssignableFrom(parameterType)) { return request.getReader(); } else if (OutputStream.class.isAssignableFrom(parameterType)) { this.responseArgumentUsed = true; return response.getOutputStream(); } else if (Writer.class.isAssignableFrom(parameterType)) { this.responseArgumentUsed = true; return response.getWriter(); } return super.resolveStandardArgument(parameterType, webRequest); }
/** * 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 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.MultipartHttpServletRequest * @see org.springframework.web.multipart.MultipartFile * @see #bind(org.springframework.beans.PropertyValues) */ public void bind(ServletRequest request) { MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request); MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class); if (multipartRequest != null) { bindMultipart(multipartRequest.getMultiFileMap(), mpvs); } addBindValues(mpvs, request); doBind(mpvs); }
/** * 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 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.portlet.multipart.MultipartActionRequest * @see org.springframework.web.multipart.MultipartFile * @see #bind(org.springframework.beans.PropertyValues) */ public void bind(PortletRequest request) { MutablePropertyValues mpvs = new PortletRequestParameterPropertyValues(request); MultipartRequest multipartRequest = PortletUtils.getNativeRequest(request, MultipartRequest.class); if (multipartRequest != null) { bindMultipart(multipartRequest.getMultiFileMap(), mpvs); } doBind(mpvs); }
/** * 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 if (ClassUtils.hasMethod(HttpServletRequest.class, "getParts")) { HttpServletRequest serlvetRequest = ((NativeWebRequest) request).getNativeRequest(HttpServletRequest.class); new Servlet3MultipartHelper(isBindEmptyMultipartFiles()).bindParts(serlvetRequest, mpvs); } } doBind(mpvs); }