use of org.apache.cxf.phase.PhaseInterceptorChain in project cxf by apache.
the class SOAPHandlerInterceptorTest method testChangeSOAPHeaderOutBound.
@Test
public void testChangeSOAPHeaderOutBound() throws Exception {
@SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
list.add(new SOAPHandler<SOAPMessageContext>() {
public boolean handleMessage(SOAPMessageContext smc) {
try {
Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
// change mustUnderstand to false
SOAPMessage message = smc.getMessage();
SOAPHeader soapHeader = message.getSOAPHeader();
Iterator<?> it = soapHeader.getChildElements(new QName("http://apache.org/hello_world_rpclit/types", "header1"));
SOAPHeaderElement headerElementNew = (SOAPHeaderElement) it.next();
SoapVersion soapVersion = Soap11.getInstance();
Attr attr = headerElementNew.getOwnerDocument().createAttributeNS(soapVersion.getNamespace(), "SOAP-ENV:mustUnderstand");
attr.setValue("false");
headerElementNew.setAttributeNodeNS(attr);
}
} catch (Exception e) {
throw new Fault(e);
}
return true;
}
public boolean handleFault(SOAPMessageContext smc) {
return true;
}
public Set<QName> getHeaders() {
return null;
}
public void close(MessageContext messageContext) {
}
});
HandlerChainInvoker invoker = new HandlerChainInvoker(list);
IMocksControl control = createNiceControl();
Binding binding = control.createMock(Binding.class);
expect(binding.getHandlerChain()).andReturn(list).anyTimes();
Exchange exchange = control.createMock(Exchange.class);
expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker).anyTimes();
SoapMessage message = new SoapMessage(new MessageImpl());
message.setExchange(exchange);
// This is to set direction to outbound
expect(exchange.getOutMessage()).andReturn(message).anyTimes();
CachedStream originalEmptyOs = new CachedStream();
message.setContent(OutputStream.class, originalEmptyOs);
InterceptorChain chain = new PhaseInterceptorChain((new PhaseManagerImpl()).getOutPhases());
// Interceptors after SOAPHandlerInterceptor DOMXMLStreamWriter to write
chain.add(new AbstractProtocolHandlerInterceptor<SoapMessage>(binding, Phase.MARSHAL) {
public void handleMessage(SoapMessage message) throws Fault {
try {
XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
SoapVersion soapVersion = Soap11.getInstance();
writer.setPrefix("soap", soapVersion.getNamespace());
writer.writeStartElement("soap", soapVersion.getEnvelope().getLocalPart(), soapVersion.getNamespace());
writer.writeNamespace("soap", soapVersion.getNamespace());
Object[] headerInfo = prepareSOAPHeader();
StaxUtils.writeElement((Element) headerInfo[1], writer, true, false);
writer.writeEndElement();
writer.flush();
} catch (Exception e) {
// do nothing
}
}
});
chain.add(new SOAPHandlerInterceptor(binding));
message.setInterceptorChain(chain);
control.replay();
chain.doIntercept(message);
control.verify();
// Verify SOAPMessage header
SOAPMessage soapMessageNew = message.getContent(SOAPMessage.class);
SOAPHeader soapHeader = soapMessageNew.getSOAPHeader();
Iterator<?> itNew = soapHeader.getChildElements(new QName("http://apache.org/hello_world_rpclit/types", "header1"));
SOAPHeaderElement headerElementNew = (SOAPHeaderElement) itNew.next();
SoapVersion soapVersion = Soap11.getInstance();
assertEquals("false", headerElementNew.getAttributeNS(soapVersion.getNamespace(), "mustUnderstand"));
originalEmptyOs.close();
}
use of org.apache.cxf.phase.PhaseInterceptorChain in project cxf by apache.
the class SOAPHandlerInterceptorTest method testChangeSOAPBodyOutBound.
// SAAJ tree is created from DOMXMLStreamWriter. Any changes to SOAPMessage should be streamed back to
// outputStream
@Test
public void testChangeSOAPBodyOutBound() throws Exception {
@SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
list.add(new SOAPHandler<SOAPMessageContext>() {
public boolean handleMessage(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
try {
smc.setMessage(prepareSOAPMessage("resources/greetMeRpcLitRespChanged.xml"));
} catch (Exception e) {
throw new Fault(e);
}
}
return true;
}
public boolean handleFault(SOAPMessageContext smc) {
return true;
}
public Set<QName> getHeaders() {
return null;
}
public void close(MessageContext messageContext) {
}
});
HandlerChainInvoker invoker = new HandlerChainInvoker(list);
IMocksControl control = createNiceControl();
Binding binding = control.createMock(Binding.class);
expect(binding.getHandlerChain()).andReturn(list).anyTimes();
Exchange exchange = control.createMock(Exchange.class);
expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker).anyTimes();
SoapMessage message = new SoapMessage(new MessageImpl());
message.setExchange(exchange);
// This is to set direction to outbound
expect(exchange.getOutMessage()).andReturn(message).anyTimes();
CachedStream originalEmptyOs = new CachedStream();
XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(originalEmptyOs);
message.setContent(XMLStreamWriter.class, writer);
InterceptorChain chain = new PhaseInterceptorChain((new PhaseManagerImpl()).getOutPhases());
// Interceptors after SOAPHandlerInterceptor DOMXMLStreamWriter to write
chain.add(new AbstractProtocolHandlerInterceptor<SoapMessage>(binding, Phase.MARSHAL) {
public void handleMessage(SoapMessage message) throws Fault {
try {
XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
SoapVersion soapVersion = Soap11.getInstance();
writer.setPrefix("soap", soapVersion.getNamespace());
writer.writeStartElement("soap", soapVersion.getEnvelope().getLocalPart(), soapVersion.getNamespace());
writer.writeNamespace("soap", soapVersion.getNamespace());
writer.writeEndElement();
writer.flush();
} catch (Exception e) {
// do nothing
}
}
});
chain.add(new SOAPHandlerInterceptor(binding));
message.setInterceptorChain(chain);
control.replay();
chain.doIntercept(message);
control.verify();
writer.flush();
// Verify SOAPMessage
SOAPMessage resultedMessage = message.getContent(SOAPMessage.class);
assertNotNull(resultedMessage);
SOAPBody bodyNew = resultedMessage.getSOAPBody();
Iterator<?> itNew = bodyNew.getChildElements(new QName("http://apache.org/hello_world_rpclit", "sendReceiveDataResponse"));
SOAPBodyElement bodyElementNew = (SOAPBodyElement) itNew.next();
Iterator<?> outIt = bodyElementNew.getChildElements(new QName("http://apache.org/hello_world_rpclit/types", "out"));
Element outElement = (SOAPElement) outIt.next();
assertNotNull(outElement);
Element elem3Element = DOMUtils.findAllElementsByTagNameNS(outElement, "http://apache.org/hello_world_rpclit/types", "elem3").get(0);
assertEquals("100", elem3Element.getTextContent());
}
use of org.apache.cxf.phase.PhaseInterceptorChain in project cxf by apache.
the class RMManager method getRetransmitChain.
/**
* Get interceptor chain for retransmitting a message.
*
* @return chain (<code>null</code> if none set)
*/
public PhaseInterceptorChain getRetransmitChain(Message msg) {
Endpoint ep = msg.getExchange().getEndpoint();
PhaseInterceptorChain pic = (PhaseInterceptorChain) ep.get(WSRM_RETRANSMIT_CHAIN);
if (pic == null) {
return null;
}
return pic.cloneChain();
}
use of org.apache.cxf.phase.PhaseInterceptorChain in project cxf by apache.
the class RMManager method initializeInterceptorChain.
/**
* Clones and saves the interceptor chain the first time this is called, so that it can be used for retransmission.
* Calls after the first are ignored.
*
* @param msg
*/
public void initializeInterceptorChain(Message msg) {
Endpoint ep = msg.getExchange().getEndpoint();
synchronized (ep) {
if (ep.get(WSRM_RETRANSMIT_CHAIN) == null) {
LOG.info("Setting retransmit chain from message");
PhaseInterceptorChain chain = (PhaseInterceptorChain) msg.getInterceptorChain();
chain = chain.cloneChain();
ep.put(WSRM_RETRANSMIT_CHAIN, chain);
}
}
}
use of org.apache.cxf.phase.PhaseInterceptorChain in project cxf by apache.
the class RMInInterceptorTest method testOrdering.
@Test
public void testOrdering() {
control.replay();
Phase p = new Phase(Phase.PRE_LOGICAL, 1);
SortedSet<Phase> phases = new TreeSet<Phase>();
phases.add(p);
PhaseInterceptorChain chain = new PhaseInterceptorChain(phases);
MAPAggregator map = new MAPAggregator();
RMInInterceptor rmi = new RMInInterceptor();
chain.add(rmi);
chain.add(map);
Iterator<Interceptor<? extends Message>> it = chain.iterator();
assertSame("Unexpected order.", rmi, it.next());
assertSame("Unexpected order.", map, it.next());
}
Aggregations