View Javadoc
1   /**
2    * This file Copyright (c) 2019 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.rest.client.value;
35  
36  import info.magnolia.rest.client.RestCallDefinition;
37  
38  import java.util.Map;
39  import java.util.stream.Collectors;
40  
41  /**
42   * Wrapper that ensures that templates are resolved for:
43   * <ul>
44   *     <li>path</li>
45   *     <li>query parameters</li>
46   *     <li>headers</li>
47   *     <li>cookies</li>
48   *     <li>body</li>
49   * </ul>
50   *
51   * Note that this wrapper does not modify underlying definition in any way.
52   */
53  public class ResolvedRestCallDefinition implements RestCallDefinition {
54  
55      private final RestCallDefinition definition;
56  
57      private final TemplateResolver resolver;
58  
59      public ResolvedRestCallDefinition(final RestCallDefinition definition, final TemplateResolver resolver) {
60          this.definition = definition;
61          this.resolver = resolver;
62      }
63  
64      @Override
65      public String getMethod() {
66          return definition.getMethod();
67      }
68  
69      @Override
70      public String getPath() {
71          return (String) resolver.resolveWithDefaults(definition.getPath(), getDefaultValues());
72      }
73  
74      @Override
75      public <T> Class<T> getEntityClass() {
76          return definition.getEntityClass();
77      }
78  
79      @Override
80      public Map<String, String> getQueryParameters() {
81          final Map<String, Object> result = (Map<String, Object>) resolver.resolveWithDefaults(definition.getQueryParameters(), getDefaultValues());
82          return result.entrySet()
83                  .stream()
84                  .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().toString()));
85      }
86  
87      @Override
88      public Map<String, String> getCookies() {
89          final Map<String, Object> result = (Map<String, Object>) resolver.resolveWithDefaults(definition.getCookies(), getDefaultValues());
90          return result.entrySet()
91                  .stream()
92                  .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().toString()));
93      }
94  
95      @Override
96      public Map<String, Object> getHeaders() {
97          return (Map<String, Object>) resolver.resolveWithDefaults(definition.getHeaders(), getDefaultValues());
98      }
99  
100     @Override
101     public Map<String, Object> getDefaultValues() {
102         return definition.getDefaultValues();
103     }
104 
105     @Override
106     public String getSecurityScheme() {
107         return definition.getSecurityScheme();
108     }
109 
110     @Override
111     public Object getBody() {
112         return resolver.resolveWithDefaults(definition.getBody(), getDefaultValues());
113     }
114 
115     @Override
116     public String getName() {
117         return definition.getName();
118     }
119 }