Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feign annotated parameter processors
- Loading branch information
1 parent
50efa85
commit 31d2f65
Showing
7 changed files
with
376 additions
and
74 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...re/src/main/java/org/springframework/cloud/netflix/feign/AnnotatedParameterProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2013-2015 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.cloud.netflix.feign; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.Collection; | ||
|
||
import feign.MethodMetadata; | ||
|
||
/** | ||
* Feign contract method parameter processor. | ||
* | ||
* @author Jakub Narloch | ||
*/ | ||
public interface AnnotatedParameterProcessor { | ||
|
||
/** | ||
* Retrieves the processor supported annotation type. | ||
* | ||
* @return the annotation type | ||
*/ | ||
Class<? extends Annotation> getAnnotationType(); | ||
|
||
/** | ||
* Process the annotated parameter. | ||
* | ||
* @param context the parameter context | ||
* @param annotation the annotation instance | ||
* @return whether the parameter is http | ||
*/ | ||
boolean processArgument(AnnotatedParameterContext context, Annotation annotation); | ||
|
||
/** | ||
* Specifies the parameter context. | ||
* | ||
* @author Jakub Narloch | ||
*/ | ||
interface AnnotatedParameterContext { | ||
|
||
/** | ||
* Retrieves the method metadata. | ||
* | ||
* @return the method metadata | ||
*/ | ||
MethodMetadata getMethodMetadata(); | ||
|
||
/** | ||
* Retrieves the index of the parameter. | ||
* | ||
* @return the parameter index | ||
*/ | ||
int getParameterIndex(); | ||
|
||
/** | ||
* Sets the parameter name. | ||
* | ||
* @param name the name of the parameter | ||
*/ | ||
void setParameterName(String name); | ||
|
||
/** | ||
* Sets the template parameter. | ||
* | ||
* @param name the template parameter | ||
* @param rest the existing parameter values | ||
* @return parameters | ||
*/ | ||
Collection<String> setTemplateParameter(String name, Collection<String> rest); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...va/org/springframework/cloud/netflix/feign/annotation/PathVariableParameterProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2013-2015 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.cloud.netflix.feign.annotation; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
import org.springframework.cloud.netflix.feign.AnnotatedParameterProcessor; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
|
||
import feign.MethodMetadata; | ||
|
||
import static feign.Util.checkState; | ||
import static feign.Util.emptyToNull; | ||
|
||
/** | ||
* {@link PathVariable} parameter processor. | ||
* | ||
* @author Jakub Narloch | ||
* @see AnnotatedParameterProcessor | ||
*/ | ||
public class PathVariableParameterProcessor implements AnnotatedParameterProcessor { | ||
|
||
private static final Class<PathVariable> ANNOTATION = PathVariable.class; | ||
|
||
@Override | ||
public Class<? extends Annotation> getAnnotationType() { | ||
return ANNOTATION; | ||
} | ||
|
||
@Override | ||
public boolean processArgument(AnnotatedParameterContext context, Annotation annotation) { | ||
String name = ANNOTATION.cast(annotation).value(); | ||
checkState(emptyToNull(name) != null, | ||
"PathVariable annotation was empty on param %s.", context.getParameterIndex()); | ||
context.setParameterName(name); | ||
|
||
MethodMetadata data = context.getMethodMetadata(); | ||
String varName = '{' + name + '}'; | ||
if (!data.template().url().contains(varName) | ||
&& !searchMapValues(data.template().queries(), varName) | ||
&& !searchMapValues(data.template().headers(), varName)) { | ||
data.formParams().add(name); | ||
} | ||
return true; | ||
} | ||
|
||
private <K, V> boolean searchMapValues(Map<K, Collection<V>> map, V search) { | ||
Collection<Collection<V>> values = map.values(); | ||
if (values == null) { | ||
return false; | ||
} | ||
for (Collection<V> entry : values) { | ||
if (entry.contains(search)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...a/org/springframework/cloud/netflix/feign/annotation/RequestHeaderParameterProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2013-2015 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.cloud.netflix.feign.annotation; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.Collection; | ||
|
||
import org.springframework.cloud.netflix.feign.AnnotatedParameterProcessor; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
|
||
import feign.MethodMetadata; | ||
|
||
import static feign.Util.checkState; | ||
import static feign.Util.emptyToNull; | ||
|
||
/** | ||
* {@link RequestHeader} parameter processor. | ||
* | ||
* @author Jakub Narloch | ||
* @see AnnotatedParameterProcessor | ||
*/ | ||
public class RequestHeaderParameterProcessor implements AnnotatedParameterProcessor { | ||
|
||
private static final Class<RequestHeader> ANNOTATION = RequestHeader.class; | ||
|
||
@Override | ||
public Class<? extends Annotation> getAnnotationType() { | ||
return ANNOTATION; | ||
} | ||
|
||
@Override | ||
public boolean processArgument(AnnotatedParameterContext context, Annotation annotation) { | ||
String name = ANNOTATION.cast(annotation).value(); | ||
checkState(emptyToNull(name) != null, | ||
"RequestHeader.value() was empty on parameter %s", context.getParameterIndex()); | ||
context.setParameterName(name); | ||
|
||
MethodMetadata data = context.getMethodMetadata(); | ||
Collection<String> header = context.setTemplateParameter(name, data.template().headers().get(name)); | ||
data.template().header(name, header); | ||
return true; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...va/org/springframework/cloud/netflix/feign/annotation/RequestParamParameterProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2013-2015 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.cloud.netflix.feign.annotation; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.Collection; | ||
|
||
import org.springframework.cloud.netflix.feign.AnnotatedParameterProcessor; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
import feign.MethodMetadata; | ||
|
||
import static feign.Util.checkState; | ||
import static feign.Util.emptyToNull; | ||
|
||
/** | ||
* {@link RequestParam} parameter processor. | ||
* | ||
* @author Jakub Narloch | ||
* @see AnnotatedParameterProcessor | ||
*/ | ||
public class RequestParamParameterProcessor implements AnnotatedParameterProcessor { | ||
|
||
private static final Class<RequestParam> ANNOTATION = RequestParam.class; | ||
|
||
@Override | ||
public Class<? extends Annotation> getAnnotationType() { | ||
return ANNOTATION; | ||
} | ||
|
||
@Override | ||
public boolean processArgument(AnnotatedParameterContext context, Annotation annotation) { | ||
String name = ANNOTATION.cast(annotation).value(); | ||
checkState(emptyToNull(name) != null, | ||
"RequestParam.value() was empty on parameter %s", context.getParameterIndex()); | ||
context.setParameterName(name); | ||
|
||
MethodMetadata data = context.getMethodMetadata(); | ||
Collection<String> query = context.setTemplateParameter(name, data.template().queries().get(name)); | ||
data.template().query(name, query); | ||
return true; | ||
} | ||
} |
Oops, something went wrong.