View Javadoc
1   /*
2    * Copyright 2022 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.apiclient.webflux.contract.spring;
18  
19  import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
20  
21  import java.lang.reflect.Method;
22  import java.util.Optional;
23  import java.util.function.Function;
24  import org.bremersee.apiclient.webflux.Invocation;
25  import org.bremersee.apiclient.webflux.contract.HttpRequestMethod;
26  import org.springframework.util.Assert;
27  import org.springframework.web.bind.annotation.DeleteMapping;
28  import org.springframework.web.bind.annotation.GetMapping;
29  import org.springframework.web.bind.annotation.PatchMapping;
30  import org.springframework.web.bind.annotation.PostMapping;
31  import org.springframework.web.bind.annotation.PutMapping;
32  import org.springframework.web.bind.annotation.RequestMapping;
33  
34  /**
35   * The http method resolver.
36   *
37   * @author Christian Bremer
38   */
39  public class HttpMethodResolver implements Function<Invocation, HttpRequestMethod> {
40  
41    @Override
42    public HttpRequestMethod apply(Invocation invocation) {
43  
44      Assert.notNull(invocation, "Invocation must be present.");
45      Method method = invocation.getMethod();
46      return Optional.ofNullable(findAnnotation(method, RequestMapping.class))
47          .filter(requestMapping -> requestMapping.method().length > 0)
48          .flatMap(requestMapping -> HttpRequestMethod.resolve(requestMapping.method()[0].name()))
49          .or(() -> Optional.ofNullable(findAnnotation(method, GetMapping.class))
50              .map(a -> HttpRequestMethod.GET))
51          .or(() -> Optional.ofNullable(findAnnotation(method, PostMapping.class))
52              .map(a -> HttpRequestMethod.POST))
53          .or(() -> Optional.ofNullable(findAnnotation(method, PutMapping.class))
54              .map(a -> HttpRequestMethod.PUT))
55          .or(() -> Optional.ofNullable(findAnnotation(method, PatchMapping.class))
56              .map(a -> HttpRequestMethod.PATCH))
57          .or(() -> Optional.ofNullable(findAnnotation(method, DeleteMapping.class))
58              .map(a -> HttpRequestMethod.DELETE))
59          .orElseThrow(() -> new IllegalStateException(
60              String.format("Cannot find request method on method '%s'.", method.getName())));
61    }
62  }