workaroud for snor issue
[vfc/nfvo/wfengine.git] / wso2 / wso2bpel-ext / wso2bpel-core / BPEL4RESTLight / src / main / java / de / unistuttgart / iaas / bpel / extensions / bpel4restlight / util / Bpel4RestLightUtil.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.extensions.bpel4restlight.util;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.apache.ode.bpel.common.FaultException;
13 import org.apache.ode.bpel.runtime.extension.ExtensionContext;
14 import org.w3c.dom.Element;
15 import org.w3c.dom.Node;
16
17 import de.unistuttgart.iaas.bpel.extensions.bpel4restlight.Bpel4RestLightOperation;
18 import de.unistuttgart.iaas.bpel.extensions.bpel4restlight.MethodAttribute;
19 import de.unistuttgart.iaas.xml.DomXmlConverter;
20
21
22 public class Bpel4RestLightUtil {
23   protected static final Log log = LogFactory.getLog(Bpel4RestLightUtil.class);
24         /**
25          * This function extracts the requestPayload specified in the passed
26          * element. This requestPayload is either the content contained in a special
27          * BPEL-Variable which is referenced by name by a special attribute of the
28          * passed element or the content contained in the first child node of the
29          * passed element
30          * 
31          * @param context ExtensionContext
32          * @param element Element from which the requestPayload has to be extracted
33          * @return RequestPayload as String
34          * @throws FaultException
35          */
36         public static String extractRequestPayload(ExtensionContext context, Element element) throws FaultException {
37                 
38                 String requestPayload = "";
39                 
40                 String requestPayloadVariableName = getMethodAttributeValue(element, MethodAttribute.REQUESTPAYLOADVARIABLE);
41                 
42                 if (requestPayloadVariableName != null && requestPayloadVariableName != "") {
43                         Node requestVariableNode = context.readVariable(requestPayloadVariableName);
44                         if (requestVariableNode.getLocalName().equals("temporary-simple-type-wrapper")) {
45                                 Bpel4RestLightOperation.wrapper = "temporary-simple-type-wrapper";
46                                 requestPayload = DomXmlConverter.nodeToString(requestVariableNode, "temporary-simple-type-wrapper");
47                         } else {
48                                 requestPayload = DomXmlConverter.nodeToString(requestVariableNode, null);
49                         }
50                         log.debug("The pure request variable as String: \n" + DomXmlConverter.nodeToString(requestVariableNode, null) + "\n");
51                 }
52                 
53                 return requestPayload;
54         }
55         
56         public static String extractAcceptHeader(ExtensionContext context, Element element) throws FaultException {
57                 return getMethodAttributeValue(element, MethodAttribute.ACCEPTHEADER);
58         }
59         
60         public static String extractContentTypeHeader(ExtensionContext context, Element element) throws FaultException {
61       return getMethodAttributeValue(element, MethodAttribute.CONTENTTYPE);
62   }
63         
64         /**
65          * This function extracts special predefined attributes (see
66          * {@link MethodAttribute}) from a passed DOM-Element
67          * 
68          * @param element Element containing the requested Attribute-Value
69          * @param methodAttribute Attribute whose content has to be returned
70          * @return Value / Content of the attribute
71          */
72         public static String getMethodAttributeValue(Element element, MethodAttribute methodAttribute) {
73                 
74                 String result = "";
75                 
76                 switch (methodAttribute) {
77                 
78                         case REQUESTURI:
79                                 result = element.getAttribute("uri");
80                                 
81                                 if (result == null || "".equals(result)) {
82                                         result = element.getAttribute("requestUri");
83                                 }
84                                 break;
85                         case REQUESTPAYLOADVARIABLE:
86                                 result = element.getAttribute("request");
87                                 
88                                 if (result == null || "".equals(result)) {
89                                         result = element.getAttribute("requestPayload");
90                                 }
91                                 break;
92                         case RESPONSEPAYLOADVARIABLE:
93                                 result = element.getAttribute("response");
94                                 
95                                 if (result == null || "".equals(result)) {
96                                         result = element.getAttribute("responsePayload");
97                                 }
98                                 break;
99                         case STATUSCODEVARIABLE:
100                                 result = element.getAttribute("statusCode");
101                                 break;
102                         case ACCEPTHEADER:
103                                 result = element.getAttribute("accept");
104                                 break;
105                         case CONTENTTYPE:
106                           result = element.getAttribute("contentType");
107                           break;
108                 }
109                 
110                 return result;
111         }
112         
113 }