diff --git a/org.springframework.core/src/main/java/org/springframework/util/DefaultMultiValueMap.java b/org.springframework.core/src/main/java/org/springframework/util/DefaultMultiValueMap.java deleted file mode 100644 index cb9c014e02..0000000000 --- a/org.springframework.core/src/main/java/org/springframework/util/DefaultMultiValueMap.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright 2002-2009 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.util; - -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * Default implementation of {@link MultiValueMap} that wraps a plain {@code Map}. - * - * @author Arjen Poutsma - * @since 3.0 - */ -public class DefaultMultiValueMap implements MultiValueMap { - - private final Map> wrappee; - - /** - * Constructs a new intance of the {@code DefaultMultiValueMap} wrapping a plain {@link LinkedHashMap}. - */ - public DefaultMultiValueMap() { - this(new LinkedHashMap>()); - } - - /** - * Constructs a new intance of the {@code DefaultMultiValueMap} wrapping the given map. - * - * @param wrappee the map to be wrapped - */ - public DefaultMultiValueMap(Map> wrappee) { - Assert.notNull(wrappee, "'wrappee' must not be null"); - this.wrappee = wrappee; - } - - /* - * MultiValueMap implementation - */ - - public void add(K key, V value) { - List values = wrappee.get(key); - if (values == null) { - values = new LinkedList(); - wrappee.put(key, values); - } - values.add(value); - } - - public V getFirst(K key) { - List values = wrappee.get(key); - return values != null ? values.get(0) : null; - } - - public void set(K key, V value) { - List values = new LinkedList(); - values.add(value); - wrappee.put(key, values); - } - - /* - * Map implementation - */ - - public int size() { - return wrappee.size(); - } - - public boolean isEmpty() { - return wrappee.isEmpty(); - } - - public boolean containsKey(Object key) { - return wrappee.containsKey(key); - } - - public boolean containsValue(Object value) { - return wrappee.containsValue(value); - } - - public List get(Object key) { - return wrappee.get(key); - } - - public List put(K key, List value) { - return wrappee.put(key, value); - } - - public List remove(Object key) { - return wrappee.remove(key); - } - - public void putAll(Map> m) { - wrappee.putAll(m); - } - - public void clear() { - wrappee.clear(); - } - - public Set keySet() { - return wrappee.keySet(); - } - - public Collection> values() { - return wrappee.values(); - } - - public Set>> entrySet() { - return wrappee.entrySet(); - } - - @Override - public int hashCode() { - return wrappee.hashCode(); - } - - @Override - public boolean equals(Object obj) { - return this.wrappee.equals(obj); - } - - @Override - public String toString() { - return wrappee.toString(); - } - -} diff --git a/org.springframework.core/src/main/java/org/springframework/util/LinkedMultiValueMap.java b/org.springframework.core/src/main/java/org/springframework/util/LinkedMultiValueMap.java new file mode 100644 index 0000000000..1fb2466b33 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/util/LinkedMultiValueMap.java @@ -0,0 +1,150 @@ +/* + * Copyright 2002-2009 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.util; + +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Simple implementation of {@link MultiValueMap} that wraps a plain {@code Map} + * (by default a {@link LinkedHashMap}, storing multiple values in a {@link LinkedList}. + * + *

This Map implementation is generally not thread-safe. It is primarily designed + * for data structures exposed from request objects, for use in a single thread only. + * + * @author Arjen Poutsma + * @author Juergen Hoeller + * @since 3.0 + */ +public class LinkedMultiValueMap implements MultiValueMap { + + private final Map> targetMap; + + + /** + * Create a new SimpleMultiValueMap that wraps the given target Map. + * @param wrappee the target Map to wrap + */ + public LinkedMultiValueMap() { + this.targetMap = new LinkedHashMap>(); + } + + /** + * Create a new SimpleMultiValueMap that wraps the given target Map. + *

Note: The given Map will be used as active underlying Map. + * Any changes in the underlying map will be reflected in the + * MultiValueMap object, and vice versa. + * @param targetMap the target Map to wrap + */ + public LinkedMultiValueMap(Map> targetMap) { + Assert.notNull(targetMap, "'targetMap' must not be null"); + this.targetMap = targetMap; + } + + // MultiValueMap implementation + + public void add(K key, V value) { + List values = this.targetMap.get(key); + if (values == null) { + values = new LinkedList(); + this.targetMap.put(key, values); + } + values.add(value); + } + + public V getFirst(K key) { + List values = this.targetMap.get(key); + return (values != null ? values.get(0) : null); + } + + public void set(K key, V value) { + List values = new LinkedList(); + values.add(value); + this.targetMap.put(key, values); + } + + + // Map implementation + + public int size() { + return this.targetMap.size(); + } + + public boolean isEmpty() { + return this.targetMap.isEmpty(); + } + + public boolean containsKey(Object key) { + return this.targetMap.containsKey(key); + } + + public boolean containsValue(Object value) { + return this.targetMap.containsValue(value); + } + + public List get(Object key) { + return this.targetMap.get(key); + } + + public List put(K key, List value) { + return this.targetMap.put(key, value); + } + + public List remove(Object key) { + return this.targetMap.remove(key); + } + + public void putAll(Map> m) { + this.targetMap.putAll(m); + } + + public void clear() { + this.targetMap.clear(); + } + + public Set keySet() { + return this.targetMap.keySet(); + } + + public Collection> values() { + return this.targetMap.values(); + } + + public Set>> entrySet() { + return this.targetMap.entrySet(); + } + + @Override + public boolean equals(Object obj) { + return this.targetMap.equals(obj); + } + + @Override + public int hashCode() { + return this.targetMap.hashCode(); + } + + @Override + public String toString() { + return this.targetMap.toString(); + } + +} diff --git a/org.springframework.core/src/main/java/org/springframework/util/MultiValueMap.java b/org.springframework.core/src/main/java/org/springframework/util/MultiValueMap.java index 684fbe669b..d9e1801d05 100644 --- a/org.springframework.core/src/main/java/org/springframework/util/MultiValueMap.java +++ b/org.springframework.core/src/main/java/org/springframework/util/MultiValueMap.java @@ -28,25 +28,22 @@ import java.util.Map; public interface MultiValueMap extends Map> { /** - * Adds the given single value to the current list of values for the given key. - * - * @param key the key - * @param value the value to be added - */ - void add(K key, V value); - - /** - * Returns the first value for the given key. - * + * Return the first value for the given key. * @param key the key * @return the first value for the specified key, or null */ V getFirst(K key); /** - * Sets the given single value under the given key. - * - * @param key the key + * Add the given single value to the current list of values for the given key. + * @param key the key + * @param value the value to be added + */ + void add(K key, V value); + + /** + * Set the given single value under the given key. + * @param key the key * @param value the value to set */ void set(K key, V value); diff --git a/org.springframework.core/src/test/java/org/springframework/util/DefaultMultiValueMapTests.java b/org.springframework.core/src/test/java/org/springframework/util/LinkedMultiValueMapTests.java similarity index 89% rename from org.springframework.core/src/test/java/org/springframework/util/DefaultMultiValueMapTests.java rename to org.springframework.core/src/test/java/org/springframework/util/LinkedMultiValueMapTests.java index 2888f5dc99..7e274557f9 100644 --- a/org.springframework.core/src/test/java/org/springframework/util/DefaultMultiValueMapTests.java +++ b/org.springframework.core/src/test/java/org/springframework/util/LinkedMultiValueMapTests.java @@ -29,13 +29,13 @@ import org.junit.Test; /** * @author Arjen Poutsma */ -public class DefaultMultiValueMapTests { +public class LinkedMultiValueMapTests { - private DefaultMultiValueMap map; + private LinkedMultiValueMap map; @Before public void setUp() { - map = new DefaultMultiValueMap(); + map = new LinkedMultiValueMap(); } @Test @@ -71,7 +71,7 @@ public class DefaultMultiValueMapTests { public void equals() { map.set("key1", "value1"); assertEquals(map, map); - MultiValueMap o1 = new DefaultMultiValueMap(); + MultiValueMap o1 = new LinkedMultiValueMap(); o1.set("key1", "value1"); assertEquals(map, o1); assertEquals(o1, map); @@ -80,4 +80,5 @@ public class DefaultMultiValueMapTests { assertEquals(map, o2); assertEquals(o2, map); } + } diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.java index 67def615d3..6c4b466214 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.java @@ -39,7 +39,7 @@ import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; -import org.springframework.util.MediaType; +import org.springframework.http.MediaType; import org.springframework.util.StringUtils; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolverTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolverTests.java index 2d381812ab..3fa76d36d6 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolverTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolverTests.java @@ -28,7 +28,7 @@ import org.junit.Before; import org.junit.Test; import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.util.MediaType; +import org.springframework.http.MediaType; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.servlet.View; diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/HttpHeaders.java b/org.springframework.web/src/main/java/org/springframework/http/HttpHeaders.java similarity index 65% rename from org.springframework.web/src/main/java/org/springframework/web/http/HttpHeaders.java rename to org.springframework.web/src/main/java/org/springframework/http/HttpHeaders.java index f895e55caf..bf7bc069b0 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/HttpHeaders.java +++ b/org.springframework.web/src/main/java/org/springframework/http/HttpHeaders.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.http; +package org.springframework.http; import java.net.URI; import java.nio.charset.Charset; @@ -31,7 +31,6 @@ import java.util.Set; import org.springframework.core.CollectionFactory; import org.springframework.util.Assert; -import org.springframework.util.MediaType; import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; @@ -39,16 +38,18 @@ import org.springframework.util.StringUtils; * Represents HTTP request and response headers, mapping string header names to list of string values. * *

In addition to the normal methods defined by {@link Map}, this class offers the following convenience methods: - *

  • {@link #getFirst(String)} returns the first value associated with a given header name
  • {@link - * #add(String, String)} adds a header value to the list of values for a header name
  • {@link #set(String, - * String)} sets the header value to a single string value
+ *
    + *
  • {@link #getFirst(String)} returns the first value associated with a given header name
  • + *
  • {@link #add(String, String)} adds a header value to the list of values for a header name
  • + *
  • {@link #set(String, String)} sets the header value to a single string value
  • + *
* *

Inspired by {@link com.sun.net.httpserver.Headers}. * * @author Arjen Poutsma * @since 3.0 */ -public final class HttpHeaders implements MultiValueMap { +public class HttpHeaders implements MultiValueMap { private static String ACCEPT = "Accept"; @@ -62,23 +63,12 @@ public final class HttpHeaders implements MultiValueMap { private static String LOCATION = "Location"; - private Map> headers = CollectionFactory.createLinkedCaseInsensitiveMapIfPossible(5); + + private final Map> headers = CollectionFactory.createLinkedCaseInsensitiveMapIfPossible(5); + /** - * Returns the list of acceptable {@linkplain MediaType media types}, as specified by the Accept header. - * - *

Returns an empty list when the acceptable media types are unspecified. - * - * @return the acceptable media types - */ - public List getAccept() { - String value = getFirst(ACCEPT); - return value != null ? MediaType.parseMediaTypes(value) : Collections.emptyList(); - } - - /** - * Sets the list of acceptable {@linkplain MediaType media types}, as specified by the Accept header. - * + * Set the list of acceptable {@linkplain MediaType media types}, as specified by the Accept header. * @param acceptableMediaTypes the acceptable media types */ public void setAccept(List acceptableMediaTypes) { @@ -86,9 +76,34 @@ public final class HttpHeaders implements MultiValueMap { } /** - * Returns the list of acceptable {@linkplain Charset charsets}, as specified by the Accept-Charset + * Return the list of acceptable {@linkplain MediaType media types}, as specified by the Accept header. + *

Returns an empty list when the acceptable media types are unspecified. + * @return the acceptable media types + */ + public List getAccept() { + String value = getFirst(ACCEPT); + return (value != null ? MediaType.parseMediaTypes(value) : Collections.emptyList()); + } + + /** + * Set the list of acceptable {@linkplain Charset charsets}, as specified by the Accept-Charset header. + * @param acceptableCharsets the acceptable charsets + */ + public void setAcceptCharset(List acceptableCharsets) { + StringBuilder builder = new StringBuilder(); + for (Iterator iterator = acceptableCharsets.iterator(); iterator.hasNext();) { + Charset charset = iterator.next(); + builder.append(charset.name().toLowerCase(Locale.ENGLISH)); + if (iterator.hasNext()) { + builder.append(", "); + } + } + set(ACCEPT_CHARSET, builder.toString()); + } + + /** + * Return the list of acceptable {@linkplain Charset charsets}, as specified by the Accept-Charset * header. - * * @return the acceptable charsets */ public List getAcceptCharset() { @@ -110,29 +125,19 @@ public final class HttpHeaders implements MultiValueMap { } /** - * Sets the list of acceptable {@linkplain Charset charsets}, as specified by the Accept-Charset header. - * - * @param acceptableCharsets the acceptable charsets + * Set the set of allowed {@link HttpMethod HTTP methods}, as specified by the Allow header. + * @param allowedMethods the allowed methods */ - public void setAcceptCharset(List acceptableCharsets) { - StringBuilder builder = new StringBuilder(); - for (Iterator iterator = acceptableCharsets.iterator(); iterator.hasNext();) { - Charset charset = iterator.next(); - builder.append(charset.name().toLowerCase(Locale.ENGLISH)); - if (iterator.hasNext()) { - builder.append(", "); - } - } - set(ACCEPT_CHARSET, builder.toString()); + public void setAllow(Set allowedMethods) { + set(ALLOW, StringUtils.collectionToCommaDelimitedString(allowedMethods)); } /** - * Returns the set of allowed {@link HttpMethod HTTP methods}, as specified by the Allow header.

- * Returns an empty set when the allowed methods are unspecified. - * + * Return the set of allowed {@link HttpMethod HTTP methods}, as specified by the Allow header. + *

Returns an empty set when the allowed methods are unspecified. * @return the allowed methods */ - public EnumSet getAllow() { + public Set getAllow() { String value = getFirst(ALLOW); if (value != null) { List allowedMethod = new ArrayList(5); @@ -148,28 +153,7 @@ public final class HttpHeaders implements MultiValueMap { } /** - * Sets the set of allowed {@link HttpMethod HTTP methods}, as specified by the Allow header. - * - * @param allowedMethods the allowed methods - */ - public void setAllow(EnumSet allowedMethods) { - set(ALLOW, StringUtils.collectionToCommaDelimitedString(allowedMethods)); - } - - /** - * Returns the length of the body in bytes, as specified by the Content-Length header.

Returns -1 - * when the content-length is unknown. - * - * @return the content length - */ - public long getContentLength() { - String value = getFirst(CONTENT_LENGTH); - return value != null ? Long.parseLong(value) : -1; - } - - /** - * Sets the length of the body in bytes, as specified by the Content-Length header. - * + * Set the length of the body in bytes, as specified by the Content-Length header. * @param contentLength the content length */ public void setContentLength(long contentLength) { @@ -177,19 +161,17 @@ public final class HttpHeaders implements MultiValueMap { } /** - * Returns the {@linkplain MediaType media type} of the body, as specified by the Content-Type header. - *

Returns null when the content-type is unknown. - * - * @return the content type + * Return the length of the body in bytes, as specified by the Content-Length header. + *

Returns -1 when the content-length is unknown. + * @return the content length */ - public MediaType getContentType() { - String value = getFirst(CONTENT_TYPE); - return value != null ? MediaType.parseMediaType(value) : null; + public long getContentLength() { + String value = getFirst(CONTENT_LENGTH); + return (value != null ? Long.parseLong(value) : -1); } /** - * Sets the {@linkplain MediaType media type} of the body, as specified by the Content-Type header. - * + * Set the {@linkplain MediaType media type} of the body, as specified by the Content-Type header. * @param mediaType the media type */ public void setContentType(MediaType mediaType) { @@ -199,32 +181,38 @@ public final class HttpHeaders implements MultiValueMap { } /** - * Returns the (new) location of a resource, as specified by the Location header.

Returns - * null when the location is unknown. - * - * @return the location + * Return the {@linkplain MediaType media type} of the body, as specified by the Content-Type header. + *

Returns null when the content-type is unknown. + * @return the content type */ - public URI getLocation() { - String value = getFirst(LOCATION); - return value != null ? URI.create(value) : null; + public MediaType getContentType() { + String value = getFirst(CONTENT_TYPE); + return (value != null ? MediaType.parseMediaType(value) : null); } /** - * Sets the (new) location of a resource, as specified by the Location header. - * + * Set the (new) location of a resource, as specified by the Location header. * @param location the location */ public void setLocation(URI location) { set(LOCATION, location.toASCIIString()); } - /* - * Single string methods + /** + * Return the (new) location of a resource, as specified by the Location header. + *

Returns null when the location is unknown. + * @return the location */ + public URI getLocation() { + String value = getFirst(LOCATION); + return (value != null ? URI.create(value) : null); + } + + + // Single string methods /** - * Returns the first header value for the given header name, if any. - * + * Return the first header value for the given header name, if any. * @param headerName the header name * @return the first header value; or null */ @@ -234,8 +222,7 @@ public final class HttpHeaders implements MultiValueMap { } /** - * Adds the given, single header value under the given name. - * + * Add the given, single header value under the given name. * @param headerName the header name * @param headerValue the header value * @throws UnsupportedOperationException if adding headers is not supported @@ -246,14 +233,13 @@ public final class HttpHeaders implements MultiValueMap { List headerValues = headers.get(headerName); if (headerValues == null) { headerValues = new LinkedList(); - headers.put(headerName, headerValues); + this.headers.put(headerName, headerValues); } headerValues.add(headerValue); } /** - * Sets the given, single header value under the given name. - * + * Set the given, single header value under the given name. * @param headerName the header name * @param headerValue the header value * @throws UnsupportedOperationException if adding headers is not supported @@ -266,77 +252,77 @@ public final class HttpHeaders implements MultiValueMap { headers.put(headerName, headerValues); } - /* - * Map implementation - */ + + // Map implementation public int size() { - return headers.size(); + return this.headers.size(); } public boolean isEmpty() { - return headers.isEmpty(); + return this.headers.isEmpty(); } public boolean containsKey(Object key) { - return headers.containsKey(key); + return this.headers.containsKey(key); } public boolean containsValue(Object value) { - return headers.containsValue(value); + return this.headers.containsValue(value); } public List get(Object key) { - return headers.get(key); + return this.headers.get(key); } public List put(String key, List value) { - return headers.put(key, value); + return this.headers.put(key, value); } public List remove(Object key) { - return headers.remove(key); + return this.headers.remove(key); } public void putAll(Map> m) { - headers.putAll(m); + this.headers.putAll(m); } public void clear() { - headers.clear(); + this.headers.clear(); } public Set keySet() { - return headers.keySet(); + return this.headers.keySet(); } public Collection> values() { - return headers.values(); + return this.headers.values(); } public Set>> entrySet() { - return headers.entrySet(); + return this.headers.entrySet(); + } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + if (!(other instanceof HttpHeaders)) { + return false; + } + HttpHeaders otherHeaders = (HttpHeaders) other; + return this.headers.equals(otherHeaders.headers); } @Override public int hashCode() { - return headers.hashCode(); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj != null && obj instanceof HttpHeaders) { - HttpHeaders other = (HttpHeaders) obj; - return this.headers.equals(other.headers); - } - return false; + return this.headers.hashCode(); } @Override public String toString() { - return headers.toString(); + return this.headers.toString(); } + } diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/HttpInputMessage.java b/org.springframework.web/src/main/java/org/springframework/http/HttpInputMessage.java similarity index 78% rename from org.springframework.web/src/main/java/org/springframework/web/http/HttpInputMessage.java rename to org.springframework.web/src/main/java/org/springframework/http/HttpInputMessage.java index ad1f1e767b..42531fdcb8 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/HttpInputMessage.java +++ b/org.springframework.web/src/main/java/org/springframework/http/HttpInputMessage.java @@ -14,14 +14,16 @@ * limitations under the License. */ -package org.springframework.web.http; +package org.springframework.http; import java.io.IOException; import java.io.InputStream; /** - * Represents a HTTP output message, consisting of {@linkplain #getHeaders() headers} and a readable {@linkplain - * #getBody() body}.

Typically implemented by a HTTP request on the server-side, or a response on the client-side. + * Represents a HTTP output message, consisting of {@linkplain #getHeaders() headers} + * and a readable {@linkplain #getBody() body}. + * + *

Typically implemented by a HTTP request on the server-side, or a response on the client-side. * * @author Arjen Poutsma * @since 3.0 @@ -29,8 +31,7 @@ import java.io.InputStream; public interface HttpInputMessage extends HttpMessage { /** - * Returns the body of the message as an input stream. - * + * Return the body of the message as an input stream. * @return the input stream body * @throws IOException in case of I/O Errors */ diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/HttpMessage.java b/org.springframework.web/src/main/java/org/springframework/http/HttpMessage.java similarity index 87% rename from org.springframework.web/src/main/java/org/springframework/web/http/HttpMessage.java rename to org.springframework.web/src/main/java/org/springframework/http/HttpMessage.java index 305eef2da4..80f7ca292d 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/HttpMessage.java +++ b/org.springframework.web/src/main/java/org/springframework/http/HttpMessage.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.http; +package org.springframework.http; /** * Represents the base interface for HTTP request and response messages. Consists of {@link HttpHeaders}, retrievable @@ -26,9 +26,9 @@ package org.springframework.web.http; public interface HttpMessage { /** - * Returns the headers of this message. - * - * @return the headers + * Return the headers of this message. + * @return a corresponding HttpHeaders object */ HttpHeaders getHeaders(); + } diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/HttpMethod.java b/org.springframework.web/src/main/java/org/springframework/http/HttpMethod.java similarity index 76% rename from org.springframework.web/src/main/java/org/springframework/web/http/HttpMethod.java rename to org.springframework.web/src/main/java/org/springframework/http/HttpMethod.java index 0d3a95d5db..361784b4ab 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/HttpMethod.java +++ b/org.springframework.web/src/main/java/org/springframework/http/HttpMethod.java @@ -14,13 +14,14 @@ * limitations under the License. */ -package org.springframework.web.http; +package org.springframework.http; /** - * Java 5 enumeration of HTTP request methods. + * Java 5 enumeration of HTTP request methods. Intended for use + * with {@link org.springframework.http.client.ClientHttpRequest} + * and {@link org.springframework.web.client.RestTemplate}. * * @author Arjen Poutsma - * @see org.springframework.web.bind.annotation.RequestMapping * @since 3.0 */ public enum HttpMethod { diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/HttpOutputMessage.java b/org.springframework.web/src/main/java/org/springframework/http/HttpOutputMessage.java similarity index 78% rename from org.springframework.web/src/main/java/org/springframework/web/http/HttpOutputMessage.java rename to org.springframework.web/src/main/java/org/springframework/http/HttpOutputMessage.java index 64c2c9bb5e..cf413f667c 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/HttpOutputMessage.java +++ b/org.springframework.web/src/main/java/org/springframework/http/HttpOutputMessage.java @@ -14,14 +14,16 @@ * limitations under the License. */ -package org.springframework.web.http; +package org.springframework.http; import java.io.IOException; import java.io.OutputStream; /** - * Represents a HTTP output message, consisting of {@linkplain #getHeaders() headers} and a writable {@linkplain - * #getBody() body}.

Typically implemented by a HTTP request on the client-side, or a response on the server-side. + * Represents a HTTP output message, consisting of {@linkplain #getHeaders() headers} + * and a writable {@linkplain #getBody() body}. + * + *

Typically implemented by a HTTP request on the client-side, or a response on the server-side. * * @author Arjen Poutsma * @since 3.0 @@ -29,8 +31,7 @@ import java.io.OutputStream; public interface HttpOutputMessage extends HttpMessage { /** - * Returns the body of the message as an output stream. - * + * Return the body of the message as an output stream. * @return the output stream body * @throws IOException in case of I/O Errors */ diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/HttpStatus.java b/org.springframework.web/src/main/java/org/springframework/http/HttpStatus.java similarity index 81% rename from org.springframework.web/src/main/java/org/springframework/web/http/HttpStatus.java rename to org.springframework.web/src/main/java/org/springframework/http/HttpStatus.java index 5bb28a0958..3acebabb79 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/HttpStatus.java +++ b/org.springframework.web/src/main/java/org/springframework/http/HttpStatus.java @@ -14,10 +14,12 @@ * limitations under the License. */ -package org.springframework.web.http; +package org.springframework.http; /** - * Java 5 enumeration of HTTP status codes.

The HTTP status code series can be retrieved via {@link #series()}. + * Java 5 enumeration of HTTP status codes. + * + *

The HTTP status code series can be retrieved via {@link #series()}. * * @author Arjen Poutsma * @see HttpStatus.Series @@ -79,10 +81,59 @@ public enum HttpStatus { GATEWAY_TIMEOUT(504), HTTP_VERSION_NOT_SUPPORTED(505); + + private final int value; + + + private HttpStatus(int value) { + this.value = value; + } + /** - * Java 5 enumeration of HTTP status series.

Retrievable via {@link HttpStatus#series()}. + * Return the integer value of this status code. */ - public enum Series { + public int value() { + return this.value; + } + + /** + * Returns the HTTP status series of this status code. + * @see HttpStatus.Series + */ + public Series series() { + return Series.valueOf(this); + } + + /** + * Return a string representation of this status code. + */ + @Override + public String toString() { + return Integer.toString(value); + } + + + /** + * Return the enum constant of this type with the specified numeric value. + * @param statusCode the numeric value of the enum to be returned + * @return the enum constant with the specified numeric value + * @throws IllegalArgumentException if this enum has no constant for the specified numeric value + */ + public static HttpStatus valueOf(int statusCode) { + for (HttpStatus status : values()) { + if (status.value == statusCode) { + return status; + } + } + throw new IllegalArgumentException("No matching constant for [" + statusCode + "]"); + } + + + /** + * Java 5 enumeration of HTTP status series. + *

Retrievable via {@link HttpStatus#series()}. + */ + public static enum Series { INFORMATIONAL(1), SUCCESSFUL(2), @@ -97,12 +148,10 @@ public enum HttpStatus { } /** - * Returns the integer value of this status series. Ranges from 1 to 5. - * - * @return the integer value + * Return the integer value of this status series. Ranges from 1 to 5. */ public int value() { - return value; + return this.value; } private static Series valueOf(HttpStatus status) { @@ -117,55 +166,4 @@ public enum HttpStatus { } - private final int value; - - private HttpStatus(int value) { - this.value = value; - } - - /** - * Returns the integer value of this status code. - * - * @return the integer value - */ - public int value() { - return value; - } - - /** - * Returns the HTTP status series of this status code. - * - * @return the series - * @see HttpStatus.Series - */ - public Series series() { - return Series.valueOf(this); - } - - /** - * Returns the enum constant of this type with the specified numeric value. - * - * @param statusCode the numeric value of the enum to be returned - * @return the enum constant with the specified numeric value - * @throws IllegalArgumentException if this enum has no constant for the specified numeric value - */ - public static HttpStatus valueOf(int statusCode) { - for (HttpStatus status : values()) { - if (status.value == statusCode) { - return status; - } - } - throw new IllegalArgumentException("No matching constant for [" + statusCode + "]"); - } - - /** - * Returns a string representation of this status code. - * - * @return a string representation - */ - @Override - public String toString() { - return Integer.toString(value); - } - } diff --git a/org.springframework.core/src/main/java/org/springframework/util/MediaType.java b/org.springframework.web/src/main/java/org/springframework/http/MediaType.java similarity index 68% rename from org.springframework.core/src/main/java/org/springframework/util/MediaType.java rename to org.springframework.web/src/main/java/org/springframework/http/MediaType.java index a3eef19387..8b5580dea2 100644 --- a/org.springframework.core/src/main/java/org/springframework/util/MediaType.java +++ b/org.springframework.web/src/main/java/org/springframework/http/MediaType.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package org.springframework.util; +package org.springframework.http; import java.nio.charset.Charset; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; @@ -26,27 +27,32 @@ import java.util.Locale; import java.util.Map; import org.springframework.core.CollectionFactory; +import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; /** * Represents an Internet Media Type, as defined in the HTTP specification. * - *

Consists of a {@linkplain #getType() type} and a {@linkplain #getSubtype() subtype}. Also has functionality to - * parse media types from a string using {@link #parseMediaType(String)}, or multiple comma-separated media types using - * {@link #parseMediaTypes(String)}. + *

Consists of a {@linkplain #getType() type} and a {@linkplain #getSubtype() subtype}. + * Also has functionality to parse media types from a string using {@link #parseMediaType(String)}, + * or multiple comma-separated media types using {@link #parseMediaTypes(String)}. * * @author Arjen Poutsma - * @see HTTP 1.1 + * @author Juergen Hoeller * @since 3.0 + * @see HTTP 1.1 */ -public final class MediaType implements Comparable { +public class MediaType implements Comparable { - public static final MediaType ALL = new MediaType(); + public static final MediaType ALL = new MediaType("*", "*"); + + private static final String WILDCARD_TYPE = "*"; private static final String PARAM_QUALITY_FACTORY = "q"; private static final String PARAM_CHARSET = "charset"; - private static final String WILDCARD_TYPE = "*"; private final String type; @@ -54,19 +60,10 @@ public final class MediaType implements Comparable { private final Map parameters; - /** - * Private constructor that creates a new {@link MediaType} representing */*. - * - * @see #ALL - */ - private MediaType() { - this(WILDCARD_TYPE, WILDCARD_TYPE); - } /** - * Create a new {@link MediaType} for the given primary type. The {@linkplain #getSubtype() subtype} is set to - * *, parameters empty. - * + * Create a new {@link MediaType} for the given primary type. + *

The {@linkplain #getSubtype() subtype} is set to *, parameters empty. * @param type the primary type */ public MediaType(String type) { @@ -74,9 +71,9 @@ public final class MediaType implements Comparable { } /** - * Create a new {@link MediaType} for the given primary type and subtype. The parameters are empty. - * - * @param type the primary type + * Create a new {@link MediaType} for the given primary type and subtype. + *

The parameters are empty. + * @param typethe primary type * @param subtype the subtype */ public MediaType(String type, String subtype) { @@ -84,9 +81,8 @@ public final class MediaType implements Comparable { } /** - * Creates a new {@link MediaType} for the given type, subtype, and character set. - * - * @param type the primary type + * Create a new {@link MediaType} for the given type, subtype, and character set. + * @param type the primary type * @param subtype the subtype * @param charSet the character set */ @@ -95,10 +91,9 @@ public final class MediaType implements Comparable { } /** - * Creates a new {@link MediaType} for the given type, subtype, and parameters. - * - * @param type the primary type - * @param subtype the subtype + * Create a new {@link MediaType} for the given type, subtype, and parameters. + * @param type the primary type + * @param subtype the subtype * @param parameters the parameters, mat be null */ public MediaType(String type, String subtype, Map parameters) { @@ -115,93 +110,30 @@ public final class MediaType implements Comparable { } } - /** - * Parses the given string into a single {@link MediaType}. - * - * @param mediaType the string to parse - * @return the media type - * @throws IllegalArgumentException if the string cannot be parsed - */ - public static MediaType parseMediaType(String mediaType) { - Assert.hasLength(mediaType, "'mediaType' must not be empty"); - String[] parts = StringUtils.tokenizeToStringArray(mediaType, ";"); - - Map parameters; - if (parts.length <= 1) { - parameters = null; - } - else { - parameters = new LinkedHashMap(parts.length - 1); - } - for (int i = 1; i < parts.length; i++) { - String part = parts[i]; - int idx = part.indexOf('='); - if (idx != -1) { - String name = part.substring(0, idx); - String value = part.substring(idx + 1, part.length()); - parameters.put(name, value); - } - } - String fullType = parts[0].trim(); - - // java.net.HttpURLConnection returns a *; q=.2 Accept header - if (WILDCARD_TYPE.equals(fullType)) { - fullType = "*/*"; - } - int idx = fullType.indexOf('/'); - String type = fullType.substring(0, idx); - String subtype = fullType.substring(idx + 1, fullType.length()); - return new MediaType(type, subtype, parameters); - } /** - * Parses the given, comma-seperated string into a list of {@link MediaType} objects. This method can be used to parse - * an Accept or Content-Type header. - * - * @param mediaTypes the string to parse - * @return the list of media types - * @throws IllegalArgumentException if the string cannot be parsed - */ - public static List parseMediaTypes(String mediaTypes) { - Assert.hasLength(mediaTypes, "'mediaTypes' must not be empty"); - String[] tokens = mediaTypes.split(",\\s*"); - List result = new ArrayList(tokens.length); - for (String token : tokens) { - result.add(parseMediaType(token)); - } - return result; - } - - /** - * Returns the primary type. - * - * @return the type + * Return the primary type. */ public String getType() { - return type; + return this.type; } /** - * Indicates whether the {@linkplain #getType() type} is the wildcard character * or not. - * - * @return whether the type is * + * Indicate whether the {@linkplain #getType() type} is the wildcard character * or not. */ public boolean isWildcardType() { return WILDCARD_TYPE.equals(type); } /** - * Returns the subtype. - * - * @return the subtype + * Return the subtype. */ public String getSubtype() { - return subtype; + return this.subtype; } /** - * Indicates whether the {@linkplain #getSubtype() subtype} is the wildcard character * or not. - * + * Indicate whether the {@linkplain #getSubtype() subtype} is the wildcard character * or not. * @return whether the subtype is * */ public boolean isWildcardSubtype() { @@ -209,39 +141,37 @@ public final class MediaType implements Comparable { } /** - * Returns the character set, as indicated by a charset parameter, if any. - * + * Return the character set, as indicated by a charset parameter, if any. * @return the character set; or null if not available */ public Charset getCharSet() { - String charSet = parameters.get(PARAM_CHARSET); - return charSet != null ? Charset.forName(charSet) : null; + String charSet = this.parameters.get(PARAM_CHARSET); + return (charSet != null ? Charset.forName(charSet) : null); } /** - * Returns the quality value, as indicated by a q parameter, if any. Defaults to 1.0. - * + * Return the quality value, as indicated by a q parameter, if any. Defaults to 1.0. * @return the quality factory */ public double getQualityValue() { - String qualityFactory = parameters.get(PARAM_QUALITY_FACTORY); - return qualityFactory != null ? Double.parseDouble(qualityFactory) : 1D; + String qualityFactory = this.parameters.get(PARAM_QUALITY_FACTORY); + return (qualityFactory != null ? Double.parseDouble(qualityFactory) : 1D); } /** - * Returns a generic parameter value, given a parameter name. - * + * Return a generic parameter value, given a parameter name. * @param name the parameter name * @return the parameter value; or null if not present */ public String getParameter(String name) { - return parameters.get(name); + return this.parameters.get(name); } + /** - * Indicates whether this {@link MediaType} includes the given media type. For instance, text/* includes - * text/plain, text/html, etc. - * + * Indicate whether this {@link MediaType} includes the given media type. + *

For instance, text/* includes text/plain, + * text/html, etc. * @param other the reference media type with which to compare * @return true if this media type includes the given media type; false otherwise */ @@ -258,14 +188,13 @@ public final class MediaType implements Comparable { } /** - * Compares this {@link MediaType} to another. Sorting with this comparator follows the general rule:

+ * Compare this {@link MediaType} to another. Sorting with this comparator follows the general rule:
* audio/basic < audio/* < */*
. That is, an explicit media type is sorted before an unspecific * media type. Quality parameters are also considered, so that
audio/* < audio/*;q=0.7; * audio/*;q=0.3
. - * * @param other the media type to compare to - * @return a negative integer, zero, or a positive integer as this media type is less than, equal to, or greater than - * the specified media type + * @return a negative integer, zero, or a positive integer as this media type is less than, equal to, + * or greater than the specified media type */ public int compareTo(MediaType other) { double qVal1 = this.getQualityValue(); @@ -303,23 +232,23 @@ public final class MediaType implements Comparable { } @Override - public boolean equals(Object o) { - if (this == o) { + public boolean equals(Object other) { + if (this == other) { return true; } - if (o != null && o instanceof MediaType) { - MediaType other = (MediaType) o; - return this.type.equals(other.type) && this.subtype.equals(other.subtype) && - this.parameters.equals(other.parameters); + if (!(other instanceof MediaType)) { + return false; } - return false; + MediaType otherType = (MediaType) other; + return (this.type.equals(otherType.type) && this.subtype.equals(otherType.subtype) && + this.parameters.equals(otherType.parameters)); } @Override public int hashCode() { - int result = type.hashCode(); - result = 31 * result + subtype.hashCode(); - result = 31 * result + parameters.hashCode(); + int result = this.type.hashCode(); + result = 31 * result + this.subtype.hashCode(); + result = 31 * result + this.parameters.hashCode(); return result; } @@ -330,15 +259,82 @@ public final class MediaType implements Comparable { return builder.toString(); } + private void appendTo(StringBuilder builder) { + builder.append(this.type); + builder.append('/'); + builder.append(this.subtype); + for (Map.Entry entry :this. parameters.entrySet()) { + builder.append(';'); + builder.append(entry.getKey()); + builder.append('='); + builder.append(entry.getValue()); + } + } + + /** - * Returns a string representation of the given list of {@link MediaType} objects. This method can be used to for an - * Accept or Content-Type header. - * + * Parse the given String into a single {@link MediaType}. + * @param mediaType the string to parse + * @return the media type + * @throws IllegalArgumentException if the string cannot be parsed + */ + public static MediaType parseMediaType(String mediaType) { + Assert.hasLength(mediaType, "'mediaType' must not be empty"); + String[] parts = StringUtils.tokenizeToStringArray(mediaType, ";"); + + Map parameters; + if (parts.length <= 1) { + parameters = null; + } + else { + parameters = new LinkedHashMap(parts.length - 1); + } + for (int i = 1; i < parts.length; i++) { + String part = parts[i]; + int idx = part.indexOf('='); + if (idx != -1) { + String name = part.substring(0, idx); + String value = part.substring(idx + 1, part.length()); + parameters.put(name, value); + } + } + String fullType = parts[0].trim(); + + // java.net.HttpURLConnection returns a *; q=.2 Accept header + if (WILDCARD_TYPE.equals(fullType)) { + fullType = "*/*"; + } + int idx = fullType.indexOf('/'); + String type = fullType.substring(0, idx); + String subtype = fullType.substring(idx + 1, fullType.length()); + return new MediaType(type, subtype, parameters); + } + + /** + * Parse the given, comma-seperated string into a list of {@link MediaType} objects. + *

This method can be used to parse an Accept or Content-Type header. * @param mediaTypes the string to parse * @return the list of media types * @throws IllegalArgumentException if the string cannot be parsed */ - public static String toString(List mediaTypes) { + public static List parseMediaTypes(String mediaTypes) { + Assert.hasLength(mediaTypes, "'mediaTypes' must not be empty"); + String[] tokens = mediaTypes.split(",\\s*"); + List result = new ArrayList(tokens.length); + for (String token : tokens) { + result.add(parseMediaType(token)); + } + return result; + } + + /** + * Return a string representation of the given list of {@link MediaType} objects. + *

This method can be used to for an Accept or Content-Type header. + * @param mediaTypes the string to parse + * @return the list of media types + * @throws IllegalArgumentException if the String cannot be parsed + */ + public static String toString(Collection mediaTypes) { StringBuilder builder = new StringBuilder(); for (Iterator iterator = mediaTypes.iterator(); iterator.hasNext();) { MediaType mediaType = iterator.next(); @@ -350,15 +346,4 @@ public final class MediaType implements Comparable { return builder.toString(); } - private void appendTo(StringBuilder builder) { - builder.append(type); - builder.append('/'); - builder.append(subtype); - for (Map.Entry entry : parameters.entrySet()) { - builder.append(';'); - builder.append(entry.getKey()); - builder.append('='); - builder.append(entry.getValue()); - } - } } diff --git a/org.springframework.web/src/main/java/org/springframework/http/MediaTypeEditor.java b/org.springframework.web/src/main/java/org/springframework/http/MediaTypeEditor.java new file mode 100644 index 0000000000..1ae84df853 --- /dev/null +++ b/org.springframework.web/src/main/java/org/springframework/http/MediaTypeEditor.java @@ -0,0 +1,50 @@ +/* + * Copyright 2002-2009 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.http; + +import java.beans.PropertyEditorSupport; + +import org.springframework.util.StringUtils; + +/** + * {@link java.beans.PropertyEditor Editor} for {@link MediaType} + * descriptors, to automatically convert String specifications + * (e.g. "text/html") to MediaType properties. + * + * @author Juergen Hoeller + * @since 3.0 + * @see MediaType + */ +public class MediaTypeEditor extends PropertyEditorSupport { + + @Override + public void setAsText(String text) { + if (StringUtils.hasText(text)) { + setValue(MediaType.parseMediaType(text)); + } + else { + setValue(null); + } + } + + @Override + public String getAsText() { + MediaType mediaType = (MediaType) getValue(); + return (mediaType != null ? mediaType.toString() : ""); + } + +} diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/client/AbstractClientHttpRequest.java b/org.springframework.web/src/main/java/org/springframework/http/client/AbstractClientHttpRequest.java similarity index 78% rename from org.springframework.web/src/main/java/org/springframework/web/http/client/AbstractClientHttpRequest.java rename to org.springframework.web/src/main/java/org/springframework/http/client/AbstractClientHttpRequest.java index 85c0326f27..ab479b11ca 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/client/AbstractClientHttpRequest.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/AbstractClientHttpRequest.java @@ -14,14 +14,14 @@ * limitations under the License. */ -package org.springframework.web.http.client; +package org.springframework.http.client; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import org.springframework.util.Assert; -import org.springframework.web.http.HttpHeaders; +import org.springframework.http.HttpHeaders; /** * Abstract base for {@link ClientHttpRequest} that makes sure that headers and body are not written multiple times. @@ -37,31 +37,36 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest { private final ByteArrayOutputStream bufferedOutput = new ByteArrayOutputStream(); + public final HttpHeaders getHeaders() { checkExecuted(); - return headers; + return this.headers; } public final OutputStream getBody() throws IOException { checkExecuted(); - return bufferedOutput; + return this.bufferedOutput; } public final ClientHttpResponse execute() throws IOException { checkExecuted(); - ClientHttpResponse result = executeInternal(headers, bufferedOutput.toByteArray()); - executed = true; + ClientHttpResponse result = executeInternal(this.headers, this.bufferedOutput.toByteArray()); + this.executed = true; return result; } + private void checkExecuted() { + Assert.state(!this.executed, "ClientHttpRequest already executed"); + } + + /** * Abstract template method that writes the given headers and content to the HTTP request. + * @param headers the HTTP headers + * @param bufferedOutput the body content + * @return the response object for the executed request */ protected abstract ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException; - private void checkExecuted() { - Assert.state(!executed, "ClientRequest already executed"); - } - } diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/client/ClientHttpRequest.java b/org.springframework.web/src/main/java/org/springframework/http/client/ClientHttpRequest.java similarity index 67% rename from org.springframework.web/src/main/java/org/springframework/web/http/client/ClientHttpRequest.java rename to org.springframework.web/src/main/java/org/springframework/http/client/ClientHttpRequest.java index ab41cc7f5f..7b4521c61a 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/client/ClientHttpRequest.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/ClientHttpRequest.java @@ -14,33 +14,33 @@ * limitations under the License. */ -package org.springframework.web.http.client; +package org.springframework.http.client; import java.io.IOException; -import org.springframework.web.http.HttpMethod; -import org.springframework.web.http.HttpOutputMessage; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpOutputMessage; /** - * Represents a client-side HTTP request. Created via an implementation of the {@link ClientHttpRequestFactory}.

A - * HttpRequest can be {@linkplain #execute() executed}, getting a {@link ClientHttpResponse} which can be - * read from. + * Represents a client-side HTTP request. Created via an implementation of the {@link ClientHttpRequestFactory}. + * + *

A HttpRequest can be {@linkplain #execute() executed}, getting a {@link ClientHttpResponse} + * which can be read from. * * @author Arjen Poutsma + * @since 3.0 * @see ClientHttpRequestFactory#createRequest(java.net.URI, HttpMethod) */ public interface ClientHttpRequest extends HttpOutputMessage { /** - * Returns the HTTP method of the request. - * - * @return the HTTP method + * Return the HTTP method of the request. + * @return the HTTP method as an HttpMethod enum value */ HttpMethod getMethod(); /** - * Executes this request, resulting in a {@link ClientHttpResponse} that can be read. - * + * Execute this request, resulting in a {@link ClientHttpResponse} that can be read. * @return the response result of the execution * @throws IOException in case of I/O errors */ diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/client/ClientHttpRequestFactory.java b/org.springframework.web/src/main/java/org/springframework/http/client/ClientHttpRequestFactory.java similarity index 68% rename from org.springframework.web/src/main/java/org/springframework/web/http/client/ClientHttpRequestFactory.java rename to org.springframework.web/src/main/java/org/springframework/http/client/ClientHttpRequestFactory.java index d44282345a..4a2a502e1a 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/client/ClientHttpRequestFactory.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/ClientHttpRequestFactory.java @@ -14,16 +14,16 @@ * limitations under the License. */ -package org.springframework.web.http.client; +package org.springframework.http.client; import java.io.IOException; import java.net.URI; -import org.springframework.web.http.HttpMethod; +import org.springframework.http.HttpMethod; /** - * Factory for {@link ClientHttpRequest} objects. Requests are created by the {@link #createRequest(URI, HttpMethod)} - * method. + * Factory for {@link ClientHttpRequest} objects. + * Requests are created by the {@link #createRequest(URI, HttpMethod)} method. * * @author Arjen Poutsma * @since 3.0 @@ -31,10 +31,10 @@ import org.springframework.web.http.HttpMethod; public interface ClientHttpRequestFactory { /** - * Creates a new {@link ClientHttpRequest} for the specified URI and HTTP method. The returned request can be written - * to, and then executed by calling {@link ClientHttpRequest#execute()}. - * - * @param uri the URI to create a request for + * Create a new {@link ClientHttpRequest} for the specified URI and HTTP method. + *

The returned request can be written to, and then executed by calling + * {@link ClientHttpRequest#execute()}. + * @param uri the URI to create a request for * @param httpMethod the HTTP method to execute * @return the created request * @throws IOException in case of I/O errors diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/client/ClientHttpResponse.java b/org.springframework.web/src/main/java/org/springframework/http/client/ClientHttpResponse.java similarity index 71% rename from org.springframework.web/src/main/java/org/springframework/web/http/client/ClientHttpResponse.java rename to org.springframework.web/src/main/java/org/springframework/http/client/ClientHttpResponse.java index 9523b72efd..aba6fb0699 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/client/ClientHttpResponse.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/ClientHttpResponse.java @@ -14,16 +14,18 @@ * limitations under the License. */ -package org.springframework.web.http.client; +package org.springframework.http.client; import java.io.IOException; -import org.springframework.web.http.HttpInputMessage; -import org.springframework.web.http.HttpStatus; +import org.springframework.http.HttpInputMessage; +import org.springframework.http.HttpStatus; /** - * Represents a client-side HTTP response. Obtained via an calling of the {@link ClientHttpRequest#execute()}.

A - * HttpResponse must be {@linkplain #close() closed}, typically in a finally block. + * Represents a client-side HTTP response. Obtained via an calling of the {@link ClientHttpRequest#execute()}. + * + *

A ClientHttpResponse must be {@linkplain #close() closed}, typically in a + * finally block. * * @author Arjen Poutsma * @since 3.0 @@ -31,16 +33,14 @@ import org.springframework.web.http.HttpStatus; public interface ClientHttpResponse extends HttpInputMessage { /** - * Returns the HTTP status code of the response. - * - * @return the HTTP status + * Return the HTTP status code of the response. + * @return the HTTP status as an HttpStatus enum value * @throws IOException in case of I/O errors */ HttpStatus getStatusCode() throws IOException; /** - * Returns the HTTP status text of the response. - * + * Return the HTTP status text of the response. * @return the HTTP status text * @throws IOException in case of I/O errors */ diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/client/commons/CommonsClientHttpRequest.java b/org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequest.java similarity index 79% rename from org.springframework.web/src/main/java/org/springframework/web/http/client/commons/CommonsClientHttpRequest.java rename to org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequest.java index f569b20f52..ebf2df4d5f 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/client/commons/CommonsClientHttpRequest.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.http.client.commons; +package org.springframework.http.client; import java.io.IOException; import java.util.List; @@ -26,16 +26,19 @@ import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; import org.apache.commons.httpclient.methods.EntityEnclosingMethod; import org.apache.commons.httpclient.methods.RequestEntity; -import org.springframework.web.http.HttpHeaders; -import org.springframework.web.http.HttpMethod; -import org.springframework.web.http.client.AbstractClientHttpRequest; -import org.springframework.web.http.client.ClientHttpResponse; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.client.AbstractClientHttpRequest; +import org.springframework.http.client.ClientHttpResponse; /** - * {@link org.springframework.web.http.client.ClientHttpRequest} implementation that uses Commons Http Client to execute - * requests. Created via the {@link CommonsClientHttpRequestFactory}. + * {@link org.springframework.http.client.ClientHttpRequest} implementation that uses + * Apache Commons HttpClient to execute requests. + * + *

Created via the {@link CommonsClientHttpRequestFactory}. * * @author Arjen Poutsma + * @since 3.0 * @see CommonsClientHttpRequestFactory#createRequest(java.net.URI, HttpMethod) */ final class CommonsClientHttpRequest extends AbstractClientHttpRequest { @@ -44,7 +47,8 @@ final class CommonsClientHttpRequest extends AbstractClientHttpRequest { private final HttpMethodBase httpMethod; - CommonsClientHttpRequest(HttpClient httpClient, HttpMethodBase httpMethod) { + + public CommonsClientHttpRequest(HttpClient httpClient, HttpMethodBase httpMethod) { this.httpClient = httpClient; this.httpMethod = httpMethod; } diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/client/commons/CommonsClientHttpRequestFactory.java b/org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequestFactory.java similarity index 60% rename from org.springframework.web/src/main/java/org/springframework/web/http/client/commons/CommonsClientHttpRequestFactory.java rename to org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequestFactory.java index cbe4e6cfc1..96ca8f1610 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/client/commons/CommonsClientHttpRequestFactory.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpRequestFactory.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.http.client.commons; +package org.springframework.http.client; import java.io.IOException; import java.net.URI; @@ -33,17 +33,18 @@ import org.apache.commons.httpclient.methods.TraceMethod; import org.springframework.beans.factory.DisposableBean; import org.springframework.util.Assert; -import org.springframework.web.http.HttpMethod; -import org.springframework.web.http.client.ClientHttpRequest; -import org.springframework.web.http.client.ClientHttpRequestFactory; +import org.springframework.http.HttpMethod; /** - * {@link org.springframework.web.http.client.ClientHttpRequestFactory} implementation that uses Jakarta Commons HttpClient to create requests.

Allows to - * use a pre-configured {@link HttpClient} instance, potentially with authentication, HTTP connection pooling, etc. + * {@link org.springframework.http.client.ClientHttpRequestFactory} implementation that uses + * Jakarta Commons HttpClient to create requests. + * + *

Allows to use a pre-configured {@link HttpClient} instance - + * potentially with authentication, HTTP connection pooling, etc. * * @author Arjen Poutsma - * @see org.springframework.web.http.client.SimpleClientHttpRequestFactory + * @since 3.0 + * @see org.springframework.http.client.SimpleClientHttpRequestFactory */ public class CommonsClientHttpRequestFactory implements ClientHttpRequestFactory, DisposableBean { @@ -51,9 +52,10 @@ public class CommonsClientHttpRequestFactory implements ClientHttpRequestFactory private HttpClient httpClient; + /** - * Create a new instance of the CommonsHttpRequestFactory with a default {@link HttpClient} that uses a - * default {@link MultiThreadedHttpConnectionManager}. + * Create a new instance of the CommonsHttpRequestFactory with a default + * {@link HttpClient} that uses a default {@link MultiThreadedHttpConnectionManager}. */ public CommonsClientHttpRequestFactory() { httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); @@ -61,32 +63,32 @@ public class CommonsClientHttpRequestFactory implements ClientHttpRequestFactory } /** - * Create a new instance of the CommonsHttpRequestFactory with the given {@link HttpClient} instance. - * - * @param httpClient the HttpClient instance to use for this sender + * Create a new instance of the CommonsHttpRequestFactory with the given + * {@link HttpClient} instance. + * @param httpClient the HttpClient instance to use for this factory */ public CommonsClientHttpRequestFactory(HttpClient httpClient) { Assert.notNull(httpClient, "httpClient must not be null"); this.httpClient = httpClient; } - /** - * Returns the HttpClient used by this message sender. - */ - public HttpClient getHttpClient() { - return httpClient; - } /** - * Set the HttpClient used by this message sender. + * Set the HttpClient used by this factory. */ public void setHttpClient(HttpClient httpClient) { this.httpClient = httpClient; } + /** + * Return the HttpClient used by this factory. + */ + public HttpClient getHttpClient() { + return this.httpClient; + } + /** * Set the socket read timeout for the underlying HttpClient. A value of 0 means never timeout. - * * @param timeout the timeout value in milliseconds * @see org.apache.commons.httpclient.params.HttpConnectionManagerParams#setSoTimeout(int) */ @@ -97,52 +99,59 @@ public class CommonsClientHttpRequestFactory implements ClientHttpRequestFactory this.httpClient.getHttpConnectionManager().getParams().setSoTimeout(timeout); } - public void destroy() throws Exception { + + public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException { + HttpMethodBase commonsHttpMethod = createCommonsHttpMethod(httpMethod, uri.toString()); + postProcessCommonsHttpMethod(commonsHttpMethod); + return new CommonsClientHttpRequest(getHttpClient(), commonsHttpMethod); + } + + /** + * Create a Commons HttpMethodBase object for the given HTTP method + * and URI specification. + * @param httpMethod the HTTP method + * @param uri the URI + * @return the Commons HttpMethodBase object + */ + protected HttpMethodBase createCommonsHttpMethod(HttpMethod httpMethod, String uri) { + switch (httpMethod) { + case GET: + return new GetMethod(uri); + case DELETE: + return new DeleteMethod(uri); + case HEAD: + return new HeadMethod(uri); + case OPTIONS: + return new OptionsMethod(uri); + case POST: + return new PostMethod(uri); + case PUT: + return new PutMethod(uri); + case TRACE: + return new TraceMethod(uri); + default: + throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod); + } + } + + /** + * Template method that allows for manipulating the {@link org.apache.commons.httpclient.HttpMethodBase} + * before it is returned as part of a {@link CommonsClientHttpRequest}. + *

The default implementation is empty. + * @param httpMethod the Commons HTTP method object to process + */ + protected void postProcessCommonsHttpMethod(HttpMethodBase httpMethod) { + } + + /** + * Shutdown hook that closes the underlying {@link HttpConnectionManager}'s + * connection pool, if any. + */ + public void destroy() { HttpConnectionManager connectionManager = getHttpClient().getHttpConnectionManager(); if (connectionManager instanceof MultiThreadedHttpConnectionManager) { ((MultiThreadedHttpConnectionManager) connectionManager).shutdown(); } } - public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException { - String uriString = uri.toString(); - HttpMethodBase httpMethodBase; - switch (httpMethod) { - case GET: - httpMethodBase = new GetMethod(uriString); - break; - case DELETE: - httpMethodBase = new DeleteMethod(uriString); - break; - case HEAD: - httpMethodBase = new HeadMethod(uriString); - break; - case OPTIONS: - httpMethodBase = new OptionsMethod(uriString); - break; - case POST: - httpMethodBase = new PostMethod(uriString); - break; - case PUT: - httpMethodBase = new PutMethod(uriString); - break; - case TRACE: - httpMethodBase = new TraceMethod(uriString); - break; - default: - throw new IllegalArgumentException("Invalid method: " + httpMethod); - } - process(httpMethodBase); - - return new CommonsClientHttpRequest(getHttpClient(), httpMethodBase); - } - - /** - * Template method that allows for manipulating the {@link org.apache.commons.httpclient.HttpMethodBase} before it is - * returned as part of a {@link CommonsClientHttpRequest}.

Default implementation is empty. - * - * @param httpMethod the Commons HTTP method to process - */ - protected void process(HttpMethodBase httpMethod) { - } -} \ No newline at end of file +} diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/client/commons/CommonsClientHttpResponse.java b/org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpResponse.java similarity index 80% rename from org.springframework.web/src/main/java/org/springframework/web/http/client/commons/CommonsClientHttpResponse.java rename to org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpResponse.java index 52be8c634e..c2b2e550ea 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/client/commons/CommonsClientHttpResponse.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpResponse.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.http.client.commons; +package org.springframework.http.client; import java.io.IOException; import java.io.InputStream; @@ -22,15 +22,18 @@ import java.io.InputStream; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpMethod; -import org.springframework.web.http.HttpHeaders; -import org.springframework.web.http.HttpStatus; -import org.springframework.web.http.client.ClientHttpResponse; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.client.ClientHttpResponse; /** - * {@link org.springframework.web.http.client.ClientHttpResponse} implementation that uses Commons Http Client to - * execute requests. Created via the {@link CommonsClientHttpRequest}. + * {@link org.springframework.http.client.ClientHttpResponse} implementation that uses + * Apache Commons HttpClient to execute requests. + * + *

Created via the {@link CommonsClientHttpRequest}. * * @author Arjen Poutsma + * @since 3.0 * @see CommonsClientHttpRequest#execute() */ final class CommonsClientHttpResponse implements ClientHttpResponse { diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/client/SimpleClientHttpRequest.java b/org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequest.java similarity index 71% rename from org.springframework.web/src/main/java/org/springframework/web/http/client/SimpleClientHttpRequest.java rename to org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequest.java index 70e45b0cbd..d4ec106de0 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/client/SimpleClientHttpRequest.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.http.client; +package org.springframework.http.client; import java.io.IOException; import java.net.HttpURLConnection; @@ -22,27 +22,29 @@ import java.util.List; import java.util.Map; import org.springframework.util.FileCopyUtils; -import org.springframework.web.http.HttpHeaders; -import org.springframework.web.http.HttpMethod; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; /** - * {@link ClientHttpRequest} implementation that uses standard J2SE facilities to execute requests. Created via the - * {@link SimpleClientHttpRequestFactory}. + * {@link ClientHttpRequest} implementation that uses standard J2SE facilities to execute requests. + * Created via the {@link SimpleClientHttpRequestFactory}. * * @author Arjen Poutsma - * @see SimpleClientHttpRequestFactory#createRequest(java.net.URI, HttpMethod) * @since 3.0 + * @see SimpleClientHttpRequestFactory#createRequest(java.net.URI, HttpMethod) */ final class SimpleClientHttpRequest extends AbstractClientHttpRequest { private final HttpURLConnection connection; - SimpleClientHttpRequest(HttpURLConnection connection) { + + public SimpleClientHttpRequest(HttpURLConnection connection) { this.connection = connection; } + public HttpMethod getMethod() { - return HttpMethod.valueOf(connection.getRequestMethod()); + return HttpMethod.valueOf(this.connection.getRequestMethod()); } @Override @@ -50,12 +52,12 @@ final class SimpleClientHttpRequest extends AbstractClientHttpRequest { for (Map.Entry> entry : headers.entrySet()) { String headerName = entry.getKey(); for (String headerValue : entry.getValue()) { - connection.addRequestProperty(headerName, headerValue); + this.connection.addRequestProperty(headerName, headerValue); } } - connection.connect(); - FileCopyUtils.copy(bufferedOutput, connection.getOutputStream()); - return new SimpleClientHttpResponse(connection); + this.connection.connect(); + FileCopyUtils.copy(bufferedOutput, this.connection.getOutputStream()); + return new SimpleClientHttpResponse(this.connection); } } diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/client/SimpleClientHttpRequestFactory.java b/org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java similarity index 82% rename from org.springframework.web/src/main/java/org/springframework/web/http/client/SimpleClientHttpRequestFactory.java rename to org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java index 7260f693d8..7deeb716c9 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/client/SimpleClientHttpRequestFactory.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java @@ -14,30 +14,28 @@ * limitations under the License. */ -package org.springframework.web.http.client; +package org.springframework.http.client; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URI; -import java.net.URL; import java.net.URLConnection; import org.springframework.util.Assert; -import org.springframework.web.http.HttpMethod; +import org.springframework.http.HttpMethod; /** * {@link ClientHttpRequestFactory} implementation that uses standard J2SE facilities. * * @author Arjen Poutsma - * @see java.net.HttpURLConnection - * @see org.springframework.web.http.client.commons.CommonsClientHttpRequestFactory * @since 3.0 + * @see java.net.HttpURLConnection + * @see CommonsClientHttpRequestFactory */ public class SimpleClientHttpRequestFactory implements ClientHttpRequestFactory { public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException { - URL url = uri.toURL(); - URLConnection urlConnection = url.openConnection(); + URLConnection urlConnection = uri.toURL().openConnection(); Assert.isInstanceOf(HttpURLConnection.class, urlConnection); HttpURLConnection connection = (HttpURLConnection) urlConnection; prepareConnection(connection, httpMethod.name()); @@ -46,9 +44,7 @@ public class SimpleClientHttpRequestFactory implements ClientHttpRequestFactory /** * Template method for preparing the given {@link HttpURLConnection}. - * - *

Default implementation prepares the connection for input and output, and sets the HTTP method. - * + *

The default implementation prepares the connection for input and output, and sets the HTTP method. * @param connection the connection to prepare * @param httpMethod the HTTP request method ({@code GET}, {@code POST}, etc.) * @throws IOException in case of I/O errors diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/client/SimpleClientHttpResponse.java b/org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java similarity index 65% rename from org.springframework.web/src/main/java/org/springframework/web/http/client/SimpleClientHttpResponse.java rename to org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java index 63ab58f1e6..9379cd5b8e 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/client/SimpleClientHttpResponse.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java @@ -14,19 +14,19 @@ * limitations under the License. */ -package org.springframework.web.http.client; +package org.springframework.http.client; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import org.springframework.util.StringUtils; -import org.springframework.web.http.HttpHeaders; -import org.springframework.web.http.HttpStatus; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; /** - * {@link ClientHttpResponse} implementation that uses standard J2SE facilities. Obtained via the {@link - * SimpleClientHttpRequest#execute()}. + * {@link ClientHttpResponse} implementation that uses standard J2SE facilities. + * Obtained via the {@link SimpleClientHttpRequest#execute()}. * * @author Arjen Poutsma * @since 3.0 @@ -37,45 +37,44 @@ final class SimpleClientHttpResponse implements ClientHttpResponse { private HttpHeaders headers; - SimpleClientHttpResponse(HttpURLConnection connection) { + + public SimpleClientHttpResponse(HttpURLConnection connection) { this.connection = connection; } + public HttpStatus getStatusCode() throws IOException { - return HttpStatus.valueOf(connection.getResponseCode()); + return HttpStatus.valueOf(this.connection.getResponseCode()); } public String getStatusText() throws IOException { - return connection.getResponseMessage(); + return this.connection.getResponseMessage(); } public HttpHeaders getHeaders() { - if (headers == null) { - headers = new HttpHeaders(); + if (this.headers == null) { + this.headers = new HttpHeaders(); // Header field 0 is the status line, so we start at 1 int i = 1; while (true) { - String name = connection.getHeaderFieldKey(i); + String name = this.connection.getHeaderFieldKey(i); if (!StringUtils.hasLength(name)) { break; } - headers.add(name, connection.getHeaderField(i)); + this.headers.add(name, this.connection.getHeaderField(i)); i++; } } - return headers; + return this.headers; } public InputStream getBody() throws IOException { - if (connection.getErrorStream() == null) { - return connection.getInputStream(); - } - else { - return connection.getErrorStream(); - } + InputStream errorStream = this.connection.getErrorStream(); + return (errorStream != null ? errorStream : this.connection.getInputStream()); } public void close() { - connection.disconnect(); + this.connection.disconnect(); } + } diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/client/package.html b/org.springframework.web/src/main/java/org/springframework/http/client/package.html similarity index 100% rename from org.springframework.web/src/main/java/org/springframework/web/http/client/package.html rename to org.springframework.web/src/main/java/org/springframework/http/client/package.html diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/support/HttpAccessor.java b/org.springframework.web/src/main/java/org/springframework/http/client/support/HttpAccessor.java similarity index 63% rename from org.springframework.web/src/main/java/org/springframework/web/client/support/HttpAccessor.java rename to org.springframework.web/src/main/java/org/springframework/http/client/support/HttpAccessor.java index e3e868d352..1b2ecd2b1b 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/client/support/HttpAccessor.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/support/HttpAccessor.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.client.support; +package org.springframework.http.client.support; import java.io.IOException; import java.net.URI; @@ -23,19 +23,21 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.util.Assert; -import org.springframework.web.http.HttpMethod; -import org.springframework.web.http.client.ClientHttpRequest; -import org.springframework.web.http.client.ClientHttpRequestFactory; -import org.springframework.web.http.client.SimpleClientHttpRequestFactory; +import org.springframework.http.HttpMethod; +import org.springframework.http.client.ClientHttpRequest; +import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.SimpleClientHttpRequestFactory; /** - * Base class for {@link org.springframework.web.client.core.RestTemplate} and other HTTP accessing gateway helpers, defining - * common properties such as the {@link ClientHttpRequestFactory} to operate on. - *

- * Not intended to be used directly. See {@link org.springframework.web.client.core.RestTemplate}. + * Base class for {@link org.springframework.web.client.RestTemplate} + * and other HTTP accessing gateway helpers, defining common properties + * such as the {@link ClientHttpRequestFactory} to operate on. + * + *

Not intended to be used directly. See {@link org.springframework.web.client.RestTemplate}. * * @author Arjen Poutsma - * @see org.springframework.web.client.core.RestTemplate + * @since 3.0 + * @see org.springframework.web.client.RestTemplate */ public abstract class HttpAccessor { @@ -46,15 +48,9 @@ public abstract class HttpAccessor { private ClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); - /** - * Returns the request factory that this accessor uses for obtaining {@link ClientHttpRequest HttpRequests} - */ - public ClientHttpRequestFactory getRequestFactory() { - return requestFactory; - } /** - * Sets the request factory that this accessor uses for obtaining {@link ClientHttpRequest HttpRequests} + * Set the request factory that this accessor uses for obtaining {@link ClientHttpRequest HttpRequests}. */ public void setRequestFactory(ClientHttpRequestFactory requestFactory) { Assert.notNull(requestFactory, "'requestFactory' must not be null"); @@ -62,9 +58,16 @@ public abstract class HttpAccessor { } /** - * Creates a new {@link ClientHttpRequest} via this template's {@link ClientHttpRequestFactory}. - * - * @param url the URL to connect to + * Return the request factory that this accessor uses for obtaining {@link ClientHttpRequest HttpRequests}. + */ + public ClientHttpRequestFactory getRequestFactory() { + return this.requestFactory; + } + + + /** + * Create a new {@link ClientHttpRequest} via this template's {@link ClientHttpRequestFactory}. + * @param url the URL to connect to * @param method the HTTP method to exectute (GET, POST, etc.) * @return the created request * @throws IOException in case of I/O errors @@ -76,4 +79,5 @@ public abstract class HttpAccessor { } return request; } + } diff --git a/org.springframework.web/src/main/java/org/springframework/http/client/support/package.html b/org.springframework.web/src/main/java/org/springframework/http/client/support/package.html new file mode 100644 index 0000000000..b6dbef257a --- /dev/null +++ b/org.springframework.web/src/main/java/org/springframework/http/client/support/package.html @@ -0,0 +1,8 @@ + + + +This package provides generic HTTP support classes, +to be used by higher-level classes like RestTemplate. + + + diff --git a/org.springframework.web/src/main/java/org/springframework/web/converter/AbstractHttpMessageConverter.java b/org.springframework.web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java similarity index 73% rename from org.springframework.web/src/main/java/org/springframework/web/converter/AbstractHttpMessageConverter.java rename to org.springframework.web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java index b9a08d3284..5d990a2528 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/converter/AbstractHttpMessageConverter.java +++ b/org.springframework.web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.converter; +package org.springframework.http.converter; import java.io.IOException; import java.util.ArrayList; @@ -26,69 +26,66 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.util.Assert; -import org.springframework.util.MediaType; -import org.springframework.web.http.HttpHeaders; -import org.springframework.web.http.HttpOutputMessage; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpOutputMessage; +import org.springframework.http.MediaType; /** * Abstract base class for most {@link HttpMessageConverter} implementations. * *

This base class adds support for setting supported {@code MediaTypes}, through the {@link - * #setSupportedMediaTypes(List) supportedMediaTypes} bean property. It also adds support for {@code Content-Type} and - * {@code Content-Length} when writing to output messages. + * #setSupportedMediaTypes(List) supportedMediaTypes} bean property. It also adds support for + * {@code Content-Type} and {@code Content-Length} when writing to output messages. * * @author Arjen Poutsma * @since 3.0 */ public abstract class AbstractHttpMessageConverter implements HttpMessageConverter { - /** - * Logger available to subclasses. - */ + /** Logger available to subclasses */ protected final Log logger = LogFactory.getLog(getClass()); private List supportedMediaTypes = Collections.emptyList(); + /** - * Constructs an {@code AbstractHttpMessageConverter} with no supported media types. - * - * @see #setSupportedMediaTypes(List) + * Construct an {@code AbstractHttpMessageConverter} with no supported media types. + * @see #setSupportedMediaTypes */ protected AbstractHttpMessageConverter() { } /** - * Constructs an {@code AbstractHttpMessageConverter} with one supported media type. + * Construct an {@code AbstractHttpMessageConverter} with one supported media type. */ protected AbstractHttpMessageConverter(MediaType supportedMediaType) { - setSupportedMediaTypes(Collections.singletonList(supportedMediaType)); + this.supportedMediaTypes = Collections.singletonList(supportedMediaType); } /** - * Constructs an {@code AbstractHttpMessageConverter} with multiple supported media type. + * Construct an {@code AbstractHttpMessageConverter} with multiple supported media type. */ protected AbstractHttpMessageConverter(MediaType... supportedMediaTypes) { - setSupportedMediaTypes(Arrays.asList(supportedMediaTypes)); + this.supportedMediaTypes = Arrays.asList(supportedMediaTypes); } - public List getSupportedMediaTypes() { - return Collections.unmodifiableList(supportedMediaTypes); - } /** - * Sets the list of {@link MediaType} objects supported by this converter. + * Set the list of {@link MediaType} objects supported by this converter. */ public void setSupportedMediaTypes(List supportedMediaTypes) { Assert.notEmpty(supportedMediaTypes, "'supportedMediaTypes' must not be empty"); this.supportedMediaTypes = new ArrayList(supportedMediaTypes); } + public List getSupportedMediaTypes() { + return Collections.unmodifiableList(this.supportedMediaTypes); + } + /** - * {@inheritDoc} - * - *

This implementation delegates to {@link #getContentType(Object)} and {@link #getContentLength(Object)}, and sets - * the corresponding headers on the output message. It then calls {@link #writeToInternal(Object, HttpOutputMessage)}. - * + *

This implementation delegates to {@link #getContentType(Object)} and {@link #getContentLength(Object)}, + * and sets the corresponding headers on the output message. It then calls + * {@link #writeToInternal(Object, HttpOutputMessage)}. * @throws HttpMessageConversionException in case of conversion errors */ public final void write(T t, HttpOutputMessage outputMessage) throws IOException { @@ -107,23 +104,19 @@ public abstract class AbstractHttpMessageConverter implements HttpMessageConv /** * Returns the content type for the given type. - * *

By default, this returns the first element of the {@link #setSupportedMediaTypes(List) supportedMediaTypes} * property, if any. Can be overriden in subclasses. - * * @param t the type to return the content type for * @return the content type, or null if not known */ protected MediaType getContentType(T t) { List mediaTypes = getSupportedMediaTypes(); - return !mediaTypes.isEmpty() ? mediaTypes.get(0) : null; + return (!mediaTypes.isEmpty() ? mediaTypes.get(0) : null); } /** * Returns the content length for the given type. - * *

By default, this returns null. Can be overriden in subclasses. - * * @param t the type to return the content length for * @return the content length, or null if not known */ @@ -133,10 +126,9 @@ public abstract class AbstractHttpMessageConverter implements HttpMessageConv /** * Abstract template method that writes the actualy body. Invoked from {@link #write(Object, HttpOutputMessage)}. - * - * @param t the object to write to the output message + * @param t the object to write to the output message * @param outputMessage the message to write to - * @throws IOException in case of I/O errors + * @throws IOException in case of I/O errors * @throws HttpMessageConversionException in case of conversion errors */ protected abstract void writeToInternal(T t, HttpOutputMessage outputMessage) throws IOException; diff --git a/org.springframework.web/src/main/java/org/springframework/web/converter/ByteArrayHttpMessageConverter.java b/org.springframework.web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java similarity index 91% rename from org.springframework.web/src/main/java/org/springframework/web/converter/ByteArrayHttpMessageConverter.java rename to org.springframework.web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java index c64455a7bc..8551f29cd8 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/converter/ByteArrayHttpMessageConverter.java +++ b/org.springframework.web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java @@ -14,15 +14,15 @@ * limitations under the License. */ -package org.springframework.web.converter; +package org.springframework.http.converter; import java.io.ByteArrayOutputStream; import java.io.IOException; import org.springframework.util.FileCopyUtils; -import org.springframework.util.MediaType; -import org.springframework.web.http.HttpInputMessage; -import org.springframework.web.http.HttpOutputMessage; +import org.springframework.http.MediaType; +import org.springframework.http.HttpInputMessage; +import org.springframework.http.HttpOutputMessage; /** * Implementation of {@link HttpMessageConverter} that can read and write byte arrays. @@ -45,7 +45,7 @@ public class ByteArrayHttpMessageConverter extends AbstractHttpMessageConverter< } public boolean supports(Class clazz) { - return byte[].class == clazz; + return byte[].class.equals(clazz); } public byte[] read(Class clazz, HttpInputMessage inputMessage) throws IOException { @@ -75,5 +75,4 @@ public class ByteArrayHttpMessageConverter extends AbstractHttpMessageConverter< FileCopyUtils.copy(bytes, outputMessage.getBody()); } - } diff --git a/org.springframework.web/src/main/java/org/springframework/web/converter/HttpMessageConversionException.java b/org.springframework.web/src/main/java/org/springframework/http/converter/HttpMessageConversionException.java similarity index 93% rename from org.springframework.web/src/main/java/org/springframework/web/converter/HttpMessageConversionException.java rename to org.springframework.web/src/main/java/org/springframework/http/converter/HttpMessageConversionException.java index f3bba11403..f7120c6e27 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/converter/HttpMessageConversionException.java +++ b/org.springframework.web/src/main/java/org/springframework/http/converter/HttpMessageConversionException.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.converter; +package org.springframework.http.converter; import org.springframework.core.NestedRuntimeException; @@ -28,7 +28,6 @@ public class HttpMessageConversionException extends NestedRuntimeException { /** * Create a new MessageConversionException. - * * @param msg the detail message */ public HttpMessageConversionException(String msg) { @@ -37,11 +36,11 @@ public class HttpMessageConversionException extends NestedRuntimeException { /** * Create a new MessageConversionException. - * - * @param msg the detail message + * @param msg the detail message * @param cause the root cause (if any) */ public HttpMessageConversionException(String msg, Throwable cause) { super(msg, cause); } + } diff --git a/org.springframework.web/src/main/java/org/springframework/web/converter/HttpMessageConverter.java b/org.springframework.web/src/main/java/org/springframework/http/converter/HttpMessageConverter.java similarity index 65% rename from org.springframework.web/src/main/java/org/springframework/web/converter/HttpMessageConverter.java rename to org.springframework.web/src/main/java/org/springframework/http/converter/HttpMessageConverter.java index bbca0e7083..ff7242abfa 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/converter/HttpMessageConverter.java +++ b/org.springframework.web/src/main/java/org/springframework/http/converter/HttpMessageConverter.java @@ -14,17 +14,17 @@ * limitations under the License. */ -package org.springframework.web.converter; +package org.springframework.http.converter; import java.io.IOException; import java.util.List; -import org.springframework.util.MediaType; -import org.springframework.web.http.HttpInputMessage; -import org.springframework.web.http.HttpOutputMessage; +import org.springframework.http.HttpInputMessage; +import org.springframework.http.HttpOutputMessage; +import org.springframework.http.MediaType; /** - * Strategy interface that specifies a converter can convert from and to HTTP request and responses. + * Strategy interface that specifies a converter can convert from and to HTTP requests and responses. * * @author Arjen Poutsma * @since 3.0 @@ -32,39 +32,34 @@ import org.springframework.web.http.HttpOutputMessage; public interface HttpMessageConverter { /** - * Indicates whether the given class is supported by this converter. - * - *

Typically implemented using an {@code instanceof} check. - * + * Indicate whether the given class is supported by this converter. * @param clazz the class to test for support * @return true if supported; false otherwise */ boolean supports(Class clazz); /** - * Returns the list of {@link MediaType} objects supported by this converter. + * Return the list of {@link MediaType} objects supported by this converter. */ List getSupportedMediaTypes(); /** - * Reads an object of the given type form the given input message, and returns it. - * - * @param clazz the type of object to return + * Read an object of the given type form the given input message, and returns it. + * @param clazz the type of object to return * @param inputMessage the HTTP input message to read from * @return the converted object - * @throws IOException in case of I/O errors + * @throws IOException in case of I/O errors * @throws HttpMessageConversionException in case of conversion errors */ T read(Class clazz, HttpInputMessage inputMessage) throws IOException; /** - * Writes an given object to the given output message. - * - * @param t the object to write to the output message + * Write an given object to the given output message. + * @param t the object to write to the output message * @param outputMessage the message to write to - * @throws IOException in case of I/O errors + * @throws IOException in case of I/O errors * @throws HttpMessageConversionException in case of conversion errors */ void write(T t, HttpOutputMessage outputMessage) throws IOException; -} \ No newline at end of file +} diff --git a/org.springframework.web/src/main/java/org/springframework/web/converter/StringHttpMessageConverter.java b/org.springframework.web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java similarity index 89% rename from org.springframework.web/src/main/java/org/springframework/web/converter/StringHttpMessageConverter.java rename to org.springframework.web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java index a77781dc37..9ad1563ad2 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/converter/StringHttpMessageConverter.java +++ b/org.springframework.web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.converter; +package org.springframework.http.converter; import java.io.IOException; import java.io.InputStreamReader; @@ -25,9 +25,9 @@ import java.util.ArrayList; import java.util.List; import org.springframework.util.FileCopyUtils; -import org.springframework.util.MediaType; -import org.springframework.web.http.HttpInputMessage; -import org.springframework.web.http.HttpOutputMessage; +import org.springframework.http.HttpInputMessage; +import org.springframework.http.HttpOutputMessage; +import org.springframework.http.MediaType; /** * Implementation of {@link HttpMessageConverter} that can read and write strings. @@ -45,11 +45,13 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter availableCharsets; + public StringHttpMessageConverter() { super(new MediaType("text", "plain", DEFAULT_CHARSET), new MediaType("text", "*")); - availableCharsets = new ArrayList(Charset.availableCharsets().values()); + this.availableCharsets = new ArrayList(Charset.availableCharsets().values()); } + public boolean supports(Class clazz) { return String.class.equals(clazz); } @@ -86,14 +88,12 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverterBy default, returns {@link Charset#availableCharsets()}. Can be overridden in subclasses. - * * @return the list of accepted charsets */ protected List getAcceptedCharsets() { - return availableCharsets; + return this.availableCharsets; } } diff --git a/org.springframework.web/src/main/java/org/springframework/web/converter/package.html b/org.springframework.web/src/main/java/org/springframework/http/converter/package.html similarity index 100% rename from org.springframework.web/src/main/java/org/springframework/web/converter/package.html rename to org.springframework.web/src/main/java/org/springframework/http/converter/package.html diff --git a/org.springframework.web/src/main/java/org/springframework/http/package.html b/org.springframework.web/src/main/java/org/springframework/http/package.html new file mode 100644 index 0000000000..740145c631 --- /dev/null +++ b/org.springframework.web/src/main/java/org/springframework/http/package.html @@ -0,0 +1,8 @@ + + + +Contains a basic abstraction over client/server-side HTTP. This package contains +the HttpInputMessage and HttpOutputMessage interfaces. + + + diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/server/ServerHttpRequest.java b/org.springframework.web/src/main/java/org/springframework/http/server/ServerHttpRequest.java similarity index 77% rename from org.springframework.web/src/main/java/org/springframework/web/http/server/ServerHttpRequest.java rename to org.springframework.web/src/main/java/org/springframework/http/server/ServerHttpRequest.java index a47735bc0c..339c4d445b 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/server/ServerHttpRequest.java +++ b/org.springframework.web/src/main/java/org/springframework/http/server/ServerHttpRequest.java @@ -14,10 +14,10 @@ * limitations under the License. */ -package org.springframework.web.http.server; +package org.springframework.http.server; -import org.springframework.web.http.HttpInputMessage; -import org.springframework.web.http.HttpMethod; +import org.springframework.http.HttpInputMessage; +import org.springframework.http.HttpMethod; /** * Represents a server-side HTTP request. @@ -28,9 +28,8 @@ import org.springframework.web.http.HttpMethod; public interface ServerHttpRequest extends HttpInputMessage { /** - * Returns the HTTP method of the request. - * - * @return the http method + * Return the HTTP method of the request. + * @return the HTTP method as an HttpMethod enum value */ HttpMethod getMethod(); diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/server/ServerHttpResponse.java b/org.springframework.web/src/main/java/org/springframework/http/server/ServerHttpResponse.java similarity index 74% rename from org.springframework.web/src/main/java/org/springframework/web/http/server/ServerHttpResponse.java rename to org.springframework.web/src/main/java/org/springframework/http/server/ServerHttpResponse.java index b3cb770c19..778fe84ab1 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/server/ServerHttpResponse.java +++ b/org.springframework.web/src/main/java/org/springframework/http/server/ServerHttpResponse.java @@ -14,10 +14,10 @@ * limitations under the License. */ -package org.springframework.web.http.server; +package org.springframework.http.server; -import org.springframework.web.http.HttpOutputMessage; -import org.springframework.web.http.HttpStatus; +import org.springframework.http.HttpOutputMessage; +import org.springframework.http.HttpStatus; /** * Represents a server-side HTTP response. @@ -28,14 +28,13 @@ import org.springframework.web.http.HttpStatus; public interface ServerHttpResponse extends HttpOutputMessage { /** - * Sets the HTTP status code of the response. - * - * @param status the HTTP status + * Set the HTTP status code of the response. + * @param status the HTTP status as an HttpStatus enum value */ void setStatusCode(HttpStatus status); /** - * Closes this response, freeing any resources created. + * Close this response, freeing any resources created. */ void close(); diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/server/ServletServerHttpRequest.java b/org.springframework.web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java similarity index 67% rename from org.springframework.web/src/main/java/org/springframework/web/http/server/ServletServerHttpRequest.java rename to org.springframework.web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java index 640d402915..00ab9b5de0 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/server/ServletServerHttpRequest.java +++ b/org.springframework.web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.http.server; +package org.springframework.http.server; import java.io.IOException; import java.io.InputStream; @@ -22,8 +22,8 @@ import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import org.springframework.util.Assert; -import org.springframework.web.http.HttpHeaders; -import org.springframework.web.http.HttpMethod; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; /** * {@link ServerHttpRequest} implementation that is based on a {@link HttpServletRequest}. @@ -37,36 +37,37 @@ public class ServletServerHttpRequest implements ServerHttpRequest { private HttpHeaders headers; + /** - * Constructs a new instance of the ServletHttpRequest based on the given {@link HttpServletRequest} - * - * @param servletRequest the HTTP Servlet request + * Construct a new instance of the ServletServerHttpRequest based on the given {@link HttpServletRequest} + * @param servletRequest the HttpServletRequest */ public ServletServerHttpRequest(HttpServletRequest servletRequest) { Assert.notNull(servletRequest, "'servletRequest' must not be null"); this.servletRequest = servletRequest; } + public HttpMethod getMethod() { - return HttpMethod.valueOf(servletRequest.getMethod()); + return HttpMethod.valueOf(this.servletRequest.getMethod()); } public HttpHeaders getHeaders() { - if (headers == null) { - headers = new HttpHeaders(); - for (Enumeration headerNames = servletRequest.getHeaderNames(); headerNames.hasMoreElements();) { + if (this.headers == null) { + this.headers = new HttpHeaders(); + for (Enumeration headerNames = this.servletRequest.getHeaderNames(); headerNames.hasMoreElements();) { String headerName = (String) headerNames.nextElement(); - for (Enumeration headerValues = servletRequest.getHeaders(headerName); - headerValues.hasMoreElements();) { + for (Enumeration headerValues = this.servletRequest.getHeaders(headerName); headerValues.hasMoreElements();) { String headerValue = (String) headerValues.nextElement(); - headers.add(headerName, headerValue); + this.headers.add(headerName, headerValue); } } } - return headers; + return this.headers; } public InputStream getBody() throws IOException { - return servletRequest.getInputStream(); + return this.servletRequest.getInputStream(); } + } diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/server/ServletServerHttpResponse.java b/org.springframework.web/src/main/java/org/springframework/http/server/ServletServerHttpResponse.java similarity index 76% rename from org.springframework.web/src/main/java/org/springframework/web/http/server/ServletServerHttpResponse.java rename to org.springframework.web/src/main/java/org/springframework/http/server/ServletServerHttpResponse.java index 940f12c8dd..a40ab6759a 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/http/server/ServletServerHttpResponse.java +++ b/org.springframework.web/src/main/java/org/springframework/http/server/ServletServerHttpResponse.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.http.server; +package org.springframework.http.server; import java.io.IOException; import java.io.OutputStream; @@ -23,8 +23,8 @@ import java.util.Map; import javax.servlet.http.HttpServletResponse; import org.springframework.util.Assert; -import org.springframework.web.http.HttpHeaders; -import org.springframework.web.http.HttpStatus; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; /** * {@link ServerHttpResponse} implementation that is based on a {@link HttpServletResponse}. @@ -40,9 +40,9 @@ public class ServletServerHttpResponse implements ServerHttpResponse { private boolean headersWritten = false; + /** - * Constructs a new instance of the ServletHttpResponse based on the given {@link HttpServletResponse} - * + * Construct a new instance of the ServletServerHttpResponse based on the given {@link HttpServletResponse}. * @param servletResponse the HTTP Servlet response */ public ServletServerHttpResponse(HttpServletResponse servletResponse) { @@ -50,32 +50,34 @@ public class ServletServerHttpResponse implements ServerHttpResponse { this.servletResponse = servletResponse; } + public void setStatusCode(HttpStatus status) { - servletResponse.setStatus(status.value()); + this.servletResponse.setStatus(status.value()); } public HttpHeaders getHeaders() { - return headers; + return this.headers; } public OutputStream getBody() throws IOException { writeHeaders(); - return servletResponse.getOutputStream(); - } - - private void writeHeaders() { - if (!headersWritten) { - for (Map.Entry> entry : headers.entrySet()) { - String headerName = entry.getKey(); - for (String headerValue : entry.getValue()) { - servletResponse.addHeader(headerName, headerValue); - } - } - headersWritten = true; - } + return this.servletResponse.getOutputStream(); } public void close() { writeHeaders(); } + + private void writeHeaders() { + if (!this.headersWritten) { + for (Map.Entry> entry : this.headers.entrySet()) { + String headerName = entry.getKey(); + for (String headerValue : entry.getValue()) { + this.servletResponse.addHeader(headerName, headerValue); + } + } + this.headersWritten = true; + } + } + } diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/server/package.html b/org.springframework.web/src/main/java/org/springframework/http/server/package.html similarity index 100% rename from org.springframework.web/src/main/java/org/springframework/web/http/server/package.html rename to org.springframework.web/src/main/java/org/springframework/http/server/package.html diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/core/SimpleHttpErrorHandler.java b/org.springframework.web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java similarity index 72% rename from org.springframework.web/src/main/java/org/springframework/web/client/core/SimpleHttpErrorHandler.java rename to org.springframework.web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java index 5b69f97b37..e59ac2b299 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/client/core/SimpleHttpErrorHandler.java +++ b/org.springframework.web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java @@ -14,28 +14,28 @@ * limitations under the License. */ -package org.springframework.web.client.core; +package org.springframework.web.client; import java.io.IOException; import org.springframework.web.client.HttpClientErrorException; -import org.springframework.web.client.HttpClientException; +import org.springframework.web.client.RestClientException; import org.springframework.web.client.HttpServerErrorException; -import org.springframework.web.http.HttpStatus; -import org.springframework.web.http.client.ClientHttpResponse; +import org.springframework.http.HttpStatus; +import org.springframework.http.client.ClientHttpResponse; /** - * Default implementation of the {@link HttpErrorHandler} interface. + * Default implementation of the {@link ResponseErrorHandler} interface. * *

This error handler checks for the status code on the {@link ClientHttpResponse}: any code with series * {@link HttpStatus.Series#CLIENT_ERROR} or {@link HttpStatus.Series#SERVER_ERROR} is considered to be an error. * This behavior can be changed by overriding the {@link #hasError(HttpStatus)} method. * * @author Arjen Poutsma - * @see RestTemplate#setErrorHandler(HttpErrorHandler) * @since 3.0 + * @see RestTemplate#setErrorHandler */ -public class SimpleHttpErrorHandler implements HttpErrorHandler { +public class DefaultResponseErrorHandler implements ResponseErrorHandler { /** * Delegates to {@link #hasError(HttpStatus)} with the response status code. @@ -46,18 +46,16 @@ public class SimpleHttpErrorHandler implements HttpErrorHandler { /** * Template method called from {@link #hasError(ClientHttpResponse)}. - * - *

Default implementation checks if the given status code is {@link HttpStatus.Series#CLIENT_ERROR} or {@link - * HttpStatus.Series#SERVER_ERROR}. Can be overridden in subclasses. - * + *

The default implementation checks if the given status code is {@link HttpStatus.Series#CLIENT_ERROR} + * or {@link HttpStatus.Series#SERVER_ERROR}. Can be overridden in subclasses. * @param statusCode the HTTP status code * @return true if the response has an error; false otherwise * @see HttpStatus.Series#CLIENT_ERROR * @see HttpStatus.Series#SERVER_ERROR */ protected boolean hasError(HttpStatus statusCode) { - return statusCode.series() == HttpStatus.Series.CLIENT_ERROR || - statusCode.series() == HttpStatus.Series.SERVER_ERROR; + return (statusCode.series() == HttpStatus.Series.CLIENT_ERROR || + statusCode.series() == HttpStatus.Series.SERVER_ERROR); } public void handleError(ClientHttpResponse response) throws IOException { @@ -68,7 +66,8 @@ public class SimpleHttpErrorHandler implements HttpErrorHandler { case SERVER_ERROR: throw new HttpServerErrorException(statusCode, response.getStatusText()); default: - throw new HttpClientException("Unknown status code [" + statusCode + "]"); + throw new RestClientException("Unknown status code [" + statusCode + "]"); } } } + diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/HttpClientErrorException.java b/org.springframework.web/src/main/java/org/springframework/web/client/HttpClientErrorException.java index 6549bf820f..d7b221549f 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/client/HttpClientErrorException.java +++ b/org.springframework.web/src/main/java/org/springframework/web/client/HttpClientErrorException.java @@ -16,20 +16,19 @@ package org.springframework.web.client; -import org.springframework.web.http.HttpStatus; +import org.springframework.http.HttpStatus; /** * Exception thrown when a HTTP 4xx is received. * * @author Arjen Poutsma - * @see org.springframework.web.client.core.SimpleHttpErrorHandler * @since 3.0 + * @see DefaultResponseErrorHandler */ public class HttpClientErrorException extends HttpStatusCodeException { /** - * Constructs a new instance of {@code HttpClientErrorException} based on a {@link HttpStatus}. - * + * Construct a new instance of {@code HttpClientErrorException} based on a {@link HttpStatus}. * @param statusCode the status code */ public HttpClientErrorException(HttpStatus statusCode) { @@ -37,12 +36,12 @@ public class HttpClientErrorException extends HttpStatusCodeException { } /** - * Constructs a new instance of {@code HttpClientErrorException} based on a {@link HttpStatus} and status text. - * + * Construct a new instance of {@code HttpClientErrorException} based on a {@link HttpStatus} and status text. * @param statusCode the status code * @param statusText the status text */ public HttpClientErrorException(HttpStatus statusCode, String statusText) { super(statusCode, statusText); } + } diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/HttpServerErrorException.java b/org.springframework.web/src/main/java/org/springframework/web/client/HttpServerErrorException.java index 62e5f6f8fc..bc2b42eeff 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/client/HttpServerErrorException.java +++ b/org.springframework.web/src/main/java/org/springframework/web/client/HttpServerErrorException.java @@ -16,20 +16,19 @@ package org.springframework.web.client; -import org.springframework.web.http.HttpStatus; +import org.springframework.http.HttpStatus; /** * Exception thrown when a HTTP 5xx is received. * * @author Arjen Poutsma - * @see org.springframework.web.client.core.SimpleHttpErrorHandler * @since 3.0 + * @see DefaultResponseErrorHandler */ public class HttpServerErrorException extends HttpStatusCodeException { /** - * Constructs a new instance of {@code HttpServerErrorException} based on a {@link HttpStatus}. - * + * Construct a new instance of {@code HttpServerErrorException} based on a {@link HttpStatus}. * @param statusCode the status code */ public HttpServerErrorException(HttpStatus statusCode) { @@ -37,8 +36,7 @@ public class HttpServerErrorException extends HttpStatusCodeException { } /** - * Constructs a new instance of {@code HttpServerErrorException} based on a {@link HttpStatus} and status text. - * + * Construct a new instance of {@code HttpServerErrorException} based on a {@link HttpStatus} and status text. * @param statusCode the status code * @param statusText the status text */ diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/HttpStatusCodeException.java b/org.springframework.web/src/main/java/org/springframework/web/client/HttpStatusCodeException.java index 249ff795e8..86b4338e8d 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/client/HttpStatusCodeException.java +++ b/org.springframework.web/src/main/java/org/springframework/web/client/HttpStatusCodeException.java @@ -16,23 +16,23 @@ package org.springframework.web.client; -import org.springframework.web.http.HttpStatus; +import org.springframework.http.HttpStatus; /** - * Abstract base class for exceptions based on a {@link HttpStatus}. + * Abstract base class for exceptions based on an {@link HttpStatus}. * * @author Arjen Poutsma * @since 3.0 */ -public abstract class HttpStatusCodeException extends HttpClientException { +public abstract class HttpStatusCodeException extends RestClientException { private final HttpStatus statusCode; private final String statusText; + /** - * Constructs a new instance of {@code HttpStatusCodeException} based on a {@link HttpStatus}. - * + * Construct a new instance of {@code HttpStatusCodeException} based on a {@link HttpStatus}. * @param statusCode the status code */ protected HttpStatusCodeException(HttpStatus statusCode) { @@ -42,8 +42,7 @@ public abstract class HttpStatusCodeException extends HttpClientException { } /** - * Constructs a new instance of {@code HttpStatusCodeException} based on a {@link HttpStatus} and status text. - * + * Construct a new instance of {@code HttpStatusCodeException} based on a {@link HttpStatus} and status text. * @param statusCode the status code * @param statusText the status text */ @@ -53,17 +52,19 @@ public abstract class HttpStatusCodeException extends HttpClientException { this.statusText = statusText; } + /** * Returns the HTTP status code. */ public HttpStatus getStatusCode() { - return statusCode; + return this.statusCode; } /** * Returns the HTTP status text. */ public String getStatusText() { - return statusText; + return this.statusText; } + } diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/core/HttpRequestCallback.java b/org.springframework.web/src/main/java/org/springframework/web/client/RequestCallback.java similarity index 78% rename from org.springframework.web/src/main/java/org/springframework/web/client/core/HttpRequestCallback.java rename to org.springframework.web/src/main/java/org/springframework/web/client/RequestCallback.java index 51c141e1d1..a6474a7309 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/client/core/HttpRequestCallback.java +++ b/org.springframework.web/src/main/java/org/springframework/web/client/RequestCallback.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package org.springframework.web.client.core; +package org.springframework.web.client; import java.io.IOException; -import org.springframework.web.http.client.ClientHttpRequest; +import org.springframework.http.client.ClientHttpRequest; /** * Callback interface for code that operates on a {@link ClientHttpRequest}. Allows to manipulate the request @@ -30,13 +30,12 @@ import org.springframework.web.http.client.ClientHttpRequest; * @see RestTemplate#execute * @since 3.0 */ -public interface HttpRequestCallback { +public interface RequestCallback { /** - * Gets called by {@link RestTemplate#execute} with an opened {@code ClientHttpRequest}. Does not need to care about - * closing the request, handling I/O errors, or about handling errors: this will all be handled by the {@code - * RestTemplate}. - * + * Gets called by {@link RestTemplate#execute} with an opened {@code ClientHttpRequest}. + * Does not need to care about closing the request or about handling errors: + * this will all be handled by the {@code RestTemplate}. * @param request the active HTTP request * @throws IOException in case of I/O errors */ diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/HttpIOException.java b/org.springframework.web/src/main/java/org/springframework/web/client/ResourceAccessException.java similarity index 67% rename from org.springframework.web/src/main/java/org/springframework/web/client/HttpIOException.java rename to org.springframework.web/src/main/java/org/springframework/web/client/ResourceAccessException.java index 865078cbf1..8ef2784252 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/client/HttpIOException.java +++ b/org.springframework.web/src/main/java/org/springframework/web/client/ResourceAccessException.java @@ -19,29 +19,27 @@ package org.springframework.web.client; import java.io.IOException; /** - * Exception thrown when a I/O error occurs. + * Exception thrown when an I/O error occurs. * * @author Arjen Poutsma * @since 3.0 */ -public class HttpIOException extends HttpClientException { +public class ResourceAccessException extends RestClientException { /** - * Constructs a new {@code HttpIOException} with the given message. - * + * Construct a new {@code HttpIOException} with the given message. * @param msg the message */ - public HttpIOException(String msg) { + public ResourceAccessException(String msg) { super(msg); } /** - * Constructs a new {@code HttpIOException} with the given message and {@link IOException}. - * + * Construct a new {@code HttpIOException} with the given message and {@link IOException}. * @param msg the message - * @param ex the {@code IOException} + * @param ex the {@code IOException} */ - public HttpIOException(String msg, IOException ex) { + public ResourceAccessException(String msg, IOException ex) { super(msg, ex); } diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/core/HttpErrorHandler.java b/org.springframework.web/src/main/java/org/springframework/web/client/ResponseErrorHandler.java similarity index 81% rename from org.springframework.web/src/main/java/org/springframework/web/client/core/HttpErrorHandler.java rename to org.springframework.web/src/main/java/org/springframework/web/client/ResponseErrorHandler.java index d7550e9aad..d4f6147481 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/client/core/HttpErrorHandler.java +++ b/org.springframework.web/src/main/java/org/springframework/web/client/ResponseErrorHandler.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package org.springframework.web.client.core; +package org.springframework.web.client; import java.io.IOException; -import org.springframework.web.http.client.ClientHttpResponse; +import org.springframework.http.client.ClientHttpResponse; /** * Strategy interface used by the {@link RestTemplate} to determine whether a particular response has an error or not. @@ -26,13 +26,12 @@ import org.springframework.web.http.client.ClientHttpResponse; * @author Arjen Poutsma * @since 3.0 */ -public interface HttpErrorHandler { +public interface ResponseErrorHandler { /** * Indicates whether the given response has any errors. - * - * Implementations will typically inspect the {@link ClientHttpResponse#getStatusCode() HttpStatus} of the response. - * + * Implementations will typically inspect the {@link ClientHttpResponse#getStatusCode() HttpStatus} + * of the response. * @param response the response to inspect * @return true if the response has an error; false otherwise * @throws IOException in case of I/O errors @@ -41,13 +40,9 @@ public interface HttpErrorHandler { /** * Handles the error in the given response. - * * This method is only called when {@link #hasError(ClientHttpResponse)} has returned true. - * * @param response the response with the error * @throws IOException in case of I/O errors - * @throws org.springframework.web.client.HttpClientException - * typically thrown by implementations of this interface */ void handleError(ClientHttpResponse response) throws IOException; } diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/core/HttpResponseExtractor.java b/org.springframework.web/src/main/java/org/springframework/web/client/ResponseExtractor.java similarity index 73% rename from org.springframework.web/src/main/java/org/springframework/web/client/core/HttpResponseExtractor.java rename to org.springframework.web/src/main/java/org/springframework/web/client/ResponseExtractor.java index 29b2b57bd1..2a0d9f6e45 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/client/core/HttpResponseExtractor.java +++ b/org.springframework.web/src/main/java/org/springframework/web/client/ResponseExtractor.java @@ -14,28 +14,28 @@ * limitations under the License. */ -package org.springframework.web.client.core; +package org.springframework.web.client; import java.io.IOException; -import org.springframework.web.http.client.ClientHttpResponse; +import org.springframework.http.client.ClientHttpResponse; /** - * Generic callback interface used by {@link RestTemplate}'s retrieval methods. Implementations of this interface - * perform the actual work of extracting data from a {@link ClientHttpResponse}, but don't need to worry about exception + * Generic callback interface used by {@link RestTemplate}'s retrieval methods + * Implementations of this interface perform the actual work of extracting data + * from a {@link ClientHttpResponse}, but don't need to worry about exception * handling or closing resources. * *

Used internally by the {@link RestTemplate}, but also useful for application code. * * @author Arjen Poutsma - * @see RestTemplate#execute * @since 3.0 + * @see RestTemplate#execute */ -public interface HttpResponseExtractor { +public interface ResponseExtractor { /** - * Extracts data from the given {@code ClientHttpResponse} and returns it. - * + * Extract data from the given {@code ClientHttpResponse} and return it. * @param response the HTTP response * @return the extracted data * @throws IOException in case of I/O errors diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/HttpClientException.java b/org.springframework.web/src/main/java/org/springframework/web/client/RestClientException.java similarity index 64% rename from org.springframework.web/src/main/java/org/springframework/web/client/HttpClientException.java rename to org.springframework.web/src/main/java/org/springframework/web/client/RestClientException.java index 89f4e0d3ba..f772ada851 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/client/HttpClientException.java +++ b/org.springframework.web/src/main/java/org/springframework/web/client/RestClientException.java @@ -19,29 +19,28 @@ package org.springframework.web.client; import org.springframework.core.NestedRuntimeException; /** - * Base class for exceptions thrown by the framework whenever it encounters client-side HTTP errors. + * Base class for exceptions thrown by {@link RestTemplate} whenever it encounters client-side HTTP errors. * * @author Arjen Poutsma * @since 3.0 */ -public class HttpClientException extends NestedRuntimeException { +public class RestClientException extends NestedRuntimeException { /** - * Constructs a new instance of {@code HttpClientException} with the given message. - * + * Construct a new instance of {@code HttpClientException} with the given message. * @param msg the message */ - public HttpClientException(String msg) { + public RestClientException(String msg) { super(msg); } /** - * Constructs a new instance of {@code HttpClientException} with the given message and exception. - * + * Construct a new instance of {@code HttpClientException} with the given message and exception. * @param msg the message - * @param ex the exception + * @param ex the exception */ - public HttpClientException(String msg, Throwable ex) { + public RestClientException(String msg, Throwable ex) { super(msg, ex); } + } diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/RestOperations.java b/org.springframework.web/src/main/java/org/springframework/web/client/RestOperations.java new file mode 100644 index 0000000000..e0722e63a2 --- /dev/null +++ b/org.springframework.web/src/main/java/org/springframework/web/client/RestOperations.java @@ -0,0 +1,205 @@ +/* + * Copyright 2002-2009 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.web.client; + +import java.net.URI; +import java.util.EnumSet; +import java.util.Map; +import java.util.Set; + +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; + +/** + * Interface specifying a basic set of RESTful operations. + * Implemented by {@link RestTemplate}. Not often used directly, but a useful + * option to enhance testability, as it can easily be mocked or stubbed. + * + * @author Arjen Poutsma + * @since 3.0 + * @see RestTemplate + */ +public interface RestOperations { + + // GET + + /** + * Retrieve a representation by doing a GET on the specified URL. + *

URI Template variables are expanded using the given URI variables, if any. + * @param uri the URI + * @param responseType the type of the return value + * @param uriVariables the variables to expand the template + * @return the converted object + */ + T getForObject(String uri, Class responseType, String... uriVariables) + throws RestClientException; + + /** + * Retrieve a representation by doing a GET on the URI template. + *

URI Template variables are expanded using the given map. + * @param uri the URI + * @param responseType the type of the return value + * @param uriVariables the map containing variables for the URI template + * @return the converted object + */ + T getForObject(String uri, Class responseType, Map uriVariables) + throws RestClientException; + + + // HEAD + + /** + * Retrieve all headers of the resource specified by the URI template. + *

URI Template variables are expanded using the given URI variables, if any. + * @param uri the URI + * @param uriVariables the variables to expand the template + * @return all HTTP headers of that resource + */ + HttpHeaders headForHeaders(String uri, String... uriVariables) throws RestClientException; + + /** + * Retrieve all headers of the resource specified by the URI template. + *

URI Template variables are expanded using the given map. + * @param uri the URI + * @param uriVariables the map containing variables for the URI template + * @return all HTTP headers of that resource + */ + HttpHeaders headForHeaders(String uri, Map uriVariables) throws RestClientException; + + + // POST + + /** + * Create a new resource by POSTing the given object to the URI template. The value of the Location, + * indicating where the new resource is stored, is returned. + *

URI Template variables are expanded using the given URI variables, if any. + * @param uri the URI + * @param request the Object to be POSTED + * @return the value for the Location header + */ + URI postForLocation(String uri, Object request, String... uriVariables) + throws RestClientException; + + /** + * Create a new resource by POSTing the given object to URI template. The value of the Location, + * indicating where the new resource is stored, is returned. + *

URI Template variables are expanded using the given map. + * @param uri the URI + * @param request the Object to be POSTed + * @param uriVariables the variables to expand the template + * @return the value for the Location header + */ + URI postForLocation(String uri, Object request, Map uriVariables) + throws RestClientException; + + + // PUT + + /** + * Create or update a resource by PUTting the given object to the URI. + *

URI Template variables are expanded using the given URI variables, if any. + * @param uri the URI + * @param request the Object to be POSTed + * @param uriVariables the variables to expand the template + */ + void put(String uri, Object request, String... uriVariables) throws RestClientException; + + /** + * Creates a new resource by PUTting the given object to URI template. + *

URI Template variables are expanded using the given map. + * @param uri the URI + * @param request the Object to be POSTed + * @param uriVariables the variables to expand the template + */ + void put(String uri, Object request, Map uriVariables) throws RestClientException; + + + // DELETE + + /** + * Delete the resources at the specified URI. + *

URI Template variables are expanded using the given URI variables, if any. + * @param uri the URI + * @param uriVariables the variables to expand in the template + */ + void delete(String uri, String... uriVariables) throws RestClientException; + + /** + * Delete the resources at the specified URI. + *

URI Template variables are expanded using the given map. + * @param uri the URI + * @param uriVariables the variables to expand the template + */ + void delete(String uri, Map uriVariables) throws RestClientException; + + + // OPTIONS + + /** + * Return the value of the Allow header for the given URI. + *

URI Template variables are expanded using the given URI variables, if any. + * @param uri the URI + * @param uriVariables the variables to expand in the template + * @return the value of the allow header + */ + Set optionsForAllow(String uri, String... uriVariables) + throws RestClientException; + + /** + * Return the value of the Allow header for the given URI. + *

URI Template variables are expanded using the given map. + * @param uri the URI + * @param uriVariables the variables to expand in the template + * @return the value of the allow header + */ + Set optionsForAllow(String uri, Map uriVariables) + throws RestClientException; + + + // general execution + + /** + * Execute the HTTP methods to the given URI, preparing the request with the {@link RequestCallback}, + * and reading the response with a {@link ResponseExtractor}. + *

URI Template variables are expanded using the given URI variables, if any. + * @param uri the URI + * @param method the HTTP method (GET, POST, etc) + * @param requestCallback object that prepares the request + * @param responseExtractor object that extracts the return value from the response + * @param uriVariables the variables to expand in the template + * @return an arbitrary object, as returned by the {@link ResponseExtractor} + */ + T execute(String uri, HttpMethod method, RequestCallback requestCallback, + ResponseExtractor responseExtractor, String... uriVariables) + throws RestClientException; + + /** + * Execute the HTTP methods to the given URI, preparing the request with the {@link RequestCallback}, + * and reading the response with a {@link ResponseExtractor}. + *

URI Template variables are expanded using the given URI variables map. + * @param uri the URI + * @param method the HTTP method (GET, POST, etc) + * @param requestCallback object that prepares the request + * @param responseExtractor object that extracts the return value from the response + * @param uriVariablesthe variables to expand in the template + * @return an arbitrary object, as returned by the {@link ResponseExtractor} + */ + T execute(String uri, HttpMethod method, RequestCallback requestCallback, + ResponseExtractor responseExtractor, Map uriVariables) + throws RestClientException; + +} diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/core/RestTemplate.java b/org.springframework.web/src/main/java/org/springframework/web/client/RestTemplate.java similarity index 58% rename from org.springframework.web/src/main/java/org/springframework/web/client/core/RestTemplate.java rename to org.springframework.web/src/main/java/org/springframework/web/client/RestTemplate.java index 09a56956e3..b0f36aacdf 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/client/core/RestTemplate.java +++ b/org.springframework.web/src/main/java/org/springframework/web/client/RestTemplate.java @@ -14,29 +14,27 @@ * limitations under the License. */ -package org.springframework.web.client.core; +package org.springframework.web.client; import java.io.IOException; import java.net.URI; import java.util.ArrayList; import java.util.Collections; -import java.util.EnumSet; import java.util.List; import java.util.Map; +import java.util.Set; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.client.ClientHttpRequest; +import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.http.client.support.HttpAccessor; +import org.springframework.http.converter.ByteArrayHttpMessageConverter; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.util.Assert; -import org.springframework.util.MediaType; -import org.springframework.web.client.HttpClientException; -import org.springframework.web.client.HttpIOException; -import org.springframework.web.client.support.HttpAccessor; -import org.springframework.web.converter.ByteArrayHttpMessageConverter; -import org.springframework.web.converter.HttpMessageConverter; -import org.springframework.web.converter.StringHttpMessageConverter; -import org.springframework.web.http.HttpHeaders; -import org.springframework.web.http.HttpMethod; -import org.springframework.web.http.client.ClientHttpRequest; -import org.springframework.web.http.client.ClientHttpRequestFactory; -import org.springframework.web.http.client.ClientHttpResponse; import org.springframework.web.util.UriTemplate; /** @@ -76,72 +74,67 @@ import org.springframework.web.util.UriTemplate; * your own converter and register it via the {@link #setMessageConverters(HttpMessageConverter[]) messageConverters} * bean property. * - *

This template uses a {@link org.springframework.web.http.client.SimpleClientHttpRequestFactory} and a {@link - * SimpleHttpErrorHandler} as default strategies for for creating HTTP connections or handling HTTP errors, respectively. + *

This template uses a {@link org.springframework.http.client.SimpleClientHttpRequestFactory} and a {@link + * DefaultResponseErrorHandler} as default strategies for for creating HTTP connections or handling HTTP errors, respectively. * These defaults can be overridden through the {@link #setRequestFactory(ClientHttpRequestFactory) requestFactory} and - * {@link #setErrorHandler(HttpErrorHandler) errorHandler} bean properties. + * {@link #setErrorHandler(ResponseErrorHandler) errorHandler} bean properties. * * @author Arjen Poutsma - * @see HttpMessageConverter - * @see HttpRequestCallback - * @see HttpResponseExtractor - * @see HttpErrorHandler * @since 3.0 + * @see HttpMessageConverter + * @see RequestCallback + * @see ResponseExtractor + * @see ResponseErrorHandler */ public class RestTemplate extends HttpAccessor implements RestOperations { - private final HttpResponseExtractor headersExtractor = new HeadersExtractor(); + private final ResponseExtractor headersExtractor = new HeadersExtractor(); - private HttpMessageConverter[] messageConverters; + private HttpMessageConverter[] messageConverters = + new HttpMessageConverter[] {new ByteArrayHttpMessageConverter(), new StringHttpMessageConverter()}; + + private ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler(); - private HttpErrorHandler errorHandler; /** - * Creates a new instance of the {@link RestTemplate} using default settings. - * + * Create a new instance of the {@link RestTemplate} using default settings. * @see #initDefaultStrategies() */ public RestTemplate() { - initDefaultStrategies(); } /** - * Creates a new instance of the {@link RestTemplate} based on the given {@link ClientHttpRequestFactory}. - * + * Create a new instance of the {@link RestTemplate} based on the given {@link ClientHttpRequestFactory}. * @param requestFactory HTTP request factory to use - * @see org.springframework.web.http.client.SimpleClientHttpRequestFactory - * @see org.springframework.web.http.client.commons.CommonsClientHttpRequestFactory + * @see org.springframework.http.client.SimpleClientHttpRequestFactory + * @see org.springframework.http.client.CommonsClientHttpRequestFactory */ public RestTemplate(ClientHttpRequestFactory requestFactory) { - initDefaultStrategies(); setRequestFactory(requestFactory); } + /** - * Initializes the default stragegies for this template. - * - *

Default implementation sets up the {@link SimpleHttpErrorHandler} and the {@link ByteArrayHttpMessageConverter} and - * {@link StringHttpMessageConverter}. + * Set the message body converters to use. These converters are used to convert + * from and to HTTP requests and responses. */ - protected void initDefaultStrategies() { - errorHandler = new SimpleHttpErrorHandler(); - messageConverters = - new HttpMessageConverter[]{new ByteArrayHttpMessageConverter(), new StringHttpMessageConverter()}; + public void setMessageConverters(HttpMessageConverter[] messageConverters) { + Assert.notEmpty(messageConverters, "'messageConverters' must not be empty"); + this.messageConverters = messageConverters; } /** - * Returns the array of message body converters. These converters are used to covert from and to HTTP requests and - * responses. + * Returnsthe message body converters. These converters are used to convert + * from and to HTTP requests and responses. */ public HttpMessageConverter[] getMessageConverters() { - return messageConverters; + return this.messageConverters; } /** - * Returns the list of message body converters that support a particular type. - * + * Returns the message body converters that support a particular type. * @param type the type to return converters for - * @return converts that support the given type + * @return converters that support the given type */ @SuppressWarnings("unchecked") protected List> getSupportedMessageConverters(Class type) { @@ -156,122 +149,128 @@ public class RestTemplate extends HttpAccessor implements RestOperations { } /** - * Sets the array of message body converters to use. These converters are used to covert from and to HTTP requests and - * responses. - * - * Note that setting this property overrides the {@linkplain #initDefaultStrategies() default strategies}. + * Set the error handler. */ - public void setMessageConverters(HttpMessageConverter[] messageConverters) { - Assert.notEmpty(messageConverters, "'messageConverters' must not be empty"); - this.messageConverters = messageConverters; - } - - /** - * Returns the error handler. By default, this is the {@link SimpleHttpErrorHandler}. - */ - public HttpErrorHandler getErrorHandler() { - return errorHandler; - } - - /** - * Sets the error handler. - */ - public void setErrorHandler(HttpErrorHandler errorHandler) { + public void setErrorHandler(ResponseErrorHandler errorHandler) { Assert.notNull(errorHandler, "'errorHandler' must not be null"); this.errorHandler = errorHandler; } + /** + * Return the error handler. By default, this is the {@link DefaultResponseErrorHandler}. + */ + public ResponseErrorHandler getErrorHandler() { + return this.errorHandler; + } + + // GET - public T getForObject(String url, Class responseType, String... urlVariables) { - checkForSupportedEntityConverter(responseType); + public T getForObject(String url, Class responseType, String... urlVariables) + throws RestClientException { + + checkForSupportedMessageConverter(responseType); return execute(url, HttpMethod.GET, new GetCallback(responseType), new HttpMessageConverterExtractor(responseType), urlVariables); } - public T getForObject(String url, Class responseType, Map urlVariables) { - checkForSupportedEntityConverter(responseType); + public T getForObject(String url, Class responseType, Map urlVariables) + throws RestClientException { + + checkForSupportedMessageConverter(responseType); return execute(url, HttpMethod.GET, new GetCallback(responseType), new HttpMessageConverterExtractor(responseType), urlVariables); } - // POST - - public URI postForLocation(String url, Object request, String... urlVariables) { - checkForSupportedEntityConverter(request.getClass()); - HttpHeaders headers = - execute(url, HttpMethod.POST, new PostPutCallback(request), headersExtractor, urlVariables); - return headers.getLocation(); - } - - public URI postForLocation(String url, Object request, Map urlVariables) { - checkForSupportedEntityConverter(request.getClass()); - HttpHeaders headers = - execute(url, HttpMethod.POST, new PostPutCallback(request), headersExtractor, urlVariables); - return headers.getLocation(); - } - - // PUT - - public void put(String url, Object request, String... urlVariables) { - checkForSupportedEntityConverter(request.getClass()); - execute(url, HttpMethod.PUT, new PostPutCallback(request), null, urlVariables); - } - - public void put(String url, Object request, Map urlVariables) { - checkForSupportedEntityConverter(request.getClass()); - execute(url, HttpMethod.PUT, new PostPutCallback(request), null, urlVariables); - } // HEAD - public HttpHeaders headForHeaders(String url, String... urlVariables) { - return execute(url, HttpMethod.HEAD, null, headersExtractor, urlVariables); + public HttpHeaders headForHeaders(String url, String... urlVariables) throws RestClientException { + return execute(url, HttpMethod.HEAD, null, this.headersExtractor, urlVariables); } - public HttpHeaders headForHeaders(String url, Map urlVariables) { - return execute(url, HttpMethod.HEAD, null, headersExtractor, urlVariables); + public HttpHeaders headForHeaders(String url, Map urlVariables) throws RestClientException { + return execute(url, HttpMethod.HEAD, null, this.headersExtractor, urlVariables); } + + // POST + + public URI postForLocation(String url, Object request, String... urlVariables) + throws RestClientException { + + checkForSupportedMessageConverter(request.getClass()); + HttpHeaders headers = + execute(url, HttpMethod.POST, new PostPutCallback(request), this.headersExtractor, urlVariables); + return headers.getLocation(); + } + + public URI postForLocation(String url, Object request, Map urlVariables) + throws RestClientException { + + checkForSupportedMessageConverter(request.getClass()); + HttpHeaders headers = + execute(url, HttpMethod.POST, new PostPutCallback(request), this.headersExtractor, urlVariables); + return headers.getLocation(); + } + + + // PUT + + public void put(String url, Object request, String... urlVariables) throws RestClientException { + checkForSupportedMessageConverter(request.getClass()); + execute(url, HttpMethod.PUT, new PostPutCallback(request), null, urlVariables); + } + + public void put(String url, Object request, Map urlVariables) throws RestClientException { + checkForSupportedMessageConverter(request.getClass()); + execute(url, HttpMethod.PUT, new PostPutCallback(request), null, urlVariables); + } + + // DELETE - public void delete(String url, String... urlVariables) { + public void delete(String url, String... urlVariables) throws RestClientException { execute(url, HttpMethod.DELETE, null, null, urlVariables); } - public void delete(String url, Map urlVariables) { + public void delete(String url, Map urlVariables) throws RestClientException { execute(url, HttpMethod.DELETE, null, null, urlVariables); } + // OPTIONS - public EnumSet optionsForAllow(String url, String... urlVariables) { - HttpHeaders headers = execute(url, HttpMethod.OPTIONS, null, headersExtractor, urlVariables); + public Set optionsForAllow(String url, String... urlVariables) + throws RestClientException { + + HttpHeaders headers = execute(url, HttpMethod.OPTIONS, null, this.headersExtractor, urlVariables); return headers.getAllow(); } - public EnumSet optionsForAllow(String url, Map urlVariables) { - HttpHeaders headers = execute(url, HttpMethod.OPTIONS, null, headersExtractor, urlVariables); + public Set optionsForAllow(String url, Map urlVariables) + throws RestClientException { + + HttpHeaders headers = execute(url, HttpMethod.OPTIONS, null, this.headersExtractor, urlVariables); return headers.getAllow(); } - // execute - public T execute(String url, - HttpMethod method, - HttpRequestCallback requestCallback, - HttpResponseExtractor responseExtractor, - String... urlVariables) { + // general execution + + public T execute(String url, HttpMethod method, RequestCallback requestCallback, + ResponseExtractor responseExtractor, String... urlVariables) + throws RestClientException { + UriTemplate uriTemplate = new UriTemplate(url); URI expanded = uriTemplate.expand(urlVariables); return doExecute(expanded, method, requestCallback, responseExtractor); } - public T execute(String url, - HttpMethod method, - HttpRequestCallback requestCallback, - HttpResponseExtractor responseExtractor, - Map urlVariables) { + public T execute(String url,HttpMethod method, RequestCallback requestCallback, + ResponseExtractor responseExtractor, Map urlVariables) + throws RestClientException { + UriTemplate uriTemplate = new UriTemplate(url); URI expanded = uriTemplate.expand(urlVariables); return doExecute(expanded, method, requestCallback, responseExtractor); @@ -279,18 +278,16 @@ public class RestTemplate extends HttpAccessor implements RestOperations { /** * Execute the given method on the provided URI. The {@link ClientHttpRequest} is processed using the {@link - * HttpRequestCallback}; the response with the {@link HttpResponseExtractor}. - * - * @param url the fully-expanded URL to connect to - * @param method the HTTP method to execute (GET, POST, etc.) - * @param requestCallback object that prepares the request. Can be null. - * @param responseExtractor object that extracts the return value from the response. Can be null. - * @return an arbitrary object, as returned by the {@link HttpResponseExtractor} + * RequestCallback}; the response with the {@link ResponseExtractor}. + * @param url the fully-expanded URL to connect to + * @param method the HTTP method to execute (GET, POST, etc.) + * @param requestCallback object that prepares the request (can be null) + * @param responseExtractor object that extracts the return value from the response (can be null) + * @return an arbitrary object, as returned by the {@link ResponseExtractor} */ - protected T doExecute(URI url, - HttpMethod method, - HttpRequestCallback requestCallback, - HttpResponseExtractor responseExtractor) { + protected T doExecute(URI url, HttpMethod method, RequestCallback requestCallback, + ResponseExtractor responseExtractor) throws RestClientException { + Assert.notNull(url, "'url' must not be null"); Assert.notNull(method, "'method' must not be null"); ClientHttpResponse response = null; @@ -311,7 +308,7 @@ public class RestTemplate extends HttpAccessor implements RestOperations { } } catch (IOException ex) { - throw new HttpIOException("I/O error: " + ex.getMessage(), ex); + throw new ResourceAccessException("I/O error: " + ex.getMessage(), ex); } finally { if (response != null) { @@ -321,14 +318,13 @@ public class RestTemplate extends HttpAccessor implements RestOperations { } /** - * Checks whether any of the registered {@linkplain #setMessageConverters(HttpMessageConverter[]) message body + * Check whether any of the registered {@linkplain #setMessageConverters(HttpMessageConverter[]) message body * converters} can convert the given type. - * * @param type the type to check for * @throws IllegalArgumentException if no supported entity converter can be found * @see HttpMessageConverter#supports(Class) */ - private void checkForSupportedEntityConverter(Class type) { + private void checkForSupportedMessageConverter(Class type) { for (HttpMessageConverter entityConverter : getMessageConverters()) { if (entityConverter.supports(type)) { return; @@ -337,30 +333,11 @@ public class RestTemplate extends HttpAccessor implements RestOperations { throw new IllegalArgumentException("Could not resolve HttpMessageConverter for [" + type.getName() + "]"); } + /** - * Request callback implementation that sets the Accept header based on the registered {@linkplain - * HttpMessageConverter entity converters}. + * Request callback implementation that prepares the request's accept headers. */ - private class AcceptHeaderCallback implements HttpRequestCallback { - - public void doWithRequest(ClientHttpRequest request) throws IOException { - List allSupportedMediaTypes = new ArrayList(); - for (HttpMessageConverter entityConverter : getMessageConverters()) { - List supportedMediaTypes = entityConverter.getSupportedMediaTypes(); - for (MediaType supportedMediaType : supportedMediaTypes) { - if (supportedMediaType.getCharSet() != null) { - supportedMediaType = - new MediaType(supportedMediaType.getType(), supportedMediaType.getSubtype()); - } - allSupportedMediaTypes.add(supportedMediaType); - } - } - Collections.sort(allSupportedMediaTypes); - request.getHeaders().setAccept(allSupportedMediaTypes); - } - } - - private class GetCallback implements HttpRequestCallback { + private class GetCallback implements RequestCallback { private final Class responseType; @@ -370,7 +347,7 @@ public class RestTemplate extends HttpAccessor implements RestOperations { public void doWithRequest(ClientHttpRequest request) throws IOException { List allSupportedMediaTypes = new ArrayList(); - for (HttpMessageConverter entityConverter : getSupportedMessageConverters(responseType)) { + for (HttpMessageConverter entityConverter : getSupportedMessageConverters(this.responseType)) { List supportedMediaTypes = entityConverter.getSupportedMediaTypes(); for (MediaType supportedMediaType : supportedMediaTypes) { if (supportedMediaType.getCharSet() != null) { @@ -385,10 +362,11 @@ public class RestTemplate extends HttpAccessor implements RestOperations { } } + /** - * Extension of {@link AcceptHeaderCallback} that writes the given object to the request stream. + * Request callback implementation that writes the given object to the request stream. */ - private class PostPutCallback implements HttpRequestCallback { + private class PostPutCallback implements RequestCallback { private final Object request; @@ -398,19 +376,17 @@ public class RestTemplate extends HttpAccessor implements RestOperations { @SuppressWarnings("unchecked") public void doWithRequest(ClientHttpRequest httpRequest) throws IOException { - for (HttpMessageConverter entityConverter : getSupportedMessageConverters(request.getClass())) { - entityConverter.write(request, httpRequest); - break; - } + HttpMessageConverter entityConverter = getSupportedMessageConverters(this.request.getClass()).get(0); + entityConverter.write(this.request, httpRequest); } - } + /** - * Response extractor that uses the registered {@linkplain HttpMessageConverter entity converters} to convert the - * response into a type T. + * Response extractor that uses the registered {@linkplain HttpMessageConverter entity converters} + * to convert the response into a type T. */ - private class HttpMessageConverterExtractor implements HttpResponseExtractor { + private class HttpMessageConverterExtractor implements ResponseExtractor { private final Class responseType; @@ -421,29 +397,31 @@ public class RestTemplate extends HttpAccessor implements RestOperations { public T extractData(ClientHttpResponse response) throws IOException { MediaType contentType = response.getHeaders().getContentType(); if (contentType == null) { - throw new HttpClientException("Cannot extract response: no Content-Type found"); + throw new RestClientException("Cannot extract response: no Content-Type found"); } - for (HttpMessageConverter messageConverter : getSupportedMessageConverters(responseType)) { + for (HttpMessageConverter messageConverter : getSupportedMessageConverters(this.responseType)) { for (MediaType supportedMediaType : messageConverter.getSupportedMediaTypes()) { if (supportedMediaType.includes(contentType)) { - return messageConverter.read(responseType, response); + return messageConverter.read(this.responseType, response); } } } - throw new HttpClientException( + throw new RestClientException( "Could not extract response: no suitable HttpMessageConverter found for " + "response type [" + - responseType.getName() + "] and content type [" + contentType + "]"); + this.responseType.getName() + "] and content type [" + contentType + "]"); } } + /** * Response extractor that extracts the response {@link HttpHeaders}. */ - private static class HeadersExtractor implements HttpResponseExtractor { + private static class HeadersExtractor implements ResponseExtractor { public HttpHeaders extractData(ClientHttpResponse response) throws IOException { return response.getHeaders(); } } + } diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/core/RestOperations.java b/org.springframework.web/src/main/java/org/springframework/web/client/core/RestOperations.java deleted file mode 100644 index 52bba14cc2..0000000000 --- a/org.springframework.web/src/main/java/org/springframework/web/client/core/RestOperations.java +++ /dev/null @@ -1,206 +0,0 @@ -/* - * Copyright 2002-2009 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.web.client.core; - -import java.net.URI; -import java.util.EnumSet; -import java.util.Map; - -import org.springframework.web.http.HttpHeaders; -import org.springframework.web.http.HttpMethod; - -/** - * Interface specifying a basic set of RESTful operations. Implemented by {@link RestTemplate}. Not often used directly, - * but a useful option to enhance testability, as it can easily be mocked or stubbed. - * - * @author Arjen Poutsma - * @see RestTemplate - * @since 3.0 - */ -public interface RestOperations { - - // GET - - /** - * Retrieves a representation by doing a GET on the specified URL. URI Template variables are expanded using the - * given URI variables, if any. - * - * @param uri the URI to GET - * @param responseType the type of the return value - * @param uriVariables the variables to expand the template - * @return the converted object - */ - T getForObject(String uri, Class responseType, String... uriVariables); - - /** - * Retrieves a representation by doing a GET on the URI template. URI Template variables are expanded using the - * given map. - * - * @param uri the URI to GET - * @param responseType the type of the return value - * @param uriVariables the map containing variables for the URI template - * @return the converted object - */ - T getForObject(String uri, Class responseType, Map uriVariables); - - // HEAD - - /** - * Retrieves all headers of the resource specified by the URI template. URI Template variables are expanded using - * the given URI variables, if any. - * - * @param uri the URI - * @param uriVariables the variables to expand the template - * @return all HTTP headers of that resource - */ - HttpHeaders headForHeaders(String uri, String... uriVariables); - - /** - * Retrieves all headers of the resource specified by the URI template. URI Template variables are expanded using - * the given map. - * - * @param uri the URI - * @param uriVariables the map containing variables for the URI template - * @return all HTTP headers of that resource - */ - HttpHeaders headForHeaders(String uri, Map uriVariables); - - // POST - - /** - * Creates a new resource by POSTing the given object to the URI template. The value of the Location, - * indicating where the new resource is stored, is returned. URI Template variables are expanded using the given URI - * variables, if any. - * - * @param uri the URI - * @param request the Object to be POSTED - * @return the value for the Location header - */ - URI postForLocation(String uri, Object request, String... uriVariables); - - /** - * Creates a new resource by POSTing the given object to URI template. The value of the Location, - * indicating where the new resource is stored, is returned. URI Template variables are expanded using the given - * map. - * - * @param uri the URI - * @param request the Object to be POSTed - * @param uriVariables the variables to expand the template - * @return the value for the Location header - */ - URI postForLocation(String uri, Object request, Map uriVariables); - - // PUT - - /** - * Creates or updates a resource by PUTting the given object to the URI. URI Template variables are expanded using - * the given URI variables, if any. - * - * @param uri the URI - * @param request the Object to be POSTed - * @param uriVariables the variables to expand the template - */ - void put(String uri, Object request, String... uriVariables); - - /** - * Creates a new resource by PUTting the given object to URI template. URI Template variables are expanded using the - * given map. - * - * @param uri the URI - * @param request the Object to be POSTed - * @param uriVariables the variables to expand the template - */ - void put(String uri, Object request, Map uriVariables); - - // DELETE - - /** - * Deletes the resources at the specified URI. URI Template variables are expanded using the given URI variables, if - * any. - * - * @param uri the URI - * @param uriVariables the variables to expand in the template - */ - void delete(String uri, String... uriVariables); - - /** - * Deletes the resources at the specified URI. URI Template variables are expanded using the given map. - * - * @param uri the URI - * @param uriVariables the variables to expand the template - */ - void delete(String uri, Map uriVariables); - - //OPTIONS - - /** - * Returns value of the Allow header for the given URI. URI Template variables are expanded using the given URI - * variables, if any. - * - * @param uri the URI - * @param uriVariables the variables to expand in the template - * @return the value of the allow header - */ - EnumSet optionsForAllow(String uri, String... uriVariables); - - /** - * Returns value of the Allow header for the given URI. URI Template variables are expanded using the given map. - * - * @param uri the URI - * @param uriVariables the variables to expand in the template - * @return the value of the allow header - */ - EnumSet optionsForAllow(String uri, Map uriVariables); - - /** - * Executes the HTTP methods to the given URI, preparing the request with the {@link HttpRequestCallback}, and - * reading the response with a {@link HttpResponseExtractor}. URI Template variables are expanded using the - * given URI variables, if any. - * - * @param uri the URI - * @param method the HTTP method (GET, POST, etc) - * @param requestCallback object that prepares the request - * @param responseExtractor object that extracts the return value from the response - * @param uriVariables the variables to expand in the template - * @return an arbitrary object, as returned by the {@link HttpResponseExtractor} - */ - T execute(String uri, - HttpMethod method, - HttpRequestCallback requestCallback, - HttpResponseExtractor responseExtractor, - String... uriVariables); - - /** - * Executes the HTTP methods to the given URI, preparing the request with the {@link HttpRequestCallback}, and - * reading the response with a {@link HttpResponseExtractor}. URI Template variables are expanded using the - * given URI variables map. - * - * @param uri the URI - * @param method the HTTP method (GET, POST, etc) - * @param requestCallback object that prepares the request - * @param responseExtractor object that extracts the return value from the response - * @param uriVariables the variables to expand in the template - * @return an arbitrary object, as returned by the {@link HttpResponseExtractor} - */ - T execute(String uri, - HttpMethod method, - HttpRequestCallback requestCallback, - HttpResponseExtractor responseExtractor, - Map uriVariables); - - -} diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/core/package.html b/org.springframework.web/src/main/java/org/springframework/web/client/core/package.html deleted file mode 100644 index fe274ba0f5..0000000000 --- a/org.springframework.web/src/main/java/org/springframework/web/client/core/package.html +++ /dev/null @@ -1,8 +0,0 @@ - - - -Core package of the client-side HTTP support. -Provides a RestTemplate class and various callback interfaces. - - - diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/core/support/package.html b/org.springframework.web/src/main/java/org/springframework/web/client/core/support/package.html deleted file mode 100644 index 9a319aa7d6..0000000000 --- a/org.springframework.web/src/main/java/org/springframework/web/client/core/support/package.html +++ /dev/null @@ -1,8 +0,0 @@ - - - -Classes supporting the org.springframework.web.client.core package. -Contains a base class for RestTemplate usage. - - - diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/package.html b/org.springframework.web/src/main/java/org/springframework/web/client/package.html index 094b557c35..712b3e8851 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/client/package.html +++ b/org.springframework.web/src/main/java/org/springframework/web/client/package.html @@ -1,7 +1,8 @@ -This package contains integration classes for client-side access of HTTP services. +Core package of the client-side web support. +Provides a RestTemplate class and various callback interfaces. diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/core/support/RestGatewaySupport.java b/org.springframework.web/src/main/java/org/springframework/web/client/support/RestGatewaySupport.java similarity index 67% rename from org.springframework.web/src/main/java/org/springframework/web/client/core/support/RestGatewaySupport.java rename to org.springframework.web/src/main/java/org/springframework/web/client/support/RestGatewaySupport.java index 0812970f1d..4ca4e142fc 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/client/core/support/RestGatewaySupport.java +++ b/org.springframework.web/src/main/java/org/springframework/web/client/support/RestGatewaySupport.java @@ -14,48 +14,42 @@ * limitations under the License. */ -package org.springframework.web.client.core.support; +package org.springframework.web.client.support; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.util.Assert; -import org.springframework.web.client.core.RestTemplate; -import org.springframework.web.http.client.ClientHttpRequestFactory; +import org.springframework.web.client.RestTemplate; +import org.springframework.http.client.ClientHttpRequestFactory; /** * Convenient super class for application classes that need REST access. * - *

Requires a {@link ClientHttpRequestFactory} or a {@link RestTemplate} instance to be set. It will create its own - * JmsTemplate if a ConnectionFactory is passed in. A custom JmsTemplate instance can be created for a given - * ConnectionFactory through overriding the createJmsTemplate method. + *

Requires a {@link ClientHttpRequestFactory} or a {@link RestTemplate} instance to be set. * * @author Arjen Poutsma - * @see #setRestTemplate(RestTemplate) - * @see RestTemplate * @since 3.0 + * @see #setRestTemplate + * @see org.springframework.web.client.RestTemplate */ public class RestGatewaySupport { - /** - * Logger available to subclasses. - */ + /** Logger available to subclasses */ protected final Log logger = LogFactory.getLog(getClass()); private RestTemplate restTemplate; + /** - * Constructs a new instance of the {@link RestGatewaySupport}, with default parameters. - * - * @see RestTemplate#RestTemplate() + * Construct a new instance of the {@link RestGatewaySupport}, with default parameters. */ public RestGatewaySupport() { - restTemplate = new RestTemplate(); + this.restTemplate = new RestTemplate(); } /** - * Constructs a new instance of the {@link RestGatewaySupport}, with the given {@link ClientHttpRequestFactory}. - * + * Construct a new instance of the {@link RestGatewaySupport}, with the given {@link ClientHttpRequestFactory}. * @see RestTemplate#RestTemplate(ClientHttpRequestFactory */ public RestGatewaySupport(ClientHttpRequestFactory requestFactory) { @@ -63,12 +57,6 @@ public class RestGatewaySupport { this.restTemplate = new RestTemplate(requestFactory); } - /** - * Returns the {@link RestTemplate} for the gateway. - */ - public RestTemplate getRestTemplate() { - return restTemplate; - } /** * Sets the {@link RestTemplate} for the gateway. @@ -78,4 +66,11 @@ public class RestGatewaySupport { this.restTemplate = restTemplate; } + /** + * Returns the {@link RestTemplate} for the gateway. + */ + public RestTemplate getRestTemplate() { + return this.restTemplate; + } + } diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/support/package.html b/org.springframework.web/src/main/java/org/springframework/web/client/support/package.html index b6dbef257a..74dca8e15e 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/client/support/package.html +++ b/org.springframework.web/src/main/java/org/springframework/web/client/support/package.html @@ -1,8 +1,8 @@ -This package provides generic HTTP support classes, -to be used by higher-level classes like RestTemplate. +Classes supporting the org.springframework.web.client package. +Contains a base class for RestTemplate usage. diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/client/commons/package.html b/org.springframework.web/src/main/java/org/springframework/web/http/client/commons/package.html deleted file mode 100644 index 77dfb3e324..0000000000 --- a/org.springframework.web/src/main/java/org/springframework/web/http/client/commons/package.html +++ /dev/null @@ -1,8 +0,0 @@ - - - -Contains an implementation of the ClientHttpRequest and -ClientHttpResponse based on Commons HTTP Client. - - - diff --git a/org.springframework.web/src/main/java/org/springframework/web/http/package.html b/org.springframework.web/src/main/java/org/springframework/web/http/package.html deleted file mode 100644 index 4dea37deb3..0000000000 --- a/org.springframework.web/src/main/java/org/springframework/web/http/package.html +++ /dev/null @@ -1,8 +0,0 @@ - - - -Contains a basic abstraction over client/server-side HTTP. This package -contains the HttpInputMessage and HttpOutputMessage. - - - diff --git a/org.springframework.web/src/test/java/org/springframework/web/http/HttpHeadersTests.java b/org.springframework.web/src/test/java/org/springframework/http/HttpHeadersTests.java similarity index 97% rename from org.springframework.web/src/test/java/org/springframework/web/http/HttpHeadersTests.java rename to org.springframework.web/src/test/java/org/springframework/http/HttpHeadersTests.java index e4ec4a8288..deb5530ca0 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/http/HttpHeadersTests.java +++ b/org.springframework.web/src/test/java/org/springframework/http/HttpHeadersTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.http; +package org.springframework.http; import java.net.URI; import java.net.URISyntaxException; @@ -27,7 +27,7 @@ import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; -import org.springframework.util.MediaType; +import org.springframework.http.MediaType; /** * @author Arjen Poutsma diff --git a/org.springframework.core/src/test/java/org/springframework/util/MediaTypeTest.java b/org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java similarity index 98% rename from org.springframework.core/src/test/java/org/springframework/util/MediaTypeTest.java rename to org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java index 3e920b839e..9f448f3eda 100644 --- a/org.springframework.core/src/test/java/org/springframework/util/MediaTypeTest.java +++ b/org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.util; +package org.springframework.http; import java.util.ArrayList; import java.util.Collections; @@ -26,7 +26,7 @@ import org.junit.Test; /** * @author Arjen Poutsma */ -public class MediaTypeTest { +public class MediaTypeTests { @Test public void includes() throws Exception { @@ -118,4 +118,5 @@ public class MediaTypeTest { } } } -} \ No newline at end of file + +} diff --git a/org.springframework.web/src/test/java/org/springframework/web/http/MockHttpInputMessage.java b/org.springframework.web/src/test/java/org/springframework/http/MockHttpInputMessage.java similarity index 97% rename from org.springframework.web/src/test/java/org/springframework/web/http/MockHttpInputMessage.java rename to org.springframework.web/src/test/java/org/springframework/http/MockHttpInputMessage.java index 828c1ddd6c..4461024f64 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/http/MockHttpInputMessage.java +++ b/org.springframework.web/src/test/java/org/springframework/http/MockHttpInputMessage.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.http; +package org.springframework.http; import java.io.ByteArrayInputStream; import java.io.IOException; diff --git a/org.springframework.web/src/test/java/org/springframework/web/http/MockHttpOutputMessage.java b/org.springframework.web/src/test/java/org/springframework/http/MockHttpOutputMessage.java similarity index 97% rename from org.springframework.web/src/test/java/org/springframework/web/http/MockHttpOutputMessage.java rename to org.springframework.web/src/test/java/org/springframework/http/MockHttpOutputMessage.java index 844eea0343..323fff1af3 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/http/MockHttpOutputMessage.java +++ b/org.springframework.web/src/test/java/org/springframework/http/MockHttpOutputMessage.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.http; +package org.springframework.http; import java.io.ByteArrayOutputStream; import java.io.IOException; diff --git a/org.springframework.web/src/test/java/org/springframework/web/http/client/AbstractHttpRequestFactoryTestCase.java b/org.springframework.web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java similarity index 97% rename from org.springframework.web/src/test/java/org/springframework/web/http/client/AbstractHttpRequestFactoryTestCase.java rename to org.springframework.web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java index a72c99dbcd..85bdf86607 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/http/client/AbstractHttpRequestFactoryTestCase.java +++ b/org.springframework.web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.http.client; +package org.springframework.http.client; import java.io.IOException; import java.net.URI; @@ -38,8 +38,8 @@ import org.mortbay.jetty.servlet.Context; import org.mortbay.jetty.servlet.ServletHolder; import org.springframework.util.FileCopyUtils; -import org.springframework.web.http.HttpMethod; -import org.springframework.web.http.HttpStatus; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; public abstract class AbstractHttpRequestFactoryTestCase { diff --git a/org.springframework.web/src/test/java/org/springframework/web/http/client/commons/CommonsHttpRequestFactoryTest.java b/org.springframework.web/src/test/java/org/springframework/http/client/CommonsHttpRequestFactoryTests.java similarity index 68% rename from org.springframework.web/src/test/java/org/springframework/web/http/client/commons/CommonsHttpRequestFactoryTest.java rename to org.springframework.web/src/test/java/org/springframework/http/client/CommonsHttpRequestFactoryTests.java index c7775992fc..0f7f5de40d 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/http/client/commons/CommonsHttpRequestFactoryTest.java +++ b/org.springframework.web/src/test/java/org/springframework/http/client/CommonsHttpRequestFactoryTests.java @@ -14,15 +14,16 @@ * limitations under the License. */ -package org.springframework.web.http.client.commons; +package org.springframework.http.client; -import org.springframework.web.http.client.AbstractHttpRequestFactoryTestCase; -import org.springframework.web.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.AbstractHttpRequestFactoryTestCase; +import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.CommonsClientHttpRequestFactory; -public class CommonsHttpRequestFactoryTest extends AbstractHttpRequestFactoryTestCase { +public class CommonsHttpRequestFactoryTests extends AbstractHttpRequestFactoryTestCase { @Override protected ClientHttpRequestFactory createRequestFactory() { return new CommonsClientHttpRequestFactory(); } -} \ No newline at end of file +} diff --git a/org.springframework.web/src/test/java/org/springframework/web/http/client/SimpleHttpRequestFactoryTests.java b/org.springframework.web/src/test/java/org/springframework/http/client/SimpleHttpRequestFactoryTests.java similarity index 94% rename from org.springframework.web/src/test/java/org/springframework/web/http/client/SimpleHttpRequestFactoryTests.java rename to org.springframework.web/src/test/java/org/springframework/http/client/SimpleHttpRequestFactoryTests.java index 544de2bf55..28f05af511 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/http/client/SimpleHttpRequestFactoryTests.java +++ b/org.springframework.web/src/test/java/org/springframework/http/client/SimpleHttpRequestFactoryTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.http.client; +package org.springframework.http.client; public class SimpleHttpRequestFactoryTests extends AbstractHttpRequestFactoryTestCase { diff --git a/org.springframework.web/src/test/java/org/springframework/web/converter/ByteArrayHttpMessageConverterTests.java b/org.springframework.web/src/test/java/org/springframework/http/converter/ByteArrayHttpMessageConverterTests.java similarity index 90% rename from org.springframework.web/src/test/java/org/springframework/web/converter/ByteArrayHttpMessageConverterTests.java rename to org.springframework.web/src/test/java/org/springframework/http/converter/ByteArrayHttpMessageConverterTests.java index 274a45f237..742456a1d4 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/converter/ByteArrayHttpMessageConverterTests.java +++ b/org.springframework.web/src/test/java/org/springframework/http/converter/ByteArrayHttpMessageConverterTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.converter; +package org.springframework.http.converter; import java.io.IOException; @@ -22,9 +22,9 @@ import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; -import org.springframework.util.MediaType; -import org.springframework.web.http.MockHttpInputMessage; -import org.springframework.web.http.MockHttpOutputMessage; +import org.springframework.http.MediaType; +import org.springframework.http.MockHttpInputMessage; +import org.springframework.http.MockHttpOutputMessage; /** * @author Arjen Poutsma diff --git a/org.springframework.web/src/test/java/org/springframework/web/converter/StringHttpMessageConverterTests.java b/org.springframework.web/src/test/java/org/springframework/http/converter/StringHttpMessageConverterTests.java similarity index 93% rename from org.springframework.web/src/test/java/org/springframework/web/converter/StringHttpMessageConverterTests.java rename to org.springframework.web/src/test/java/org/springframework/http/converter/StringHttpMessageConverterTests.java index abb92f8945..6c19bb1282 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/converter/StringHttpMessageConverterTests.java +++ b/org.springframework.web/src/test/java/org/springframework/http/converter/StringHttpMessageConverterTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.converter; +package org.springframework.http.converter; import java.io.IOException; import java.nio.charset.Charset; @@ -24,9 +24,9 @@ import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; -import org.springframework.util.MediaType; -import org.springframework.web.http.MockHttpInputMessage; -import org.springframework.web.http.MockHttpOutputMessage; +import org.springframework.http.MediaType; +import org.springframework.http.MockHttpInputMessage; +import org.springframework.http.MockHttpOutputMessage; /** * @author Arjen Poutsma diff --git a/org.springframework.web/src/test/java/org/springframework/web/http/server/ServletHttpRequestTests.java b/org.springframework.web/src/test/java/org/springframework/http/server/ServletHttpRequestTests.java similarity index 94% rename from org.springframework.web/src/test/java/org/springframework/web/http/server/ServletHttpRequestTests.java rename to org.springframework.web/src/test/java/org/springframework/http/server/ServletHttpRequestTests.java index 3921338ac8..e090b89939 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/http/server/ServletHttpRequestTests.java +++ b/org.springframework.web/src/test/java/org/springframework/http/server/ServletHttpRequestTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.http.server; +package org.springframework.http.server; import java.util.List; @@ -24,8 +24,8 @@ import org.junit.Test; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.util.FileCopyUtils; -import org.springframework.web.http.HttpHeaders; -import org.springframework.web.http.HttpMethod; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; /** * @author Arjen Poutsma diff --git a/org.springframework.web/src/test/java/org/springframework/web/http/server/ServletHttpResponseTest.java b/org.springframework.web/src/test/java/org/springframework/http/server/ServletHttpResponseTest.java similarity index 93% rename from org.springframework.web/src/test/java/org/springframework/web/http/server/ServletHttpResponseTest.java rename to org.springframework.web/src/test/java/org/springframework/http/server/ServletHttpResponseTest.java index c4bbbcc49e..7becb7f924 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/http/server/ServletHttpResponseTest.java +++ b/org.springframework.web/src/test/java/org/springframework/http/server/ServletHttpResponseTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.http.server; +package org.springframework.http.server; import java.util.List; @@ -24,8 +24,8 @@ import org.junit.Test; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.util.FileCopyUtils; -import org.springframework.web.http.HttpHeaders; -import org.springframework.web.http.HttpStatus; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; /** * @author Arjen Poutsma diff --git a/org.springframework.web/src/test/java/org/springframework/web/client/core/RestTemplateIntegrationTests.java b/org.springframework.web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java similarity index 92% rename from org.springframework.web/src/test/java/org/springframework/web/client/core/RestTemplateIntegrationTests.java rename to org.springframework.web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java index 52fb2816e1..89f14f52e3 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/client/core/RestTemplateIntegrationTests.java +++ b/org.springframework.web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java @@ -14,12 +14,13 @@ * limitations under the License. */ -package org.springframework.web.client.core; +package org.springframework.web.client; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.EnumSet; +import java.util.Set; import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; @@ -37,11 +38,9 @@ import org.mortbay.jetty.Server; import org.mortbay.jetty.servlet.Context; import org.mortbay.jetty.servlet.ServletHolder; +import org.springframework.http.HttpMethod; +import org.springframework.http.client.CommonsClientHttpRequestFactory; import org.springframework.util.FileCopyUtils; -import org.springframework.web.client.HttpClientErrorException; -import org.springframework.web.client.HttpServerErrorException; -import org.springframework.web.http.HttpMethod; -import org.springframework.web.http.client.commons.CommonsClientHttpRequestFactory; /** * @author Arjen Poutsma @@ -103,11 +102,12 @@ public class RestTemplateIntegrationTests { @Test public void optionsForAllow() { - EnumSet allowed = template.optionsForAllow("http://localhost:8889/get"); + Set allowed = template.optionsForAllow("http://localhost:8889/get"); assertEquals("Invalid response", EnumSet.of(HttpMethod.GET, HttpMethod.OPTIONS, HttpMethod.HEAD, HttpMethod.TRACE), allowed); } + /** * Servlet that returns and error message for a given status code. */ @@ -125,6 +125,7 @@ public class RestTemplateIntegrationTests { } } + private static class GetServlet extends HttpServlet { private final byte[] buf; @@ -145,6 +146,7 @@ public class RestTemplateIntegrationTests { } } + private static class PostServlet extends HttpServlet { private final String s; diff --git a/org.springframework.web/src/test/java/org/springframework/web/client/core/RestTemplateTest.java b/org.springframework.web/src/test/java/org/springframework/web/client/RestTemplateTests.java similarity index 90% rename from org.springframework.web/src/test/java/org/springframework/web/client/core/RestTemplateTest.java rename to org.springframework.web/src/test/java/org/springframework/web/client/RestTemplateTests.java index a6a321bdfd..06ed125d9b 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/client/core/RestTemplateTest.java +++ b/org.springframework.web/src/test/java/org/springframework/web/client/RestTemplateTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.client.core; +package org.springframework.web.client; import java.io.IOException; import java.net.URI; @@ -22,31 +22,29 @@ import java.util.Collections; import java.util.EnumSet; import java.util.List; import java.util.Map; +import java.util.Set; import static org.easymock.EasyMock.*; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; -import org.springframework.util.MediaType; -import org.springframework.web.client.HttpClientException; -import org.springframework.web.client.HttpIOException; -import org.springframework.web.client.HttpServerErrorException; -import org.springframework.web.converter.ByteArrayHttpMessageConverter; -import org.springframework.web.converter.HttpMessageConverter; -import org.springframework.web.converter.StringHttpMessageConverter; -import org.springframework.web.http.HttpHeaders; -import org.springframework.web.http.HttpMethod; -import org.springframework.web.http.HttpStatus; -import org.springframework.web.http.client.ClientHttpRequest; -import org.springframework.web.http.client.ClientHttpRequestFactory; -import org.springframework.web.http.client.ClientHttpResponse; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.client.ClientHttpRequest; +import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.http.converter.ByteArrayHttpMessageConverter; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.StringHttpMessageConverter; /** * @author Arjen Poutsma */ @SuppressWarnings("unchecked") -public class RestTemplateTest { +public class RestTemplateTests { private RestTemplate template; @@ -56,7 +54,7 @@ public class RestTemplateTest { private ClientHttpResponse response; - private HttpErrorHandler errorHandler; + private ResponseErrorHandler errorHandler; private HttpMessageConverter converter; @@ -65,7 +63,7 @@ public class RestTemplateTest { requestFactory = createMock(ClientHttpRequestFactory.class); request = createMock(ClientHttpRequest.class); response = createMock(ClientHttpResponse.class); - errorHandler = createMock(HttpErrorHandler.class); + errorHandler = createMock(ResponseErrorHandler.class); converter = createMock(HttpMessageConverter.class); template = new RestTemplate(requestFactory); template.setErrorHandler(errorHandler); @@ -200,7 +198,7 @@ public class RestTemplateTest { template.getForObject("http://example.com/{p}", String.class, "resource"); fail("UnsupportedMediaTypeException expected"); } - catch (HttpClientException ex) { + catch (RestClientException ex) { // expected } verifyMocks(); @@ -309,7 +307,7 @@ public class RestTemplateTest { replayMocks(); - EnumSet result = template.optionsForAllow("http://example.com"); + Set result = template.optionsForAllow("http://example.com"); assertEquals("Invalid OPTIONS result", expected, result); verifyMocks(); @@ -330,7 +328,7 @@ public class RestTemplateTest { template.getForObject("http://example.com/resource", String.class); fail("RestClientException expected"); } - catch (HttpIOException ex) { + catch (ResourceAccessException ex) { // expected }