@ -1,11 +1,11 @@
@@ -1,11 +1,11 @@
/ *
* Copyright 2002 - 2010 the original author or authors .
* Copyright 2002 - 2011 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
@ -16,11 +16,20 @@
@@ -16,11 +16,20 @@
package org.springframework.http.server ;
import java.io.ByteArrayInputStream ;
import java.io.ByteArrayOutputStream ;
import java.io.IOException ;
import java.io.InputStream ;
import java.io.OutputStreamWriter ;
import java.io.Writer ;
import java.net.URI ;
import java.net.URISyntaxException ;
import java.net.URLEncoder ;
import java.util.Arrays ;
import java.util.Enumeration ;
import java.util.Iterator ;
import java.util.List ;
import java.util.Map ;
import javax.servlet.http.HttpServletRequest ;
import org.springframework.http.HttpHeaders ;
@ -35,13 +44,19 @@ import org.springframework.util.Assert;
@@ -35,13 +44,19 @@ import org.springframework.util.Assert;
* /
public class ServletServerHttpRequest implements ServerHttpRequest {
private static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded" ;
private static final String POST_METHOD = "POST" ;
private static final String FORM_CHARSET = "UTF-8" ;
private final HttpServletRequest servletRequest ;
private HttpHeaders headers ;
/ * *
* Construct a new instance of the ServletServerHttpRequest based on the given { @link HttpServletRequest }
*
* @param servletRequest the HttpServletRequest
* /
public ServletServerHttpRequest ( HttpServletRequest servletRequest ) {
@ -49,7 +64,6 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
@@ -49,7 +64,6 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
this . servletRequest = servletRequest ;
}
public HttpMethod getMethod ( ) {
return HttpMethod . valueOf ( this . servletRequest . getMethod ( ) ) ;
}
@ -57,8 +71,8 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
@@ -57,8 +71,8 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
public URI getURI ( ) {
try {
return new URI ( servletRequest . getScheme ( ) , null , servletRequest . getServerName ( ) ,
servletRequest . getServerPort ( ) , servletRequest . getRequestURI ( ) ,
servletRequest . getQueryString ( ) , null ) ;
servletRequest . getServerPort ( ) , servletRequest . getRequestURI ( ) , servletRequest . getQueryString ( ) ,
null ) ;
}
catch ( URISyntaxException ex ) {
throw new IllegalStateException ( "Could not get HttpServletRequest URI: " + ex . getMessage ( ) , ex ) ;
@ -70,7 +84,8 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
@@ -70,7 +84,8 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
this . headers = new HttpHeaders ( ) ;
for ( Enumeration headerNames = this . servletRequest . getHeaderNames ( ) ; headerNames . hasMoreElements ( ) ; ) {
String headerName = ( String ) headerNames . nextElement ( ) ;
for ( Enumeration headerValues = this . servletRequest . getHeaders ( headerName ) ; headerValues . hasMoreElements ( ) ; ) {
for ( Enumeration headerValues = this . servletRequest . getHeaders ( headerName ) ;
headerValues . hasMoreElements ( ) ; ) {
String headerValue = ( String ) headerValues . nextElement ( ) ;
this . headers . add ( headerName , headerValue ) ;
}
@ -80,7 +95,45 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
@@ -80,7 +95,45 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
}
public InputStream getBody ( ) throws IOException {
return this . servletRequest . getInputStream ( ) ;
if ( isFormSubmittal ( this . servletRequest ) ) {
return getFormBody ( this . servletRequest ) ;
}
else {
return this . servletRequest . getInputStream ( ) ;
}
}
private boolean isFormSubmittal ( HttpServletRequest request ) {
return FORM_CONTENT_TYPE . equals ( request . getContentType ( ) ) & &
POST_METHOD . equalsIgnoreCase ( request . getMethod ( ) ) ;
}
private InputStream getFormBody ( HttpServletRequest request ) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ;
Writer writer = new OutputStreamWriter ( bos , FORM_CHARSET ) ;
Map < String , String [ ] > form = request . getParameterMap ( ) ;
for ( Iterator < String > nameIterator = form . keySet ( ) . iterator ( ) ; nameIterator . hasNext ( ) ; ) {
String name = nameIterator . next ( ) ;
List < String > values = Arrays . asList ( form . get ( name ) ) ;
for ( Iterator < String > valueIterator = values . iterator ( ) ; valueIterator . hasNext ( ) ; ) {
String value = valueIterator . next ( ) ;
writer . write ( URLEncoder . encode ( name , FORM_CHARSET ) ) ;
if ( value ! = null ) {
writer . write ( '=' ) ;
writer . write ( URLEncoder . encode ( value , FORM_CHARSET ) ) ;
if ( valueIterator . hasNext ( ) ) {
writer . write ( '&' ) ;
}
}
}
if ( nameIterator . hasNext ( ) ) {
writer . append ( '&' ) ;
}
}
writer . flush ( ) ;
return new ByteArrayInputStream ( bos . toByteArray ( ) ) ;
}
}