messagetests.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 886 行 · 第 1/3 页
JAVA
886 行
// OM
StringReader sr = new StringReader(sampleEnvelopeNoHeader);
XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(inflow, null);
OMElement omElement = builder.getSOAPEnvelope();
// The JAX-WS layer creates a Message from the OM
MessageFactory mf = (MessageFactory)
FactoryRegistry.getFactory(MessageFactory.class);
Message m = mf.createFrom(omElement, null);
// Check to see if the message is a fault. The client/server will always call this method.
// The Message must respond appropriately without doing a conversion.
boolean isFault = m.isFault();
assertTrue(!isFault);
assertTrue("XMLPart Representation is " + m.getXMLPartContentType(),
"OM".equals(m.getXMLPartContentType()));
// The next thing that will happen
// is the proxy code will ask for the business object (String).
XMLStringBlockFactory blockFactory =
(XMLStringBlockFactory) FactoryRegistry.getFactory(XMLStringBlockFactory.class);
Block block = m.getBodyBlock(null, blockFactory);
Object bo = block.getBusinessObject(true);
assertTrue(bo instanceof String);
// The block should be consumed
assertTrue(block.isConsumed());
// Check the String for accuracy
assertTrue(sampleText.equals(bo.toString()));
}
/**
* Create a JAXBBlock containing a JAX-B business object
* and simulate a normal Dispatch<Object> output flow
* @throws Exception
*/
public void testJAXBOutflow() throws Exception {
// Create a SOAP 1.1 Message
MessageFactory mf = (MessageFactory)
FactoryRegistry.getFactory(MessageFactory.class);
Message m = mf.create(Protocol.soap11);
// Get the BlockFactory
JAXBBlockFactory bf = (JAXBBlockFactory)
FactoryRegistry.getFactory(JAXBBlockFactory.class);
// Create the JAX-B object
ObjectFactory of = new ObjectFactory();
EchoStringResponse obj = of.createEchoStringResponse();
obj.setEchoStringReturn("sample return value");
// Create the JAXBContext
JAXBBlockContext context = new JAXBBlockContext(EchoStringResponse.class.getPackage().getName());
// Create a JAXBBlock using the Echo object as the content. This simulates
// what occurs on the outbound JAX-WS Dispatch<Object> client
Block block = bf.createFrom(obj, context, null);
// Add the block to the message as normal body content.
m.setBodyBlock(block);
// Check to see if the message is a fault. The client/server will always call this method.
// The Message must respond appropriately without doing a conversion.
boolean isFault = m.isFault();
assertTrue(!isFault);
assertTrue("XMLPart Representation is " + m.getXMLPartContentType(),
"SPINE".equals(m.getXMLPartContentType()));
// On an outbound flow, we need to convert the Message
// to an OMElement, specifically an OM SOAPEnvelope,
// so we can set it on the Axis2 MessageContext
org.apache.axiom.soap.SOAPEnvelope env =
(org.apache.axiom.soap.SOAPEnvelope) m.getAsOMElement();
// Check to see if the message is a fault. The client/server will always call this method.
// The Message must respond appropriately without doing a conversion.
isFault = m.isFault();
assertTrue(!isFault);
assertTrue("XMLPart Representation is " + m.getXMLPartContentType(),
"OM".equals(m.getXMLPartContentType()));
// Serialize the Envelope using the same mechanism as the
// HTTP client.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
env.serializeAndConsume(baos, new OMOutputFormat());
// To check that the output is correct, get the String contents of the
// reader
String newText = baos.toString();
TestLogger.logger.debug(newText);
assertTrue(newText.contains(sampleJAXBText));
assertTrue(newText.contains("soap"));
assertTrue(newText.contains("Envelope"));
assertTrue(newText.contains("Body"));
}
/**
* Same as JAXBOutputflow, but has an additional check
* to make sure that the JAXB serialization is deferrred
* until the actual serialization of the message.
* @throws Exception
*/
public void testJAXBOutflowPerf() throws Exception {
// Create a SOAP 1.1 Message
MessageFactory mf = (MessageFactory)
FactoryRegistry.getFactory(MessageFactory.class);
Message m = mf.create(Protocol.soap11);
// Get the BlockFactory
JAXBBlockFactory bf = (JAXBBlockFactory)
FactoryRegistry.getFactory(JAXBBlockFactory.class);
// Create the JAX-B object
ObjectFactory of = new ObjectFactory();
EchoStringResponse obj = of.createEchoStringResponse();
obj.setEchoStringReturn("sample return value");
// Create the JAXBContext
JAXBBlockContext context = new JAXBBlockContext(EchoStringResponse.class.getPackage().getName());
// Create a JAXBBlock using the Echo object as the content. This simulates
// what occurs on the outbound JAX-WS Dispatch<Object> client
Block block = bf.createFrom(obj, context, null);
// Add the block to the message as normal body content.
m.setBodyBlock(block);
// Check to see if the message is a fault. The client/server will always call this method.
// The Message must respond appropriately without doing a conversion.
boolean isFault = m.isFault();
assertTrue(!isFault);
assertTrue("XMLPart Representation is " + m.getXMLPartContentType(),
"SPINE".equals(m.getXMLPartContentType()));
// On an outbound flow, we need to convert the Message
// to an OMElement, specifically an OM SOAPEnvelope,
// so we can set it on the Axis2 MessageContext
org.apache.axiom.soap.SOAPEnvelope env =
(org.apache.axiom.soap.SOAPEnvelope) m.getAsOMElement();
// Check to see if the message is a fault. The client/server will always call this method.
// The Message must respond appropriately without doing a conversion.
isFault = m.isFault();
assertTrue(!isFault);
assertTrue("XMLPart Representation is " + m.getXMLPartContentType(),
"OM".equals(m.getXMLPartContentType()));
// PERFORMANCE CHECK:
// The element in the body should be an OMSourcedElement
OMElement o = env.getBody().getFirstElement();
assertTrue(o instanceof OMSourcedElementImpl);
assertTrue(((OMSourcedElementImpl)o).isExpanded() == false);
// Serialize the Envelope using the same mechanism as the
// HTTP client.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
env.serializeAndConsume(baos, new OMOutputFormat());
// To check that the output is correct, get the String contents of the
// reader
String newText = baos.toString();
TestLogger.logger.debug(newText);
assertTrue(newText.contains(sampleJAXBText));
assertTrue(newText.contains("soap"));
assertTrue(newText.contains("Envelope"));
assertTrue(newText.contains("Body"));
}
private final int NO_PERSIST = 0;
private final int PERSIST = 1;
private final int SAVE_AND_PERSIST = 2;
public void testJAXBInflow_soap11() throws Exception {
_testJAXBInflow(sampleJAXBEnvelope11, NO_PERSIST);
}
public void testJAXBInflow_soap12() throws Exception {
_testJAXBInflow(sampleJAXBEnvelope12, NO_PERSIST);
}
public void testJAXBInflow_soap11_withPersist() throws Exception {
_testJAXBInflow(sampleJAXBEnvelope11, PERSIST);
}
public void testJAXBInflow_soap12_withPersist() throws Exception {
_testJAXBInflow(sampleJAXBEnvelope12, PERSIST);
}
public void testJAXBInflow_soap11_withSaveAndPersist() throws Exception {
_testJAXBInflow(sampleJAXBEnvelope11, SAVE_AND_PERSIST);
}
public void testJAXBInflow_soap12_withSaveAndPersist() throws Exception {
_testJAXBInflow(sampleJAXBEnvelope12, SAVE_AND_PERSIST);
}
public void _testJAXBInflow(String sampleJAXBEnvelope, int persist) throws Exception {
// Create a SOAP OM out of the sample incoming XML. This
// simulates what Axis2 will be doing with the inbound message.
StringReader sr = new StringReader(sampleJAXBEnvelope);
XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr);
StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(inflow, null);
OMElement omElement = builder.getSOAPEnvelope();
// Create a SOAP 1.1 Message from the sample incoming XML
MessageFactory mf = (MessageFactory)
FactoryRegistry.getFactory(MessageFactory.class);
Message m = mf.createFrom(omElement, null);
// Check to see if the message is a fault. The client/server will always call this method.
// The Message must respond appropriately without doing a conversion.
boolean isFault = m.isFault();
assertTrue(!isFault);
assertTrue("XMLPart Representation is " + m.getXMLPartContentType(),
"OM".equals(m.getXMLPartContentType()));
String saveMsgText = "";
if (persist == SAVE_AND_PERSIST) {
// Simulate saving the message so that it can be fully rebuilt.
saveMsgText = m.getAsOMElement().toString();
}
// Get the BlockFactory
JAXBBlockFactory bf = (JAXBBlockFactory)
FactoryRegistry.getFactory(JAXBBlockFactory.class);
// Create the JAXBContext instance that will be used
// to deserialize the JAX-B object content in the message.
JAXBBlockContext context = new JAXBBlockContext(EchoStringResponse.class.getPackage().getName());
// Get the JAXBBlock that wraps the content
Block b = m.getBodyBlock(context, bf);
// Check to see if the message is a fault. The client/server will always call this method.
// The Message must respond appropriately without doing a conversion.
isFault = m.isFault();
assertTrue(!isFault);
assertTrue("XMLPart Representation is " + m.getXMLPartContentType(),
"SPINE".equals(m.getXMLPartContentType()));
// Get the business object from the block, which should be a
// JAX-B object
Object bo = b.getBusinessObject(true);
m.setPostPivot();
// Simulate restoring the message
if (persist == SAVE_AND_PERSIST) {
sr = new StringReader(saveMsgText);
XMLStreamReader saveMsgReader = inputFactory.createXMLStreamReader(sr);
builder = new StAXSOAPModelBuilder(saveMsgReader, null);
omElement = builder.getSOAPEnvelope();
m = mf.createFrom(omElement, null);
}
// Check to make sure the right object was returned
assertNotNull(bo);
assertTrue(bo instanceof EchoStringResponse);
// Check to make sure the content of that object is correct
EchoStringResponse esr = (EchoStringResponse) bo;
assertNotNull(esr.getEchoStringReturn());
assertTrue(esr.getEchoStringReturn().equals("sample return value"));
// Simulate outbound
if (persist == PERSIST) {
String persistMsg = m.getAsOMElement().toString();
// We should be able to persist the message, but the persisted message WON'T contain the echoStringResponse contents
assertTrue(persistMsg.contains("Body"));
assertTrue(persistMsg.contains("echoStringResponse"));
assertTrue(!persistMsg.contains("sample return value"));
} else if (persist == SAVE_AND_PERSIST) {
String persistMsg = m.getAsOMElement().toString();
// We should be able to persist the message, and the persisted message WILL contain the echoStringResponse contents
assertTrue(persistMsg.contains("Body"));
assertTrue(persistMsg.contains("echoStringResponse"));
assertTrue(persistMsg.contains("sample return value"));
}
// After persistance: Sandesha may inspect the body. Make sure this does not cause an error
org.apache.axiom.soap.SOAPEnvelope env = (org.apache.axiom.soap.SOAPEnvelope) m.getAsOMElement();
QName qName = new QName("uri://fake", "fake");
env.getBody().getFirstChildWithName(qName);
}
SAAJConverter converter = null;
private SAAJConverter getSAAJConverter() {
if (converter == null) {
SAAJConverterFactory factory = (
SAAJConverterFactory)FactoryRegistry.getFactory(SAAJConverterFactory.class);
converter = factory.getSAAJConverter();
}
return converter;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?