KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > clientstate > serialize > ClientStateTag


1 /* Copyright 2004 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at:
2  http://adventurebuilder.dev.java.net/LICENSE.txt
3  $Id: ClientStateTag.java,v 1.1 2005/05/17 05:12:35 gmurray71 Exp $ */

4
5 package com.sun.j2ee.blueprints.clientstate.serialize;
6
7 import java.io.*;
8 import java.util.*;
9
10 // Apache Commons- Tag-Lib Imports
11
import org.apache.commons.codec.base64.Base64;
12
13 // Import for encryption/decryption routines
14
import com.sun.j2ee.blueprints.clientstate.ByteArrayGuard;
15 import com.sun.j2ee.blueprints.clientstate.SessionPasswordStrategy;
16
17 // J2EE Imports
18
import javax.servlet.jsp.*;
19 import javax.servlet.jsp.tagext.*;
20 import javax.servlet.http.*;
21
22 /**
23  * This tag saves state in the page and provides a button or
24  * image link within a form with the current page parameters
25  * and the page request attributes by encoding them as hidden
26  * form variables that are serialized using Base 64 Encoded Strings.
27  */

28 public class ClientStateTag extends BodyTagSupport {
29         
30     private String JavaDoc beanName;
31     private String JavaDoc targetURL;
32     private boolean secure = false;
33     private Class JavaDoc serializableClass = null;
34          
35     public void setAction(String JavaDoc targetURL) {
36         this.targetURL = targetURL;
37     }
38     
39     public void setBeanName(String JavaDoc 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 JavaDoc buffer = new StringBuffer JavaDoc();
55         buffer.append("<form method=\"POST\" action=\"" + targetURL + "\">");
56         // insert any parameters that may have been added via sub tags
57

58         /**
59          * Serialize the request attribute scoped serialize object
60          * identified by beanName into the page
61         */

62         
63         Object JavaDoc value = request.getAttribute(beanName);
64         
65         // we need the serializable to do checks
66
if (serializableClass == null) {
67             try {
68                 serializableClass = Class.forName("java.io.Serializable");
69             } catch (java.lang.ClassNotFoundException JavaDoc cnf) {
70                 System.err.println("ClientStateTag caught: " + cnf);
71             }
72         }
73         // check if seralizable
74
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 JavaDoc(Base64.encode(data), "ISO-8859-1") + "\" />");
93                 } catch (java.io.IOException JavaDoc 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         // put in the body content
103
BodyContent bc = getBodyContent();
104         buffer.append(bc.getString());
105         buffer.append("</form>");
106         // write the output to the output stream
107
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         // reset everything
114
beanName = null;
115         targetURL = null;
116         return EVAL_PAGE;
117     }
118 }
119
Popular Tags