1 23 24 package org.infoglue.cms.plugins.opentree; 25 26 import java.io.BufferedInputStream ; 27 import java.io.BufferedReader ; 28 import java.io.InputStream ; 29 import java.io.InputStreamReader ; 30 import java.io.PrintWriter ; 31 import java.net.URL ; 32 import java.net.URLConnection ; 33 import java.net.URLDecoder ; 34 import java.net.URLEncoder ; 35 import java.util.ArrayList ; 36 import java.util.Enumeration ; 37 import java.util.Hashtable ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 import java.util.StringTokenizer ; 41 42 import org.infoglue.cms.net.CommunicationEnvelope; 43 import org.infoglue.cms.net.Node; 44 45 46 public class Communicator 47 { 48 private String serverAddress = null; 49 private String entityName = null; 50 private Integer repositoryId = null; 51 52 public Communicator(String serverAddress, String entityName, Integer repositoryId) 53 { 54 this.serverAddress = serverAddress; 55 this.entityName = entityName; 56 this.repositoryId = repositoryId; 57 } 58 59 public Node getRootNode() throws Exception 60 { 61 CommunicationEnvelope requestEnvelope = new CommunicationEnvelope(); 62 requestEnvelope.setAction("selectRootNode"); 63 64 List arguments = new ArrayList (); 65 Node n = new Node(); 66 n.setId(this.repositoryId); 67 arguments.add(n); 68 requestEnvelope.setNodes(arguments); 69 70 CommunicationEnvelope responseEnvelope = callService(requestEnvelope); 71 72 Node rootNode = null; 73 int status = Integer.parseInt(responseEnvelope.getStatus()); 74 75 77 if(status == 0) 78 { 79 List nodes = responseEnvelope.getNodes(); 80 rootNode = (Node)nodes.get(0); 82 } 84 85 return rootNode; 86 } 87 88 public Node getNode(Integer nodeId) 89 { 90 CommunicationEnvelope requestEnvelope = new CommunicationEnvelope(); 91 requestEnvelope.setAction("selectNode"); 92 93 List arguments = new ArrayList (); 94 Node n = new Node(); 95 n.setId(nodeId); 96 arguments.add(n); 97 requestEnvelope.setNodes(arguments); 98 99 CommunicationEnvelope responseEnvelope = callService(requestEnvelope); 100 101 Node node = null; 102 int status = Integer.parseInt(responseEnvelope.getStatus()); 103 104 System.out.println("status:" + status); 105 106 if(status == 0) 107 { 108 List nodes = responseEnvelope.getNodes(); 109 node = (Node)nodes.get(0); 111 } 113 114 return node; 115 } 116 117 118 public List getChildNodeList(Integer parentId) 119 { 120 List childContents = null; 121 CommunicationEnvelope requestEnvelope = new CommunicationEnvelope(); 122 requestEnvelope.setAction("selectChildNodes"); 123 124 List arguments = new ArrayList (); 125 Node n = new Node(); 126 n.setId(parentId); 127 arguments.add(n); 128 requestEnvelope.setNodes(arguments); 129 130 CommunicationEnvelope responseEnvelope = callService(requestEnvelope); 131 132 Node rootVO = null; 133 int status = Integer.parseInt(responseEnvelope.getStatus()); 134 135 137 if(status == 0) 138 { 139 childContents = responseEnvelope.getNodes(); 140 } 141 142 return childContents; 143 } 144 145 146 147 148 149 public CommunicationEnvelope callService(CommunicationEnvelope requestEnvelope) 150 { 151 PrintWriter outputToServlet = null; 152 BufferedInputStream inputFromServlet = null; 153 CommunicationEnvelope responseEnvelope = null; 154 155 try 156 { 157 String url = serverAddress; 158 159 try 160 { 161 Hashtable hash = serializeEnvelope(requestEnvelope); 162 String response = postToUrl(serverAddress, hash); 164 166 responseEnvelope = deserializeEnvelope(httpEncodedStringToHashtable(response)); 167 } 169 catch (Exception e) 170 { 171 e.printStackTrace(); 172 } 173 174 } 175 catch (Exception e) 176 { 177 System.out.println(e.toString()); 178 } 179 180 return responseEnvelope; 181 } 182 183 184 194 195 private String postToUrl(String urlAddress, Hashtable inHash) throws Exception 196 { 197 URL url = new URL (urlAddress); 198 URLConnection urlConn = url.openConnection(); 199 urlConn.setAllowUserInteraction(false); 200 urlConn.setDoOutput (true); 201 urlConn.setDoInput (true); 202 urlConn.setUseCaches (false); 203 urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 204 PrintWriter printout = new PrintWriter (urlConn.getOutputStream(), true); 205 String argString = ""; 206 if(inHash != null) 207 { 208 argString = toEncodedString(inHash); 209 } 210 printout.print(argString); 211 printout.flush(); 212 printout.close (); 213 InputStream inStream = null; 214 inStream = urlConn.getInputStream(); 215 InputStreamReader inStreamReader = new InputStreamReader (inStream); 216 BufferedReader buffer = new BufferedReader (inStreamReader); 217 StringBuffer strbuf = new StringBuffer (); 218 String line; 219 while((line = buffer.readLine()) != null) 220 { 221 strbuf.append(line); 222 } 223 String readData = strbuf.toString(); 224 buffer.close(); 225 return readData; 226 } 227 228 229 235 236 private String toEncodedString(Hashtable inHash) throws Exception 237 { 238 StringBuffer buffer = new StringBuffer (); 239 Enumeration names = inHash.keys(); 240 while(names.hasMoreElements()) 241 { 242 String name = names.nextElement().toString(); 243 String value = inHash.get(name).toString(); 244 buffer.append(URLEncoder.encode(name, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8")); 245 if(names.hasMoreElements()) 246 { 247 buffer.append("&"); 248 } 249 } 250 return buffer.toString(); 251 } 252 253 254 255 264 265 public Hashtable httpEncodedStringToHashtable(String encodedstrang) throws Exception 266 { 267 Hashtable anropin = new Hashtable (); 268 StringTokenizer andsplitter = new StringTokenizer (encodedstrang,"&"); 269 while (andsplitter.hasMoreTokens()) 270 { 271 String namevaluepair = andsplitter.nextToken(); 272 StringTokenizer equalsplitter = new StringTokenizer (namevaluepair,"="); 273 if (equalsplitter.countTokens() == 2) 274 { 275 String name = equalsplitter.nextToken(); 276 String value = equalsplitter.nextToken(); 277 anropin.put(URLDecoder.decode(URLDecoder.decode(name, "UTF-8"), "UTF-8"),URLDecoder.decode(URLDecoder.decode(value, "UTF-8"), "UTF-8")); 278 } 279 } 280 return anropin; 281 } 282 283 284 285 286 private Hashtable serializeEnvelope(CommunicationEnvelope requestEnvelope) 287 { 288 Hashtable hash = new Hashtable (); 289 System.out.println("Serializing:" + requestEnvelope); 290 hash.put("action", requestEnvelope.getAction()); 291 hash.put("status", requestEnvelope.getStatus()); 292 293 List nodes = requestEnvelope.getNodes(); 294 int i = 0; 295 Iterator iterator = nodes.iterator(); 296 while(iterator.hasNext()) 297 { 298 Node n = (Node)iterator.next(); 299 hash.put("nodeList." + i + ".id", "" + n.getId()); 300 hash.put("nodeList." + i + ".name", "" + n.getName()); 301 hash.put("nodeList." + i + ".isBranch", "" + n.getIsBranch()); 302 i++; 303 } 304 305 return hash; 306 } 307 308 309 private CommunicationEnvelope deserializeEnvelope(Hashtable hash) 310 { 311 CommunicationEnvelope communicationEnvelope = new CommunicationEnvelope(); 312 communicationEnvelope.setAction("" + hash.get("action")); 313 communicationEnvelope.setStatus("" + hash.get("status")); 314 317 List nodes = new ArrayList (); 318 int i = 0; 319 String id = (String )hash.get("nodeList." + i + ".id"); 320 while(id != null) 321 { 322 Node n = new Node(); 323 n.setId(new Integer (id)); 324 n.setName((String )hash.get("nodeList." + i + ".name")); 325 n.setIsBranch(new Boolean ((String )hash.get("nodeList." + i + ".isBranch"))); 326 nodes.add(n); 327 i++; 329 id = (String )hash.get("nodeList." + i + ".id"); 330 } 331 communicationEnvelope.setNodes(nodes); 332 333 return communicationEnvelope; 334 } 335 336 337 } | Popular Tags |