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