From 2fc88d416c3207c382b9b35f847a25c1114cbdd5 Mon Sep 17 00:00:00 2001 From: 10090474 Date: Sun, 22 Jan 2017 16:06:27 +0800 Subject: Merger the code provide by lvbo chenbo. Change-Id: I6b4b0402dea075e8d4d4629dfef3fe16d7930901 Issue-id: OCS-198 Signed-off-by: 10090474 --- .../Bpel4RestLightExtensionBundle.java | 48 +++++++ .../bpel4restlight/Bpel4RestLightOperation.java | 147 ++++++++++++++++++++ .../bpel4restlight/EPRDemoOperation.java | 74 ++++++++++ .../extensions/bpel4restlight/MethodAttribute.java | 17 +++ .../bpel4restlight/util/Bpel4RestLightUtil.java | 113 ++++++++++++++++ .../iaas/bpel/util/BPELVariableInjectionUtil.java | 149 +++++++++++++++++++++ .../de/unistuttgart/iaas/bpel/util/BpelUtil.java | 102 ++++++++++++++ 7 files changed, 650 insertions(+) create mode 100644 wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/Bpel4RestLightExtensionBundle.java create mode 100644 wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/Bpel4RestLightOperation.java create mode 100644 wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/EPRDemoOperation.java create mode 100644 wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/MethodAttribute.java create mode 100644 wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/util/Bpel4RestLightUtil.java create mode 100644 wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/util/BPELVariableInjectionUtil.java create mode 100644 wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/util/BpelUtil.java (limited to 'wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel') diff --git a/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/Bpel4RestLightExtensionBundle.java b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/Bpel4RestLightExtensionBundle.java new file mode 100644 index 0000000..1423315 --- /dev/null +++ b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/Bpel4RestLightExtensionBundle.java @@ -0,0 +1,48 @@ +/** + * Copyright 2011 IAAS University of Stuttgart
+ *
+ * + * @author uwe.breitenbuecher@iaas.uni-stuttgart.de + * + */ +/** + * Copyright 2016 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package de.unistuttgart.iaas.bpel.extensions.bpel4restlight; + +import org.apache.ode.bpel.runtime.extension.AbstractExtensionBundle; + + +public class Bpel4RestLightExtensionBundle extends AbstractExtensionBundle { + + public static final String NAMESPACE = "http://iaas.uni-stuttgart.de/bpel/extensions/bpel4restlight"; + + + /** {@inheritDoc} */ + @Override + public String getNamespaceURI() { + return NAMESPACE; + } + + /** {@inheritDoc} */ + @Override + public void registerExtensionActivities() { + super.registerExtensionOperation("logNodes", EPRDemoOperation.class); + super.registerExtensionOperation("PUT", Bpel4RestLightOperation.class); + super.registerExtensionOperation("GET", Bpel4RestLightOperation.class); + super.registerExtensionOperation("POST", Bpel4RestLightOperation.class); + super.registerExtensionOperation("DELETE", Bpel4RestLightOperation.class); + } +} diff --git a/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/Bpel4RestLightOperation.java b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/Bpel4RestLightOperation.java new file mode 100644 index 0000000..2335f49 --- /dev/null +++ b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/Bpel4RestLightOperation.java @@ -0,0 +1,147 @@ +package de.unistuttgart.iaas.bpel.extensions.bpel4restlight; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.util.Properties; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.ode.bpel.common.FaultException; +import org.apache.ode.bpel.runtime.extension.AbstractSyncExtensionOperation; +import org.apache.ode.bpel.runtime.extension.ExtensionContext; +import org.opentosca.bpel4restlight.rest.HighLevelRestApi; +import org.opentosca.bpel4restlight.rest.HttpMethod; +import org.opentosca.bpel4restlight.rest.HttpResponseMessage; +/** + * + * Copyright 2011 IAAS University of Stuttgart
+ *
+ * + * This class provides 4 BPEL4RestLight ExtensionActivity-operations which + * correspond to the 4 typical REST-Operations GET, PUT, POST and Delete. + * + * @author uwe.breitenbuecher@iaas.uni-stuttgart.de + * + */ +import org.w3c.dom.Element; + +import de.unistuttgart.iaas.bpel.extensions.bpel4restlight.util.Bpel4RestLightUtil; +import de.unistuttgart.iaas.bpel.util.BPELVariableInjectionUtil; +import de.unistuttgart.iaas.bpel.util.BpelUtil; + + +public class Bpel4RestLightOperation extends AbstractSyncExtensionOperation { + protected static final Log log = LogFactory.getLog(Bpel4RestLightOperation.class); + private static final String EXT_PROPERTIES = "wso2bps-ext.properties"; // extra properties setting + private static final String MSB_URL = "MSB_URL"; // http://msb:port, ext property msb url, if exist replace the request url + + + public static String wrapper = "temporary-simple-type-wrapper"; + private static String msbUrl = null; + + static { + + try { + File file = new File(EXT_PROPERTIES); + if(file.exists()) { + Properties p=new Properties(); + InputStream inputStream = new FileInputStream(file); + p.load(inputStream); + inputStream.close(); + msbUrl = p.getProperty(MSB_URL); + } + + } catch (Exception e) { + e.printStackTrace(); + } + + } + + + private void processResponseMessage(HttpResponseMessage responseMessage, ExtensionContext context, Element element) throws FaultException { + // Write responsePayload to designated variable + String responsePayloadVariableName = Bpel4RestLightUtil.getMethodAttributeValue(element, MethodAttribute.RESPONSEPAYLOADVARIABLE); + String statusCodeVariableName = Bpel4RestLightUtil.getMethodAttributeValue(element, MethodAttribute.STATUSCODEVARIABLE); + + if (responsePayloadVariableName != null && !responsePayloadVariableName.equals("")) { + BpelUtil.writeResponsePayloadToVariable(context, responseMessage.getResponseBody(), responsePayloadVariableName, Bpel4RestLightOperation.wrapper); + } + + if (statusCodeVariableName != null && !statusCodeVariableName.equals("")) { + String StatusCode = "" + responseMessage.getStatusCode(); // int can not be casted to String + + BpelUtil.writeResponsePayloadToVariable(context, StatusCode, statusCodeVariableName, Bpel4RestLightOperation.wrapper); + + } + } + + /** {@inheritDoc} */ + @Override + protected void runSync(ExtensionContext context, Element element) throws FaultException { + element = BPELVariableInjectionUtil.replaceExtensionVariables(context, element); + log.debug("LocalName of edited element: " + element.getLocalName()); + String httpMethod = element.getLocalName(); + + // Extract requestUri + String requestUri = getRequestUrl(element); + + HttpResponseMessage responseMessage = null; + String acceptHeader = Bpel4RestLightUtil.extractAcceptHeader(context, element); + String contentTypeHeader = Bpel4RestLightUtil.extractContentTypeHeader(context, element); + String requestPayload = Bpel4RestLightUtil.extractRequestPayload(context, element); + + // Execute corresponding HttpMethod via the HighLevelRestApi + switch (HttpMethod.valueOf(httpMethod)) { + + case PUT: { + responseMessage = HighLevelRestApi.Put(requestUri, requestPayload, acceptHeader, contentTypeHeader); + break; + } + + case POST: { + responseMessage = HighLevelRestApi.Post(requestUri, requestPayload, acceptHeader, contentTypeHeader); + break; + } + + case GET: { + responseMessage = HighLevelRestApi.Get(requestUri, acceptHeader, contentTypeHeader); + break; + } + + case DELETE: { + responseMessage = HighLevelRestApi.Delete(requestUri, acceptHeader, contentTypeHeader); + break; + } + } + + processResponseMessage(responseMessage, context, element); + // Bpel4RestLightOperation.wrapper = null; + } + + + /** + * getRequestUrl + * get request url from element, if exists msbUrl property, then replace the ip and port + * @param element + * @return + */ + private String getRequestUrl(Element element) { + String requestUri = Bpel4RestLightUtil.getMethodAttributeValue(element, MethodAttribute.REQUESTURI); + log.debug("original url:" + requestUri); + if(msbUrl == null || "".equals(msbUrl) || "http://msb:port".equals(msbUrl)) { + return requestUri; + } else { + requestUri = requestUri.substring(requestUri.indexOf("//") + 2); + int index = requestUri.indexOf("/"); + if(index == -1) { + + } else { + requestUri = requestUri.substring(index); + } + requestUri = msbUrl + requestUri; + log.debug("changed url:" + requestUri); + return requestUri; + } + } +} diff --git a/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/EPRDemoOperation.java b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/EPRDemoOperation.java new file mode 100644 index 0000000..fd7b4b3 --- /dev/null +++ b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/EPRDemoOperation.java @@ -0,0 +1,74 @@ +/** + * + * Copyright 2011 IAAS University of Stuttgart
+ *
+ * + * THIS CLASS IS USED FOR TEST PURPOSES! + * + * @author uwe.breitenbuecher@iaas.uni-stuttgart.de + * + */ +package de.unistuttgart.iaas.bpel.extensions.bpel4restlight; + +import java.net.HttpURLConnection; +import java.net.URL; + +import org.apache.ode.bpel.common.FaultException; +import org.apache.ode.bpel.o.OPartnerLink; +import org.apache.ode.bpel.runtime.PartnerLinkInstance; +import org.apache.ode.bpel.runtime.extension.AbstractSyncExtensionOperation; +import org.apache.ode.bpel.runtime.extension.ExtensionContext; +import org.w3c.dom.Element; + +import de.unistuttgart.iaas.xml.DomXmlConverter; + + +public class EPRDemoOperation extends AbstractSyncExtensionOperation { + + /** {@inheritDoc} */ + @Override + protected void runSync(ExtensionContext context, Element element) throws FaultException { + System.out.println("bin hier"); + try { + for (OPartnerLink l : context.getInternalInstance().getProcessModel().allPartnerLinks) { + + PartnerLinkInstance pli = context.getPartnerLinkInstance(l); + + Element epr = context.getInternalInstance().fetchPartnerRoleEndpointReferenceData(pli); + System.out.println("EPR|||"); + System.out.println(DomXmlConverter.nodeToString(epr, null)); + + System.out.println(epr.getChildNodes().item(0).getChildNodes().item(1).getNodeName()); + System.out.println(epr.getChildNodes().item(0).getChildNodes().item(1).getTextContent()); + System.out.println(".."); + System.out.println(epr.getChildNodes().item(0).getChildNodes().item(1).getChildNodes().item(0).getTextContent()); + + epr.getChildNodes().item(0).getChildNodes().item(1).getChildNodes().item(0).setTextContent("http://localhost:8084/FRP/rrr"); + + System.out.println(epr.getChildNodes().item(0).getChildNodes().item(1).getChildNodes().item(0).getTextContent()); + + System.out.println(context.getDUDir().toString()); + + } + + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("ich bin hiiiiiiiiiier!!"); + + try { + URL url = new URL("http://localhost:8084/FRP/rrr"); + + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("PUT"); + + int responseCode = connection.getResponseCode(); + System.out.println(responseCode); + + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/MethodAttribute.java b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/MethodAttribute.java new file mode 100644 index 0000000..ce80c16 --- /dev/null +++ b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/MethodAttribute.java @@ -0,0 +1,17 @@ +/** + * + * Copyright 2011 IAAS University of Stuttgart
+ *
+ * + * This enum is used to decouple DOM-Attribute names from their + * String-representation within a certain library (for portability issues) + * + * @author uwe.breitenbuecher@iaas.uni-stuttgart.de + * + */ +package de.unistuttgart.iaas.bpel.extensions.bpel4restlight; + + +public enum MethodAttribute { + REQUESTURI, REQUESTPAYLOADVARIABLE, RESPONSEPAYLOADVARIABLE, STATUSCODEVARIABLE, ACCEPTHEADER, CONTENTTYPE; +} \ No newline at end of file diff --git a/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/util/Bpel4RestLightUtil.java b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/util/Bpel4RestLightUtil.java new file mode 100644 index 0000000..1c9d7d1 --- /dev/null +++ b/wso2bpel-ext/wso2bpel-core/BPEL4RESTLight/src/main/java/de/unistuttgart/iaas/bpel/extensions/bpel4restlight/util/Bpel4RestLightUtil.java @@ -0,0 +1,113 @@ +/** + * Copyright 2011 IAAS University of Stuttgart
+ *
+ * + * @author uwe.breitenbuecher@iaas.uni-stuttgart.de + * + */ +package de.unistuttgart.iaas.bpel.extensions.bpel4restlight.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.ode.bpel.common.FaultException; +import org.apache.ode.bpel.runtime.extension.ExtensionContext; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +import de.unistuttgart.iaas.bpel.extensions.bpel4restlight.Bpel4RestLightOperation; +import de.unistuttgart.iaas.bpel.extensions.bpel4restlight.MethodAttribute; +import de.unistuttgart.iaas.xml.DomXmlConverter; + + +public class Bpel4RestLightUtil { + protected static final Log log = LogFactory.getLog(Bpel4RestLightUtil.class); + /** + * This function extracts the requestPayload specified in the passed + * element. This requestPayload is either the content contained in a special + * BPEL-Variable which is referenced by name by a special attribute of the + * passed element or the content contained in the first child node of the + * passed element + * + * @param context ExtensionContext + * @param element Element from which the requestPayload has to be extracted + * @return RequestPayload as String + * @throws FaultException + */ + public static String extractRequestPayload(ExtensionContext context, Element element) throws FaultException { + + String requestPayload = ""; + + String requestPayloadVariableName = getMethodAttributeValue(element, MethodAttribute.REQUESTPAYLOADVARIABLE); + + if (requestPayloadVariableName != null && requestPayloadVariableName != "") { + Node requestVariableNode = context.readVariable(requestPayloadVariableName); + if (requestVariableNode.getLocalName().equals("temporary-simple-type-wrapper")) { + Bpel4RestLightOperation.wrapper = "temporary-simple-type-wrapper"; + requestPayload = DomXmlConverter.nodeToString(requestVariableNode, "temporary-simple-type-wrapper"); + } else { + requestPayload = DomXmlConverter.nodeToString(requestVariableNode, null); + } + log.debug("The pure request variable as String: \n" + DomXmlConverter.nodeToString(requestVariableNode, null) + "\n"); + } + + return requestPayload; + } + + public static String extractAcceptHeader(ExtensionContext context, Element element) throws FaultException { + return getMethodAttributeValue(element, MethodAttribute.ACCEPTHEADER); + } + + public static String extractContentTypeHeader(ExtensionContext context, Element element) throws FaultException { + return getMethodAttributeValue(element, MethodAttribute.CONTENTTYPE); + } + + /** + * This function extracts special predefined attributes (see + * {@link MethodAttribute}) from a passed DOM-Element + * + * @param element Element containing the requested Attribute-Value + * @param methodAttribute Attribute whose content has to be returned + * @return Value / Content of the attribute + */ + public static String getMethodAttributeValue(Element element, MethodAttribute methodAttribute) { + + String result = ""; + + switch (methodAttribute) { + + case REQUESTURI: + result = element.getAttribute("uri"); + + if (result == null || "".equals(result)) { + result = element.getAttribute("requestUri"); + } + break; + case REQUESTPAYLOADVARIABLE: + result = element.getAttribute("request"); + + if (result == null || "".equals(result)) { + result = element.getAttribute("requestPayload"); + } + break; + case RESPONSEPAYLOADVARIABLE: + result = element.getAttribute("response"); + + if (result == null || "".equals(result)) { + result = element.getAttribute("responsePayload"); + } + break; + case STATUSCODEVARIABLE: + result = element.getAttribute("statusCode"); + break; + case ACCEPTHEADER: + result = element.getAttribute("accept"); + break; + case CONTENTTYPE: + result = element.getAttribute("contentType"); + break; + } + + return result; + } + +} 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
+ *
+ * + * @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 + ""; + } + 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(); + } + +} -- cgit 1.2.3-korg