Skip to content

Commit

Permalink
Reformats code according to Google Java Style
Browse files Browse the repository at this point in the history
Files had various formatting differences, as did pull requests. Rather than
create our own style, this inherits and requires the well documented Google
Java Style.
  • Loading branch information
Adrian Cole committed Feb 2, 2015
1 parent 42fc476 commit 207530d
Show file tree
Hide file tree
Showing 89 changed files with 3,426 additions and 2,395 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Adds `Feign.Builder.build()`
* Opens constructor for Gson and Jackson codecs which accepts type adapters
* Adds EmptyTarget for interfaces who exclusively declare URI methods
* Reformats code according to [Google Java Style](https://google-styleguide.googlecode.com/svn/trunk/javaguide.html)

### Version 7.1
* Introduces feign.@Param to annotate template parameters. Users must migrate from `javax.inject.@Named` to `feign.@Param` before updating to Feign 8.0.
Expand Down
31 changes: 31 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Contributing to Feign

If you would like to contribute code you can do so through GitHub by forking the repository and sending a pull request (on a branch other than `master` or `gh-pages`).

When submitting code, please ensure you follow the [Google Style Guide](http://google-styleguide.googlecode.com/svn/trunk/javaguide.html). For example, you can format code with Intellij using [this file](https://google-styleguide.googlecode.com/svn/trunk/intellij-java-google-style.xml).

## License

By contributing your code, you agree to license your contribution under the terms of the APLv2: https://github.com/Netflix/Feign/blob/master/LICENSE

All files are released with the Apache 2.0 license.

If you are adding a new file it should have a header like this:

```
/**
* Copyright 2013 Netflix, Inc.
*
* 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.
*/
```
16 changes: 7 additions & 9 deletions core/src/main/java/feign/Body.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* A possibly templated body of a PUT or POST command. variables wrapped in curly braces are expanded before the
* request is submitted.
* <br>
* ex.
* <br>
* A possibly templated body of a PUT or POST command. variables wrapped in curly braces are
* expanded before the request is submitted. <br> ex. <br>
* <pre>
* &#064;Body(&quot;&lt;v01:getResourceRecordsOfZone&gt;&lt;zoneName&gt;{zoneName}&lt;/zoneName&gt;&lt;rrType&gt;0&lt;/rrType&gt;&lt;/v01:getResourceRecordsOfZone&gt;&quot;)
* List&lt;Record&gt; listByZone(&#64;Param(&quot;zoneName&quot;) String zoneName);
* </pre>
* <br>
* Note that if you'd like curly braces literally in the body, urlencode
* them first.
* <br> Note that if you'd like curly braces literally in the body, urlencode them first.
*
* @see RequestTemplate#expand(String, Map)
*/
@Target(METHOD) @Retention(RUNTIME) public @interface Body {
@Target(METHOD)
@Retention(RUNTIME)
public @interface Body {

String value();
}
37 changes: 25 additions & 12 deletions core/src/main/java/feign/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@
import static feign.Util.ENCODING_GZIP;

/**
* Submits HTTP {@link Request requests}. Implementations are expected to be
* thread-safe.
* Submits HTTP {@link Request requests}. Implementations are expected to be thread-safe.
*/
public interface Client {

/**
* Executes a request against its {@link Request#url() url} and returns a
* response.
* Executes a request against its {@link Request#url() url} and returns a response.
*
* @param request safe to replay.
* @param options options to apply to this request.
Expand All @@ -53,22 +52,28 @@ public interface Client {
Response execute(Request request, Options options) throws IOException;

public static class Default implements Client {

private final SSLSocketFactory sslContextFactory;
private final HostnameVerifier hostnameVerifier;

/** Null parameters imply platform defaults. */
/**
* Null parameters imply platform defaults.
*/
public Default(SSLSocketFactory sslContextFactory, HostnameVerifier hostnameVerifier) {
this.sslContextFactory = sslContextFactory;
this.hostnameVerifier = hostnameVerifier;
}

@Override public Response execute(Request request, Options options) throws IOException {
@Override
public Response execute(Request request, Options options) throws IOException {
HttpURLConnection connection = convertAndSend(request, options);
return convertResponse(connection);
}

HttpURLConnection convertAndSend(Request request, Options options) throws IOException {
final HttpURLConnection connection = (HttpURLConnection) new URL(request.url()).openConnection();
final HttpURLConnection
connection =
(HttpURLConnection) new URL(request.url()).openConnection();
if (connection instanceof HttpsURLConnection) {
HttpsURLConnection sslCon = (HttpsURLConnection) connection;
if (sslContextFactory != null) {
Expand All @@ -85,12 +90,16 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce
connection.setRequestMethod(request.method());

Collection<String> contentEncodingValues = request.headers().get(CONTENT_ENCODING);
boolean gzipEncodedRequest = contentEncodingValues != null && contentEncodingValues.contains(ENCODING_GZIP);
boolean
gzipEncodedRequest =
contentEncodingValues != null && contentEncodingValues.contains(ENCODING_GZIP);

boolean hasAcceptHeader = false;
Integer contentLength = null;
for (String field : request.headers().keySet()) {
if (field.equalsIgnoreCase("Accept")) hasAcceptHeader = true;
if (field.equalsIgnoreCase("Accept")) {
hasAcceptHeader = true;
}
for (String value : request.headers().get(field)) {
if (field.equals(CONTENT_LENGTH)) {
if (!gzipEncodedRequest) {
Expand All @@ -103,7 +112,9 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce
}
}
// Some servers choke on the default accept string.
if (!hasAcceptHeader) connection.addRequestProperty("Accept", "*/*");
if (!hasAcceptHeader) {
connection.addRequestProperty("Accept", "*/*");
}

if (request.body() != null) {
if (contentLength != null) {
Expand Down Expand Up @@ -135,13 +146,15 @@ Response convertResponse(HttpURLConnection connection) throws IOException {
Map<String, Collection<String>> headers = new LinkedHashMap<String, Collection<String>>();
for (Map.Entry<String, List<String>> field : connection.getHeaderFields().entrySet()) {
// response message
if (field.getKey() != null)
if (field.getKey() != null) {
headers.put(field.getKey(), field.getValue());
}
}

Integer length = connection.getContentLength();
if (length == -1)
if (length == -1) {
length = null;
}
InputStream stream;
if (status >= 400) {
stream = connection.getErrorStream();
Expand Down
67 changes: 44 additions & 23 deletions core/src/main/java/feign/Contract.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ public interface Contract {

abstract class BaseContract implements Contract {

@Override public List<MethodMetadata> parseAndValidatateMetadata(Class<?> declaring) {
@Override
public List<MethodMetadata> parseAndValidatateMetadata(Class<?> declaring) {
List<MethodMetadata> metadata = new ArrayList<MethodMetadata>();
for (Method method : declaring.getDeclaredMethods()) {
if (method.getDeclaringClass() == Object.class)
if (method.getDeclaringClass() == Object.class) {
continue;
}
metadata.add(parseAndValidatateMetadata(method));
}
return metadata;
Expand All @@ -60,8 +62,9 @@ public MethodMetadata parseAndValidatateMetadata(Method method) {
for (Annotation methodAnnotation : method.getAnnotations()) {
processAnnotationOnMethod(data, methodAnnotation, method);
}
checkState(data.template().method() != null, "Method %s not annotated with HTTP method type (ex. GET, POST)",
method.getName());
checkState(data.template().method() != null,
"Method %s not annotated with HTTP method type (ex. GET, POST)",
method.getName());
Class<?>[] parameterTypes = method.getParameterTypes();

Annotation[][] parameterAnnotations = method.getParameterAnnotations();
Expand All @@ -74,7 +77,8 @@ public MethodMetadata parseAndValidatateMetadata(Method method) {
if (parameterTypes[i] == URI.class) {
data.urlIndex(i);
} else if (!isHttpAnnotation) {
checkState(data.formParams().isEmpty(), "Body parameters cannot be used with form parameters.");
checkState(data.formParams().isEmpty(),
"Body parameters cannot be used with form parameters.");
checkState(data.bodyIndex() == null, "Method has too many Body parameters: %s", method);
data.bodyIndex(i);
data.bodyType(method.getGenericParameterTypes()[i]);
Expand All @@ -88,22 +92,26 @@ public MethodMetadata parseAndValidatateMetadata(Method method) {
* @param annotation annotations present on the current method annotation.
* @param method method currently being processed.
*/
protected abstract void processAnnotationOnMethod(MethodMetadata data, Annotation annotation, Method method);
protected abstract void processAnnotationOnMethod(MethodMetadata data, Annotation annotation,
Method method);

/**
* @param data metadata collected so far relating to the current java method.
* @param annotations annotations present on the current parameter annotation.
* @param paramIndex if you find a name in {@code annotations}, call {@link #nameParam(MethodMetadata, String,
* int)} with this as the last parameter.
* @return true if you called {@link #nameParam(MethodMetadata, String, int)} after finding an http-relevant
* annotation.
* @param paramIndex if you find a name in {@code annotations}, call {@link
* #nameParam(MethodMetadata, String, int)} with this as the last parameter.
* @return true if you called {@link #nameParam(MethodMetadata, String, int)} after finding an
* http-relevant annotation.
*/
protected abstract boolean processAnnotationsOnParameter(MethodMetadata data, Annotation[] annotations, int paramIndex);
protected abstract boolean processAnnotationsOnParameter(MethodMetadata data,
Annotation[] annotations,
int paramIndex);


protected Collection<String> addTemplatedParam(Collection<String> possiblyNull, String name) {
if (possiblyNull == null)
if (possiblyNull == null) {
possiblyNull = new ArrayList<String>();
}
possiblyNull.add(String.format("{%s}", name));
return possiblyNull;
}
Expand All @@ -112,7 +120,9 @@ protected Collection<String> addTemplatedParam(Collection<String> possiblyNull,
* links a parameter name to its index in the method signature.
*/
protected void nameParam(MethodMetadata data, String name, int i) {
Collection<String> names = data.indexToName().containsKey(i) ? data.indexToName().get(i) : new ArrayList<String>();
Collection<String>
names =
data.indexToName().containsKey(i) ? data.indexToName().get(i) : new ArrayList<String>();
names.add(name);
data.indexToName().put(i, names);
}
Expand All @@ -121,11 +131,13 @@ protected void nameParam(MethodMetadata data, String name, int i) {
class Default extends BaseContract {

@Override
protected void processAnnotationOnMethod(MethodMetadata data, Annotation methodAnnotation, Method method) {
protected void processAnnotationOnMethod(MethodMetadata data, Annotation methodAnnotation,
Method method) {
Class<? extends Annotation> annotationType = methodAnnotation.annotationType();
if (annotationType == RequestLine.class) {
String requestLine = RequestLine.class.cast(methodAnnotation).value();
checkState(emptyToNull(requestLine) != null, "RequestLine annotation was empty on method %s.", method.getName());
checkState(emptyToNull(requestLine) != null,
"RequestLine annotation was empty on method %s.", method.getName());
if (requestLine.indexOf(' ') == -1) {
data.template().method(requestLine);
return;
Expand All @@ -136,20 +148,25 @@ protected void processAnnotationOnMethod(MethodMetadata data, Annotation methodA
data.template().append(requestLine.substring(requestLine.indexOf(' ') + 1));
} else {
// skip HTTP version
data.template().append(requestLine.substring(requestLine.indexOf(' ') + 1, requestLine.lastIndexOf(' ')));
data.template().append(
requestLine.substring(requestLine.indexOf(' ') + 1, requestLine.lastIndexOf(' ')));
}
} else if (annotationType == Body.class) {
String body = Body.class.cast(methodAnnotation).value();
checkState(emptyToNull(body) != null, "Body annotation was empty on method %s.", method.getName());
checkState(emptyToNull(body) != null, "Body annotation was empty on method %s.",
method.getName());
if (body.indexOf('{') == -1) {
data.template().body(body);
} else {
data.template().bodyTemplate(body);
}
} else if (annotationType == Headers.class) {
String[] headersToParse = Headers.class.cast(methodAnnotation).value();
checkState(headersToParse.length > 0, "Headers annotation was empty on method %s.", method.getName());
Map<String, Collection<String>> headers = new LinkedHashMap<String, Collection<String>>(headersToParse.length);
checkState(headersToParse.length > 0, "Headers annotation was empty on method %s.",
method.getName());
Map<String, Collection<String>>
headers =
new LinkedHashMap<String, Collection<String>>(headersToParse.length);
for (String header : headersToParse) {
int colon = header.indexOf(':');
String name = header.substring(0, colon);
Expand All @@ -163,13 +180,15 @@ protected void processAnnotationOnMethod(MethodMetadata data, Annotation methodA
}

@Override
protected boolean processAnnotationsOnParameter(MethodMetadata data, Annotation[] annotations, int paramIndex) {
protected boolean processAnnotationsOnParameter(MethodMetadata data, Annotation[] annotations,
int paramIndex) {
boolean isHttpAnnotation = false;
for (Annotation annotation : annotations) {
Class<? extends Annotation> annotationType = annotation.annotationType();
if (annotationType == Param.class) {
String name = ((Param) annotation).value();
checkState(emptyToNull(name) != null, "Param annotation was empty on param %s.", paramIndex);
checkState(emptyToNull(name) != null, "Param annotation was empty on param %s.",
paramIndex);
nameParam(data, name, paramIndex);
if (annotationType == Param.class) {
Class<? extends Param.Expander> expander = ((Param) annotation).expander();
Expand All @@ -191,12 +210,14 @@ protected boolean processAnnotationsOnParameter(MethodMetadata data, Annotation[

private <K, V> boolean searchMapValues(Map<K, Collection<V>> map, V search) {
Collection<Collection<V>> values = map.values();
if (values == null)
if (values == null) {
return false;
}

for (Collection<V> entry : values) {
if (entry.contains(search))
if (entry.contains(search)) {
return true;
}
}

return false;
Expand Down
Loading

0 comments on commit 207530d

Please sign in to comment.