1 19 20 package net.sourceforge.jcetaglib.taglib.crypto; 21 22 import net.sourceforge.jcetaglib.lib.Clean; 23 import net.sourceforge.jcetaglib.lib.Digesters; 24 25 import javax.servlet.jsp.JspException ; 26 import javax.servlet.jsp.JspWriter ; 27 import javax.servlet.jsp.PageContext ; 28 import javax.servlet.jsp.tagext.BodyTagSupport ; 29 import java.io.IOException ; 30 31 51 public class Digest extends BodyTagSupport { 52 53 private static final String PAGE = "page"; 54 private static final String REQUEST = "request"; 55 private static final String SESSION = "session"; 56 private static final String APPLICATION = "application"; 57 58 private StringBuffer value; private String var; private int scope = PageContext.PAGE_SCOPE; 62 private String algorithm = "Tiger"; private String file; 65 private StringBuffer input; private StringBuffer output; 68 public static int getScope(String scope) { 69 int ret = PageContext.PAGE_SCOPE; 71 if (REQUEST.equalsIgnoreCase(scope)) 72 ret = PageContext.REQUEST_SCOPE; 73 else if (SESSION.equalsIgnoreCase(scope)) 74 ret = PageContext.SESSION_SCOPE; 75 else if (APPLICATION.equalsIgnoreCase(scope)) 76 ret = PageContext.APPLICATION_SCOPE; 77 else if (PAGE.equalsIgnoreCase(scope)) 78 ret = PageContext.PAGE_SCOPE; 79 80 return ret; 81 } 83 public int doEndTag() throws JspException { 84 if (value != null) { 86 input = value; 88 } else { 89 if (bodyContent == null || bodyContent.getString() == null) { 91 input = new StringBuffer (""); 92 } else { 93 input = new StringBuffer (bodyContent.getString().trim()); 94 } 95 } 96 97 try { 98 if (file != null) { 99 output = Digesters.hashFile(file, algorithm); 100 } else { 101 output = Digesters.hash(input, algorithm); 102 } 103 104 } catch (Exception e) { 105 throw new JspException ("JCE Exception: " + e.getMessage(), e); 106 } 107 108 if (var != null) { 110 if (output != null) { 111 pageContext.setAttribute(var, output, scope); 112 } 113 } else { 114 if (bodyContent != null) { 115 bodyContent.clearBody(); 116 } 117 118 try { 119 JspWriter w = pageContext.getOut(); 120 w.print(output); 121 } catch (IOException ex) { 122 throw new JspException (ex.getMessage(), ex); 123 } 124 } 125 126 return EVAL_PAGE; 127 } 129 public void release() { 130 Clean.blank(value); 132 Clean.blank(input); 133 Clean.blank(output); 134 135 super.release(); 136 } 138 145 public void setValue(StringBuffer value) { 146 this.value = value; 147 } 148 149 public StringBuffer getValue() { 150 return value; 151 } 152 153 160 public void setVar(String var) { 161 this.var = var; 162 } 163 164 public String getVar() { 165 return var; 166 } 167 168 175 public void setScope(String scope) { 176 this.scope = getScope(scope); 177 } 178 179 186 public void setAlgorithm(String algorithm) { 187 this.algorithm = algorithm; 188 } 189 190 197 public void setFile(String file) { 198 this.file = file; 199 } 200 } | Popular Tags |