|
| 1 | +/** |
| 2 | + * Copyright 2012-2019 The Feign Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | +package feign.spring; |
| 15 | + |
| 16 | +import java.lang.annotation.Annotation; |
| 17 | +import java.lang.reflect.Method; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Collection; |
| 20 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 21 | +import org.springframework.web.bind.annotation.PathVariable; |
| 22 | +import org.springframework.web.bind.annotation.RequestBody; |
| 23 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 24 | +import org.springframework.web.bind.annotation.RequestParam; |
| 25 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 26 | +import feign.Contract.BaseContract; |
| 27 | +import feign.DeclarativeContract; |
| 28 | +import feign.MethodMetadata; |
| 29 | + |
| 30 | +public class SpringContract extends DeclarativeContract { |
| 31 | + |
| 32 | + static final String ACCEPT = "Accept"; |
| 33 | + static final String CONTENT_TYPE = "Content-Type"; |
| 34 | + |
| 35 | + public SpringContract() { |
| 36 | + registerClassAnnotation(RequestMapping.class, (requestMapping, data) -> { |
| 37 | + appendMappings(data, requestMapping.value()); |
| 38 | + |
| 39 | + if (requestMapping.method().length == 1) |
| 40 | + data.template().method(requestMapping.method()[0].name()); |
| 41 | + |
| 42 | + handleProducesAnnotation(data, requestMapping.produces()); |
| 43 | + handleConsumesAnnotation(data, requestMapping.consumes()); |
| 44 | + }); |
| 45 | + |
| 46 | + registerMethodAnnotation(RequestMapping.class, (requestMapping, data) -> { |
| 47 | + String[] mappings = requestMapping.value(); |
| 48 | + appendMappings(data, mappings); |
| 49 | + |
| 50 | + if (requestMapping.method().length == 1) |
| 51 | + data.template().method(requestMapping.method()[0].name()); |
| 52 | + }); |
| 53 | + |
| 54 | + registerMethodAnnotation(ResponseBody.class, (body, data) -> { |
| 55 | + handleConsumesAnnotation(data, "application/json"); |
| 56 | + }); |
| 57 | + registerMethodAnnotation(ExceptionHandler.class, (ann, data) -> { |
| 58 | + data.ignoreMethod(); |
| 59 | + }); |
| 60 | + registerParameterAnnotation(PathVariable.class, (parameterAnnotation, data, paramIndex) -> { |
| 61 | + String name = PathVariable.class.cast(parameterAnnotation).value(); |
| 62 | + nameParam(data, name, paramIndex); |
| 63 | + }); |
| 64 | + |
| 65 | + registerParameterAnnotation(RequestBody.class, (body, data, paramIndex) -> { |
| 66 | + handleProducesAnnotation(data, "application/json"); |
| 67 | + }); |
| 68 | + registerParameterAnnotation(RequestParam.class, (parameterAnnotation, data, paramIndex) -> { |
| 69 | + String name = RequestParam.class.cast(parameterAnnotation).value(); |
| 70 | + Collection<String> query = addTemplatedParam(data.template().queries().get(name), name); |
| 71 | + data.template().query(name, query); |
| 72 | + nameParam(data, name, paramIndex); |
| 73 | + }); |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + private void appendMappings(MethodMetadata data, String[] mappings) { |
| 78 | + for (int i = 0; i < mappings.length; i++) { |
| 79 | + String methodAnnotationValue = mappings[i]; |
| 80 | + if (!methodAnnotationValue.startsWith("/") && !data.template().url().endsWith("/")) { |
| 81 | + methodAnnotationValue = "/" + methodAnnotationValue; |
| 82 | + } |
| 83 | + if (data.template().url().endsWith("/") && methodAnnotationValue.startsWith("/")) { |
| 84 | + methodAnnotationValue = methodAnnotationValue.substring(1); |
| 85 | + } |
| 86 | + |
| 87 | + data.template().uri(data.template().url() + methodAnnotationValue); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private void handleProducesAnnotation(MethodMetadata data, String... produces) { |
| 92 | + if (produces.length == 0) |
| 93 | + return; |
| 94 | + data.template().removeHeader(ACCEPT); // remove any previous produces |
| 95 | + data.template().header(ACCEPT, produces[0]); |
| 96 | + } |
| 97 | + |
| 98 | + private void handleConsumesAnnotation(MethodMetadata data, String... consumes) { |
| 99 | + if (consumes.length == 0) |
| 100 | + return; |
| 101 | + data.template().removeHeader(CONTENT_TYPE); // remove any previous consumes |
| 102 | + data.template().header(CONTENT_TYPE, consumes[0]); |
| 103 | + } |
| 104 | + |
| 105 | + protected Collection<String> addTemplatedParam(Collection<String> possiblyNull, String name) { |
| 106 | + if (possiblyNull == null) { |
| 107 | + possiblyNull = new ArrayList<String>(); |
| 108 | + } |
| 109 | + possiblyNull.add(String.format("{%s}", name)); |
| 110 | + return possiblyNull; |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments