01: /*
02: * Copyright 2007 Gerd Ziegler (www.gerdziegler.de)
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: * http://www.apache.org/licenses/LICENSE-2.0
07: * Unless required by applicable law or agreed to in writing,
08: * software distributed under the License is distributed on an
09: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
10: * either express or implied. See the License for the specific
11: * language governing permissions and limitations under the License.
12: * 20.11.2007
13: * @author www.gerdziegler.de
14: */
15: package org.ztemplates.web.impl;
16:
17: import java.util.Map;
18: import java.util.Set;
19:
20: import javax.servlet.ServletContext;
21:
22: import org.ztemplates.actions.ZISecurityProvider;
23: import org.ztemplates.actions.urlhandler.ZIUrlHandler;
24: import org.ztemplates.actions.urlhandler.tree.ZUrlHandlerFactory;
25: import org.ztemplates.classpath.ZIClassRepository;
26: import org.ztemplates.web.ZIActionService;
27: import org.ztemplates.web.ZIServiceRepository;
28: import org.ztemplates.web.ZTemplates;
29:
30: public class ZActionServiceImpl implements ZIActionService {
31: private ZIUrlHandler urlHandler;
32:
33: public void init(ZIServiceRepository serviceRepository)
34: throws Exception {
35: String key = ZIUrlHandler.class.getName();
36: ServletContext servletContext = serviceRepository
37: .getServletContext();
38: urlHandler = (ZIUrlHandler) servletContext.getAttribute(key);
39: if (urlHandler == null) {
40: String contextPath = serviceRepository.getRequest()
41: .getContextPath();
42: String prefix = servletContext.getInitParameter("prefix");
43: if (prefix != null) {
44: prefix = contextPath + prefix;
45: } else {
46: prefix = contextPath;
47: }
48: ZIClassRepository classRepository = serviceRepository
49: .getClasspathService().getClassRepository();
50:
51: // because this is cached, get the request-time service
52: ZISecurityProvider securityProvider = new ZISecurityProvider() {
53:
54: public String addSecurityToUrl(String url,
55: Set<String> roles) {
56: return ZTemplates.getSecurityService()
57: .getSecurityProvider().addSecurityToUrl(
58: url, roles);
59: }
60:
61: public String getUserName() {
62: return ZTemplates.getSecurityService()
63: .getSecurityProvider().getUserName();
64: }
65:
66: public boolean isUserInRole(String role) {
67: return ZTemplates.getSecurityService()
68: .getSecurityProvider().isUserInRole(role);
69: }
70:
71: public String removeSecurityFromUrl(String url) {
72: return ZTemplates.getSecurityService()
73: .getSecurityProvider()
74: .removeSecurityFromUrl(url);
75: }
76: };
77:
78: urlHandler = ZUrlHandlerFactory.create(classRepository,
79: securityProvider, prefix);
80: servletContext.setAttribute(key, urlHandler);
81: }
82: }
83:
84: public void printInfo(StringBuffer sb) {
85: urlHandler.printInfo(sb);
86: }
87:
88: public Object process(String url, Map<String, String[]> paramMap)
89: throws Exception {
90: return urlHandler.process(url, paramMap);
91: }
92:
93: public String createUrl(Object action) throws Exception {
94: return urlHandler.createUrl(action);
95: }
96: }
|