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.util.Arrays;
20  import java.util.function.Function;
21  import org.bremersee.apiclient.webflux.Invocation;
22  import org.springframework.http.MediaType;
23  import org.springframework.web.bind.annotation.DeleteMapping;
24  import org.springframework.web.bind.annotation.GetMapping;
25  import org.springframework.web.bind.annotation.PatchMapping;
26  import org.springframework.web.bind.annotation.PostMapping;
27  import org.springframework.web.bind.annotation.PutMapping;
28  import org.springframework.web.bind.annotation.RequestMapping;
29  
30  /**
31   * The accept resolver.
32   *
33   * @author Christian Bremer
34   */
35  public class AcceptResolver implements Function<Invocation, MediaType> {
36  
37    @Override
38    public MediaType apply(Invocation invocation) {
39      return Arrays.stream(
40              invocation.findAnnotationValueOnMethod(
41                      RequestMapping.class, a -> a.produces().length > 0, RequestMapping::produces)
42                  .or(() -> invocation.findAnnotationValueOnMethod(
43                      GetMapping.class, a -> a.produces().length > 0, GetMapping::produces))
44                  .or(() -> invocation.findAnnotationValueOnMethod(
45                      PostMapping.class, a -> a.produces().length > 0, PostMapping::produces))
46                  .or(() -> invocation.findAnnotationValueOnMethod(
47                      PutMapping.class, a -> a.produces().length > 0, PutMapping::produces))
48                  .or(() -> invocation.findAnnotationValueOnMethod(
49                      PatchMapping.class, a -> a.produces().length > 0, PatchMapping::produces))
50                  .or(() -> invocation.findAnnotationValueOnMethod(
51                      DeleteMapping.class, a -> a.produces().length > 0, DeleteMapping::produces))
52                  .orElse(new String[0]))
53          .map(this::parseMediaType)
54          .findFirst()
55          .orElse(MediaType.ALL);
56    }
57  
58    /**
59     * Parse media type.
60     *
61     * @param mediaType the media type
62     * @return the media type (can be {@code null}
63     */
64    protected MediaType parseMediaType(String mediaType) {
65      try {
66        return MediaType.parseMediaType(mediaType);
67      } catch (RuntimeException ignored) {
68        return null;
69      }
70    }
71  }