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 java.util.Objects.nonNull;
20  
21  import java.util.List;
22  import java.util.stream.Collectors;
23  import org.bremersee.apiclient.webflux.Invocation;
24  import org.bremersee.apiclient.webflux.InvocationParameter;
25  import org.bremersee.apiclient.webflux.contract.RequestBodyInserter;
26  import org.springframework.web.bind.annotation.RequestBody;
27  
28  /**
29   * The abstract request body inserter.
30   *
31   * @author Christian Bremer
32   */
33  public abstract class AbstractRequestBodyInserter implements RequestBodyInserter {
34  
35    @Override
36    public boolean canInsert(Invocation invocation) {
37      return canInsert(findPossibleBodies(invocation));
38    }
39  
40    /**
41     * Can insert.
42     *
43     * @param possibleBodies the possible bodies
44     * @return the boolean
45     */
46    protected boolean canInsert(List<InvocationParameter> possibleBodies) {
47      return !possibleBodies.isEmpty();
48    }
49  
50    /**
51     * Find possible bodies list.
52     *
53     * @param invocation the invocation
54     * @return the list
55     */
56    protected List<InvocationParameter> findPossibleBodies(Invocation invocation) {
57      return invocation.toMethodParameterStream()
58          .filter(this::isPossibleBody)
59          .sorted(new RequestBodyComparator())
60          .collect(Collectors.toList());
61    }
62  
63    /**
64     * Is possible body boolean.
65     *
66     * @param invocationParameter the invocation parameter
67     * @return the boolean
68     */
69    protected boolean isPossibleBody(InvocationParameter invocationParameter) {
70      return nonNull(invocationParameter.getValue())
71          && isPossibleBodyValue(invocationParameter)
72          && hasMappingAnnotation(invocationParameter);
73    }
74  
75    /**
76     * Is possible body value boolean.
77     *
78     * @param invocationParameter the invocation parameter
79     * @return the boolean
80     */
81    protected abstract boolean isPossibleBodyValue(InvocationParameter invocationParameter);
82  
83    /**
84     * Has mapping annotation boolean.
85     *
86     * @param invocationParameter the invocation parameter
87     * @return the boolean
88     */
89    protected boolean hasMappingAnnotation(InvocationParameter invocationParameter) {
90      return invocationParameter.hasParameterAnnotation(RequestBody.class);
91    }
92  
93  }