KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > treeservice > JServiceBuilder


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.treeservice;
25
26 import java.io.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.net.URLEncoder JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.Hashtable JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34
35 import javax.servlet.ServletConfig JavaDoc;
36 import javax.servlet.ServletException JavaDoc;
37 import javax.servlet.http.HttpServlet JavaDoc;
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40
41 import org.apache.log4j.Logger;
42 import org.infoglue.cms.net.CommunicationEnvelope;
43 import org.infoglue.cms.net.Node;
44
45 public abstract class JServiceBuilder extends HttpServlet JavaDoc
46 {
47     private final static Logger logger = Logger.getLogger(JServiceBuilder.class.getName());
48
49     protected HttpServletRequest JavaDoc request;
50      
51     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc
52     {
53         super.init(config);
54     }
55
56     public void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc
57     {
58         this.request = request;
59         Hashtable JavaDoc inHash = requestToHashtable(request);
60         logger.info("Got request...");
61         
62         PrintWriter JavaDoc out = null;
63         
64         try
65         {
66             
67             
68             // get an input stream from the applet
69
out = response.getWriter();
70             
71             CommunicationEnvelope requestEnvelope = deserializeEnvelope(inHash);
72             CommunicationEnvelope reponseEnvelope = execute(requestEnvelope);
73             String JavaDoc responseString = toEncodedString(serializeEnvelope(reponseEnvelope));
74                         
75             // send back a confirmation message to the applet
76
logger.info("Sending the string to the applet:" + responseString);
77             out.println(responseString);
78                 
79             out.flush();
80             out.close();
81             logger.info("Complete.");
82         }
83         catch (Exception JavaDoc e)
84         {
85             e.printStackTrace();
86         }
87     }
88
89
90     /**
91      * Encodes a hash table to an URL encoded string.
92      *
93      * @param inHash The hash table you would like to encode
94      * @return A URL encoded string.
95      */

96         
97     private String JavaDoc toEncodedString(Hashtable JavaDoc inHash) throws Exception JavaDoc
98     {
99         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
100         Enumeration JavaDoc names = inHash.keys();
101         while(names.hasMoreElements())
102         {
103             String JavaDoc name = names.nextElement().toString();
104             String JavaDoc value = inHash.get(name).toString();
105             buffer.append(URLEncoder.encode(name, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8"));
106             if(names.hasMoreElements())
107             {
108                 buffer.append("&");
109             }
110         }
111         return buffer.toString();
112     }
113     
114     
115     private CommunicationEnvelope deserializeEnvelope(Hashtable JavaDoc hash)
116     {
117         CommunicationEnvelope communicationEnvelope = new CommunicationEnvelope();
118         communicationEnvelope.setAction("" + hash.get("action"));
119         communicationEnvelope.setStatus("" + hash.get("status"));
120         logger.info("Action:" + communicationEnvelope.getAction());
121         logger.info("Status:" + communicationEnvelope.getStatus());
122         
123         List JavaDoc nodes = new ArrayList JavaDoc();
124         int i = 0;
125         String JavaDoc id = (String JavaDoc)hash.get("nodeList." + i + ".id");
126         while(id != null)
127         {
128             Node n = new Node();
129             n.setId(new Integer JavaDoc(id));
130             n.setName((String JavaDoc)hash.get("nodeList." + i + ".name"));
131             n.setIsBranch(new Boolean JavaDoc((String JavaDoc)hash.get("nodeList." + i + ".isBranch")));
132             nodes.add(n);
133             logger.info("Node:" + n);
134             i++;
135             id = (String JavaDoc)hash.get("nodeList." + i + ".id");
136         }
137         communicationEnvelope.setNodes(nodes);
138                 
139         return communicationEnvelope;
140     }
141
142
143     private Hashtable JavaDoc serializeEnvelope(CommunicationEnvelope requestEnvelope)
144     {
145         Hashtable JavaDoc hash = new Hashtable JavaDoc();
146         logger.info("Serializing:" + requestEnvelope);
147         hash.put("action", requestEnvelope.getAction());
148         hash.put("status", requestEnvelope.getStatus());
149         
150         List JavaDoc nodes = requestEnvelope.getNodes();
151         int i = 0;
152         Iterator JavaDoc iterator = nodes.iterator();
153         while(iterator.hasNext())
154         {
155             Node n = (Node)iterator.next();
156             hash.put("nodeList." + i + ".id", "" + n.getId());
157             hash.put("nodeList." + i + ".name", "" + n.getName());
158             hash.put("nodeList." + i + ".isBranch", "" + n.getIsBranch());
159             i++;
160         }
161                 
162         return hash;
163     }
164
165     public Hashtable JavaDoc requestToHashtable(HttpServletRequest JavaDoc req)
166     {
167         Hashtable JavaDoc input = new Hashtable JavaDoc();
168                 
169         for (Enumeration JavaDoc e = req.getParameterNames(); e.hasMoreElements() ;)
170         {
171             String JavaDoc name = (String JavaDoc)e.nextElement();
172             String JavaDoc value = (String JavaDoc)req.getParameter(name);
173             input.put(name, value);
174         }
175         
176         return input;
177         
178     }
179
180
181     public abstract CommunicationEnvelope execute(CommunicationEnvelope envelope);
182 }
Popular Tags