workaroud for snor issue
[vfc/nfvo/wfengine.git] / wso2bpel-ext / wso2bpel-core / BPEL4RESTLight / src / main / java / de / unistuttgart / iaas / bpel / util / BpelUtil.java
1 /**
2  * Copyright 2011 IAAS University of Stuttgart <br>
3  * <br>
4  * 
5  * @author uwe.breitenbuecher@iaas.uni-stuttgart.de
6  * 
7  */
8 package de.unistuttgart.iaas.bpel.util;
9
10 import java.io.IOException;
11 import java.io.StringReader;
12
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.DocumentBuilderFactory;
15 import javax.xml.parsers.ParserConfigurationException;
16
17 import org.apache.ode.bpel.common.FaultException;
18 import org.apache.ode.bpel.evar.ExternalVariableModuleException;
19 import org.apache.ode.bpel.o.OScope.Variable;
20 import org.apache.ode.bpel.runtime.extension.ExtensionContext;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Node;
23 import org.xml.sax.InputSource;
24 import org.xml.sax.SAXException;
25
26
27 public class BpelUtil {
28         
29         /**
30          * This function writes a passed content to a specified processVariable
31          * (referenced by name). The content will be converted into its
32          * DOM-Representation for overwriting the processVariableContent (therefore
33          * it has to be XML-serializable, e.g. for complex data types there have to
34          * be JAX-B Annotations within the corresponding Class)
35          * 
36          * @param context ExtensionContext needed to access the processVariable
37          * @param content New content for the specified processVariable
38          * @param processVariableName Variable whose content has to be overwritten
39          * @throws FaultException
40          */
41         public static void writeContentToBPELVariable(ExtensionContext context, Object content, String processVariableName, String wrapper) throws FaultException {
42                 // check the node
43                 System.out.println("The content object: " + content + "\n");
44                 // small hack for test
45                 Node hackNode = null;
46                 System.out.println("Trying to parse string to dom: " + ((String) content) + "\n");
47                 
48                 if (wrapper != null) {
49                         // a hack for simple type wrapper
50                         content = "<" + wrapper + ">" + (String) content + "</" + wrapper + ">";
51                 }
52                 try {
53                         hackNode = stringToDom((String) content);
54                 } catch (ParserConfigurationException e1) {
55                         // TODO Auto-generated catch block
56                         e1.printStackTrace();
57                 } catch (SAXException e1) {
58                         // TODO Auto-generated catch block
59                         e1.printStackTrace();
60                 } catch (IOException e1) {
61                         // TODO Auto-generated catch block
62                         e1.printStackTrace();
63                 }
64                 
65                 Variable bpelVariable = context.getVisibleVariables().get(processVariableName);
66                 if (hackNode == null) {
67                         System.out.println("hackNode is null! \n");
68                 }
69                 if (bpelVariable == null) {
70                         System.out.println("bpelVariable is null! \n");
71                 }
72                 try {
73                         // replaced responseAsNode to hackNode
74                         context.writeVariable(bpelVariable, hackNode);
75                 } catch (ExternalVariableModuleException e) {
76                         e.printStackTrace();
77                 }
78                 
79         }
80         
81         /**
82          * This function writes a String to a BPEL Variable of type XSD-String
83          * 
84          * @param context ExtensionContext
85          * @param responsePayload ResponsePayload as String
86          * @param processVariableName Name of the target BPEL variable
87          * @throws FaultException
88          */
89         public static void writeResponsePayloadToVariable(ExtensionContext context, Object responsePayload, String processVariableName, String wrapper) throws FaultException {
90                 BpelUtil.writeContentToBPELVariable(context, responsePayload, processVariableName, wrapper);
91         }
92         
93         private static Node stringToDom(String xmlString) throws ParserConfigurationException, SAXException, IOException {
94                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
95                 factory.setNamespaceAware(true);
96                 DocumentBuilder builder = factory.newDocumentBuilder();
97                 InputSource is = new InputSource(new StringReader(xmlString));
98                 Document d = builder.parse(is);
99                 return d.getFirstChild();
100         }
101         
102 }