1 23 24 package org.infoglue.cms.treeservice; 25 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import java.net.URLEncoder ; 29 import java.util.ArrayList ; 30 import java.util.Enumeration ; 31 import java.util.Hashtable ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 35 import javax.servlet.ServletConfig ; 36 import javax.servlet.ServletException ; 37 import javax.servlet.http.HttpServlet ; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 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 46 { 47 private final static Logger logger = Logger.getLogger(JServiceBuilder.class.getName()); 48 49 protected HttpServletRequest request; 50 51 public void init(ServletConfig config) throws ServletException 52 { 53 super.init(config); 54 } 55 56 public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException 57 { 58 this.request = request; 59 Hashtable inHash = requestToHashtable(request); 60 logger.info("Got request..."); 61 62 PrintWriter out = null; 63 64 try 65 { 66 67 68 out = response.getWriter(); 70 71 CommunicationEnvelope requestEnvelope = deserializeEnvelope(inHash); 72 CommunicationEnvelope reponseEnvelope = execute(requestEnvelope); 73 String responseString = toEncodedString(serializeEnvelope(reponseEnvelope)); 74 75 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 e) 84 { 85 e.printStackTrace(); 86 } 87 } 88 89 90 96 97 private String toEncodedString(Hashtable inHash) throws Exception 98 { 99 StringBuffer buffer = new StringBuffer (); 100 Enumeration names = inHash.keys(); 101 while(names.hasMoreElements()) 102 { 103 String name = names.nextElement().toString(); 104 String 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 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 nodes = new ArrayList (); 124 int i = 0; 125 String id = (String )hash.get("nodeList." + i + ".id"); 126 while(id != null) 127 { 128 Node n = new Node(); 129 n.setId(new Integer (id)); 130 n.setName((String )hash.get("nodeList." + i + ".name")); 131 n.setIsBranch(new Boolean ((String )hash.get("nodeList." + i + ".isBranch"))); 132 nodes.add(n); 133 logger.info("Node:" + n); 134 i++; 135 id = (String )hash.get("nodeList." + i + ".id"); 136 } 137 communicationEnvelope.setNodes(nodes); 138 139 return communicationEnvelope; 140 } 141 142 143 private Hashtable serializeEnvelope(CommunicationEnvelope requestEnvelope) 144 { 145 Hashtable hash = new Hashtable (); 146 logger.info("Serializing:" + requestEnvelope); 147 hash.put("action", requestEnvelope.getAction()); 148 hash.put("status", requestEnvelope.getStatus()); 149 150 List nodes = requestEnvelope.getNodes(); 151 int i = 0; 152 Iterator 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 requestToHashtable(HttpServletRequest req) 166 { 167 Hashtable input = new Hashtable (); 168 169 for (Enumeration e = req.getParameterNames(); e.hasMoreElements() ;) 170 { 171 String name = (String )e.nextElement(); 172 String value = (String )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 |