Search in sources :

Example 16 with SOAPPart

use of javax.xml.soap.SOAPPart in project cxf by apache.

the class W3CDOMStreamReaderTest method testReader.

@Test
public void testReader() throws Exception {
    ByteArrayInputStream is = new ByteArrayInputStream("<Test xmlns=\"http://example.org/types\"><argument>foobar</argument></Test>".getBytes());
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware(true);
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage msg = factory.createMessage();
    SOAPPart part = msg.getSOAPPart();
    Document doc = docBuilder.parse(is);
    W3CDOMStreamWriter writer = new W3CDOMStreamWriter(part.getEnvelope());
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(new DOMSource(doc));
    StaxUtils.copy(reader, writer);
    assertTrue(StaxUtils.toString(writer.getDocument()).endsWith(RESULT));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) MessageFactory(javax.xml.soap.MessageFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SOAPPart(javax.xml.soap.SOAPPart) Document(org.w3c.dom.Document) SOAPMessage(javax.xml.soap.SOAPMessage) Test(org.junit.Test)

Example 17 with SOAPPart

use of javax.xml.soap.SOAPPart in project cxf by apache.

the class SoapFaultSerializerTest method testCXF4181.

@Test
public void testCXF4181() throws Exception {
    // Try WITH SAAJ
    SoapMessage m = new SoapMessage(new MessageImpl());
    m.put(Message.HTTP_REQUEST_METHOD, "POST");
    m.setVersion(Soap12.getInstance());
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(this.getClass().getResourceAsStream("cxf4181.xml"));
    m.setContent(XMLStreamReader.class, reader);
    new SAAJPreInInterceptor().handleMessage(m);
    new ReadHeadersInterceptor(null).handleMessage(m);
    new StartBodyInterceptor().handleMessage(m);
    new SAAJInInterceptor().handleMessage(m);
    new Soap12FaultInInterceptor().handleMessage(m);
    Node nd = m.getContent(Node.class);
    SOAPPart part = (SOAPPart) nd;
    assertEquals("S", part.getEnvelope().getPrefix());
    assertEquals("S2", part.getEnvelope().getHeader().getPrefix());
    assertEquals("S3", part.getEnvelope().getBody().getPrefix());
    SOAPFault fault = part.getEnvelope().getBody().getFault();
    assertEquals("S", fault.getPrefix());
    assertEquals("Authentication Failure", fault.getFaultString());
    SoapFault fault2 = (SoapFault) m.getContent(Exception.class);
    assertNotNull(fault2);
    assertEquals(Soap12.getInstance().getSender(), fault2.getFaultCode());
    assertEquals(new QName("http://schemas.xmlsoap.org/ws/2005/02/trust", "FailedAuthentication"), fault2.getSubCode());
    Element el = part.getEnvelope().getBody();
    nd = el.getFirstChild();
    int count = 0;
    while (nd != null) {
        if (nd instanceof Element) {
            count++;
        }
        nd = nd.getNextSibling();
    }
    assertEquals(1, count);
    // Try WITHOUT SAAJ
    m = new SoapMessage(new MessageImpl());
    m.setVersion(Soap12.getInstance());
    reader = StaxUtils.createXMLStreamReader(this.getClass().getResourceAsStream("cxf4181.xml"));
    m.setContent(XMLStreamReader.class, reader);
    m.put(Message.HTTP_REQUEST_METHOD, "POST");
    new ReadHeadersInterceptor(null).handleMessage(m);
    new StartBodyInterceptor().handleMessage(m);
    new Soap12FaultInInterceptor().handleMessage(m);
    // nd = m.getContent(Node.class);
    fault2 = (SoapFault) m.getContent(Exception.class);
    assertNotNull(fault2);
    assertEquals(Soap12.getInstance().getSender(), fault2.getFaultCode());
    assertEquals(new QName("http://schemas.xmlsoap.org/ws/2005/02/trust", "FailedAuthentication"), fault2.getSubCode());
}
Also used : SAAJPreInInterceptor(org.apache.cxf.binding.soap.saaj.SAAJInInterceptor.SAAJPreInInterceptor) SoapFault(org.apache.cxf.binding.soap.SoapFault) XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SAAJInInterceptor(org.apache.cxf.binding.soap.saaj.SAAJInInterceptor) SOAPPart(javax.xml.soap.SOAPPart) SOAPFault(javax.xml.soap.SOAPFault) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 18 with SOAPPart

use of javax.xml.soap.SOAPPart in project cxf by apache.

the class SAAJInInterceptor method handleMessage.

@SuppressWarnings("unchecked")
public void handleMessage(SoapMessage message) throws Fault {
    if (isGET(message)) {
        return;
    }
    Boolean bodySet = (Boolean) message.get(BODY_FILLED_IN);
    if (Boolean.TRUE.equals(bodySet)) {
        return;
    }
    message.put(BODY_FILLED_IN, Boolean.TRUE);
    try {
        SOAPMessage soapMessage = message.getContent(SOAPMessage.class);
        if (soapMessage == null) {
            MessageFactory factory = preInterceptor.getFactory(message);
            soapMessage = factory.createMessage();
            message.setContent(SOAPMessage.class, soapMessage);
        }
        XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
        if (xmlReader == null) {
            return;
        }
        final SOAPPart part = soapMessage.getSOAPPart();
        Document node = (Document) message.getContent(Node.class);
        if (node != part && node != null) {
            StaxUtils.copy(node, new SAAJStreamWriter(part));
        } else {
            SOAPEnvelope env = soapMessage.getSOAPPart().getEnvelope();
            if (node == null) {
                adjustPrefixes(env, (String) message.get(ReadHeadersInterceptor.ENVELOPE_PREFIX), (String) message.get(ReadHeadersInterceptor.BODY_PREFIX));
            }
            List<XMLEvent> events = (List<XMLEvent>) message.get(ReadHeadersInterceptor.ENVELOPE_EVENTS);
            applyEvents(events, env);
            SOAPBody body = soapMessage.getSOAPBody();
            events = (List<XMLEvent>) message.get(ReadHeadersInterceptor.BODY_EVENTS);
            applyEvents(events, body);
        }
        message.setContent(Node.class, soapMessage.getSOAPPart());
        Collection<Attachment> atts = message.getAttachments();
        if (atts != null) {
            for (Attachment a : atts) {
                if (a.getDataHandler().getDataSource() instanceof AttachmentDataSource) {
                    try {
                        ((AttachmentDataSource) a.getDataHandler().getDataSource()).cache(message);
                    } catch (IOException e) {
                        throw new Fault(e);
                    }
                }
                AttachmentPart ap = soapMessage.createAttachmentPart(a.getDataHandler());
                Iterator<String> i = a.getHeaderNames();
                while (i != null && i.hasNext()) {
                    String h = i.next();
                    String val = a.getHeader(h);
                    ap.addMimeHeader(h, val);
                }
                if (StringUtils.isEmpty(ap.getContentId())) {
                    ap.setContentId(a.getId());
                }
                soapMessage.addAttachmentPart(ap);
            }
        }
        // replace header element if necessary
        if (message.hasHeaders()) {
            replaceHeaders(soapMessage, message);
        }
        if (soapMessage.getSOAPPart().getEnvelope().getHeader() == null) {
            soapMessage.getSOAPPart().getEnvelope().addHeader();
        }
        // If we have an xmlReader that already is counting the attributes and such
        // then we don't want to rely on the system level defaults in StaxUtils.copy
        // CXF-6173
        boolean secureReader = StaxUtils.isSecureReader(xmlReader, message);
        StaxUtils.copy(xmlReader, new SAAJStreamWriter(soapMessage.getSOAPPart(), soapMessage.getSOAPPart().getEnvelope().getBody()), true, !secureReader);
        DOMSource bodySource = new DOMSource(soapMessage.getSOAPPart().getEnvelope().getBody());
        xmlReader = StaxUtils.createXMLStreamReader(bodySource);
        xmlReader.nextTag();
        // move past body tag
        xmlReader.nextTag();
        message.setContent(XMLStreamReader.class, xmlReader);
    } catch (SOAPException soape) {
        throw new SoapFault(new org.apache.cxf.common.i18n.Message("SOAPHANDLERINTERCEPTOR_EXCEPTION", BUNDLE), soape, message.getVersion().getSender());
    } catch (XMLStreamException e) {
        throw new SoapFault(new org.apache.cxf.common.i18n.Message("SOAPHANDLERINTERCEPTOR_EXCEPTION", BUNDLE), e, message.getVersion().getSender());
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) SoapFault(org.apache.cxf.binding.soap.SoapFault) XMLStreamReader(javax.xml.stream.XMLStreamReader) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) Message(org.apache.cxf.message.Message) SOAPMessage(javax.xml.soap.SOAPMessage) Node(org.w3c.dom.Node) AttachmentDataSource(org.apache.cxf.attachment.AttachmentDataSource) Attachment(org.apache.cxf.message.Attachment) SoapFault(org.apache.cxf.binding.soap.SoapFault) Fault(org.apache.cxf.interceptor.Fault) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) Document(org.w3c.dom.Document) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPException(javax.xml.soap.SOAPException) SOAPPart(javax.xml.soap.SOAPPart) List(java.util.List) ArrayList(java.util.ArrayList) MessageFactory(javax.xml.soap.MessageFactory) AttachmentPart(javax.xml.soap.AttachmentPart) IOException(java.io.IOException) SOAPBody(javax.xml.soap.SOAPBody) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 19 with SOAPPart

use of javax.xml.soap.SOAPPart in project pentaho-platform by pentaho.

the class XMLABaseComponent method discover.

/**
 * discover
 *
 * @param request
 * @param discoverUrl
 * @param restrictions
 * @param properties
 * @param rh
 * @throws XMLAException
 */
private void discover(final String request, final URL discoverUrl, final Map restrictions, final Map properties, final Rowhandler rh) throws XMLAException {
    try {
        SOAPConnection connection = scf.createConnection();
        SOAPMessage msg = mf.createMessage();
        MimeHeaders mh = msg.getMimeHeaders();
        // $NON-NLS-1$ //$NON-NLS-2$
        mh.setHeader("SOAPAction", "\"urn:schemas-microsoft-com:xml-analysis:Discover\"");
        SOAPPart soapPart = msg.getSOAPPart();
        SOAPEnvelope envelope = soapPart.getEnvelope();
        // $NON-NLS-1$//$NON-NLS-2$
        envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        // $NON-NLS-1$ //$NON-NLS-2$
        envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
        SOAPBody body = envelope.getBody();
        // $NON-NLS-1$//$NON-NLS-2$
        Name nDiscover = envelope.createName("Discover", "", XMLABaseComponent.XMLA_URI);
        SOAPElement eDiscover = body.addChildElement(nDiscover);
        eDiscover.setEncodingStyle(XMLABaseComponent.ENCODING_STYLE);
        // $NON-NLS-1$//$NON-NLS-2$
        Name nPara = envelope.createName("RequestType", "", XMLABaseComponent.XMLA_URI);
        SOAPElement eRequestType = eDiscover.addChildElement(nPara);
        eRequestType.addTextNode(request);
        // add the parameters
        if (restrictions != null) {
            addParameterList(envelope, eDiscover, "Restrictions", "RestrictionList", // $NON-NLS-1$ //$NON-NLS-2$
            restrictions);
        }
        // $NON-NLS-1$//$NON-NLS-2$
        addParameterList(envelope, eDiscover, "Properties", "PropertyList", properties);
        msg.saveChanges();
        debug(// $NON-NLS-1$
        Messages.getInstance().getString("XMLABaseComponent.DEBUG_0006_DISCOVER_REQUEST") + request);
        logSoapMsg(msg);
        // run the call
        SOAPMessage reply = connection.call(msg, discoverUrl);
        debug(// $NON-NLS-1$
        Messages.getInstance().getString("XMLABaseComponent.DEBUG_0007_DISCOVER_RESPONSE") + request);
        logSoapMsg(reply);
        errorCheck(reply);
        SOAPElement eRoot = findDiscoverRoot(reply);
        // $NON-NLS-1$ //$NON-NLS-2$
        Name nRow = envelope.createName("row", "", XMLABaseComponent.ROWS_URI);
        Iterator itRow = eRoot.getChildElements(nRow);
        while (itRow.hasNext()) {
            // RowLoop
            SOAPElement eRow = (SOAPElement) itRow.next();
            rh.handleRow(eRow, envelope);
        }
        // RowLoop
        connection.close();
    } catch (UnsupportedOperationException e) {
        throw new XMLAException(e);
    } catch (SOAPException e) {
        throw new XMLAException(e);
    }
}
Also used : MimeHeaders(javax.xml.soap.MimeHeaders) SOAPBody(javax.xml.soap.SOAPBody) SOAPException(javax.xml.soap.SOAPException) SOAPPart(javax.xml.soap.SOAPPart) SOAPElement(javax.xml.soap.SOAPElement) Iterator(java.util.Iterator) SOAPConnection(javax.xml.soap.SOAPConnection) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) SOAPMessage(javax.xml.soap.SOAPMessage) Name(javax.xml.soap.Name)

Example 20 with SOAPPart

use of javax.xml.soap.SOAPPart in project pentaho-platform by pentaho.

the class XMLABaseComponent method soapFault.

/**
 * check SOAP reply for Error, return fault Code
 *
 * @param reply   the message to check
 * @param aReturn ArrayList containing faultcode,faultstring,faultactor
 */
private boolean soapFault(final SOAPMessage reply, final String[] faults) throws SOAPException {
    SOAPPart sp = reply.getSOAPPart();
    SOAPEnvelope envelope = sp.getEnvelope();
    SOAPBody body = envelope.getBody();
    if (!body.hasFault()) {
        return false;
    }
    SOAPFault fault = body.getFault();
    faults[0] = fault.getFaultCode();
    faults[1] = fault.getFaultString();
    faults[2] = fault.getFaultActor();
    // probably not neccessary with Microsoft;
    Detail detail = fault.getDetail();
    if (detail == null) {
        return true;
    }
    // $NON-NLS-1$
    String detailMsg = "";
    Iterator it = detail.getDetailEntries();
    for (; it.hasNext(); ) {
        DetailEntry det = (DetailEntry) it.next();
        Iterator ita = det.getAllAttributes();
        for (boolean cont = false; ita.hasNext(); cont = true) {
            Name name = (Name) ita.next();
            if (cont) {
                // $NON-NLS-1$
                detailMsg += "; ";
            }
            detailMsg += name.getLocalName();
            // $NON-NLS-1$
            detailMsg += " = ";
            detailMsg += det.getAttributeValue(name);
        }
    }
    faults[3] = detailMsg;
    return true;
}
Also used : SOAPBody(javax.xml.soap.SOAPBody) DetailEntry(javax.xml.soap.DetailEntry) SOAPPart(javax.xml.soap.SOAPPart) Iterator(java.util.Iterator) SOAPFault(javax.xml.soap.SOAPFault) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) Detail(javax.xml.soap.Detail) Name(javax.xml.soap.Name)

Aggregations

SOAPPart (javax.xml.soap.SOAPPart)45 SOAPMessage (javax.xml.soap.SOAPMessage)30 SOAPBody (javax.xml.soap.SOAPBody)28 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)26 SOAPException (javax.xml.soap.SOAPException)26 MessageFactory (javax.xml.soap.MessageFactory)17 SOAPElement (javax.xml.soap.SOAPElement)14 Iterator (java.util.Iterator)13 Element (org.w3c.dom.Element)13 StreamSource (javax.xml.transform.stream.StreamSource)12 QName (javax.xml.namespace.QName)11 IOException (java.io.IOException)10 InputStream (java.io.InputStream)10 Name (javax.xml.soap.Name)10 NodeList (org.w3c.dom.NodeList)9 BufferedWriter (java.io.BufferedWriter)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 OutputStreamWriter (java.io.OutputStreamWriter)7 HttpURLConnection (java.net.HttpURLConnection)7 SOAPHeader (javax.xml.soap.SOAPHeader)6