Search in sources :

Example 6 with PartialXMLStreamReader

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()));
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) OperationInfo(org.apache.cxf.service.model.OperationInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) XMLStreamReader(javax.xml.stream.XMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) Message(org.apache.cxf.message.Message) MessageContentsList(org.apache.cxf.message.MessageContentsList) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SourceDataBinding(org.apache.cxf.databinding.source.SourceDataBinding) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) URI(java.net.URI) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) BindingInfo(org.apache.cxf.service.model.BindingInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) ArrayList(java.util.ArrayList) List(java.util.List) QName(javax.xml.namespace.QName) Service(org.apache.cxf.service.Service) MessageInfo(org.apache.cxf.service.model.MessageInfo) Exchange(org.apache.cxf.message.Exchange) XPathUtils(org.apache.cxf.helpers.XPathUtils) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 7 with PartialXMLStreamReader

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);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Test(org.junit.Test)

Example 8 with PartialXMLStreamReader

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());
            }
        }
    }
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) SoapFault(org.apache.cxf.binding.soap.SoapFault) XMLStreamReader(javax.xml.stream.XMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) Message(org.apache.cxf.common.i18n.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) HashMap(java.util.HashMap) HeaderProcessor(org.apache.cxf.headers.HeaderProcessor) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Fault(org.apache.cxf.interceptor.Fault) SoapFault(org.apache.cxf.binding.soap.SoapFault) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr) NamedNodeMap(org.w3c.dom.NamedNodeMap) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) SoapVersion(org.apache.cxf.binding.soap.SoapVersion) XMLStreamException(javax.xml.stream.XMLStreamException) DataBinding(org.apache.cxf.databinding.DataBinding) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) HashMap(java.util.HashMap) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Aggregations

QName (javax.xml.namespace.QName)8 XMLStreamReader (javax.xml.stream.XMLStreamReader)8 PartialXMLStreamReader (org.apache.cxf.staxutils.PartialXMLStreamReader)8 HashMap (java.util.HashMap)6 Test (org.junit.Test)5 SoapVersion (org.apache.cxf.binding.soap.SoapVersion)3 InputStream (java.io.InputStream)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 SoapHeader (org.apache.cxf.binding.soap.SoapHeader)2 SourceDataBinding (org.apache.cxf.databinding.source.SourceDataBinding)2 Endpoint (org.apache.cxf.endpoint.Endpoint)2 Exchange (org.apache.cxf.message.Exchange)2 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)2 Message (org.apache.cxf.message.Message)2 MessageContentsList (org.apache.cxf.message.MessageContentsList)2 MessageImpl (org.apache.cxf.message.MessageImpl)2 Service (org.apache.cxf.service.Service)2 BindingInfo (org.apache.cxf.service.model.BindingInfo)2