1 | /* Copyright 2006-2009 the original author or authors. |
---|
2 | * |
---|
3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | * you may not use this file except in compliance with the License. |
---|
5 | * You may obtain a copy of the License at |
---|
6 | * |
---|
7 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | * |
---|
9 | * Unless required by applicable law or agreed to in writing, software |
---|
10 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | * See the License for the specific language governing permissions and |
---|
13 | * limitations under the License. |
---|
14 | */ |
---|
15 | package org.codehaus.groovy.grails.plugins.springsecurity; |
---|
16 | |
---|
17 | import java.util.Map; |
---|
18 | |
---|
19 | import javax.servlet.http.HttpServletRequest; |
---|
20 | |
---|
21 | import org.springframework.security.ConfigAttributeDefinition; |
---|
22 | import org.springframework.security.intercept.web.FilterInvocation; |
---|
23 | import org.springframework.util.Assert; |
---|
24 | import org.springframework.util.StringUtils; |
---|
25 | |
---|
26 | /** |
---|
27 | * @author <a href='mailto:beckwithb@studentsonly.com'>Burt Beckwith</a> |
---|
28 | */ |
---|
29 | public class RequestmapFilterInvocationDefinition extends AbstractFilterInvocationDefinition { |
---|
30 | |
---|
31 | private boolean _initialized; |
---|
32 | |
---|
33 | private String _requestMapClass; |
---|
34 | private String _requestMapPathFieldName; |
---|
35 | private String _requestMapConfigAttributeField; |
---|
36 | private RequestmapFilterInvocationDefinitionHelper _helper; |
---|
37 | |
---|
38 | @Override |
---|
39 | protected String determineUrl(final FilterInvocation filterInvocation) { |
---|
40 | HttpServletRequest request = filterInvocation.getHttpRequest(); |
---|
41 | String requestUrl = request.getRequestURI().substring(request.getContextPath().length()); |
---|
42 | return lowercaseAndStringQuerystring(requestUrl); |
---|
43 | } |
---|
44 | |
---|
45 | @Override |
---|
46 | protected void initialize() { |
---|
47 | if (!_initialized) { |
---|
48 | reset(); |
---|
49 | _initialized = true; |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * Call at startup or when <code>Requestmap</code> instances have been added, removed, or changed. |
---|
55 | */ |
---|
56 | public synchronized void reset() { |
---|
57 | Map<String, String> data = _helper.loadRequestmaps(); |
---|
58 | _compiled.clear(); |
---|
59 | |
---|
60 | for (Map.Entry<String, String> entry : data.entrySet()) { |
---|
61 | String pattern = entry.getKey(); |
---|
62 | String[] tokens = StringUtils.commaDelimitedListToStringArray(entry.getValue()); |
---|
63 | storeMapping(pattern, tokens); |
---|
64 | } |
---|
65 | |
---|
66 | if (_log.isTraceEnabled()) { |
---|
67 | _log.trace("configs: " + _compiled); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | private void storeMapping(final String pattern, final String[] tokens) { |
---|
72 | |
---|
73 | ConfigAttributeDefinition configAttribute = new ConfigAttributeDefinition(tokens); |
---|
74 | |
---|
75 | Object key = getUrlMatcher().compile(pattern); |
---|
76 | |
---|
77 | ConfigAttributeDefinition replaced = _compiled.put(key, configAttribute); |
---|
78 | if (replaced != null) { |
---|
79 | _log.warn("replaced rule for '" + key + "' with roles " + replaced.getConfigAttributes() |
---|
80 | + " with roles " + configAttribute.getConfigAttributes()); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * Dependency injection for the Requestmap class name. |
---|
86 | * @param name the class name |
---|
87 | */ |
---|
88 | public void setRequestMapClass(final String name) { |
---|
89 | _requestMapClass = name; |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Dependency injection for the Requestmap config attribute (e.g. roles) field name. |
---|
94 | * @param name |
---|
95 | */ |
---|
96 | public void setRequestMapConfigAttributeField(final String name) { |
---|
97 | _requestMapConfigAttributeField = name; |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * Dependency injection for the Requestmap path field name. |
---|
102 | * @param name |
---|
103 | */ |
---|
104 | public void setRequestMapPathFieldName(final String name) { |
---|
105 | _requestMapPathFieldName = name; |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * {@inheritDoc} |
---|
110 | * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() |
---|
111 | */ |
---|
112 | @Override |
---|
113 | public void afterPropertiesSet() { |
---|
114 | super.afterPropertiesSet(); |
---|
115 | Assert.notNull(_requestMapClass, "Requestmap class name is required"); |
---|
116 | Assert.notNull(_requestMapPathFieldName, "Requestmap path field name is required"); |
---|
117 | Assert.notNull(_requestMapConfigAttributeField, "Requestmap config attribute field name is required"); |
---|
118 | |
---|
119 | _helper = new RequestmapFilterInvocationDefinitionHelper(_requestMapClass, |
---|
120 | _requestMapPathFieldName, _requestMapConfigAttributeField); |
---|
121 | } |
---|
122 | } |
---|