Source Code Cross Referenced for RolesInterceptor.java in  » Web-Framework » struts-2.0.11 » org » apache » struts2 » interceptor » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » struts 2.0.11 » org.apache.struts2.interceptor 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: RolesInterceptor.java 478625 2006-11-23 17:31:52Z wsmoak $
003:         *
004:         * Licensed to the Apache Software Foundation (ASF) under one
005:         * or more contributor license agreements.  See the NOTICE file
006:         * distributed with this work for additional information
007:         * regarding copyright ownership.  The ASF licenses this file
008:         * to you under the Apache License, Version 2.0 (the
009:         * "License"); you may not use this file except in compliance
010:         * with the License.  You may obtain a copy of the License at
011:         *
012:         *  http://www.apache.org/licenses/LICENSE-2.0
013:         *
014:         * Unless required by applicable law or agreed to in writing,
015:         * software distributed under the License is distributed on an
016:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017:         * KIND, either express or implied.  See the License for the
018:         * specific language governing permissions and limitations
019:         * under the License.
020:         */
021:        package org.apache.struts2.interceptor;
022:
023:        import java.util.ArrayList;
024:        import java.util.Arrays;
025:        import java.util.Collections;
026:        import java.util.List;
027:
028:        import javax.servlet.http.HttpServletRequest;
029:        import javax.servlet.http.HttpServletResponse;
030:
031:        import com.opensymphony.xwork2.ActionInvocation;
032:        import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
033:
034:        import org.apache.struts2.ServletActionContext;
035:
036:        /**
037:         * <!-- START SNIPPET: description --> This interceptor ensures that the action
038:         * will only be executed if the user has the correct role. <!--
039:         * END SNIPPET: description -->
040:         *
041:         * <p/> <u>Interceptor parameters:</u>
042:         *
043:         * <!-- START SNIPPET: parameters -->
044:         *
045:         * <ul>
046:         *
047:         * <li>allowedRoles - a comma-separated list of roles to allow</li>
048:         *
049:         * <li>disallowedRoles - a comma-separated list of roles to disallow</li>
050:         *
051:         * </ul>
052:         *
053:         * <!-- END SNIPPET: parameters -->
054:         *
055:         * <!-- START SNIPPET: extending --> There are two extensions to the
056:         * existing interceptor:
057:         * <ul>
058:         *   <li>isAllowed(HttpServletRequest,Object) - whether or not to allow
059:         *       the passed action execution with this request</li>
060:         *   <li>handleRejection(ActionInvocation) - handles an unauthorized
061:         *       request.</li>
062:         * </ul>
063:         * <!-- END SNIPPET: extending -->
064:         *
065:         * <pre>
066:         *  &lt;!-- START SNIPPET: example --&gt;
067:         *  &lt;!-- only allows the admin and member roles --&gt;
068:         *  &lt;action name=&quot;someAction&quot; class=&quot;com.examples.SomeAction&quot;&gt;
069:         *      &lt;interceptor-ref name=&quot;completeStack&quot;/&gt;
070:         *      &lt;interceptor-ref name=&quot;roles&quot;&gt;
071:         *        &lt;param name=&quot;allowedRoles&quot;&gt;admin,member&lt;/param&gt;
072:         *      &lt;/interceptor-ref&gt;
073:         *      &lt;result name=&quot;success&quot;&gt;good_result.ftl&lt;/result&gt;
074:         *  &lt;/action&gt;
075:         *  &lt;!-- END SNIPPET: example --&gt;
076:         * </pre>
077:         */
078:        public class RolesInterceptor extends AbstractInterceptor {
079:
080:            private List<String> allowedRoles = new ArrayList<String>();
081:            private List<String> disallowedRoles = new ArrayList<String>();
082:
083:            public void setAllowedRoles(String roles) {
084:                this .allowedRoles = stringToList(roles);
085:            }
086:
087:            public void setDisallowedRoles(String roles) {
088:                this .disallowedRoles = stringToList(roles);
089:            }
090:
091:            public String intercept(ActionInvocation invocation)
092:                    throws Exception {
093:                HttpServletRequest request = ServletActionContext.getRequest();
094:                HttpServletResponse response = ServletActionContext
095:                        .getResponse();
096:                String result = null;
097:                if (!isAllowed(request, invocation.getAction())) {
098:                    result = handleRejection(invocation, response);
099:                } else {
100:                    result = invocation.invoke();
101:                }
102:                return result;
103:            }
104:
105:            /**
106:             * Splits a string into a List
107:             */
108:            protected List<String> stringToList(String val) {
109:                if (val != null) {
110:                    String[] list = val.split("[ ]*,[ ]*");
111:                    return Arrays.asList(list);
112:                } else {
113:                    return Collections.EMPTY_LIST;
114:                }
115:            }
116:
117:            /**
118:             * Determines if the request should be allowed for the action
119:             *
120:             * @param request The request
121:             * @param action The action object
122:             * @return True if allowed, false otherwise
123:             */
124:            protected boolean isAllowed(HttpServletRequest request,
125:                    Object action) {
126:                if (allowedRoles.size() > 0) {
127:                    boolean result = false;
128:                    for (String role : allowedRoles) {
129:                        if (request.isUserInRole(role)) {
130:                            result = true;
131:                        }
132:                    }
133:                    return result;
134:                } else if (disallowedRoles.size() > 0) {
135:                    for (String role : disallowedRoles) {
136:                        if (request.isUserInRole(role)) {
137:                            return false;
138:                        }
139:                    }
140:                }
141:                return true;
142:            }
143:
144:            /**
145:             * Handles a rejection by sending a 403 HTTP error
146:             *
147:             * @param invocation The invocation
148:             * @return The result code
149:             * @throws Exception
150:             */
151:            protected String handleRejection(ActionInvocation invocation,
152:                    HttpServletResponse response) throws Exception {
153:                response.sendError(HttpServletResponse.SC_FORBIDDEN);
154:                return null;
155:            }
156:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.