Author: midon
Date: Thu Jun 26 19:29:10 2008
New Revision: 672092
URL: http://svn.apache.org/viewvc?rev=672092&view=rev
Log:
ODE-309: Header part-1, extract HTTP Response Headers and stuff them in the ODE
message
Modified:
ode/branches/APACHE_ODE_1.1/axis2/src/main/java/org/apache/ode/axis2/httpbinding/HttpExternalService.java
ode/branches/APACHE_ODE_1.1/axis2/src/main/java/org/apache/ode/axis2/httpbinding/HttpMethodConverter.java
Modified:
ode/branches/APACHE_ODE_1.1/axis2/src/main/java/org/apache/ode/axis2/httpbinding/HttpExternalService.java
URL:
http://svn.apache.org/viewvc/ode/branches/APACHE_ODE_1.1/axis2/src/main/java/org/apache/ode/axis2/httpbinding/HttpExternalService.java?rev=672092&r1=672091&r2=672092&view=diff
==============================================================================
---
ode/branches/APACHE_ODE_1.1/axis2/src/main/java/org/apache/ode/axis2/httpbinding/HttpExternalService.java
(original)
+++
ode/branches/APACHE_ODE_1.1/axis2/src/main/java/org/apache/ode/axis2/httpbinding/HttpExternalService.java
Thu Jun 26 19:29:10 2008
@@ -44,7 +44,6 @@
import org.apache.ode.utils.wsdl.WsdlUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
-import org.w3c.dom.Node;
import javax.wsdl.Binding;
import javax.wsdl.BindingOperation;
@@ -336,7 +335,7 @@
if (log.isDebugEnabled()) log.debug(errmsg);
odeMex.replyWithFailure(MessageExchange.FailureType.OTHER, errmsg,
helper.prepareDetailsElement(method));
} else {
- Part partDef = (Part)
opBinding.getOperation().getFault(faultDef.getName()).getMessage().getParts().values().iterator().next();
+ Part partDef = (Part)
faultDef.getMessage().getParts().values().iterator().next();
// build the element to be sent back
Document odeMsg = DOMUtils.newDocument();
@@ -349,6 +348,10 @@
QName faultType = new QName(targetNamespace,
faultDef.getName());
Message response = odeMex.createMessage(faultType);
response.setMessage(odeMsgEl);
+
+ // extract and set headers
+
httpMethodConverter.extractHttpResponseHeaders(response, method,
faultDef.getMessage(), opBinding.getBindingOutput());
+
// finally send the fault. We did it!
odeMex.replyWithFault(faultType, response);
}
@@ -392,7 +395,8 @@
log.error(errmsg);
odeMex.replyWithFailure(MessageExchange.FailureType.OTHER,
errmsg, null);
} else {
- Message odeResponse =
odeMex.createMessage(odeMex.getOperation().getOutput().getMessage().getQName());
+ javax.wsdl.Message outputMessage =
odeMex.getOperation().getOutput().getMessage();
+ Message odeResponse =
odeMex.createMessage(outputMessage.getQName());
// handle the body if any
if (bodyAsStream != null) {
@@ -401,8 +405,8 @@
Element bodyElement =
DOMUtils.parse(bodyAsStream).getDocumentElement();
// we expect a single part per output message
// see
org.apache.ode.axis2.httpbinding.HttpBindingValidator call in constructor
- Part part = (Part)
odeMex.getOperation().getOutput().getMessage().getParts().values().iterator().next();
- Element partElement = processBodyElement(part,
bodyElement);
+ Part part = (Part)
outputMessage.getParts().values().iterator().next();
+ Element partElement =
httpMethodConverter.createPartElement(part, bodyElement);
odeResponse.setPart(part.getName(), partElement);
} catch (Exception e) {
String errmsg = "Unable to parse the response
body: " + e.getMessage();
@@ -411,6 +415,10 @@
return;
}
}
+
+ // handle headers
+
httpMethodConverter.extractHttpResponseHeaders(odeResponse, method,
outputMessage, opBinding.getBindingOutput());
+
try {
if (log.isInfoEnabled())
@@ -429,48 +437,6 @@
return;
}
}
-
- /**
- * Create the element to be inserted into [EMAIL PROTECTED] Message}.
- * If the part has a non-null element name, the bodyElement is simply
appended.
- * Else if the bodyElement has a text content, the value is set to the
message.
- * Else append all nodes of bodyElement to the returned element.
Attributes are ignored.
- * <p/>
- * The name of the returned element is the part name.
- *
- * @param part
- * @param bodyElement
- * @return the element to insert "as is" to ODE message
- */
- private Element processBodyElement(Part part, Element bodyElement) {
- Document doc = DOMUtils.newDocument();
- Element partElement = doc.createElementNS(null, part.getName());
- if (part.getElementName() != null) {
- partElement.appendChild(doc.importNode(bodyElement, true));
- } else {
- if (DOMUtils.isEmptyElement(bodyElement)) {
- // Append an empty text node.
- // Warning! setting an empty string with setTextContent
has not effect. See javadoc.
- partElement.appendChild(doc.createTextNode(""));
- } else {
- String textContent = DOMUtils.getTextContent(bodyElement);
- if (textContent != null) {
- // this is a simple type
- partElement.setTextContent(textContent);
- } else {
- // this is a complex type, import every child
- // !!! Attributes are ignored
- for (int m = 0; m <
bodyElement.getChildNodes().getLength(); m++) {
- Node child = bodyElement.getChildNodes().item(m);
- partElement.appendChild(doc.importNode(child,
true));
- }
- }
- }
-
- }
- return partElement;
- }
-
}
}
Modified:
ode/branches/APACHE_ODE_1.1/axis2/src/main/java/org/apache/ode/axis2/httpbinding/HttpMethodConverter.java
URL:
http://svn.apache.org/viewvc/ode/branches/APACHE_ODE_1.1/axis2/src/main/java/org/apache/ode/axis2/httpbinding/HttpMethodConverter.java?rev=672092&r1=672091&r2=672092&view=diff
==============================================================================
---
ode/branches/APACHE_ODE_1.1/axis2/src/main/java/org/apache/ode/axis2/httpbinding/HttpMethodConverter.java
(original)
+++
ode/branches/APACHE_ODE_1.1/axis2/src/main/java/org/apache/ode/axis2/httpbinding/HttpMethodConverter.java
Thu Jun 26 19:29:10 2008
@@ -19,12 +19,8 @@
package org.apache.ode.axis2.httpbinding;
-import org.apache.axis2.transport.http.HttpTransportProperties;
-import org.apache.commons.httpclient.HostConfiguration;
+import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethod;
-import org.apache.commons.httpclient.HttpState;
-import org.apache.commons.httpclient.URI;
-import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
@@ -34,36 +30,38 @@
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpParams;
+import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.apache.commons.lang.StringUtils;
import org.apache.ode.axis2.Properties;
import org.apache.ode.axis2.util.URLEncodedTransformer;
import org.apache.ode.axis2.util.UrlReplacementTransformer;
+import org.apache.ode.bpel.epr.MutableEndpoint;
import org.apache.ode.bpel.iapi.PartnerRoleMessageExchange;
import org.apache.ode.utils.DOMUtils;
+import org.apache.ode.utils.Namespaces;
import org.apache.ode.utils.wsdl.Messages;
import org.apache.ode.utils.wsdl.WsdlUtils;
-import org.apache.ode.bpel.epr.MutableEndpoint;
-import org.apache.axiom.soap.SOAPEnvelope;
-import org.apache.axiom.soap.SOAPFault;
import org.w3c.dom.Element;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
import javax.wsdl.Binding;
import javax.wsdl.BindingInput;
import javax.wsdl.BindingOperation;
+import javax.wsdl.BindingOutput;
import javax.wsdl.Message;
import javax.wsdl.Operation;
import javax.wsdl.Part;
-import javax.wsdl.Fault;
+import javax.wsdl.extensions.UnknownExtensibilityElement;
import javax.wsdl.extensions.http.HTTPOperation;
import javax.wsdl.extensions.mime.MIMEContent;
import javax.xml.namespace.QName;
import java.io.UnsupportedEncodingException;
+import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
-import java.util.Collection;
public class HttpMethodConverter {
@@ -100,10 +98,6 @@
return method;
}
- public void parseHttpResponse(org.apache.ode.bpel.iapi.Message odeMessage,
HttpMethod response, Operation op) {
-
- }
-
/**
* create and initialize the http method.
@@ -225,4 +219,111 @@
}
-}
\ No newline at end of file
+ /**
+ * Create the element to be associated with this part into the [EMAIL
PROTECTED] org.apache.ode.bpel.iapi.Message}.
+ * <br/>An element named with the part name will be returned. the content
of this element depends on the part.
+ * <p/>If the part has a non-null element name, a new element will be
created and named accordingly then the text value is inserted in this new
element.
+ * <br/>else the given text content is simply set on the part element.
+ *
+ * @param part
+ * @param textContent
+ * @return an element named with the part name will be returned
+ */
+ public Element createPartElement(Part part, String textContent) {
+ Document doc = DOMUtils.newDocument();
+ Element partElement = doc.createElementNS(null, part.getName());
+ if (part.getElementName() != null) {
+ Element element =
doc.createElementNS(part.getElementName().getNamespaceURI(),
part.getElementName().getLocalPart());
+ element.setTextContent(textContent);
+ partElement.appendChild(element);
+ }else{
+ partElement.setTextContent(textContent);
+ }
+ return partElement;
+ }
+
+
+ /**
+ * Create the element to be associated with this part into the [EMAIL
PROTECTED] org.apache.ode.bpel.iapi.Message}.
+ * <p/>If the part has a non-null element name, the bodyElement is simply
appended.
+ * Else if the bodyElement has a text content, the value is set to the
message.
+ * Else append all nodes of bodyElement to the returned element.
Attributes are ignored.
+ * <p/>
+ * The name of the returned element is the part name.
+ *
+ * @param part
+ * @param receivedElement
+ * @return the element to insert "as is" to ODE message
+ */
+ public Element createPartElement(Part part, Element receivedElement) {
+ Document doc = DOMUtils.newDocument();
+ Element partElement = doc.createElementNS(null, part.getName());
+ if (part.getElementName() != null) {
+ partElement.appendChild(doc.importNode(receivedElement, true));
+ } else {
+ if (DOMUtils.isEmptyElement(receivedElement)) {
+ // Append an empty text node.
+ // Warning! setting an empty string with setTextContent has
not effect. See javadoc.
+ partElement.appendChild(doc.createTextNode(""));
+ } else {
+ String textContent = DOMUtils.getTextContent(receivedElement);
+ if (textContent != null) {
+ // this is a simple type
+ partElement.setTextContent(textContent);
+ } else {
+ // this is a complex type, import every child
+ // !!! Attributes are ignored
+ for (int m = 0; m <
receivedElement.getChildNodes().getLength(); m++) {
+ Node child = receivedElement.getChildNodes().item(m);
+ partElement.appendChild(doc.importNode(child, true));
+ }
+ }
+ }
+
+ }
+ return partElement;
+ }
+
+ /**
+ * Process the HTTP Response Headers.
+ * <p/>
+ * First go through the list of Namespaces.ODE_HTTP_EXTENSION_NS:header
elements included in the output binding. For each of them, set the header value
as the value of the message part.
+ * <b/>Then add all HTTP headers as header part in the message. The name
of the header would be the part name.
+ * @param odeMessage
+ * @param method
+ * @param messageDef
+ * @param bindingOutput
+ */
+ public void extractHttpResponseHeaders(org.apache.ode.bpel.iapi.Message
odeMessage, HttpMethod method, Message messageDef, BindingOutput bindingOutput)
{
+ Collection<UnknownExtensibilityElement> headerBindings =
WsdlUtils.getHttpHeaders(bindingOutput.getExtensibilityElements());
+
+ // iterate through the list of header bindings
+ // and set the message parts accordingly
+ for (Iterator<UnknownExtensibilityElement> iterator =
headerBindings.iterator(); iterator.hasNext();) {
+ Element binding = iterator.next().getElement();
+ String partName = binding.getAttribute("part");
+ String headerName = binding.getAttribute("name");
+
+ Part part = messageDef.getPart(partName);
+ if (StringUtils.isNotEmpty(partName)) {
+ odeMessage.setPart(partName, createPartElement(part,
method.getRequestHeader(headerName).getValue()));
+ } else {
+ if(log.isErrorEnabled()) {
+ String errMsg = "Invalid binding: missing required
attribute! Part name: "+new QName(Namespaces.ODE_HTTP_EXTENSION_NS, "part");
+ log.error(errMsg);
+ throw new RuntimeException(errMsg);
+ }
+ }
+ }
+
+ // also add all HTTP headers into the messade as header parts
+ Header[] reqHeaders = method.getResponseHeaders();
+ for (int i = 0; i < reqHeaders.length; i++) {
+ Header h = reqHeaders[i];
+ odeMessage.setHeaderPart(h.getName(), h.getValue());
+ }
+ }
+
+
+}
+