1 19 20 package net.sourceforge.jcetaglib.taglib.x509; 21 22 import javax.servlet.jsp.JspException ; 23 import javax.servlet.jsp.PageContext ; 24 import javax.servlet.jsp.tagext.BodyTagSupport ; 25 26 45 46 public class PEMtoDER extends BodyTagSupport { 47 private static final String PAGE = "page"; 48 private static final String REQUEST = "request"; 49 private static final String SESSION = "session"; 50 private static final String APPLICATION = "application"; 51 52 protected String pemstring; 53 54 private int scope = PageContext.PAGE_SCOPE; 55 private String derstring; 56 57 public static String replace(String text, String repl, String with) { 58 if (text == null) { 59 return null; 60 } 61 62 StringBuffer buf = new StringBuffer (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 scope) { 73 int ret = PageContext.PAGE_SCOPE; 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 } 87 public int doEndTag() throws JspException { 88 String input; 89 String output; 90 String beginKey = null; 91 String endKey = null; 92 93 if (pemstring != null) { 95 input = pemstring; 97 } else { 98 if (bodyContent == null || bodyContent.getString() == null) { 100 input = ""; 101 } else { 102 input = bodyContent.getString().trim(); 103 } 104 } 105 106 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 } 127 134 public void setScope(String scope) { 135 this.scope = getScope(scope); 136 } 137 138 145 public void setPemstring(String pemstring) { 146 this.pemstring = pemstring; 147 } 148 149 156 public void setDerstring(String derstring) { 157 this.derstring = derstring; 158 } 159 160 public String getDerstring() { 161 return derstring; 162 } 163 } | Popular Tags |