1 4 5 package com.sun.j2ee.blueprints.clientstate.serialize; 6 7 import java.io.*; 8 import java.util.*; 9 10 import org.apache.commons.codec.base64.Base64; 12 13 import com.sun.j2ee.blueprints.clientstate.ByteArrayGuard; 15 import com.sun.j2ee.blueprints.clientstate.SessionPasswordStrategy; 16 17 import javax.servlet.jsp.*; 19 import javax.servlet.jsp.tagext.*; 20 import javax.servlet.http.*; 21 22 28 public class ClientStateTag extends BodyTagSupport { 29 30 private String beanName; 31 private String targetURL; 32 private boolean secure = false; 33 private Class serializableClass = null; 34 35 public void setAction(String targetURL) { 36 this.targetURL = targetURL; 37 } 38 39 public void setBeanName(String beanName) { 40 this.beanName = beanName; 41 } 42 43 public void setSecure(boolean secure) { 44 this.secure = secure; 45 } 46 47 public int doStartTag() throws JspTagException { 48 return EVAL_BODY_BUFFERED; 49 } 50 51 public int doEndTag() throws JspTagException { 52 HttpServletRequest request = 53 (HttpServletRequest) pageContext.getRequest(); 54 StringBuffer buffer = new StringBuffer (); 55 buffer.append("<form method=\"POST\" action=\"" + targetURL + "\">"); 56 58 62 63 Object value = request.getAttribute(beanName); 64 65 if (serializableClass == null) { 67 try { 68 serializableClass = Class.forName("java.io.Serializable"); 69 } catch (java.lang.ClassNotFoundException cnf) { 70 System.err.println("ClientStateTag caught: " + cnf); 71 } 72 } 73 if ((serializableClass != null) && (value != null)) { 75 if (serializableClass.isAssignableFrom(value.getClass())) { 76 try { 77 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 78 ObjectOutput out = new ObjectOutputStream(bos); 79 out.writeObject(value); 80 out.close(); 81 byte[] plaindata = bos.toByteArray(); 82 byte[] data = null; 83 if (secure) { 84 ByteArrayGuard bag = new ByteArrayGuard(new SessionPasswordStrategy(request.getSession())); 85 bag.encrypt(plaindata); 86 buffer.append(" <input type=\"hidden\" name=\"secure\" value=\"true\" />"); 87 } else { 88 data = plaindata; 89 } 90 buffer.append(" <input type=\"hidden\" name=\"beanName\" value=\"" + beanName + "\" />"); 91 buffer.append(" <input type=\"hidden\" name=\"" + beanName + "\" value=\"" + 92 new String (Base64.encode(data), "ISO-8859-1") + "\" />"); 93 } catch (java.io.IOException iox) { 94 System.err.println("ClientStateTag caught IOException: " + iox); 95 } 96 } else { 97 System.out.println(beanName + " is not Serializeable."); 98 } 99 } else if (value == null) { 100 System.out.println(beanName + " does not exist."); 101 } 102 BodyContent bc = getBodyContent(); 104 buffer.append(bc.getString()); 105 buffer.append("</form>"); 106 try { 108 JspWriter out = pageContext.getOut(); 109 out.print(buffer.toString()); 110 } catch (IOException ioe) { 111 System.err.println("ClientStateTag: Problems with writing..."); 112 } 113 beanName = null; 115 targetURL = null; 116 return EVAL_PAGE; 117 } 118 } 119 | Popular Tags |