View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    *     http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package org.gbif.ws.client;
15  
16  import org.gbif.api.annotation.PartialDate;
17  
18  import java.lang.annotation.Annotation;
19  import java.lang.reflect.Method;
20  import java.util.Collection;
21  
22  import org.springframework.cloud.openfeign.AnnotatedParameterProcessor;
23  
24  import feign.MethodMetadata;
25  import feign.Util;
26  
27  /**
28   * Process method arguments annotated with annotation {@link PartialDate}.
29   * Should be used instead of {@link org.springframework.web.bind.annotation.RequestParam}.
30   * Only for date parameters!
31   */
32  public class PartialDateParameterProcessor implements AnnotatedParameterProcessor {
33  
34    private static final Class<PartialDate> ANNOTATION = PartialDate.class;
35  
36    private PartialDateExpandereExpander">PartialDateExpander partialDateExpander = new PartialDateExpander();
37  
38    @Override
39    public Class<? extends Annotation> getAnnotationType() {
40      return ANNOTATION;
41    }
42  
43    @Override
44    public boolean processArgument(
45        AnnotatedParameterContext context, Annotation annotation, Method method) {
46      int parameterIndex = context.getParameterIndex();
47      MethodMetadata data = context.getMethodMetadata();
48      PartialDate requestParam = ANNOTATION.cast(annotation);
49      String name = requestParam.value();
50  
51      Util.checkState(Util.emptyToNull(name) != null, "PartialDate.value() was null");
52  
53      context.setParameterName(name);
54  
55      data.indexToExpander().put(parameterIndex, partialDateExpander);
56      Collection<String> query =
57          context.setTemplateParameter(name, data.template().queries().get(name));
58      data.template().query(name, query);
59  
60      return true;
61    }
62  }