KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > components > Token


1 package com.opensymphony.webwork.components;
2
3 import com.opensymphony.webwork.util.TokenHelper;
4 import com.opensymphony.xwork.util.OgnlValueStack;
5
6 import javax.servlet.http.HttpServletRequest JavaDoc;
7 import javax.servlet.http.HttpServletResponse JavaDoc;
8 import java.util.Map JavaDoc;
9
10 /**
11  * User: plightbo
12  * Date: Jul 20, 2005
13  * Time: 7:02:04 AM
14  */

15 public class Token extends UIBean {
16     final public static String JavaDoc TEMPLATE = "token";
17
18     public Token(OgnlValueStack stack, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
19         super(stack, request, response);
20     }
21
22     protected String JavaDoc getDefaultTemplate() {
23         return TEMPLATE;
24     }
25
26     /**
27      * First looks for the token in the PageContext using the supplied name (or {@link com.opensymphony.webwork.util.TokenHelper#DEFAULT_TOKEN_NAME}
28      * if no name is provided) so that the same token can be re-used for the scope of a request for the same name. If
29      * the token is not in the PageContext, a new Token is created and set into the Session and the PageContext with
30      * the name.
31      */

32     protected void evaluateExtraParams() {
33         super.evaluateExtraParams();
34
35         String JavaDoc tokenName;
36         Map JavaDoc parameters = getParameters();
37
38         if (parameters.containsKey("name")) {
39             tokenName = (String JavaDoc) parameters.get("name");
40         } else {
41             if (name == null) {
42                 tokenName = TokenHelper.DEFAULT_TOKEN_NAME;
43             } else {
44                 tokenName = findString(name);
45
46                 if (tokenName == null) {
47                     tokenName = name;
48                 }
49             }
50
51             addParameter("name", tokenName);
52         }
53
54         String JavaDoc token = buildToken(tokenName);
55         addParameter("token", token);
56         addParameter("tokenNameField", TokenHelper.TOKEN_NAME_FIELD);
57     }
58
59     /**
60      * @deprecated Templates should use $parameters from now on, not $tag.
61      * This will be removed in a future version of WebWork.
62      */

63     public String JavaDoc getTokenNameField() {
64         return TokenHelper.TOKEN_NAME_FIELD;
65     }
66
67     private String JavaDoc buildToken(String JavaDoc name) {
68         Map JavaDoc context = stack.getContext();
69         Object JavaDoc myToken = context.get(name);
70
71         if (myToken == null) {
72             myToken = TokenHelper.setToken(name, request);
73             context.put(name, myToken);
74         }
75
76         return myToken.toString();
77     }
78 }
79
Popular Tags