Skip to content

Commit

Permalink
Merge pull request #11 from Netflix/cleanup
Browse files Browse the repository at this point in the history
cleaned up usage of util and removed unused URI parser
  • Loading branch information
Adrian Cole committed Jul 7, 2013
2 parents 83a4df9 + 52b74c9 commit 232c974
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 53 deletions.
7 changes: 0 additions & 7 deletions feign-core/src/main/java/feign/MethodHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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;
}
Expand Down
37 changes: 0 additions & 37 deletions feign-core/src/main/java/feign/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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}.
*/
Expand All @@ -144,11 +114,4 @@ public static <T> T[] toArray(Iterable<? extends T> iterable, Class<T> type) {
public static <T> Collection<T> valuesOrEmpty(Map<String, Collection<T>> map, String key) {
return map.containsKey(key) ? map.get(key) : Collections.<T>emptyList();
}

public static <T> T firstOrNull(Map<String, Collection<T>> map, String key) {
if (map.containsKey(key) && !map.get(key).isEmpty()) {
return map.get(key).iterator().next();
}
return null;
}
}
10 changes: 9 additions & 1 deletion feign-core/src/main/java/feign/codec/ErrorDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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> T firstOrNull(Map<String, Collection<T>> map, String key) {
if (map.containsKey(key) && !map.get(key).isEmpty()) {
return map.get(key).iterator().next();
}
return null;
}
};

/**
Expand Down
3 changes: 1 addition & 2 deletions feign-core/src/test/java/feign/DefaultContractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String, String> sortedLowercaseHeaders = TreeMultimap.create();
for (String key : input.headers().keySet()) {
sortedLowercaseHeaders.putAll(trimToLowercase.apply(key),
Expand Down
18 changes: 15 additions & 3 deletions feign-jaxrs/src/main/java/feign/jaxrs/JAXRSModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 232c974

Please sign in to comment.