You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by ng...@apache.org on 2007/04/16 21:49:40 UTC
svn commit: r529382 - in /webservices/axis2/trunk/java/modules/jaxws:
src/org/apache/axis2/jaxws/handler/LogicalMessageImpl.java
test/org/apache/axis2/jaxws/handler/context/LogicalMessageContextTests.java
Author: ngallardo
Date: Mon Apr 16 12:49:39 2007
New Revision: 529382
URL: http://svn.apache.org/viewvc?view=rev&rev=529382
Log:
AXIS2-2532
Allow the payload to be accessed multiple times.
Modified:
webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/handler/LogicalMessageImpl.java
webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/handler/context/LogicalMessageContextTests.java
Modified: webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/handler/LogicalMessageImpl.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/handler/LogicalMessageImpl.java?view=diff&rev=529382&r1=529381&r2=529382
==============================================================================
--- webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/handler/LogicalMessageImpl.java (original)
+++ webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/handler/LogicalMessageImpl.java Mon Apr 16 12:49:39 2007
@@ -18,9 +18,20 @@
*/
package org.apache.axis2.jaxws.handler;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+
import javax.xml.bind.JAXBContext;
import javax.xml.stream.XMLStreamException;
+import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerFactoryConfigurationError;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.LogicalMessage;
import javax.xml.ws.WebServiceException;
@@ -66,17 +77,19 @@
Object payload = null;
try {
Block block = message.getBodyBlock(context, factory);
- payload = block.getBusinessObject(true);
+ Object content = block.getBusinessObject(true);
// For now, we have to create a new Block from the original content
// and set that back on the message. The Block is not currently
// able to create a copy of itself just yet.
- Block cacheBlock = factory.createFrom(payload, context, block.getQName());
+ Payloads payloads = createPayloads(content);
+
+ Block cacheBlock = factory.createFrom(payloads.CACHE_PAYLOAD, context, block.getQName());
message.setBodyBlock(cacheBlock);
- } catch (WebServiceException e) {
- ExceptionFactory.makeWebServiceException(e);
+
+ payload = payloads.HANDLER_PAYLOAD;
} catch (XMLStreamException e) {
- ExceptionFactory.makeWebServiceException(e);
+ throw ExceptionFactory.makeWebServiceException(e);
}
return payload;
@@ -108,4 +121,64 @@
message.setBodyBlock(block);
}
}
+
+ private Payloads createPayloads(Object content) {
+ if (content == null) {
+ return null;
+ }
+
+ Payloads payloads = new Payloads();
+
+ if (Source.class.isAssignableFrom(content.getClass())) {
+ try {
+ Transformer trans = TransformerFactory.newInstance().newTransformer();
+
+ // First we have to get the content out of the original
+ // Source object so we can build the cache from there.
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ StreamResult result = new StreamResult(baos);
+
+ Source source = (Source) content;
+ trans.transform(source, result);
+ trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+ byte[] bytes = baos.toByteArray();
+
+ // Given that we've consumed the original Source object,
+ // we need to create another one with the original content
+ // and assign it back.
+ ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
+ payloads.HANDLER_PAYLOAD = new StreamSource(bais);
+
+ // We need a different byte[] for the cache so that we're not just
+ // building two Source objects that point to the same array.
+ byte[] cacheBytes = new byte[bytes.length];
+ System.arraycopy(bytes, 0, cacheBytes, 0, bytes.length);
+
+ // Now build the Soure object for the cache.
+ ByteArrayInputStream cacheBais = new ByteArrayInputStream(cacheBytes);
+ payloads.CACHE_PAYLOAD = new StreamSource(cacheBais);
+ } catch (TransformerConfigurationException e) {
+ throw ExceptionFactory.makeWebServiceException(e);
+ } catch (TransformerFactoryConfigurationError e) {
+ throw ExceptionFactory.makeWebServiceException(e);
+ } catch (TransformerException e) {
+ throw ExceptionFactory.makeWebServiceException(e);
+ }
+ } else {
+ // no cache implemented yet
+ payloads.HANDLER_PAYLOAD = content;
+ payloads.CACHE_PAYLOAD = content;
+ }
+
+ return payloads;
+ }
+
+ /*
+ * A simple holder for the different payload objects.
+ */
+ class Payloads {
+ Object HANDLER_PAYLOAD; // The payload object that will be returned to the handler
+ Object CACHE_PAYLOAD; // The payload object that will be used for the cache
+ }
+
}
Modified: webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/handler/context/LogicalMessageContextTests.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/handler/context/LogicalMessageContextTests.java?view=diff&rev=529382&r1=529381&r2=529382
==============================================================================
--- webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/handler/context/LogicalMessageContextTests.java (original)
+++ webservices/axis2/trunk/java/modules/jaxws/test/org/apache/axis2/jaxws/handler/context/LogicalMessageContextTests.java Mon Apr 16 12:49:39 2007
@@ -122,6 +122,29 @@
}
/**
+ * Test to make sure we can get the payload multiple times from the same LogicalMessage.
+ * @throws Exception
+ */
+ public void testGetMultiplePayloadsAsSource() throws Exception {
+ LogicalMessageContext lmc = createSampleContext();
+
+ LogicalMessage msg = lmc.getMessage();
+ assertTrue("The returned LogicalMessage was null", msg != null);
+
+ int loopCount = 3;
+ for (int i = 0; i < loopCount; ++i) {
+ Source payload = msg.getPayload();
+ assertTrue("Attempt number " + i + " to get the payload (Source) was null", payload != null);
+
+
+ String resultContent = _getStringFromSource(payload);
+ assertTrue("The content returned in loop " + i + " was null", resultContent != null);
+ assertTrue("The content returned in loop " + i + " was incomplete, unexpected element", resultContent.indexOf("echoString") > -1);
+ assertTrue("The content returned in loop " + i + " was incomplete, unexpected content", resultContent.indexOf(INPUT) > -1);
+ }
+ }
+
+ /**
* Tests the setting of the payload when the original content is a fault.
* @throws Exception
*/
---------------------------------------------------------------------
To unsubscribe, e-mail: axis-cvs-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-cvs-help@ws.apache.org