use of org.apache.cxf.staxutils.PartialXMLStreamReader in project cxf by apache.
the class DocLiteralInInterceptorTest method testUnmarshalSourceDataWrapped.
@Test
public void testUnmarshalSourceDataWrapped() throws Exception {
XMLStreamReader reader = StaxUtils.createXMLStreamReader(getClass().getResourceAsStream("resources/docLitWrappedReq.xml"));
assertEquals(XMLStreamConstants.START_ELEMENT, reader.nextTag());
XMLStreamReader filteredReader = new PartialXMLStreamReader(reader, new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"));
// advance the xml reader to the message parts
StaxUtils.read(filteredReader);
assertEquals(XMLStreamConstants.START_ELEMENT, reader.nextTag());
Message m = new MessageImpl();
// request to keep the document as wrapped
m.put(DocLiteralInInterceptor.KEEP_PARAMETERS_WRAPPER, true);
Exchange exchange = new ExchangeImpl();
Service service = control.createMock(Service.class);
exchange.put(Service.class, service);
EasyMock.expect(service.getDataBinding()).andReturn(new SourceDataBinding()).anyTimes();
EasyMock.expect(service.size()).andReturn(0).anyTimes();
EasyMock.expect(service.isEmpty()).andReturn(true).anyTimes();
Endpoint endpoint = control.createMock(Endpoint.class);
exchange.put(Endpoint.class, endpoint);
// wrapped
OperationInfo operationInfo = new OperationInfo();
MessageInfo messageInfo = new MessageInfo(operationInfo, Type.INPUT, new QName(NS, "foo"));
messageInfo.addMessagePart(new MessagePartInfo(new QName(NS, "personId"), null));
messageInfo.addMessagePart(new MessagePartInfo(new QName(NS, "ssn"), null));
messageInfo.getMessagePart(0).setConcreteName(new QName(NS, "personId"));
messageInfo.getMessagePart(1).setConcreteName(new QName(NS, "ssn"));
operationInfo.setInput("inputName", messageInfo);
// wrapper
OperationInfo operationInfoWrapper = new OperationInfo();
MessageInfo messageInfoWrapper = new MessageInfo(operationInfo, Type.INPUT, new QName(NS, "foo"));
messageInfoWrapper.addMessagePart(new MessagePartInfo(new QName(NS, "GetPerson"), null));
messageInfoWrapper.getMessagePart(0).setConcreteName(new QName(NS, "GetPerson"));
operationInfoWrapper.setInput("inputName", messageInfoWrapper);
operationInfoWrapper.setUnwrappedOperation(operationInfo);
ServiceInfo serviceInfo = control.createMock(ServiceInfo.class);
EasyMock.expect(serviceInfo.getName()).andReturn(new QName("http://foo.com", "service")).anyTimes();
InterfaceInfo interfaceInfo = control.createMock(InterfaceInfo.class);
EasyMock.expect(serviceInfo.getInterface()).andReturn(interfaceInfo).anyTimes();
EasyMock.expect(interfaceInfo.getName()).andReturn(new QName("http://foo.com", "interface")).anyTimes();
BindingInfo bindingInfo = new BindingInfo(serviceInfo, "");
BindingOperationInfo boi = new BindingOperationInfo(bindingInfo, operationInfoWrapper);
exchange.put(BindingOperationInfo.class, boi);
EndpointInfo endpointInfo = control.createMock(EndpointInfo.class);
BindingInfo binding = control.createMock(BindingInfo.class);
EasyMock.expect(endpoint.getEndpointInfo()).andReturn(endpointInfo).anyTimes();
EasyMock.expect(endpointInfo.getBinding()).andReturn(binding).anyTimes();
EasyMock.expect(binding.getProperties()).andReturn(new HashMap<String, Object>()).anyTimes();
EasyMock.expect(endpointInfo.getProperties()).andReturn(new HashMap<String, Object>()).anyTimes();
EasyMock.expect(endpoint.size()).andReturn(0).anyTimes();
EasyMock.expect(endpoint.isEmpty()).andReturn(true).anyTimes();
EasyMock.expect(endpointInfo.getService()).andReturn(serviceInfo).anyTimes();
EasyMock.expect(endpointInfo.getName()).andReturn(new QName("http://foo.com", "endpoint")).anyTimes();
EasyMock.expect(endpointInfo.getProperty("URI", URI.class)).andReturn(new URI("dummy")).anyTimes();
List<OperationInfo> operations = new ArrayList<>();
EasyMock.expect(interfaceInfo.getOperations()).andReturn(operations).anyTimes();
m.setExchange(exchange);
m.put(Message.SCHEMA_VALIDATION_ENABLED, false);
m.setContent(XMLStreamReader.class, reader);
control.replay();
new DocLiteralInInterceptor().handleMessage(m);
MessageContentsList params = (MessageContentsList) m.getContent(List.class);
// we expect a wrapped document
assertEquals(1, params.size());
Map<String, String> ns = new HashMap<>();
ns.put("ns", NS);
XPathUtils xu = new XPathUtils(ns);
assertEquals("hello", xu.getValueString("//ns:GetPerson/ns:personId", ((DOMSource) params.get(0)).getNode().getFirstChild()));
assertEquals("1234", xu.getValueString("//ns:GetPerson/ns:ssn", ((DOMSource) params.get(0)).getNode().getFirstChild()));
}
use of org.apache.cxf.staxutils.PartialXMLStreamReader in project cxf by apache.
the class InTransformReaderTest method testReadPartialWithComplexRequestSameNamespace.
@Test
public void testReadPartialWithComplexRequestSameNamespace() throws Exception {
XMLStreamReader reader = StaxUtils.createXMLStreamReader(InTransformReader.class.getResourceAsStream("../resources/complexReqIn1.xml"));
Map<String, String> inMap = new HashMap<>();
inMap.put("{http://cxf.apache.org/transform/header/element}*", "{http://cxf.apache.org/transform/header/element}*");
reader = new InTransformReader(reader, inMap, null, null, null, false);
QName bodyTag = new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body");
PartialXMLStreamReader filteredReader = new PartialXMLStreamReader(reader, bodyTag);
XMLStreamReader reader2 = StaxUtils.createXMLStreamReader(InTransformReader.class.getResourceAsStream("../resources/complexReq1partial.xml"));
TransformTestUtils.verifyReaders(reader2, filteredReader, false, true);
}
use of org.apache.cxf.staxutils.PartialXMLStreamReader in project cxf by apache.
the class ReadHeadersInterceptor method handleMessage.
// CHECKSTYLE:OFF MethodLength
public void handleMessage(SoapMessage message) {
if (isGET(message)) {
LOG.fine("ReadHeadersInterceptor skipped in HTTP GET method");
return;
}
/*
* Reject OPTIONS, and any other noise that is not allowed in SOAP.
*/
final String verb = (String) message.get(org.apache.cxf.message.Message.HTTP_REQUEST_METHOD);
if (verb != null && !"POST".equals(verb)) {
Fault formula405 = new Fault("HTTP verb was not GET or POST", LOG);
formula405.setStatusCode(405);
throw formula405;
}
XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
boolean closeNeeded = false;
if (xmlReader == null) {
InputStream in = message.getContent(InputStream.class);
if (in == null) {
throw new RuntimeException("Can't find input stream in message");
}
xmlReader = StaxUtils.createXMLStreamReader(in);
closeNeeded = true;
}
try {
if (xmlReader.getEventType() == XMLStreamConstants.START_ELEMENT || xmlReader.nextTag() == XMLStreamConstants.START_ELEMENT) {
SoapVersion soapVersion = readVersion(xmlReader, message);
if (soapVersion == Soap12.getInstance() && version == Soap11.getInstance()) {
message.setVersion(version);
throw new SoapFault(new Message("INVALID_11_VERSION", LOG), version.getVersionMismatch());
}
XMLStreamReader filteredReader = new PartialXMLStreamReader(xmlReader, message.getVersion().getBody());
Node nd = message.getContent(Node.class);
W3CDOMStreamWriter writer = message.get(W3CDOMStreamWriter.class);
Document doc = null;
if (writer != null) {
StaxUtils.copy(filteredReader, writer);
doc = writer.getDocument();
} else if (nd instanceof Document) {
doc = (Document) nd;
StaxUtils.readDocElements(doc, doc, filteredReader, false, false);
} else {
final boolean addNC = MessageUtils.getContextualBoolean(message, "org.apache.cxf.binding.soap.addNamespaceContext", false);
Map<String, String> bodyNC = addNC ? new HashMap<String, String>() : null;
if (addNC) {
// add the Envelope-Level declarations
addCurrentNamespaceDecls(xmlReader, bodyNC);
}
HeadersProcessor processor = new HeadersProcessor(soapVersion);
doc = processor.process(filteredReader);
if (doc != null) {
message.setContent(Node.class, doc);
} else {
message.put(ENVELOPE_EVENTS, processor.getEnvAttributeAndNamespaceEvents());
message.put(BODY_EVENTS, processor.getBodyAttributeAndNamespaceEvents());
message.put(ENVELOPE_PREFIX, processor.getEnvelopePrefix());
message.put(BODY_PREFIX, processor.getBodyPrefix());
}
if (addNC) {
// add the Body-level declarations
addCurrentNamespaceDecls(xmlReader, bodyNC);
message.put("soap.body.ns.context", bodyNC);
}
}
// Find header
if (doc != null) {
Element element = doc.getDocumentElement();
QName header = soapVersion.getHeader();
List<Element> elemList = DOMUtils.findAllElementsByTagNameNS(element, header.getNamespaceURI(), header.getLocalPart());
for (Element elem : elemList) {
Element hel = DOMUtils.getFirstElement(elem);
while (hel != null) {
// which otherwise would be lost.
if (elem.hasAttributes()) {
NamedNodeMap nnp = elem.getAttributes();
for (int ct = 0; ct < nnp.getLength(); ct++) {
Node attr = nnp.item(ct);
Node headerAttrNode = hel.hasAttributes() ? hel.getAttributes().getNamedItemNS(attr.getNamespaceURI(), attr.getLocalName()) : null;
if (headerAttrNode == null) {
Attr attribute = hel.getOwnerDocument().createAttributeNS(attr.getNamespaceURI(), attr.getNodeName());
attribute.setNodeValue(attr.getNodeValue());
hel.setAttributeNodeNS(attribute);
}
}
}
HeaderProcessor p = bus == null ? null : bus.getExtension(HeaderManager.class).getHeaderProcessor(hel.getNamespaceURI());
Object obj;
DataBinding dataBinding = null;
if (p == null || p.getDataBinding() == null) {
obj = hel;
} else {
dataBinding = p.getDataBinding();
DataReader<Node> dataReader = dataBinding.createReader(Node.class);
dataReader.setAttachments(message.getAttachments());
dataReader.setProperty(DataReader.ENDPOINT, message.getExchange().getEndpoint());
dataReader.setProperty(Message.class.getName(), message);
obj = dataReader.read(hel);
}
SoapHeader shead = new SoapHeader(new QName(hel.getNamespaceURI(), hel.getLocalName()), obj, dataBinding);
String mu = hel.getAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameMustUnderstand());
String act = hel.getAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameRole());
if (!StringUtils.isEmpty(act)) {
shead.setActor(act);
}
shead.setMustUnderstand(Boolean.valueOf(mu) || "1".equals(mu));
// mark header as inbound header.(for distinguishing between the direction to
// avoid piggybacking of headers from request->server->response.
shead.setDirection(SoapHeader.Direction.DIRECTION_IN);
message.getHeaders().add(shead);
hel = DOMUtils.getNextElement(hel);
}
}
}
if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.IN, message)) {
message.getInterceptorChain().add(new CheckClosingTagsInterceptor());
}
}
} catch (XMLStreamException e) {
throw new SoapFault(new Message("XML_STREAM_EXC", LOG, e.getMessage()), e, message.getVersion().getSender());
} finally {
if (closeNeeded) {
try {
StaxUtils.close(xmlReader);
} catch (XMLStreamException e) {
throw new SoapFault(new Message("XML_STREAM_EXC", LOG, e.getMessage()), e, message.getVersion().getSender());
}
}
}
}
Aggregations