Source Code Cross Referenced for TokenHelper.java in  » J2EE » webwork-2.2.6 » com » opensymphony » webwork » util » 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 » J2EE » webwork 2.2.6 » com.opensymphony.webwork.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2006 by OpenSymphony
003:         * All rights reserved.
004:         */
005:        package com.opensymphony.webwork.util;
006:
007:        import com.opensymphony.util.GUID;
008:        import com.opensymphony.xwork.ActionContext;
009:        import com.opensymphony.xwork.util.LocalizedTextUtil;
010:        import org.apache.commons.logging.Log;
011:        import org.apache.commons.logging.LogFactory;
012:
013:        import java.util.Map;
014:
015:        /**
016:         * TokenHelper
017:         *
018:         * @author Jason Carreira
019:         * @author Rainer Hermanns
020:         * @author Nils-Helge Garli
021:         * @author Claus Ibson
022:         */
023:        public class TokenHelper {
024:
025:            /**
026:             * The default name to map the token value
027:             */
028:            public static final String DEFAULT_TOKEN_NAME = "webwork.token";
029:
030:            /**
031:             * The name of the field which will hold the token name
032:             */
033:            public static final String TOKEN_NAME_FIELD = "webwork.token.name";
034:            private static final Log LOG = LogFactory.getLog(TokenHelper.class);
035:
036:            /**
037:             * Sets a transaction token into the session using the default token name.
038:             *
039:             * @return the token string
040:             */
041:            public static String setToken() {
042:                return setToken(DEFAULT_TOKEN_NAME);
043:            }
044:
045:            /**
046:             * Sets a transaction token into the session using the provided token name.
047:             *
048:             * @param tokenName the name to store into the session with the token as the value
049:             * @return the token string
050:             */
051:            public static String setToken(String tokenName) {
052:                Map session = ActionContext.getContext().getSession();
053:                String token = GUID.generateGUID();
054:                try {
055:                    session.put(tokenName, token);
056:                } catch (IllegalStateException e) {
057:                    // WW-1182 explain to user what the problem is
058:                    String msg = "Error creating HttpSession due response is commited to client. You can use the CreateSessionInterceptor or create the HttpSession from your action before the result is rendered to the client: "
059:                            + e.getMessage();
060:                    LOG.error(msg, e);
061:                    throw new IllegalArgumentException(msg);
062:                }
063:
064:                return token;
065:            }
066:
067:            /**
068:             * Gets a transaction token into the session using the default token name.
069:             * 
070:             * @return token
071:             */
072:            public static String getToken() {
073:                return getToken(DEFAULT_TOKEN_NAME);
074:            }
075:
076:            /**
077:             * Gets the Token value from the params in the ServletActionContext using the given name
078:             *
079:             * @param tokenName the name of the parameter which holds the token value
080:             * @return the token String or null, if the token could not be found
081:             */
082:            public static String getToken(String tokenName) {
083:                Map params = ActionContext.getContext().getParameters();
084:                String[] tokens = (String[]) params.get(tokenName);
085:                String token;
086:
087:                if ((tokens == null) || (tokens.length < 1)) {
088:                    LOG.warn("Could not find token mapped to token name "
089:                            + tokenName);
090:
091:                    return null;
092:                }
093:
094:                token = tokens[0];
095:
096:                return token;
097:            }
098:
099:            /**
100:             * Gets the token name from the Parameters in the ServletActionContext
101:             *
102:             * @return the token name found in the params, or null if it could not be found
103:             */
104:            public static String getTokenName() {
105:                Map params = ActionContext.getContext().getParameters();
106:
107:                if (!params.containsKey(TOKEN_NAME_FIELD)) {
108:                    LOG.warn("Could not find token name in params.");
109:
110:                    return null;
111:                }
112:
113:                String[] tokenNames = (String[]) params.get(TOKEN_NAME_FIELD);
114:                String tokenName;
115:
116:                if ((tokenNames == null) || (tokenNames.length < 1)) {
117:                    LOG.warn("Got a null or empty token name.");
118:
119:                    return null;
120:                }
121:
122:                tokenName = tokenNames[0];
123:
124:                return tokenName;
125:            }
126:
127:            /**
128:             * Checks for a valid transaction token in the current request params. If a valid token is found, it is
129:             * removed so the it is not valid again.
130:             *
131:             * @return false if there was no token set into the params (check by looking for {@link #TOKEN_NAME_FIELD}), true if a valid token is found
132:             */
133:            public static boolean validToken() {
134:                String tokenName = getTokenName();
135:
136:                if (tokenName == null) {
137:                    if (LOG.isDebugEnabled())
138:                        LOG.debug("no token name found -> Invalid token ");
139:                    return false;
140:                }
141:
142:                String token = getToken(tokenName);
143:
144:                if (token == null) {
145:                    if (LOG.isDebugEnabled())
146:                        LOG.debug("no token found for token name " + tokenName
147:                                + " -> Invalid token ");
148:                    return false;
149:                }
150:
151:                Map session = ActionContext.getContext().getSession();
152:                String sessionToken = (String) session.get(tokenName);
153:
154:                if (!token.equals(sessionToken)) {
155:                    LOG
156:                            .warn(LocalizedTextUtil
157:                                    .findText(
158:                                            TokenHelper.class,
159:                                            "webwork.internal.invalid.token",
160:                                            ActionContext.getContext()
161:                                                    .getLocale(),
162:                                            "Form token {0} does not match the session token {1}.",
163:                                            new Object[] { token, sessionToken }));
164:
165:                    return false;
166:                }
167:
168:                // remove the token so it won't be used again
169:                session.remove(tokenName);
170:
171:                return true;
172:            }
173:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.