diff options
author | 10090474 <yuan.hu1@zte.com.cn> | 2017-01-22 16:06:27 +0800 |
---|---|---|
committer | 10090474 <yuan.hu1@zte.com.cn> | 2017-01-22 16:13:11 +0800 |
commit | 2fc88d416c3207c382b9b35f847a25c1114cbdd5 (patch) | |
tree | f50c8d0b419997bab319bb0d12746d90760495de /wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/util | |
parent | b9d6eed278f02c75865fa97f4ba6020e348f2e93 (diff) |
Merger the code provide by lvbo chenbo.
Change-Id: I6b4b0402dea075e8d4d4629dfef3fe16d7930901
Issue-id: OCS-198
Signed-off-by: 10090474 <yuan.hu1@zte.com.cn>
Diffstat (limited to 'wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/util')
2 files changed, 251 insertions, 0 deletions
diff --git a/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/util/BPELVariableInjectionUtil.java b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/util/BPELVariableInjectionUtil.java new file mode 100644 index 0000000..2298bf1 --- /dev/null +++ b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/util/BPELVariableInjectionUtil.java @@ -0,0 +1,149 @@ +/** + * Copyright 2011 + * + * @author Uwe Breitenbuecher + * + * This class provides some methods for BPEL-Variable-Injection + */ + package de.unistuttgart.iaas.bpel.util; + +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.apache.ode.bpel.common.FaultException; +import org.apache.ode.bpel.runtime.extension.ExtensionContext; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + + +public class BPELVariableInjectionUtil { + + /** + * This method serializes a Node into a String + * + * @param node + * @return String representation of the node + */ + public static String nodeToString(Node node) { + try { + + if (node != null && node.getLocalName().equals("temporary-simple-type-wrapper")) { + // this is a temporary hack for string variables and the likes, + // as you may see ODE wrappes simpletypes in wrapper-elements, + // but this isn't great here + return node.getTextContent(); + } + + // Create transformer + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + + // Transform Node into a String representation by regarding some + // formatting rules + StringWriter stringWriter = new StringWriter(); + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3"); + transformer.transform(new DOMSource(node), new StreamResult(stringWriter)); + + // Return build string + return stringWriter.toString(); + } catch (Exception e) { + e.printStackTrace(); + } + + // If any error occurs, return empty string + return ""; + } + + /** + * This method executes the BPEL-Variable-Injection. It replaces referenced + * BPEL-Variables with corresponding content + * + * @param context ExtensionContext of process + * @param element DOM-Representation of the BPEL-Code in which the + * Variable-Injection has to be done + * @return modified BPEL-Code as DOM-Representation + */ + public static Element replaceExtensionVariables(ExtensionContext context, Element element) { + + try { + String BPELCodeAsString; + + // Transform BPEL-Code (DOM-Representation) into a String + BPELCodeAsString = nodeToString(element); + + // Find and replace referenced BPEL-Variables + int startIndex = BPELCodeAsString.indexOf("$bpelvar["); + if (startIndex != -1) { + while (startIndex != -1) { + int endIndex = startIndex; + while (BPELCodeAsString.charAt(endIndex) != ']') { + endIndex++; + } + + // Extract name of referenced variable + String variableName = BPELCodeAsString.substring(startIndex + 9, endIndex); + + // Extract content of referenced variable + Node variableContent = context.readVariable(variableName); + + System.out.println("Replacing variable " + variableName + "(" + variableContent.getNamespaceURI() + " " + variableContent.getLocalName() + ") with content: \n"); + System.out.println("NodeValue(): " + variableContent.getNodeValue() + "\n"); + System.out.println("TextContent(): " + variableContent.getTextContent()); + System.out.println("The full bpel script (before change) as string: \n" + BPELCodeAsString + "\n"); + + // Replace variable-reference with corresponding content + BPELCodeAsString = BPELCodeAsString.replace("$bpelvar[" + variableName + "]", nodeToString(variableContent)); + + System.out.println("The full bpel script as string: \n" + BPELCodeAsString + "\n"); + startIndex = BPELCodeAsString.indexOf("$bpelvar["); + } + + // Transform modified code (String) into DOM-Representation + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + DocumentBuilder builder = factory.newDocumentBuilder(); + + InputSource inputSource = new InputSource(); + inputSource.setCharacterStream(new StringReader(BPELCodeAsString)); + Document newDocument = builder.parse(inputSource); + + // Return first child (because Document root is not needed) + return (Element) newDocument.getFirstChild(); + + } else { + + // If no referenced variables are found, return original code + return element; + } + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } catch (SAXException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (FaultException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return null; + } + +} diff --git a/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/util/BpelUtil.java b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/util/BpelUtil.java new file mode 100644 index 0000000..33c0d54 --- /dev/null +++ b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/util/BpelUtil.java @@ -0,0 +1,102 @@ +/** + * Copyright 2011 IAAS University of Stuttgart <br> + * <br> + * + * @author uwe.breitenbuecher@iaas.uni-stuttgart.de + * + */ +package de.unistuttgart.iaas.bpel.util; + +import java.io.IOException; +import java.io.StringReader; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.ode.bpel.common.FaultException; +import org.apache.ode.bpel.evar.ExternalVariableModuleException; +import org.apache.ode.bpel.o.OScope.Variable; +import org.apache.ode.bpel.runtime.extension.ExtensionContext; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + + +public class BpelUtil { + + /** + * This function writes a passed content to a specified processVariable + * (referenced by name). The content will be converted into its + * DOM-Representation for overwriting the processVariableContent (therefore + * it has to be XML-serializable, e.g. for complex data types there have to + * be JAX-B Annotations within the corresponding Class) + * + * @param context ExtensionContext needed to access the processVariable + * @param content New content for the specified processVariable + * @param processVariableName Variable whose content has to be overwritten + * @throws FaultException + */ + public static void writeContentToBPELVariable(ExtensionContext context, Object content, String processVariableName, String wrapper) throws FaultException { + // check the node + System.out.println("The content object: " + content + "\n"); + // small hack for test + Node hackNode = null; + System.out.println("Trying to parse string to dom: " + ((String) content) + "\n"); + + if (wrapper != null) { + // a hack for simple type wrapper + content = "<" + wrapper + ">" + (String) content + "</" + wrapper + ">"; + } + try { + hackNode = stringToDom((String) content); + } catch (ParserConfigurationException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } catch (SAXException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + Variable bpelVariable = context.getVisibleVariables().get(processVariableName); + if (hackNode == null) { + System.out.println("hackNode is null! \n"); + } + if (bpelVariable == null) { + System.out.println("bpelVariable is null! \n"); + } + try { + // replaced responseAsNode to hackNode + context.writeVariable(bpelVariable, hackNode); + } catch (ExternalVariableModuleException e) { + e.printStackTrace(); + } + + } + + /** + * This function writes a String to a BPEL Variable of type XSD-String + * + * @param context ExtensionContext + * @param responsePayload ResponsePayload as String + * @param processVariableName Name of the target BPEL variable + * @throws FaultException + */ + public static void writeResponsePayloadToVariable(ExtensionContext context, Object responsePayload, String processVariableName, String wrapper) throws FaultException { + BpelUtil.writeContentToBPELVariable(context, responsePayload, processVariableName, wrapper); + } + + private static Node stringToDom(String xmlString) throws ParserConfigurationException, SAXException, IOException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + DocumentBuilder builder = factory.newDocumentBuilder(); + InputSource is = new InputSource(new StringReader(xmlString)); + Document d = builder.parse(is); + return d.getFirstChild(); + } + +} |