View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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.cms.security.auth.callback;
35  
36  import info.magnolia.cms.util.SimpleUrlPattern;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.voting.Voter;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  
43  /**
44   * A base class for {@link HttpClientCallback} implementations, providing a default set of configurable filters to
45   * accept the request.
46   *
47   * $Id$
48   */
49  public abstract class AbstractHttpClientCallback implements HttpClientCallback {
50  
51      // TODO use regexes ? Or both ?
52      private SimpleUrlPattern originalUrlPattern;
53      private SimpleUrlPattern urlPattern;
54      private SimpleUrlPattern hostPattern;
55      private Voter voters;
56  
57      @Override
58      public boolean accepts(HttpServletRequest request) {
59          boolean accepted = true;
60  
61          if (accepted && hostPattern != null) {
62              accepted = hostPattern.match(request.getServerName());
63          }
64  
65          if (accepted && originalUrlPattern != null) {
66              accepted = originalUrlPattern.match(MgnlContext.getAggregationState().getOriginalURI());
67          }
68  
69          if (accepted && urlPattern != null) {
70              accepted = urlPattern.match(MgnlContext.getAggregationState().getCurrentURI());
71          }
72  
73          if (accepted && voters != null) {
74              accepted = voters.vote(request) > 0;
75          }
76  
77          // TODO we could also accept based on site or whatever or other param
78          //      * add a simple interface similar to PatternDelegate, without the getDelegate() method ?
79          //      * on the other hand we already have Voters
80          //        ** propose a revision of Voters ?
81  
82          return accepted;
83      }
84  
85      /**
86       * Override this method to provide the specific functionality.
87       * This implementation is only kept for backwards compatibility with versions prior to 4.5, and merely delegates
88       * the call to {@link #doCallback(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}.
89       */
90      @Override
91      public void handle(HttpServletRequest request, HttpServletResponse response) {
92          this.doCallback(request, response);
93      }
94  
95      /**
96       * Since 4.5, one can simply implement {@link #handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}
97       * when subclassing AbstractHttpClientCallback.
98       * @deprecated since 4.5
99       */
100     protected void doCallback(HttpServletRequest request, HttpServletResponse response) {
101         throw new IllegalStateException("This method should not be called ! See Javadoc.");
102     }
103 
104     // ------- configuration methods
105 
106     protected SimpleUrlPattern getOriginalUrlPattern() {
107         return originalUrlPattern;
108     }
109 
110     public void setOriginalUrlPattern(SimpleUrlPattern originalUrlPattern) {
111         this.originalUrlPattern = originalUrlPattern;
112     }
113 
114     protected SimpleUrlPattern getUrlPattern() {
115         return urlPattern;
116     }
117 
118     public void setUrlPattern(SimpleUrlPattern urlPattern) {
119         this.urlPattern = urlPattern;
120     }
121 
122     protected SimpleUrlPattern getHostPattern() {
123         return hostPattern;
124     }
125 
126     public void setHostPattern(SimpleUrlPattern hostPattern) {
127         this.hostPattern = hostPattern;
128     }
129 
130     public Voter getVoters() {
131         return voters;
132     }
133 
134     public void setVoters(Voter voters) {
135         this.voters = voters;
136     }
137 }