View Javadoc
1   /**
2    * This file Copyright (c) 2020 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.cors;
35  
36  import info.magnolia.cms.filters.MgnlFilter;
37  import info.magnolia.cms.util.SimpleUrlPattern;
38  import info.magnolia.context.WebContext;
39  
40  import java.util.List;
41  import java.util.Optional;
42  
43  import javax.inject.Inject;
44  import javax.inject.Provider;
45  import javax.servlet.http.HttpServletRequest;
46  
47  /**
48   * CORS Filter implementation that may be configured to act on certain URIs.
49   */
50  public class SelfConfiguredCorsFilter extends AbstractCorsFilter implements CorsConfiguration {
51  
52      private final Provider<WebContext> webContextProvider;
53  
54      private final DefaultCorsConfigurationConfiguration">DefaultCorsConfiguration corsConfiguration = new DefaultCorsConfiguration();
55  
56      @Inject
57      public SelfConfiguredCorsFilter(final Provider<WebContext> webContextProvider) {
58          this.webContextProvider = webContextProvider;
59      }
60  
61      @Override
62      public boolean matches(final HttpServletRequest request) {
63          final String uri = webContextProvider.get().getAggregationState().getCurrentURI();
64          return super.matches(request) && corsConfiguration.getUris().stream().anyMatch(pattern -> pattern.match(uri));
65      }
66  
67      @Override
68      protected Optional<MgnlFilter> getCorsResponseFilter() {
69          return Optional.of(new CorsResponseFilter(corsConfiguration));
70      }
71  
72      @Override
73      public List<SimpleUrlPattern> getUris() {
74          return corsConfiguration.getUris();
75      }
76  
77      public void setUris(final List<SimpleUrlPattern> uris) {
78          corsConfiguration.setUris(uris);
79      }
80  
81      @Override
82      public List<String> getAllowedOrigins() {
83          return corsConfiguration.getAllowedOrigins();
84      }
85  
86      public void setAllowedOrigins(final List<String> allowedOrigins) {
87          corsConfiguration.setAllowedOrigins(allowedOrigins);
88      }
89  
90      @Override
91      public List<String> getAllowedMethods() {
92          return corsConfiguration.getAllowedMethods();
93      }
94  
95      public void setAllowedMethods(final List<String> allowedMethods) {
96          corsConfiguration.setAllowedMethods(allowedMethods);
97      }
98  
99      @Override
100     public List<String> getAllowedHeaders() {
101         return corsConfiguration.getAllowedHeaders();
102     }
103 
104     public void setAllowedHeaders(final List<String> allowedHeaders) {
105         corsConfiguration.setAllowedHeaders(allowedHeaders);
106     }
107 
108     @Override
109     public int getMaxAge() {
110         return corsConfiguration.getMaxAge();
111     }
112 
113     public void setMaxAge(final int maxAge) {
114         corsConfiguration.setMaxAge(maxAge);
115     }
116 
117     @Override
118     public boolean isSupportsCredentials() {
119         return corsConfiguration.isSupportsCredentials();
120     }
121 
122     public void setSupportsCredentials(final boolean supportsCredentials) {
123         corsConfiguration.setSupportsCredentials(supportsCredentials);
124     }
125 }