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


001:        /*
002:         * Copyright (c) 2002-2007 by OpenSymphony
003:         * All rights reserved.
004:         */
005:        package com.opensymphony.webwork.interceptor;
006:
007:        import java.util.Collections;
008:        import java.util.LinkedHashMap;
009:        import java.util.Map;
010:        import java.util.Set;
011:
012:        import javax.servlet.http.Cookie;
013:        import javax.servlet.http.HttpServletRequest;
014:
015:        import org.apache.commons.logging.Log;
016:        import org.apache.commons.logging.LogFactory;
017:
018:        import com.opensymphony.webwork.ServletActionContext;
019:        import com.opensymphony.xwork.ActionContext;
020:        import com.opensymphony.xwork.ActionInvocation;
021:        import com.opensymphony.xwork.interceptor.AroundInterceptor;
022:        import com.opensymphony.xwork.util.OgnlValueStack;
023:        import com.opensymphony.xwork.util.TextParseUtil;
024:
025:        /**
026:         * <!-- START SNIPPET: description -->
027:         * 
028:         * The aim of this interceptor is to set values in the stack/action based on cookie name/value 
029:         * of interest. <p/>
030:         * 
031:         * If an asterik is present in cookiesName parameter, it will be assume that 
032:         * all cookies name are to be injected into webwork's action, even though 
033:         * cookiesName is comma-separated by other values, eg (cookie1,*,cookie2). <p/>
034:         * 
035:         * If cookiesName is left empty it will assume that no cookie will be injected 
036:         * into WebWork's action. <p/>
037:         * 
038:         * If an asterik is present in cookiesValue parameter, it will assume that all 
039:         * cookies name irrespective of its value will be injected into WebWork's action so
040:         * long as the cookie name matches those specified in cookiesName parameter.<p/>
041:         * 
042:         * If cookiesValue is left empty it will assume that all cookie that match the cookieName
043:         * parameter will be injected into WebWork's action.<p/>
044:         * 
045:         * The action could implements {@link CookiesAware} in order to have a {@link Map}
046:         * of filtered cookies set into it. <p/>
047:         * 
048:         * <!-- END SNIPPET: description -->
049:         * 
050:         * 
051:         * <!-- START SNIPPET: parameters -->
052:         * 
053:         * <ul>
054:         * 	<li>cookiesName (mandatory) - Name of cookies to be injected into the action. If more
055:         * 								   than one cookie name is desired it could be comma-separated.
056:         * 								   If all cookies name is desired, it could simply be *, an asterik.
057:         * 								   When many cookies name are comma-separated either of the cookie
058:         * 						          that match the name in the comma-separated list will be qualified.</li>
059:         *     <li>cookiesValue (mandatory) - Value of cookies that if its name matches cookieName attribute
060:         *                                                         and its value matched this, will be injected into WebWork's 
061:         *                                                         action. If more than one cookie name is desired it could be 
062:         *                                                         comma-separated. If left empty, it will assume any value would
063:         *                                                         be ok. If more than one value is specified (comma-separated) 
064:         *                                                         it will assume a match if either value is matched.
065:         * </ul>
066:         * 
067:         * <!-- END SNIPPET: parameters -->
068:         * 
069:         * 
070:         * <!-- START SNIPPET: extending -->
071:         * 
072:         * <ul>
073:         * 	populateCookieValueIntoStack - this method will decide if this cookie value is qualified to be 
074:         * 													   populated into the value stack (hence into the action itself)
075:         * 	injectIntoCookiesAwareAction - this method will inject selected cookies (as a java.util.Map) into 
076:         * 													  action that implements {@link CookiesAware}.
077:         * </ul>
078:         * 
079:         * <!-- END SNIPPET: extending -->
080:         * 
081:         * <pre>
082:         * <!-- START SNIPPET: example -->
083:         * 
084:         * &lt;!--
085:         *   This example will inject cookies named either 'cookie1' or 'cookie2' whose
086:         *   value could be either 'cookie1value' or 'cookie2value' into WebWork's action.
087:         * --&gt;
088:         * &lt;action ... &gt;
089:         *    &lt;interceptor-ref name="cookie"&gt;
090:         *        &lt;param name="cookiesName"&gt;cookie1, cookie2&lt;/param&gt;
091:         *        &lt;param name="cookiesValue"&gt;cookie1value, cookie2value&lt;/param&gt;
092:         *    &lt;/interceptor-ref&gt;
093:         *    ....
094:         * &lt;/action&gt;
095:         * 
096:         * 
097:         * &lt;!--
098:         * 	This example will inject cookies named either 'cookie1' or 'cookie2' 
099:         *     regardless of their value into WebWork's action.
100:         * --&gt;
101:         * &lt;action ... &gt;
102:         *   &lt;interceptor-ref name="cookie"&gt;
103:         *      &lt;param name="cookiesName"&gt;cookie1, cookie2&lt;/param&gt;
104:         *      &lt;param name="cookiesValue"&gt;*&lt;/param&gt;
105:         *   &lt;interceptor-ref&gt;
106:         *   ...
107:         * &lt;/action&gt;
108:         * 
109:         * 
110:         * &lt;!--
111:         * 	This example will inject cookies named either 'cookie1' with value 
112:         *      'cookie1value' or 'cookie2' with value 'cookie2value' into WebWork's
113:         *      action.
114:         * --&gt;
115:         * &lt;action ... &gt;
116:         *   &lt;interceptor-ref name="cookie"&gt;
117:         *      &lt;param name="cookiesName"&gt;cookie1&lt;/param&gt;
118:         *      &lt;param name="cookiesValue"&gt;cookie1value&lt;/param&gt;
119:         *   &lt;/interceptor-ref&gt;
120:         *   &lt;interceptor-ref name="cookie"&gt;
121:         *   	&lt;param name="cookiesName"&lt;cookie2&lt;/param&gt;
122:         *     &lt;param name="cookiesValue"&gt;cookie2value&lt;/param&gt;
123:         *   &lt;/interceptor-ref&gt;
124:         *   ....
125:         * &lt;/action&gt;
126:         * 
127:         * &lt;!--
128:         *    This example will inject any cookies regardless of its value into 
129:         *    WebWork's action.
130:         *  --&gt;
131:         * &lt;action ... &gt;
132:         *   &lt;interceptor-ref name="cookie"&gt;
133:         *      &lt;param name="cookiesName"&gt;*&lt;/param&gt;
134:         *      &lt;param name="cookiesValue"&gt;*&lt;/param&gt;
135:         *   &lt;/interceptor-ref&gt;
136:         *    ...
137:         * &lt;/action&gt;
138:         * 
139:         * <!-- END SNIPPET: example -->
140:         * </pre>
141:         * 
142:         * @see CookiesAware
143:         *
144:         * @author Matthew Payne
145:         * @author tmjee
146:         * @version $Date: 2007-01-09 19:08:00 +0100 (Tue, 09 Jan 2007) $ $Id: CookieInterceptor.java 2800 2007-01-09 18:08:00Z tmjee $
147:         */
148:        public class CookieInterceptor extends AroundInterceptor {
149:
150:            private static final long serialVersionUID = 4153142432948747305L;
151:
152:            private static final Log LOG = LogFactory
153:                    .getLog(CookieInterceptor.class);
154:
155:            private Set cookiesNameSet = Collections.EMPTY_SET;
156:            private Set cookiesValueSet = Collections.EMPTY_SET;
157:
158:            /**
159:             * Set the <code>cookiesName</code> which if matche will allow the cookie 
160:             * to be injected into action, could be comma-separated string.
161:             * 
162:             * @param cookiesName
163:             */
164:            public void setCookiesName(String cookiesName) {
165:                if (cookiesName != null)
166:                    this .cookiesNameSet = TextParseUtil
167:                            .commaDelimitedStringToSet(cookiesName);
168:            }
169:
170:            /**
171:             * Set the <code>cookiesValue</code> which if matched (together with matching 
172:             * cookiesName) will caused the cookie to be injected into action, could be 
173:             * comma-separated string.
174:             * 
175:             * @param cookiesValue
176:             */
177:            public void setCookiesValue(String cookiesValue) {
178:                if (cookiesValue != null)
179:                    this .cookiesValueSet = TextParseUtil
180:                            .commaDelimitedStringToSet(cookiesValue);
181:            }
182:
183:            /**
184:             * No operation.
185:             */
186:            public void destroy() {
187:            }
188:
189:            /**
190:             * No operation.
191:             */
192:            public void init() {
193:            }
194:
195:            /**
196:             * Inject cookies into action if they match the <code>cookiesName</code>
197:             * and <code>cookiesValue</code> configured.
198:             * 
199:             * @param invocation
200:             */
201:            protected void before(ActionInvocation invocation) throws Exception {
202:
203:                if (LOG.isDebugEnabled())
204:                    LOG.debug("start interception");
205:
206:                final OgnlValueStack stack = ActionContext.getContext()
207:                        .getValueStack();
208:                HttpServletRequest request = ServletActionContext.getRequest();
209:
210:                // contains selected cookies
211:                Map cookiesMap = new LinkedHashMap();
212:
213:                Cookie cookies[] = request.getCookies();
214:                for (int a = 0; a < cookies.length; a++) {
215:                    String name = cookies[a].getName();
216:                    String value = cookies[a].getValue();
217:
218:                    if (cookiesNameSet.contains("*")) {
219:                        if (LOG.isDebugEnabled())
220:                            LOG
221:                                    .debug("contains cookie name [*] in configured cookies name set, cookie with name ["
222:                                            + name
223:                                            + "] with value ["
224:                                            + value
225:                                            + "] will be injected");
226:                        populateCookieValueIntoStack(name, value, cookiesMap,
227:                                stack);
228:                    } else if (cookiesNameSet.contains(cookies[a].getName())) {
229:                        populateCookieValueIntoStack(name, value, cookiesMap,
230:                                stack);
231:                    }
232:                }
233:
234:                injectIntoCookiesAwareAction(invocation.getAction(), cookiesMap);
235:            }
236:
237:            /**
238:             * No operation.
239:             */
240:            protected void after(ActionInvocation invocation, String result)
241:                    throws Exception {
242:                if (LOG.isDebugEnabled())
243:                    LOG.debug("after interception");
244:            }
245:
246:            /**
247:             * Hook that populate cookie value into ognl value stack (hence the action) 
248:             * if the criteria is satisfied (if the cookie value matches with those configured).
249:             * 
250:             * @param cookieName
251:             * @param cookieValue
252:             * @param cookiesMap
253:             * @param stack
254:             */
255:            protected void populateCookieValueIntoStack(String cookieName,
256:                    String cookieValue, Map cookiesMap, OgnlValueStack stack) {
257:                if (cookiesValueSet.isEmpty() || cookiesValueSet.contains("*")) {
258:                    // If the interceptor is configured to accept any cookie value
259:                    // OR
260:                    // no cookiesValue is defined, so as long as the cookie name match
261:                    // we'll inject it into WebWork's action
262:                    if (LOG.isDebugEnabled()) {
263:                        if (cookiesValueSet.isEmpty())
264:                            LOG
265:                                    .debug("no cookie value is configured, cookie with name ["
266:                                            + cookieName
267:                                            + "] with value ["
268:                                            + cookieValue
269:                                            + "] will be injected");
270:                        else if (cookiesValueSet.contains("*"))
271:                            LOG
272:                                    .debug("interceptor is configured to accept any value, cookie with name ["
273:                                            + cookieName
274:                                            + "] with value ["
275:                                            + cookieValue
276:                                            + "] will be injected");
277:                    }
278:                    cookiesMap.put(cookieName, cookieValue);
279:                    stack.setValue(cookieName, cookieValue);
280:                } else {
281:                    // if cookiesValues is specified, the cookie's value must match before we
282:                    // inject them into WebWork's action
283:                    if (cookiesValueSet.contains(cookieValue)) {
284:                        if (LOG.isDebugEnabled())
285:                            LOG
286:                                    .debug("both configured cookie name and value matched, cookie ["
287:                                            + cookieName
288:                                            + "] with value ["
289:                                            + cookieValue
290:                                            + "] will be injected");
291:                        cookiesMap.put(cookieName, cookieValue);
292:                        stack.setValue(cookieName, cookieValue);
293:                    }
294:                }
295:            }
296:
297:            /**
298:             * Hook that set the <code>cookiesMap</code> into action that implements
299:             * {@link CookiesAware}.
300:             * 
301:             * @param action
302:             * @param cookiesMap
303:             */
304:            protected void injectIntoCookiesAwareAction(Object action,
305:                    Map cookiesMap) {
306:                if (action instanceof  CookiesAware) {
307:                    if (LOG.isDebugEnabled())
308:                        LOG
309:                                .debug("action ["
310:                                        + action
311:                                        + "] implements CookiesAware, injecting cookies map ["
312:                                        + cookiesMap + "]");
313:                    ((CookiesAware) action).setCookiesMap(cookiesMap);
314:                }
315:            }
316:
317:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.