KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jcetaglib > taglib > x509 > PEMtoDER


1 /*
2   Name: PEMtoDER.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.x509;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.PageContext JavaDoc;
24 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
25
26 /**
27  * JSP tag for converting PEM strings to DER strings
28  *
29  * @jsp.tag
30  * name="pemtoder"
31  * display-name="PEMtoDER"
32  * body-content="JSP"
33  * example="<%-- Convert the PEM string to DER format--%>

34  * <jce:pemtoder

35  * 	pemstring=\"<%= pageContext.getAttribute(\"certificate\", PageContext.PAGE_SCOPE) %>\"

36  * 	derstring=\"derformattedstring\"/>"
37  *
38  * description="JSP tag for converting PEM strings to DER strings"
39  *
40  * @author Gert Van Ham
41  * @author hamgert@users.sourceforge.net
42  * @author http://jcetaglib.sourceforge.net
43  * @version $Id: PEMtoDER.java,v 1.4 2004/04/15 07:28:36 hamgert Exp $
44  */

45
46 public class PEMtoDER extends BodyTagSupport JavaDoc {
47     private static final String JavaDoc PAGE = "page";
48     private static final String JavaDoc REQUEST = "request";
49     private static final String JavaDoc SESSION = "session";
50     private static final String JavaDoc APPLICATION = "application";
51
52     protected String JavaDoc pemstring;
53
54     private int scope = PageContext.PAGE_SCOPE;
55     private String JavaDoc derstring;
56
57     public static String JavaDoc replace(String JavaDoc text, String JavaDoc repl, String JavaDoc with) {
58         if (text == null) {
59             return null;
60         }
61
62         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(text.length());
63         int start = 0, end = 0;
64         while ((end = text.indexOf(repl, start)) != -1) {
65             buf.append(text.substring(start, end)).append(with);
66             start = end + repl.length();
67         }
68         buf.append(text.substring(start));
69         return buf.toString();
70     }
71
72     public static int getScope(String JavaDoc scope) {
73         int ret = PageContext.PAGE_SCOPE; // default
74

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

87     public int doEndTag() throws JspException JavaDoc {
88         String JavaDoc input;
89         String JavaDoc output;
90         String JavaDoc beginKey = null;
91         String JavaDoc endKey = null;
92
93         // determine the value by...
94
if (pemstring != null) {
95             // ... reading our attribute
96
input = pemstring;
97         } else {
98             // ... retrieving and trimming our body
99
if (bodyContent == null || bodyContent.getString() == null) {
100                 input = "";
101             } else {
102                 input = bodyContent.getString().trim();
103             }
104         }
105
106         // Check for boundaries
107
if (input.indexOf("-----BEGIN CERTIFICATE-----") != -1) {
108             beginKey = "-----BEGIN CERTIFICATE-----";
109             endKey = "-----END CERTIFICATE-----";
110         } else if (input.indexOf("-----BEGIN CERTIFICATE REQUEST-----") != -1) {
111             beginKey = "-----BEGIN CERTIFICATE REQUEST-----";
112             endKey = "-----END CERTIFICATE REQUEST-----";
113         } else if (input.indexOf("-----BEGIN PRIVATE KEY-----") != -1) {
114             beginKey = "-----BEGIN PRIVATE KEY-----";
115             endKey = "-----END PRIVATE KEY-----";
116         }
117
118         output = replace(input, "\n", "");
119         output = replace(output, beginKey, "");
120         output = replace(output, endKey, "");
121
122         pageContext.setAttribute(derstring, output, scope);
123
124         return EVAL_PAGE;
125     } //doEndTag()
126

127     /**
128      * @jsp.attribute
129      * description="Scope of the return variables. Can be 'page', 'session', 'request' or 'application'. Default is 'page'"
130      * type="java.lang.String"
131      * required="false"
132      * rtexprvalue="false"
133      */

134     public void setScope(String JavaDoc scope) {
135         this.scope = getScope(scope);
136     }
137
138     /**
139      * @jsp.attribute
140      * description="Optional attribute, the PEM string. The body of the tag will be taken if omitted"
141      * type="java.lang.String"
142      * required="false"
143      * rtexprvalue="true"
144      */

145     public void setPemstring(String JavaDoc pemstring) {
146         this.pemstring = pemstring;
147     }
148
149     /**
150      * @jsp.attribute
151      * description="Return variable to store the DER string"
152      * type="java.lang.String"
153      * required="true"
154      * rtexprvalue="true"
155      */

156     public void setDerstring(String JavaDoc derstring) {
157         this.derstring = derstring;
158     }
159
160     public String JavaDoc getDerstring() {
161         return derstring;
162     }
163 }
Popular Tags