1 5 package org.enhydra.jawe.wfxml; 6 7 import java.io.*; 8 import java.io.ByteArrayOutputStream ; 9 import java.io.FileReader ; 10 import java.io.PrintWriter ; 11 import java.net.HttpURLConnection ; 12 import java.net.URL ; 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import javax.xml.parsers.DocumentBuilder ; 17 import javax.xml.parsers.DocumentBuilderFactory ; 18 import javax.xml.transform.Transformer ; 19 import javax.xml.transform.TransformerFactory ; 20 import javax.xml.transform.dom.DOMSource ; 21 import javax.xml.transform.stream.StreamResult ; 22 import org.enhydra.jawe.JaWE; 23 import org.enhydra.jawe.JaWEConfig; 24 import org.enhydra.jawe.actions.Save; 25 import org.w3c.dom.Document ; 26 import org.w3c.dom.Node ; 27 import org.w3c.dom.NodeList ; 28 29 32 public class WfXMLConnector { 33 34 private static final String REQUEST_METHOD = "POST"; 35 36 private static final String HRP_ACCEPT_NAME = "Accept"; 37 38 private static final String HTTP_ACCEPT_VALUE = "application/soap+xml, text/*"; 39 40 private static final String HRP_USER_AGENT_NAME = "User-Agent"; 41 42 private static final String HRP_CACHE_CONTROL_NAME = "Cache-Control"; 43 44 private static final String HRP_PRAGMA_NAME = "Pragma"; 45 46 private static final String HRP_CONTENT_TYPE_NAME = "Content-Type"; 47 48 private static final String HRP_SOAP_ACTION = "SOAPAction"; 49 50 private static final String HRP_USER_AGENT_VALUE = "JaWE/1.4.2"; 51 52 private static final String HRP_CACHE_CONTROL_VALUE = "no-cache"; 53 54 private static final String HRP_PRAGMA_VALUE = "no-cache"; 55 56 private static final String HRP_CONTENT_TYPE_VALUE = "text/xml; charset=utf-8"; 57 58 private static final String REQUEST_HEADER = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"" 59 + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" 60 + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" 61 + "<soapenv:Header>" 62 + "<as:Request xmlns:as=\"http://www.oasis-open.org/asap/0.9/asap.xsd\">" 63 + "<as:SenderKey>http://jawe.objectweb.org/</as:SenderKey>" 64 + "<as:ReceiverKey>" 65 + "${1}" 66 + "</as:ReceiverKey>" 67 + "</as:Request>" 68 + "</soapenv:Header>"; 69 70 public static void main(String [] args) { 71 try { 72 URL url = new URL ("http://grunf:8080/axis/services/wfxmlRegistryBinding"); 73 wfxmlSetDefinition(url, "/tmp/asap_test.xpdl"); 74 80 } catch (Throwable e) { 83 e.printStackTrace(); 84 } 85 } 86 87 public static boolean wfxmlSetDefinition(URL url, String fullFilename) throws Exception { 88 HttpURLConnection conn = (HttpURLConnection ) url.openConnection(); 89 90 PrintWriter bos = openConnection(conn, 91 "http://www.wfmc.org/wfxml/2.0/wfxml/factory/SetDefinition"); 92 FileReader fr = new FileReader (fullFilename); 93 bos.println("<soapenv:Body>" 94 + "<SetDefinitionRq xmlns=\"http://www.wfmc.org/wfxml/2.0/\">" 95 + "<ProcessLanguage>XPDL</ProcessLanguage>" 96 + "<Definition>"); 97 BufferedReader br = new BufferedReader(fr); 98 br.readLine(); 99 while (br.ready()) { 100 bos.println(br.readLine()); 101 } 102 bos.println("</Definition>" 103 + "</SetDefinitionRq>" + "</soapenv:Body>" 104 + "</soapenv:Envelope>"); 105 bos.flush(); 106 br.close(); 107 fr.close(); 108 conn.connect(); 109 int response = conn.getResponseCode(); 110 System.out.println("rc="+response); 111 bos.close(); 112 conn.disconnect(); 113 114 return isResponse2XX(response); 115 } 116 117 public static void wfxmlSetDefinition2 (URL url, String pkgCnt) throws Exception { 118 HttpURLConnection conn = (HttpURLConnection ) url.openConnection(); 119 120 PrintWriter bos = openConnection(conn, 121 "http://www.wfmc.org/wfxml/2.0/wfxml/factory/SetDefinition"); 122 bos.println("<soapenv:Body>" 123 + "<SetDefinitionRq xmlns=\"http://www.wfmc.org/wfxml/2.0/\">" 124 + "<ProcessLanguage>XPDL</ProcessLanguage>" 125 + "<Definition>"); 126 bos.println(pkgCnt); 127 bos.println("</Definition>" 128 + "</SetDefinitionRq>" + "</soapenv:Body>" 129 + "</soapenv:Envelope>"); 130 bos.flush(); 131 conn.connect(); 132 int response = conn.getResponseCode(); 133 bos.close(); 134 conn.disconnect(); 135 136 if (!isResponse2XX(response)) { 137 throw new Exception ("Invalid response"); 138 } 139 } 140 141 public static boolean wfxmlNewDefinition(URL url, String fullFilename) throws Exception { 142 HttpURLConnection conn = (HttpURLConnection ) url.openConnection(); 143 144 PrintWriter bos = openConnection(conn, 145 "http://www.wfmc.org/wfxml/2.0/wfxml/registry/NewDefinition"); 146 FileReader fr = new FileReader (fullFilename); 147 bos.println("<soapenv:Body>" 148 + "<NewDefinitionRq xmlns=\"http://www.wfmc.org/wfxml/2.0/\">" 149 + "<ProcessLanguage>XPDL</ProcessLanguage>" 150 + "<Definition>"); 151 BufferedReader br = new BufferedReader(fr); 152 br.readLine(); 153 while (br.ready()) { 154 bos.println(br.readLine()); 155 } 156 bos.println("</Definition>" 157 + "</NewDefinitionRq>" + "</soapenv:Body>" 158 + "</soapenv:Envelope>"); 159 bos.flush(); 160 br.close(); 161 fr.close(); 162 conn.connect(); 163 int response = conn.getResponseCode(); 164 bos.close(); 165 conn.disconnect(); 166 167 return isResponse2XX(response); 168 } 169 170 public static void wfxmlNewDefinition2 (URL url, String pkgCnt) throws Exception { 171 HttpURLConnection conn = (HttpURLConnection ) url.openConnection(); 172 173 PrintWriter bos = openConnection(conn, 174 "http://www.wfmc.org/wfxml/2.0/wfxml/registry/NewDefinition"); 175 bos.println("<soapenv:Body>" 176 + "<NewDefinitionRq xmlns=\"http://www.wfmc.org/wfxml/2.0/\">" 177 + "<ProcessLanguage>XPDL</ProcessLanguage>" 178 + "<Definition>"); 179 bos.println(pkgCnt); 180 bos.println("</Definition>" 181 + "</NewDefinitionRq>" + "</soapenv:Body>" 182 + "</soapenv:Envelope>"); 183 bos.flush(); 184 conn.connect(); 185 int response = conn.getResponseCode(); 186 bos.close(); 187 conn.disconnect(); 188 189 if (!isResponse2XX(response)) { 190 throw new Exception ("Invalid response"); 191 } 192 193 } 194 195 public static Node wfxmlGetDefinition(URL url) throws Exception { 196 Node ret = null; 197 198 HttpURLConnection conn = (HttpURLConnection ) url.openConnection(); 199 PrintWriter bos = openConnection(conn, 200 "http://www.wfmc.org/wfxml/2.0/wfxml/factory/GetDefinition"); 201 bos.println("<soapenv:Body>" 202 + "<GetDefinitionRq xmlns=\"http://www.wfmc.org/wfxml/2.0/\">" 203 + "<ProcessLanguage>XPDL</ProcessLanguage>" 204 + "</GetDefinitionRq>" + "</soapenv:Body>" 205 + "</soapenv:Envelope>"); 206 bos.flush(); 207 conn.connect(); 208 int response = conn.getResponseCode(); 209 conn.getContentLength(); 210 if (isResponse2XX(response)) { 211 Document b = DocumentBuilderFactory.newInstance() 212 .newDocumentBuilder() 213 .parse(conn.getInputStream()); 214 System.err.println(b.getNodeName()); 215 System.err.println(b.getFirstChild().getNodeName()); 216 System.err.println(b.getFirstChild() 217 .getLastChild() 218 .getFirstChild() 219 .getFirstChild()); 220 ret = b.getFirstChild() 221 .getLastChild() 222 .getFirstChild() 223 .getFirstChild(); 224 bos.close(); 225 conn.disconnect(); 226 return ret; 227 } else { 228 bos.close(); 229 conn.disconnect(); 230 throw new Exception ("Invalid response"); 231 } 232 } 233 234 public static String wfxmlGetDefinition2 (URL url) throws Exception { 235 Node ret = wfxmlGetDefinition(url); 236 return node2String(ret); 237 } 238 239 public static List wfxmlListDefinitions(URL url) throws Exception { 240 List ret = new ArrayList (); 241 242 HttpURLConnection conn = (HttpURLConnection ) url.openConnection(); 243 PrintWriter bos = openConnection(conn, 244 "http://www.wfmc.org/wfxml/2.0/wfxml/factory/ListDefinitions"); 245 bos.println("<soapenv:Body>" 246 + "<ListDefinitionsRq xmlns=\"http://www.wfmc.org/wfxml/2.0/\"/>" 247 + "</soapenv:Body> </soapenv:Envelope>"); 248 bos.flush(); 249 conn.connect(); 250 int response = conn.getResponseCode(); 251 252 if (isResponse2XX(response)) { 253 Document b = DocumentBuilderFactory.newInstance() 254 .newDocumentBuilder() 255 .parse(conn.getInputStream()); 256 NodeList f = b.getFirstChild() 257 .getLastChild() 258 .getFirstChild() 259 .getChildNodes(); 260 for (int i = 0; i < f.getLength(); ++i) { 261 ret.add(new URL (f.item(i) 262 .getFirstChild() 263 .getFirstChild() 264 .getNodeValue())); 265 } 266 return ret; 267 } else { 268 throw new Exception ("Invalid response"); 269 } 270 } 271 272 public static List wfxmlListDefinitions(URL url,DefInfos dis) throws Exception { 273 List ret=new ArrayList (); 274 275 HttpURLConnection conn = (HttpURLConnection ) url.openConnection(); 276 PrintWriter bos = openConnection(conn, 277 "http://www.wfmc.org/wfxml/2.0/wfxml/factory/ListDefinitions"); 278 bos.println("<soapenv:Body>" 279 + "<ListDefinitionsRq xmlns=\"http://www.wfmc.org/wfxml/2.0/\"/>" 280 + "</soapenv:Body> </soapenv:Envelope>"); 281 bos.flush(); 282 conn.connect(); 283 int response = conn.getResponseCode(); 284 285 if (isResponse2XX(response)) { 286 Document b = DocumentBuilderFactory.newInstance() 287 .newDocumentBuilder() 288 .parse(conn.getInputStream()); 289 NodeList f = b.getFirstChild() 290 .getLastChild() 291 .getFirstChild() 292 .getChildNodes(); 293 for (int i = 0; i < f.getLength(); ++i) { 294 NodeList nl=f.item(i).getChildNodes(); 295 String dk=nl.item(0).getFirstChild().getNodeValue(); 296 String n=""; 297 if (nl.item(1).getFirstChild()!=null) { 298 n=nl.item(1).getFirstChild().getNodeValue(); 299 } 300 String d=""; 301 if (nl.item(2).getFirstChild()!=null) { 302 d=nl.item(2).getFirstChild().getNodeValue(); 303 } 304 String v=""; 305 if (nl.item(3).getFirstChild()!=null) { 306 v=nl.item(3).getFirstChild().getNodeValue(); 307 } 308 String s=""; 309 if (nl.item(4).getFirstChild()!=null) { 310 s=nl.item(4).getFirstChild().getNodeValue(); 311 } 312 DefInfo di=(DefInfo)dis.generateNewElement(); 313 di.set("DefinitionKey",dk); 314 di.set("Name",n); 315 di.set("Description",d); 316 di.set("Version",v); 317 di.set("Status",s); 318 ret.add(di); 319 } 320 return ret; 321 } else { 322 throw new Exception ("Invalid response"); 323 } 324 } 325 326 public static String xpdlToString () throws Exception { 327 Document document = null; 328 329 330 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 331 DocumentBuilder dbuilder = dbf.newDocumentBuilder(); 332 document = dbuilder.newDocument(); 333 ByteArrayOutputStream baos=new ByteArrayOutputStream (); 334 335 org.enhydra.jawe.xml.elements.Package pkg= 336 JaWE.getInstance().getRealXMLPackage(); 337 Save.updateExtendedAttributesForWorkflowProcesses(); 338 339 JaWE.getInstance().getPackageEditor().getGraph().getXPDLObject().toXML(document); 341 342 TransformerFactory tFactory = 344 TransformerFactory.newInstance(); 345 Transformer transformer = tFactory.newTransformer(); 346 transformer.setOutputProperty("indent","yes"); 347 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4"); 348 transformer.setOutputProperty("encoding",JaWEConfig.getInstance().getEncoding()); 349 transformer.setOutputProperty("omit-xml-declaration","yes"); 350 DOMSource source = new DOMSource (document); 351 StreamResult result = new StreamResult (baos); 352 transformer.transform(source,result); 353 354 String s=baos.toString(JaWEConfig.getInstance().getEncoding()); 355 baos.close(); 357 return s; 358 } 359 360 public static String node2String (Node n) throws Exception { 361 362 ByteArrayOutputStream baos=new ByteArrayOutputStream (); 363 364 TransformerFactory tFactory = 366 TransformerFactory.newInstance(); 367 Transformer transformer = tFactory.newTransformer(); 368 transformer.setOutputProperty("indent","yes"); 369 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4"); 370 transformer.setOutputProperty("encoding",JaWEConfig.getInstance().getEncoding()); 371 DOMSource source = new DOMSource (n); 372 StreamResult result = new StreamResult (baos); 373 transformer.transform(source,result); 374 375 String s=baos.toString(JaWEConfig.getInstance().getEncoding()); 376 baos.close(); 378 return s; 379 } 380 381 private static PrintWriter openConnection(HttpURLConnection conn, 382 String action) throws Exception { 383 conn.setRequestMethod(REQUEST_METHOD); 384 conn.addRequestProperty(HRP_ACCEPT_NAME, HTTP_ACCEPT_VALUE); 385 conn.addRequestProperty(HRP_USER_AGENT_NAME, HRP_USER_AGENT_VALUE); 386 conn.addRequestProperty(HRP_CACHE_CONTROL_NAME, HRP_CACHE_CONTROL_VALUE); 387 conn.addRequestProperty(HRP_PRAGMA_NAME, HRP_PRAGMA_VALUE); 388 conn.addRequestProperty(HRP_CONTENT_TYPE_NAME, HRP_CONTENT_TYPE_VALUE); 389 conn.addRequestProperty(HRP_SOAP_ACTION, action); 390 conn.setDoOutput(true); 391 String toPrint = REQUEST_HEADER.replaceFirst("\\$\\{1\\}",conn.getURL().toString()); 392 PrintWriter bos = new PrintWriter (conn.getOutputStream()); 393 bos.println(toPrint); 394 return bos; 395 } 396 397 private static boolean isResponse2XX(int response) { 398 return HttpURLConnection.HTTP_ACCEPTED == response 399 || HttpURLConnection.HTTP_OK == response 400 || HttpURLConnection.HTTP_CREATED == response; 401 } 402 403 public static void listNodes(Node m, String space) { 404 NodeList nl = m.getChildNodes(); 405 for (int i = 0; i < nl.getLength(); i++) { 406 Node n = nl.item(i); 407 System.out.println(space + "i=" + i + ", n=" + n.getClass().getName() + ", v=" 408 + n.getNodeValue()); 409 listNodes(n, space + " "); 410 } 411 } 412 413 } 414 | Popular Tags |