use of org.apache.cxf.headers.Header in project cxf by apache.
the class SoapHeaderInterceptor method handleMessage.
public void handleMessage(Message m) throws Fault {
SoapMessage message = (SoapMessage) m;
SoapVersion soapVersion = message.getVersion();
Exchange exchange = message.getExchange();
MessageContentsList parameters = MessageContentsList.getContentsList(message);
if (null == parameters) {
parameters = new MessageContentsList();
}
BindingOperationInfo bop = exchange.getBindingOperationInfo();
if (null == bop) {
return;
}
if (bop.isUnwrapped()) {
bop = bop.getWrappedOperation();
}
boolean client = isRequestor(message);
BindingMessageInfo bmi = client ? bop.getOutput() : bop.getInput();
if (bmi == null) {
// one way operation.
return;
}
List<SoapHeaderInfo> headers = bmi.getExtensors(SoapHeaderInfo.class);
if (headers == null || headers.isEmpty()) {
return;
}
boolean supportsNode = this.supportsDataReader(message, Node.class);
Service service = ServiceModelUtil.getService(message.getExchange());
for (SoapHeaderInfo header : headers) {
MessagePartInfo mpi = header.getPart();
try {
if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.IN, message)) {
Schema schema = EndpointReferenceUtils.getSchema(service.getServiceInfos().get(0), message.getExchange().getBus());
validateHeader(message, mpi, schema);
}
} catch (Fault f) {
if (!isRequestor(message)) {
f.setFaultCode(Fault.FAULT_CODE_CLIENT);
}
throw f;
}
if (mpi.getTypeClass() != null) {
Header param = findHeader(message, mpi);
Object object = null;
if (param != null) {
message.getHeaders().remove(param);
if (param.getDataBinding() == null) {
Node source = (Node) param.getObject();
if (source instanceof Element) {
// need to remove these attributes as they
// would cause validation failures
Element el = (Element) source;
el.removeAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameMustUnderstand());
el.removeAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameRole());
}
if (supportsNode) {
object = getNodeDataReader(message).read(mpi, source);
} else {
W3CDOMStreamReader reader = new W3CDOMStreamReader((Element) source);
try {
// advance into the first tag
reader.nextTag();
} catch (XMLStreamException e) {
// ignore
}
object = getDataReader(message, XMLStreamReader.class).read(mpi, reader);
}
} else {
object = param.getObject();
}
}
parameters.put(mpi, object);
}
}
if (!parameters.isEmpty()) {
message.setContent(List.class, parameters);
}
}
use of org.apache.cxf.headers.Header in project cxf by apache.
the class SAAJInInterceptor method replaceHeaders.
public static void replaceHeaders(SOAPMessage soapMessage, SoapMessage message) throws SOAPException {
SOAPHeader header = SAAJUtils.getHeader(soapMessage);
if (header == null) {
return;
}
Element elem = DOMUtils.getFirstElement(header);
elem = (Element) DOMUtils.getDomElement(elem);
while (elem != null) {
Bus b = message.getExchange() == null ? null : message.getExchange().getBus();
HeaderProcessor p = null;
if (b != null && b.getExtension(HeaderManager.class) != null) {
p = b.getExtension(HeaderManager.class).getHeaderProcessor(elem.getNamespaceURI());
}
Object obj;
DataBinding dataBinding = null;
if (p == null || p.getDataBinding() == null) {
obj = elem;
} else {
dataBinding = p.getDataBinding();
obj = p.getDataBinding().createReader(Node.class).read(elem);
}
SoapHeader shead = new SoapHeader(new QName(elem.getNamespaceURI(), elem.getLocalName()), obj, dataBinding);
shead.setDirection(SoapHeader.Direction.DIRECTION_IN);
String mu = elem.getAttributeNS(message.getVersion().getNamespace(), message.getVersion().getAttrNameMustUnderstand());
String act = elem.getAttributeNS(message.getVersion().getNamespace(), message.getVersion().getAttrNameRole());
shead.setActor(act);
shead.setMustUnderstand(Boolean.valueOf(mu) || "1".equals(mu));
Header oldHdr = message.getHeader(new QName(elem.getNamespaceURI(), elem.getLocalName()));
if (oldHdr != null) {
message.getHeaders().remove(oldHdr);
}
message.getHeaders().add(shead);
elem = DOMUtils.getNextElement(elem);
}
}
use of org.apache.cxf.headers.Header in project cxf by apache.
the class ReadHeaderInterceptorTest method testHandleHeader.
@Test
public void testHandleHeader() {
try {
prepareSoapMessage("test-soap-header.xml");
} catch (IOException ioe) {
fail("Failed in creating soap message");
}
staxIntc.handleMessage(soapMessage);
soapMessage.getInterceptorChain().doIntercept(soapMessage);
// check the xmlReader should be placed on the first entry of the body element
XMLStreamReader xmlReader = soapMessage.getContent(XMLStreamReader.class);
assertEquals("check the first entry of body", "itinerary", xmlReader.getLocalName());
List<Header> eleHeaders = soapMessage.getHeaders();
List<Element> headerChilds = new ArrayList<>();
Iterator<Header> iter = eleHeaders.iterator();
while (iter.hasNext()) {
Header hdr = iter.next();
if (hdr.getObject() instanceof Element) {
headerChilds.add((Element) hdr.getObject());
}
}
assertEquals(2, headerChilds.size());
for (int i = 0; i < headerChilds.size(); i++) {
Element ele = headerChilds.get(i);
if (ele.getLocalName().equals("reservation")) {
Element reservation = ele;
List<Element> reservationChilds = new ArrayList<>();
Element elem = DOMUtils.getFirstElement(reservation);
while (elem != null) {
reservationChilds.add(elem);
elem = DOMUtils.getNextElement(elem);
}
assertEquals(2, reservationChilds.size());
assertEquals("reference", reservationChilds.get(0).getLocalName());
assertEquals("uuid:093a2da1-q345-739r-ba5d-pqff98fe8j7d", reservationChilds.get(0).getTextContent());
assertEquals("dateAndTime", reservationChilds.get(1).getLocalName());
assertEquals("2001-11-29T13:20:00.000-05:00", reservationChilds.get(1).getTextContent());
}
if (ele.getLocalName().equals("passenger")) {
Element passenger = ele;
assertNotNull(passenger);
Element child = DOMUtils.getFirstElement(passenger);
assertNotNull("passenger should have a child element", child);
assertEquals("name", child.getLocalName());
assertEquals("Bob", child.getTextContent());
}
}
}
use of org.apache.cxf.headers.Header in project cxf by apache.
the class JAXWSMethodInvokerTest method testFaultHeadersCopy.
@Test
public void testFaultHeadersCopy() throws Throwable {
ExceptionService serviceObject = new ExceptionService();
Method serviceMethod = ExceptionService.class.getMethod("invoke", new Class[] {});
Exchange ex = new ExchangeImpl();
prepareInMessage(ex, true);
Message msg = new MessageImpl();
SoapMessage outMessage = new SoapMessage(msg);
ex.setOutMessage(outMessage);
JAXWSMethodInvoker jaxwsMethodInvoker = prepareJAXWSMethodInvoker(ex, serviceObject, serviceMethod);
try {
jaxwsMethodInvoker.invoke(ex, new MessageContentsList(new Object[] {}));
fail("Expected fault");
} catch (Fault fault) {
Message outMsg = ex.getOutMessage();
Assert.assertNotNull(outMsg);
@SuppressWarnings("unchecked") List<Header> headers = (List<Header>) outMsg.get(Header.HEADER_LIST);
Assert.assertEquals(1, headers.size());
Assert.assertEquals(TEST_HEADER_NAME, headers.get(0).getName());
}
}
use of org.apache.cxf.headers.Header in project cxf by apache.
the class MAPCodec method encodeReferenceParameters.
private void encodeReferenceParameters(AddressingProperties maps, SoapMessage msg, JAXBContext ctx) throws JAXBException {
Element header = null;
EndpointReferenceType toEpr = maps.getToEndpointReference();
if (null != toEpr) {
ReferenceParametersType params = toEpr.getReferenceParameters();
if (null != params) {
for (Object o : params.getAny()) {
if (o instanceof Element || o instanceof JAXBElement) {
if (header == null) {
header = getHeaderFactory().getHeader(msg.getVersion());
}
JAXBElement<?> jaxbEl = null;
if (o instanceof Element) {
Element e = (Element) o;
Node importedNode = header.getOwnerDocument().importNode(e, true);
header.appendChild(importedNode);
} else {
jaxbEl = (JAXBElement<?>) o;
ctx.createMarshaller().marshal(jaxbEl, header);
}
Element lastAdded = (Element) header.getLastChild();
header.removeChild(lastAdded);
addIsReferenceParameterMarkerAttribute(lastAdded, maps.getNamespaceURI());
Header holder = new Header(new QName(lastAdded.getNamespaceURI(), lastAdded.getLocalName()), lastAdded);
msg.getHeaders().add(holder);
} else {
LOG.log(Level.WARNING, "IGNORE_NON_ELEMENT_REF_PARAM_MSG", o);
}
}
}
}
}
Aggregations