use of javax.xml.soap.SOAPHeaderElement 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 javax.xml.soap.SOAPHeaderElement in project cxf by apache.
the class SOAPMessageContextImpl method getHeaders.
public Object[] getHeaders(QName name, JAXBContext context, boolean allRoles) {
SOAPMessage msg = getMessage();
SOAPHeader header;
try {
header = msg.getSOAPPart().getEnvelope().getHeader();
if (header == null || !header.hasChildNodes()) {
return new Object[0];
}
List<Object> ret = new ArrayList<>();
Iterator<SOAPHeaderElement> it = CastUtils.cast(header.examineAllHeaderElements());
while (it.hasNext()) {
SOAPHeaderElement she = it.next();
if ((allRoles || roles.contains(she.getActor())) && name.equals(she.getElementQName())) {
ret.add(JAXBUtils.unmarshall(context, she));
}
}
return ret.toArray(new Object[0]);
} catch (SOAPException | JAXBException e) {
throw new WebServiceException(e);
}
}
use of javax.xml.soap.SOAPHeaderElement in project cxf by apache.
the class TestMustUnderstandHandler method handleMessage.
public boolean handleMessage(SOAPMessageContext ctx) {
boolean continueProcessing = true;
try {
Object b = ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
boolean outbound = (Boolean) b;
SOAPMessage msg = ctx.getMessage();
if (isServerSideHandler()) {
if (outbound) {
QName qname = new QName("http://cxf.apache.org/mu", "MU");
SOAPPart soapPart = msg.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPHeader header = envelope.getHeader();
if (header == null) {
header = envelope.addHeader();
}
SOAPHeaderElement headerElement = header.addHeaderElement(envelope.createName("MU", "ns1", qname.getNamespaceURI()));
// QName soapMustUnderstand = new QName("http://schemas.xmlsoap.org/soap/envelope/",
// "mustUnderstand");
Name name = SOAPFactory.newInstance().createName("mustUnderstand", "soap", "http://schemas.xmlsoap.org/soap/envelope/");
headerElement.addAttribute(name, "1");
} else {
getHandlerInfoList(ctx).add(getHandlerId());
}
}
} catch (SOAPException e) {
e.printStackTrace();
}
return continueProcessing;
}
use of javax.xml.soap.SOAPHeaderElement in project hutool by looly.
the class SoapClient method addSOAPHeader.
/**
* 增加SOAP头信息,方法返回{@link SOAPHeaderElement}可以设置具体属性和子节点
*
* @param localName 头节点名称
* @param value 头节点的值
* @return {@link SOAPHeaderElement}
* @since 5.4.7
*/
public SOAPHeaderElement addSOAPHeader(String localName, String value) {
final SOAPHeaderElement soapHeaderElement = addSOAPHeader(localName);
soapHeaderElement.setTextContent(value);
return soapHeaderElement;
}
use of javax.xml.soap.SOAPHeaderElement in project quickstarts by jboss-switchyard.
the class OrderProcessor method process.
@Override
public void process(Exchange exchange) throws Exception {
ServiceDomain domain = ((SwitchYardCamelContext) exchange.getContext()).getServiceDomain();
String socketAddr = (String) domain.getProperty("soapClientPort");
String socketAddrFault = (String) domain.getProperty("httpPort");
Order order = exchange.getIn().getBody(Order.class);
System.out.println("Received Order " + order.getItem() + " with quantity " + order.getQuantity() + ".");
if (order.getItem().equals("Airbus")) {
throw new UnknownItem("Sorry, Airbus is no longer available with us!");
}
SOAPHeaderElement messageId = (SOAPHeaderElement) exchange.getIn().getHeaders().get("{http://www.w3.org/2005/08/addressing}MessageID");
if (messageId == null) {
messageId = (SOAPHeaderElement) exchange.getIn().getHeaders().get("{http://www.w3.org/2005/08/addressing}messageid");
}
exchange.getOut().getHeaders().put("{http://www.w3.org/2005/08/addressing}ReplyTo", "http://localhost:" + socketAddr + "/soap-addressing/client/ResponseService");
exchange.getOut().getHeaders().put("{http://www.w3.org/2005/08/addressing}RelatesTo", messageId.getValue());
exchange.getOut().getHeaders().put("{http://www.w3.org/2005/08/addressing}FaultTo", "http://localhost:" + socketAddrFault + "/soap-addressing/fault/FaultService");
exchange.getOut().setBody(order);
}
Aggregations