|
| 1 | +/* |
| 2 | + * Copyright 2002-2011 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.web.filter; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.nio.charset.Charset; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.Enumeration; |
| 26 | +import java.util.LinkedHashMap; |
| 27 | +import java.util.LinkedHashSet; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Set; |
| 31 | + |
| 32 | +import javax.servlet.FilterChain; |
| 33 | +import javax.servlet.ServletException; |
| 34 | +import javax.servlet.http.HttpServletRequest; |
| 35 | +import javax.servlet.http.HttpServletRequestWrapper; |
| 36 | +import javax.servlet.http.HttpServletResponse; |
| 37 | + |
| 38 | +import org.springframework.http.HttpInputMessage; |
| 39 | +import org.springframework.http.converter.FormHttpMessageConverter; |
| 40 | +import org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter; |
| 41 | +import org.springframework.http.server.ServletServerHttpRequest; |
| 42 | +import org.springframework.util.LinkedMultiValueMap; |
| 43 | +import org.springframework.util.MultiValueMap; |
| 44 | + |
| 45 | +/** |
| 46 | + * {@link javax.servlet.Filter} that makes form encoded data available through the |
| 47 | + * {@code ServletRequest.getParameter*()} family of methods during HTTP PUT requests. |
| 48 | + * |
| 49 | + * <p>The Servlet spec requires form data to be available for HTTP POST but not for |
| 50 | + * HTTP PUT requests. This filter intercepts HTTP PUT requests |
| 51 | + * where {@code 'Content-Type:application/x-www-form-urlencoded'}, reads the form |
| 52 | + * data from the body of the request, and wraps the ServletRequest in order to make |
| 53 | + * the form data available as request parameters. |
| 54 | + * |
| 55 | + * @author Rossen Stoyanchev |
| 56 | + * @since 3.1 |
| 57 | + */ |
| 58 | +public class HttpPutFormContentFilter extends OncePerRequestFilter { |
| 59 | + |
| 60 | + private static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded"; |
| 61 | + |
| 62 | + private final FormHttpMessageConverter formConverter = new XmlAwareFormHttpMessageConverter(); |
| 63 | + |
| 64 | + /** |
| 65 | + * The default character set to use for reading form data. |
| 66 | + */ |
| 67 | + public void setCharset(Charset charset) { |
| 68 | + this.formConverter.setCharset(charset); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + protected void doFilterInternal(final HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) |
| 73 | + throws ServletException, IOException { |
| 74 | + |
| 75 | + if ("PUT".equals(request.getMethod()) && isFormContentType(request)) { |
| 76 | + HttpInputMessage inputMessage = new ServletServerHttpRequest(request) { |
| 77 | + @Override |
| 78 | + public InputStream getBody() throws IOException { |
| 79 | + return request.getInputStream(); |
| 80 | + } |
| 81 | + }; |
| 82 | + MultiValueMap<String, String> formParameters = formConverter.read(null, inputMessage); |
| 83 | + HttpServletRequest wrapper = new HttpPutFormContentRequestWrapper(request, formParameters); |
| 84 | + filterChain.doFilter(wrapper, response); |
| 85 | + } |
| 86 | + else { |
| 87 | + filterChain.doFilter(request, response); |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + private boolean isFormContentType(HttpServletRequest request) { |
| 93 | + String contentType = request.getContentType(); |
| 94 | + return ((contentType != null) && contentType.equals(FORM_CONTENT_TYPE)); |
| 95 | + } |
| 96 | + |
| 97 | + private static class HttpPutFormContentRequestWrapper extends HttpServletRequestWrapper { |
| 98 | + |
| 99 | + private MultiValueMap<String, String> formParameters; |
| 100 | + |
| 101 | + public HttpPutFormContentRequestWrapper(HttpServletRequest request, MultiValueMap<String, String> parameters) { |
| 102 | + super(request); |
| 103 | + this.formParameters = (parameters != null) ? parameters : new LinkedMultiValueMap<String, String>(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public String getParameter(String name) { |
| 108 | + String queryStringValue = super.getParameter(name); |
| 109 | + String formValue = this.formParameters.getFirst(name); |
| 110 | + return (queryStringValue != null) ? queryStringValue : formValue; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public Map<String, String[]> getParameterMap() { |
| 115 | + Map<String, String[]> result = new LinkedHashMap<String, String[]>(); |
| 116 | + Enumeration<String> names = this.getParameterNames(); |
| 117 | + while (names.hasMoreElements()) { |
| 118 | + String name = names.nextElement(); |
| 119 | + result.put(name, this.getParameterValues(name)); |
| 120 | + } |
| 121 | + return result; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public Enumeration<String> getParameterNames() { |
| 126 | + Set<String> names = new LinkedHashSet<String>(); |
| 127 | + names.addAll(Collections.list(super.getParameterNames())); |
| 128 | + names.addAll(this.formParameters.keySet()); |
| 129 | + return Collections.enumeration(names); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public String[] getParameterValues(String name) { |
| 134 | + String[] queryStringValues = super.getParameterValues(name); |
| 135 | + List<String> formValues = this.formParameters.get(name); |
| 136 | + if (formValues == null) { |
| 137 | + return queryStringValues; |
| 138 | + } |
| 139 | + else if (queryStringValues == null) { |
| 140 | + return formValues.toArray(new String[formValues.size()]); |
| 141 | + } |
| 142 | + else { |
| 143 | + List<String> result = new ArrayList<String>(); |
| 144 | + result.addAll(Arrays.asList(queryStringValues)); |
| 145 | + result.addAll(formValues); |
| 146 | + return result.toArray(new String[result.size()]); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments