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 java.net.URI;
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.function.BiConsumer;
23  import java.util.function.BiFunction;
24  import java.util.function.Function;
25  import org.bremersee.apiclient.webflux.Invocation;
26  import org.bremersee.apiclient.webflux.ReactiveContract;
27  import org.bremersee.apiclient.webflux.contract.CookiesConsumer;
28  import org.bremersee.apiclient.webflux.contract.HeadersConsumer;
29  import org.bremersee.apiclient.webflux.contract.RequestBodyInserterRegistry;
30  import org.bremersee.apiclient.webflux.contract.RequestUriFunction;
31  import org.bremersee.apiclient.webflux.contract.RequestUriSpecFunction;
32  import org.bremersee.apiclient.webflux.contract.ResponseFunction;
33  import org.reactivestreams.Publisher;
34  import org.springframework.http.HttpHeaders;
35  import org.springframework.util.MultiValueMap;
36  import org.springframework.web.reactive.function.client.WebClient;
37  import org.springframework.web.reactive.function.client.WebClient.RequestBodyUriSpec;
38  import org.springframework.web.reactive.function.client.WebClient.RequestHeadersUriSpec;
39  import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
40  import org.springframework.web.util.UriBuilder;
41  
42  /**
43   * The reactive spring contract.
44   *
45   * @author Christian Bremer
46   */
47  public class ReactiveSpringContract implements ReactiveContract {
48  
49    private final ContentTypeResolver contentTypeResolver = new ContentTypeResolver();
50  
51    @Override
52    public BiConsumer<Invocation, MultiValueMap<String, String>> getCookiesConsumer() {
53      return CookiesConsumer.builder()
54          .cookiesResolver(new CookiesResolver())
55          .build();
56    }
57  
58    @Override
59    public BiConsumer<Invocation, HttpHeaders> getHeadersConsumer() {
60      return HeadersConsumer.builder()
61          .contentTypeResolver(contentTypeResolver)
62          .acceptResolver(new AcceptResolver())
63          .headersResolver(new RequestHeadersResolver())
64          .build();
65    }
66  
67    @Override
68    public BiFunction<Invocation, UriBuilder, URI> getRequestUriFunction() {
69      return RequestUriFunction.builder()
70          .requestPathResolver(new RequestPathResolver())
71          .pathVariablesResolver(new PathVariablesResolver())
72          .requestParametersResolvers(getRequestParametersResolvers())
73          .build();
74    }
75  
76    /**
77     * Gets request parameters resolvers.
78     *
79     * @return the request parameters resolvers
80     */
81    protected List<Function<Invocation, MultiValueMap<String, Object>>>
82    getRequestParametersResolvers() {
83  
84      List<Function<Invocation, MultiValueMap<String, Object>>> list = new ArrayList<>();
85      list.add(new RequestParametersResolver());
86      if (Extensions.isSortPresent) {
87        list.add(new SortRequestParameterResolver());
88      }
89      if (Extensions.isPageablePresent) {
90        list.add(new PageableRequestParameterResolver());
91      }
92      return list;
93    }
94  
95    @Override
96    public BiFunction<Invocation, WebClient, RequestHeadersUriSpec<?>> getRequestUriSpecFunction() {
97      return RequestUriSpecFunction.builder()
98          .httpMethodResolver(new HttpMethodResolver())
99          .build();
100   }
101 
102   @Override
103   public BiFunction<Invocation, RequestBodyUriSpec, RequestHeadersUriSpec<?>>
104   getRequestBodyInserterFunction() {
105 
106     return RequestBodyInserterRegistry.builder()
107         .addRequestBodyInserters(new FormDataInserter()
108             .withContentTypeResolver(contentTypeResolver))
109         .addRequestBodyInserters(new MultipartDataInserter()
110             .withContentTypeResolver(contentTypeResolver))
111         .addRequestBodyInserters(new ResourceInserter())
112         .addRequestBodyInserters(new DataBuffersInserter())
113         .addRequestBodyInserters(new PublisherInserter())
114         .addRequestBodyInserters(new ValueInserter())
115         .build();
116   }
117 
118   @Override
119   public BiFunction<Invocation, ResponseSpec, Publisher<?>> getResponseFunction() {
120     return new ResponseFunction();
121   }
122 }