From 52b74c985dda3642189aaca62d75d2a9850f60c6 Mon Sep 17 00:00:00 2001 From: adriancole Date: Sat, 6 Jul 2013 17:10:31 -0700 Subject: [PATCH] cleaned up usage of util and removed unused URI parser --- .../src/main/java/feign/MethodHandler.java | 7 ---- feign-core/src/main/java/feign/Util.java | 37 ------------------- .../main/java/feign/codec/ErrorDecoder.java | 10 ++++- .../test/java/feign/DefaultContractTest.java | 3 +- .../feign/examples/AWSSignatureVersion4.java | 3 +- .../main/java/feign/jaxrs/JAXRSModule.java | 18 +++++++-- .../java/feign/jaxrs/JAXRSContractTest.java | 2 +- 7 files changed, 27 insertions(+), 53 deletions(-) diff --git a/feign-core/src/main/java/feign/MethodHandler.java b/feign-core/src/main/java/feign/MethodHandler.java index 0761b927e2..149addf91e 100644 --- a/feign-core/src/main/java/feign/MethodHandler.java +++ b/feign-core/src/main/java/feign/MethodHandler.java @@ -17,7 +17,6 @@ import java.io.IOException; import java.lang.reflect.Type; -import java.net.URI; import javax.inject.Inject; import javax.inject.Provider; @@ -28,9 +27,7 @@ import static feign.FeignException.errorExecuting; import static feign.FeignException.errorReading; -import static feign.Util.LOCATION; import static feign.Util.checkNotNull; -import static feign.Util.firstOrNull; final class MethodHandler { @@ -109,10 +106,6 @@ public Object executeAndDecode(String configKey, RequestTemplate template, Type if (response.status() >= 200 && response.status() < 300) { if (returnType.equals(Response.class)) { return response; - } else if (returnType == URI.class && response.body() == null) { - String location = firstOrNull(response.headers(), LOCATION); - if (location != null) - return URI.create(location); } else if (returnType == void.class) { return null; } diff --git a/feign-core/src/main/java/feign/Util.java b/feign-core/src/main/java/feign/Util.java index c001aed24d..57206e8dae 100644 --- a/feign-core/src/main/java/feign/Util.java +++ b/feign-core/src/main/java/feign/Util.java @@ -31,27 +31,10 @@ public class Util { private Util() { // no instances } - // feign.Util - /** - * The HTTP Accept header field name. - */ - public static final String ACCEPT = "Accept"; /** * The HTTP Content-Length header field name. */ public static final String CONTENT_LENGTH = "Content-Length"; - /** - * The HTTP Content-Type header field name. - */ - public static final String CONTENT_TYPE = "Content-Type"; - /** - * The HTTP Host header field name. - */ - public static final String HOST = "Host"; - /** - * The HTTP Location header field name. - */ - public static final String LOCATION = "Location"; /** * The HTTP Retry-After header field name. */ @@ -108,19 +91,6 @@ public static String emptyToNull(String string) { return string == null || string.isEmpty() ? null : string; } - public static String join(char separator, String... parts) { - if (parts == null || parts.length == 0) - return ""; - StringBuilder to = new StringBuilder(); - for (int i = 0; i < parts.length; i++) { - to.append(parts[i]); - if (i + 1 < parts.length) { - to.append(separator); - } - } - return to.toString(); - } - /** * Adapted from {@code com.google.common.base.Strings#emptyToNull}. */ @@ -144,11 +114,4 @@ public static T[] toArray(Iterable iterable, Class type) { public static Collection valuesOrEmpty(Map> map, String key) { return map.containsKey(key) ? map.get(key) : Collections.emptyList(); } - - public static T firstOrNull(Map> map, String key) { - if (map.containsKey(key) && !map.get(key).isEmpty()) { - return map.get(key).iterator().next(); - } - return null; - } } diff --git a/feign-core/src/main/java/feign/codec/ErrorDecoder.java b/feign-core/src/main/java/feign/codec/ErrorDecoder.java index 31d163ebf4..08a75faaf0 100644 --- a/feign-core/src/main/java/feign/codec/ErrorDecoder.java +++ b/feign-core/src/main/java/feign/codec/ErrorDecoder.java @@ -19,7 +19,9 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Collection; import java.util.Date; +import java.util.Map; import feign.FeignException; import feign.Response; @@ -28,7 +30,6 @@ import static feign.FeignException.errorStatus; import static feign.Util.RETRY_AFTER; import static feign.Util.checkNotNull; -import static feign.Util.firstOrNull; import static java.util.Locale.US; import static java.util.concurrent.TimeUnit.NANOSECONDS; import static java.util.concurrent.TimeUnit.SECONDS; @@ -86,6 +87,13 @@ public Object decode(String methodKey, Response response, Type type) throws Thro throw new RetryableException(exception.getMessage(), exception, retryAfter); throw exception; } + + private T firstOrNull(Map> map, String key) { + if (map.containsKey(key) && !map.get(key).isEmpty()) { + return map.get(key).iterator().next(); + } + return null; + } }; /** diff --git a/feign-core/src/test/java/feign/DefaultContractTest.java b/feign-core/src/test/java/feign/DefaultContractTest.java index 672b1f8773..8fc0dc2b71 100644 --- a/feign-core/src/test/java/feign/DefaultContractTest.java +++ b/feign-core/src/test/java/feign/DefaultContractTest.java @@ -24,7 +24,6 @@ import javax.inject.Named; -import static feign.Util.CONTENT_TYPE; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNull; @@ -142,7 +141,7 @@ interface BodyWithoutParameters { @Test public void producesAddsContentTypeHeader() throws Exception { MethodMetadata md = contract.parseAndValidatateMetadata(BodyWithoutParameters.class.getDeclaredMethod("post")); - assertEquals(md.template().headers().get(CONTENT_TYPE), ImmutableSet.of("application/xml")); + assertEquals(md.template().headers().get("Content-Type"), ImmutableSet.of("application/xml")); } interface WithURIParam { diff --git a/feign-core/src/test/java/feign/examples/AWSSignatureVersion4.java b/feign-core/src/test/java/feign/examples/AWSSignatureVersion4.java index dedc5a63b7..40c83eda84 100644 --- a/feign-core/src/test/java/feign/examples/AWSSignatureVersion4.java +++ b/feign-core/src/test/java/feign/examples/AWSSignatureVersion4.java @@ -37,7 +37,6 @@ import static com.google.common.collect.Iterables.transform; import static com.google.common.hash.Hashing.sha256; import static com.google.common.io.BaseEncoding.base16; -import static feign.Util.HOST; import static feign.Util.UTF_8; // http://docs.aws.amazon.com/general/latest/gr/signature-version-4.html @@ -54,7 +53,7 @@ public AWSSignatureVersion4(String accessKey, String secretKey) { } @Override public Request apply(RequestTemplate input) { - input.header(HOST, URI.create(input.url()).getHost()); + input.header("Host", URI.create(input.url()).getHost()); TreeMultimap sortedLowercaseHeaders = TreeMultimap.create(); for (String key : input.headers().keySet()) { sortedLowercaseHeaders.putAll(trimToLowercase.apply(key), diff --git a/feign-jaxrs/src/main/java/feign/jaxrs/JAXRSModule.java b/feign-jaxrs/src/main/java/feign/jaxrs/JAXRSModule.java index 07d6b9399d..ae01579bc0 100644 --- a/feign-jaxrs/src/main/java/feign/jaxrs/JAXRSModule.java +++ b/feign-jaxrs/src/main/java/feign/jaxrs/JAXRSModule.java @@ -33,13 +33,12 @@ import feign.Contract; import feign.MethodMetadata; -import static feign.Util.ACCEPT; -import static feign.Util.CONTENT_TYPE; import static feign.Util.checkState; -import static feign.Util.join; @dagger.Module(library = true) public final class JAXRSModule { + static final String ACCEPT = "Accept"; + static final String CONTENT_TYPE = "Content-Type"; @Provides Contract provideContract() { return new JAXRSContract(); @@ -103,4 +102,17 @@ protected boolean processAnnotationsOnParameter(MethodMetadata data, Annotation[ return isHttpParam; } } + + private static String join(char separator, String... parts) { + if (parts == null || parts.length == 0) + return ""; + StringBuilder to = new StringBuilder(); + for (int i = 0; i < parts.length; i++) { + to.append(parts[i]); + if (i + 1 < parts.length) { + to.append(separator); + } + } + return to.toString(); + } } diff --git a/feign-jaxrs/src/test/java/feign/jaxrs/JAXRSContractTest.java b/feign-jaxrs/src/test/java/feign/jaxrs/JAXRSContractTest.java index 40a4370c8b..6f02f9f9f3 100644 --- a/feign-jaxrs/src/test/java/feign/jaxrs/JAXRSContractTest.java +++ b/feign-jaxrs/src/test/java/feign/jaxrs/JAXRSContractTest.java @@ -42,7 +42,7 @@ import feign.MethodMetadata; import feign.Response; -import static feign.Util.CONTENT_TYPE; +import static feign.jaxrs.JAXRSModule.CONTENT_TYPE; import static javax.ws.rs.HttpMethod.DELETE; import static javax.ws.rs.HttpMethod.GET; import static javax.ws.rs.HttpMethod.POST;