KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > wfxml > WfXMLConnector


1 /*
2  * Created on Dec 28, 2004
3  *
4  */

5 package org.enhydra.jawe.wfxml;
6
7 import java.io.*;
8 import java.io.ByteArrayOutputStream JavaDoc;
9 import java.io.FileReader JavaDoc;
10 import java.io.PrintWriter JavaDoc;
11 import java.net.HttpURLConnection JavaDoc;
12 import java.net.URL JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import javax.xml.parsers.DocumentBuilder JavaDoc;
17 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
18 import javax.xml.transform.Transformer JavaDoc;
19 import javax.xml.transform.TransformerFactory JavaDoc;
20 import javax.xml.transform.dom.DOMSource JavaDoc;
21 import javax.xml.transform.stream.StreamResult JavaDoc;
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 JavaDoc;
26 import org.w3c.dom.Node JavaDoc;
27 import org.w3c.dom.NodeList JavaDoc;
28
29 /**
30  * @author V.Puskas
31  */

32 public class WfXMLConnector {
33
34    private static final String JavaDoc REQUEST_METHOD = "POST";
35
36    private static final String JavaDoc HRP_ACCEPT_NAME = "Accept";
37
38    private static final String JavaDoc HTTP_ACCEPT_VALUE = "application/soap+xml, text/*";
39
40    private static final String JavaDoc HRP_USER_AGENT_NAME = "User-Agent";
41
42    private static final String JavaDoc HRP_CACHE_CONTROL_NAME = "Cache-Control";
43
44    private static final String JavaDoc HRP_PRAGMA_NAME = "Pragma";
45
46    private static final String JavaDoc HRP_CONTENT_TYPE_NAME = "Content-Type";
47
48    private static final String JavaDoc HRP_SOAP_ACTION = "SOAPAction";
49
50    private static final String JavaDoc HRP_USER_AGENT_VALUE = "JaWE/1.4.2";
51
52    private static final String JavaDoc HRP_CACHE_CONTROL_VALUE = "no-cache";
53
54    private static final String JavaDoc HRP_PRAGMA_VALUE = "no-cache";
55
56    private static final String JavaDoc HRP_CONTENT_TYPE_VALUE = "text/xml; charset=utf-8";
57
58    private static final String JavaDoc 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 JavaDoc[] args) {
71       try {
72          URL JavaDoc url = new URL JavaDoc("http://grunf:8080/axis/services/wfxmlRegistryBinding");
73          wfxmlSetDefinition(url, "/tmp/asap_test.xpdl");
74          /*List l = wfxmlListDefinitions(url);
75           for (Iterator it = l.iterator(); it.hasNext();) {
76           url = (URL) it.next();
77           System.err.println("##" + url);
78           wfxmlGetDefinition(url);
79           }*/

80          //wfxmlSetDefinition(url, "/tmp/Application_Repository.xpdl");
81
//wfxmlSetDefinition(url, "/tmp/test-JavaScript.xpdl");
82
} catch (Throwable JavaDoc e) {
83          e.printStackTrace();
84       }
85    }
86
87    public static boolean wfxmlSetDefinition(URL JavaDoc url, String JavaDoc fullFilename) throws Exception JavaDoc {
88       HttpURLConnection JavaDoc conn = (HttpURLConnection JavaDoc) url.openConnection();
89
90       PrintWriter JavaDoc bos = openConnection(conn,
91                                        "http://www.wfmc.org/wfxml/2.0/wfxml/factory/SetDefinition");
92       FileReader JavaDoc fr = new FileReader JavaDoc(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 JavaDoc url, String JavaDoc pkgCnt) throws Exception JavaDoc {
118       HttpURLConnection JavaDoc conn = (HttpURLConnection JavaDoc) url.openConnection();
119
120       PrintWriter JavaDoc 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 JavaDoc ("Invalid response");
138       }
139    }
140
141    public static boolean wfxmlNewDefinition(URL JavaDoc url, String JavaDoc fullFilename) throws Exception JavaDoc {
142       HttpURLConnection JavaDoc conn = (HttpURLConnection JavaDoc) url.openConnection();
143
144       PrintWriter JavaDoc bos = openConnection(conn,
145                                        "http://www.wfmc.org/wfxml/2.0/wfxml/registry/NewDefinition");
146       FileReader JavaDoc fr = new FileReader JavaDoc(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 JavaDoc url, String JavaDoc pkgCnt) throws Exception JavaDoc {
171       HttpURLConnection JavaDoc conn = (HttpURLConnection JavaDoc) url.openConnection();
172
173       PrintWriter JavaDoc 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 JavaDoc ("Invalid response");
191       }
192
193    }
194
195    public static Node JavaDoc wfxmlGetDefinition(URL JavaDoc url) throws Exception JavaDoc {
196       Node JavaDoc ret = null;
197
198       HttpURLConnection JavaDoc conn = (HttpURLConnection JavaDoc) url.openConnection();
199       PrintWriter JavaDoc 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 JavaDoc 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 JavaDoc ("Invalid response");
231       }
232    }
233
234    public static String JavaDoc wfxmlGetDefinition2 (URL JavaDoc url) throws Exception JavaDoc {
235       Node JavaDoc ret = wfxmlGetDefinition(url);
236       return node2String(ret);
237    }
238
239    public static List JavaDoc wfxmlListDefinitions(URL JavaDoc url) throws Exception JavaDoc {
240       List JavaDoc ret = new ArrayList JavaDoc();
241
242       HttpURLConnection JavaDoc conn = (HttpURLConnection JavaDoc) url.openConnection();
243       PrintWriter JavaDoc 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 JavaDoc b = DocumentBuilderFactory.newInstance()
254             .newDocumentBuilder()
255             .parse(conn.getInputStream());
256          NodeList JavaDoc f = b.getFirstChild()
257             .getLastChild()
258             .getFirstChild()
259             .getChildNodes();
260          for (int i = 0; i < f.getLength(); ++i) {
261             ret.add(new URL JavaDoc(f.item(i)
262                                .getFirstChild()
263                                .getFirstChild()
264                                .getNodeValue()));
265          }
266          return ret;
267       } else {
268          throw new Exception JavaDoc ("Invalid response");
269       }
270    }
271
272    public static List JavaDoc wfxmlListDefinitions(URL JavaDoc url,DefInfos dis) throws Exception JavaDoc {
273       List JavaDoc ret=new ArrayList JavaDoc();
274
275       HttpURLConnection JavaDoc conn = (HttpURLConnection JavaDoc) url.openConnection();
276       PrintWriter JavaDoc 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 JavaDoc b = DocumentBuilderFactory.newInstance()
287             .newDocumentBuilder()
288             .parse(conn.getInputStream());
289          NodeList JavaDoc f = b.getFirstChild()
290             .getLastChild()
291             .getFirstChild()
292             .getChildNodes();
293          for (int i = 0; i < f.getLength(); ++i) {
294             NodeList JavaDoc nl=f.item(i).getChildNodes();
295             String JavaDoc dk=nl.item(0).getFirstChild().getNodeValue();
296             String JavaDoc n="";
297             if (nl.item(1).getFirstChild()!=null) {
298                n=nl.item(1).getFirstChild().getNodeValue();
299             }
300             String JavaDoc d="";
301             if (nl.item(2).getFirstChild()!=null) {
302                d=nl.item(2).getFirstChild().getNodeValue();
303             }
304             String JavaDoc v="";
305             if (nl.item(3).getFirstChild()!=null) {
306                v=nl.item(3).getFirstChild().getNodeValue();
307             }
308             String JavaDoc 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 JavaDoc ("Invalid response");
323       }
324    }
325
326    public static String JavaDoc xpdlToString () throws Exception JavaDoc {
327       Document JavaDoc document = null;
328
329
330       DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
331       DocumentBuilder JavaDoc dbuilder = dbf.newDocumentBuilder();
332       document = dbuilder.newDocument();
333       ByteArrayOutputStream JavaDoc baos=new ByteArrayOutputStream JavaDoc();
334
335       org.enhydra.jawe.xml.elements.Package pkg=
336          JaWE.getInstance().getRealXMLPackage();
337       Save.updateExtendedAttributesForWorkflowProcesses();
338
339       // Here we get all document elements
340
JaWE.getInstance().getPackageEditor().getGraph().getXPDLObject().toXML(document);
341
342       // Use a Transformer for output
343
TransformerFactory JavaDoc tFactory =
344          TransformerFactory.newInstance();
345       Transformer JavaDoc 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 JavaDoc source = new DOMSource JavaDoc(document);
351       StreamResult JavaDoc result = new StreamResult JavaDoc(baos);
352       transformer.transform(source,result);
353
354       String JavaDoc s=baos.toString(JaWEConfig.getInstance().getEncoding());
355       //String s=baos.toString();
356
baos.close();
357       return s;
358    }
359
360    public static String JavaDoc node2String (Node JavaDoc n) throws Exception JavaDoc {
361
362       ByteArrayOutputStream JavaDoc baos=new ByteArrayOutputStream JavaDoc();
363
364       // Use a Transformer for output
365
TransformerFactory JavaDoc tFactory =
366          TransformerFactory.newInstance();
367       Transformer JavaDoc 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 JavaDoc source = new DOMSource JavaDoc(n);
372       StreamResult JavaDoc result = new StreamResult JavaDoc(baos);
373       transformer.transform(source,result);
374
375       String JavaDoc s=baos.toString(JaWEConfig.getInstance().getEncoding());
376       //System.out.println(s);
377
baos.close();
378       return s;
379    }
380
381    private static PrintWriter JavaDoc openConnection(HttpURLConnection JavaDoc conn,
382                                              String JavaDoc action) throws Exception JavaDoc {
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 JavaDoc toPrint = REQUEST_HEADER.replaceFirst("\\$\\{1\\}",conn.getURL().toString());
392       PrintWriter JavaDoc bos = new PrintWriter JavaDoc(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 JavaDoc m, String JavaDoc space) {
404       NodeList JavaDoc nl = m.getChildNodes();
405       for (int i = 0; i < nl.getLength(); i++) {
406          Node JavaDoc 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