KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jcetaglib > taglib > crypto > CreateFormDigest


1 /*
2   Name: CreateFormDigest.java
3   Licensing: LGPL
4
5   API: Sun (http://java.sun.com) JCE 1.2.2 API (cleanroom implementation by Bouncy Castle)
6   Provider: Bouncy Castle (http://www.bouncycastle.org)
7
8   Disclaimer:
9
10   COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND,
11   EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE
12   IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE
13   RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE
14   PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR)
15   ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY
16   CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED
17   HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
18 */

19
20 package net.sourceforge.jcetaglib.taglib.crypto;
21
22 import net.sourceforge.jcetaglib.lib.Clean;
23 import net.sourceforge.jcetaglib.lib.Digesters;
24 import net.sourceforge.jcetaglib.tools.safeText;
25
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.JspWriter JavaDoc;
28 import javax.servlet.jsp.PageContext JavaDoc;
29 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 /**
33  * JSP tag creating a form digest
34  *
35  * @jsp.tag
36  * name="createformdigest"
37  * display-name="CreateFormDigest"
38  * body-content="JSP"
39  * example="
40  * <%-- Creates and prints a form digest out of param1 and param2 using SHA-512 --%>

41  * <jce:createformdigest

42  * 	value=\"<%= new StringBuffer(\"param1=value&param2=value\") %>\"

43  * 	digest=\"SHA-512\"

44  * 	keyfile=\"c:/jspkey.key\"

45  * 	algorithm=\"AES\"

46  * 	passphrase=\"<%= new StringBuffer(\"mypassword\") %>\"/>"
47  *
48  * description="JSP tag creating a form digest"
49  *
50  * @author Gert Van Ham
51  * @author hamgert@users.sourceforge.net
52  * @author http://jcetaglib.sourceforge.net
53  * @version $Id: CreateFormDigest.java,v 1.5 2004/04/15 07:28:35 hamgert Exp $
54  */

55 public class CreateFormDigest extends BodyTagSupport JavaDoc {
56     private static final String JavaDoc PAGE = "page";
57     private static final String JavaDoc REQUEST = "request";
58     private static final String JavaDoc SESSION = "session";
59     private static final String JavaDoc APPLICATION = "application";
60
61     private StringBuffer JavaDoc value; // tag attribute
62
private String JavaDoc var; // tag attribute
63
private int scope = PageContext.PAGE_SCOPE; // tag attribute
64

65     private String JavaDoc digest = "MD5"; // tag attribute
66

67     private String JavaDoc algorithm = "AES"; // tag attribute
68
private String JavaDoc keyfile; // tag attribute
69
private StringBuffer JavaDoc passphrase; // tag attribute
70

71     private StringBuffer JavaDoc input; // what we'll store in scope:var
72
private StringBuffer JavaDoc output; // return text
73

74     public static int getScope(String JavaDoc scope) {
75         int ret = PageContext.PAGE_SCOPE; // default
76

77         if (REQUEST.equalsIgnoreCase(scope))
78             ret = PageContext.REQUEST_SCOPE;
79         else if (SESSION.equalsIgnoreCase(scope))
80             ret = PageContext.SESSION_SCOPE;
81         else if (APPLICATION.equalsIgnoreCase(scope))
82             ret = PageContext.APPLICATION_SCOPE;
83         else if (PAGE.equalsIgnoreCase(scope))
84             ret = PageContext.PAGE_SCOPE;
85
86         return ret;
87     } //getScope()
88

89     public int doEndTag() throws JspException JavaDoc {
90         // determine the value by...
91
if (value != null) {
92             // ... reading our attribute
93
input = value;
94         } else {
95             // ... retrieving and trimming our body
96
if (bodyContent == null || bodyContent.getString() == null) {
97                 input = new StringBuffer JavaDoc("");
98             } else {
99                 input = new StringBuffer JavaDoc(bodyContent.getString().trim());
100             }
101         }
102
103         try {
104             safeText t = new safeText();
105
106             output = Digesters.formDigest(input, digest, keyfile, passphrase, algorithm);
107         } catch (Exception JavaDoc e) {
108             throw new JspException JavaDoc("JCE Exception: " + e.getMessage(), e);
109         }
110
111         // decide what to do with the result
112
if (var != null && var != "") {
113             if (output != null) {
114                 pageContext.setAttribute(var, output, scope);
115             }
116         } else {
117             if (bodyContent != null) {
118                 bodyContent.clearBody();
119             }
120
121             try {
122                 JspWriter JavaDoc w = pageContext.getOut();
123                 w.print(output);
124             } catch (IOException JavaDoc ex) {
125                 throw new JspException JavaDoc(ex.getMessage(), ex);
126             }
127         }
128
129         return EVAL_PAGE;
130     } // doEndTag()
131

132     public void release() {
133         // Cleanup all sensitive information
134
Clean.blank(value);
135         Clean.blank(passphrase);
136         Clean.blank(input);
137         Clean.blank(output);
138
139         super.release();
140     } //release()
141

142
143     /**
144      * @jsp.attribute
145      * description="Optional attribute, the form string to create a digest from. The body of the tag will be taken if omitted."
146      * type="java.lang.StringBuffer"
147      * required="false"
148      * rtexprvalue="true"
149      */

150     public void setValue(StringBuffer JavaDoc value) {
151         this.value = value;
152     }
153
154
155     public StringBuffer JavaDoc getValue() {
156         return value;
157     }
158
159     /**
160      * @jsp.attribute
161      * description="Optional attribute, variable to store the form digest. The digest will be printed if omitted"
162      * type="java.lang.String"
163      * required="false"
164      * rtexprvalue="false"
165      */

166     public void setVar(String JavaDoc var) {
167         this.var = var;
168     }
169
170     public String JavaDoc getVar() {
171         return var;
172     }
173
174     /**
175      * @jsp.attribute
176      * description="Scope of the 'var' attribute. Can be 'page', 'session', 'request' or 'application'. Default is 'page'"
177      * type="java.lang.String"
178      * required="false"
179      * rtexprvalue="false"
180      */

181     public void setScope(String JavaDoc scope) {
182         this.scope = getScope(scope);
183     }
184
185     /**
186      * @jsp.attribute
187      * description="The form digest algorithm (default is MD5) "
188      * type="java.lang.String"
189      * required="false"
190      * rtexprvalue="true"
191      */

192     public void setDigest(String JavaDoc digest) {
193         this.digest = digest;
194     }
195
196     /**
197      * @jsp.attribute
198      * description="The key algorithm. Default is AES"
199      * type="java.lang.String"
200      * required="false"
201      * rtexprvalue="true"
202      */

203     public void setAlgorithm(String JavaDoc algorithm) {
204         this.algorithm = algorithm;
205     }
206
207     /**
208      * @jsp.attribute
209      * description="The symmetric key file(name)"
210      * type="java.lang.String"
211      * required="true"
212      * rtexprvalue="true"
213      */

214     public void setKeyfile(String JavaDoc keyfile) {
215         this.keyfile = keyfile;
216     }
217
218     /**
219      * @jsp.attribute
220      * description="Keystore passphrase"
221      * type="java.lang.StringBuffer"
222      * required="true"
223      * rtexprvalue="true"
224      */

225     public void setPassphrase(StringBuffer JavaDoc passphrase) {
226         this.passphrase = passphrase;
227     }
228 }
Popular Tags