polishing

This commit is contained in:
Juergen Hoeller
2010-06-07 22:21:22 +00:00
parent de866a0ff6
commit 4cef52a86f
7 changed files with 129 additions and 110 deletions

View File

@@ -53,13 +53,14 @@ final class CommonsClientHttpRequest extends AbstractClientHttpRequest {
this.httpMethod = httpMethod;
}
public HttpMethod getMethod() {
return HttpMethod.valueOf(httpMethod.getName());
return HttpMethod.valueOf(this.httpMethod.getName());
}
public URI getURI() {
try {
return URI.create(httpMethod.getURI().getEscapedURI());
return URI.create(this.httpMethod.getURI().getEscapedURI());
}
catch (URIException ex) {
throw new IllegalStateException("Could not get HttpMethod URI: " + ex.getMessage(), ex);
@@ -74,13 +75,13 @@ final class CommonsClientHttpRequest extends AbstractClientHttpRequest {
httpMethod.addRequestHeader(headerName, headerValue);
}
}
if (httpMethod instanceof EntityEnclosingMethod) {
EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) httpMethod;
if (this.httpMethod instanceof EntityEnclosingMethod) {
EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) this.httpMethod;
RequestEntity requestEntity = new ByteArrayRequestEntity(output);
entityEnclosingMethod.setRequestEntity(requestEntity);
}
httpClient.executeMethod(httpMethod);
return new CommonsClientHttpResponse(httpMethod);
this.httpClient.executeMethod(this.httpMethod);
return new CommonsClientHttpResponse(this.httpMethod);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 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.
@@ -24,7 +24,6 @@ import org.apache.commons.httpclient.HttpMethod;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
/**
* {@link org.springframework.http.client.ClientHttpResponse} implementation that uses
@@ -42,34 +41,36 @@ final class CommonsClientHttpResponse implements ClientHttpResponse {
private HttpHeaders headers;
CommonsClientHttpResponse(HttpMethod httpMethod) {
this.httpMethod = httpMethod;
}
public HttpStatus getStatusCode() {
return HttpStatus.valueOf(httpMethod.getStatusCode());
return HttpStatus.valueOf(this.httpMethod.getStatusCode());
}
public String getStatusText() {
return httpMethod.getStatusText();
return this.httpMethod.getStatusText();
}
public HttpHeaders getHeaders() {
if (headers == null) {
headers = new HttpHeaders();
for (Header header : httpMethod.getResponseHeaders()) {
headers.add(header.getName(), header.getValue());
if (this.headers == null) {
this.headers = new HttpHeaders();
for (Header header : this.httpMethod.getResponseHeaders()) {
this.headers.add(header.getName(), header.getValue());
}
}
return headers;
return this.headers;
}
public InputStream getBody() throws IOException {
return httpMethod.getResponseBodyAsStream();
return this.httpMethod.getResponseBodyAsStream();
}
public void close() {
httpMethod.releaseConnection();
this.httpMethod.releaseConnection();
}
}

View File

@@ -97,6 +97,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
private List<HttpMessageConverter<?>> partConverters = new ArrayList<HttpMessageConverter<?>>();
public FormHttpMessageConverter() {
this.partConverters.add(new ByteArrayHttpMessageConverter());
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
@@ -106,6 +107,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
this.partConverters.add(new SourceHttpMessageConverter());
}
/**
* Set the message body converters to use. These converters are used to convert objects to MIME parts.
*/
@@ -121,6 +123,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
this.charset = charset;
}
public boolean canRead(Class<?> clazz, MediaType mediaType) {
if (!MultiValueMap.class.isAssignableFrom(clazz)) {
return false;
@@ -151,8 +154,8 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
}
public MultiValueMap<String, String> read(Class<? extends MultiValueMap<String, ?>> clazz,
HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
MediaType contentType = inputMessage.getHeaders().getContentType();
Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : this.charset;
String body = FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset));
@@ -201,9 +204,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
}
private void writeForm(MultiValueMap<String, String> form, HttpOutputMessage outputMessage) throws IOException {
outputMessage.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED);
StringBuilder builder = new StringBuilder();
for (Iterator<String> nameIterator = form.keySet().iterator(); nameIterator.hasNext();) {
String name = nameIterator.next();
@@ -225,8 +226,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
FileCopyUtils.copy(builder.toString(), new OutputStreamWriter(outputMessage.getBody(), charset));
}
private void writeMultipart(MultiValueMap<String, Object> parts, HttpOutputMessage outputMessage)
throws IOException {
private void writeMultipart(MultiValueMap<String, Object> parts, HttpOutputMessage outputMessage) throws IOException {
byte[] boundary = generateMultipartBoundary();
Map<String, String> parameters = Collections.singletonMap("boundary", new String(boundary, "US-ASCII"));
@@ -304,7 +304,6 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
/**
* Generate a multipart boundary.
*
* <p>Default implementation returns a random boundary. Can be overridden in subclasses.
*/
protected byte[] generateMultipartBoundary() {
@@ -316,11 +315,9 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
}
/**
* Returns the filename of the given multipart part. This value will be used for the {@code Content-Disposition} header.
*
* Return the filename of the given multipart part. This value will be used for the {@code Content-Disposition} header.
* <p>Default implementation returns {@link Resource#getFilename()} if the part is a {@code Resource}, and
* {@code null} in other cases. Can be overridden in subclasses.
*
* @param part the part to determine the file name for
* @return the filename, or {@code null} if not known
*/
@@ -334,10 +331,10 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
}
}
/**
* Implementation of {@link org.springframework.http.HttpOutputMessage} used for writing multipart data.
*/
private class MultipartHttpOutputMessage implements HttpOutputMessage {
private final HttpHeaders headers = new HttpHeaders();
@@ -386,7 +383,6 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
throw new IllegalStateException(ex);
}
}
}
}

View File

@@ -32,11 +32,11 @@ import org.springframework.util.StringUtils;
* retrievable via {@link HttpServletRequest#getMethod()}. Since browsers currently only
* support GET and POST, a common technique - used by the Prototype library, for instance -
* is to use a normal POST with an additional hidden form field (<code>_method</code>)
* to pass the "real" HTTP method. This filter reads that parameter, and changes of
* {@link HttpServletRequestWrapper#getMethod()} accordingly.
* to pass the "real" HTTP method along. This filter reads that parameter and changes
* the {@link HttpServletRequestWrapper#getMethod()} return value accordingly.
*
* <p>The name of the request parameter defaults to <code>_method</code>, but can be
* changed via the {@link #setMethodParam(String) methodParam} property.
* adapted via the {@link #setMethodParam(String) methodParam} property.
*
* <p><b>NOTE: This filter needs to run after multipart processing in case of a multipart
* POST request, due to its inherent need for checking a POST body parameter.</b>
@@ -70,7 +70,7 @@ public class HiddenHttpMethodFilter extends OncePerRequestFilter {
String paramValue = request.getParameter(this.methodParam);
if ("POST".equals(request.getMethod()) && StringUtils.hasLength(paramValue)) {
String method = paramValue.toUpperCase(Locale.ENGLISH);
HttpServletRequest wrapper = new HttpMethodRequestWrapper(method, request);
HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method);
filterChain.doFilter(wrapper, response);
}
else {
@@ -87,7 +87,7 @@ public class HiddenHttpMethodFilter extends OncePerRequestFilter {
private final String method;
public HttpMethodRequestWrapper(String method, HttpServletRequest request) {
public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
super(request);
this.method = method;
}