Search in sources :

Example 16 with MessageFactory

use of javax.xml.soap.MessageFactory in project jaffa-framework by jaffa-projects.

the class WebServiceInvoker method createSOAPMessage.

/**
 * Generate SOAPMessage based on the input arguments.
 * <p>
 * For example:
 *   <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:j="http://product1.mirotechnologies.com/material/core/StockBalanceFinder">
 *     <SOAP-ENV:Header/>
 *     <SOAP-ENV:Body>
 *       <j:performInquiry>
 *         <arg0>
 *           <part><operator>Equals</operator><values>P1000</values></part>
 *         </arg0>
 *       </j:performInquiry>
 *     </SOAP-ENV:Body>
 *   </SOAP-ENV:Envelope>
 * @param arguments the arguments for the WebService.
 * @return a SOAPMessage.
 * @throws SOAPException if any SOAP error occurs.
 * @throws JAXBException if any XML (un)marshalling error occurs.
 */
protected SOAPMessage createSOAPMessage(Object... arguments) throws SOAPException, JAXBException {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage message = messageFactory.createMessage();
    SOAPPart messagePart = message.getSOAPPart();
    SOAPEnvelope envelope = messagePart.getEnvelope();
    String tns = this.obtainTargetNamespace();
    if (tns != null) {
        envelope.addNamespaceDeclaration(CUSTOM_PREFIX, tns);
    }
    SOAPBody body = message.getSOAPBody();
    SOAPElement operationElement = tns != null ? body.addBodyElement(envelope.createQName(getOperationName(), CUSTOM_PREFIX)) : body.addBodyElement(envelope.createName(getOperationName()));
    // Add authentication
    addAuthorization(message);
    // An arg{i} node will be created for each argument
    if (arguments != null) {
        Annotation[][] parameterAnnotations = null;
        for (Method m : getWebServiceClass().getMethods()) {
            if (m.getName().equals(getOperationName()) && m.getAnnotation(WebMethod.class) != null) {
                parameterAnnotations = m.getParameterAnnotations();
            }
        }
        for (int i = 0; i < arguments.length; i++) {
            Object argument = arguments[i];
            Class argumentClass = argument.getClass();
            String webParamName = null;
            if (parameterAnnotations != null && parameterAnnotations.length > 0) {
                for (Annotation[] annotations : parameterAnnotations) {
                    for (Annotation annotation : annotations) {
                        if (annotation instanceof WebParam) {
                            WebParam webParam = (WebParam) annotation;
                            webParamName = webParam.name();
                            if (log.isDebugEnabled()) {
                                log.debug("webParamName :" + webParamName);
                            }
                        }
                    }
                }
            }
            if (Collection.class.isAssignableFrom(argumentClass)) {
                JAXBContext jc = null;
                Marshaller marshaller = null;
                List list = (List) argument;
                for (Object arrayElement : list) {
                    if (jc == null) {
                        jc = JAXBHelper.obtainJAXBContext(arrayElement.getClass());
                        marshaller = jc.createMarshaller();
                    }
                    if (webParamName != null) {
                        marshaller.marshal(new JAXBElement(new QName(webParamName), arrayElement.getClass(), arrayElement), operationElement);
                    } else {
                        marshaller.marshal(new JAXBElement(new QName("arg0"), arrayElement.getClass(), arrayElement), operationElement);
                    }
                }
            } else if (argumentClass.isArray()) {
                argumentClass = argumentClass.getComponentType();
                JAXBContext jc = JAXBHelper.obtainJAXBContext(argumentClass);
                Marshaller marshaller = jc.createMarshaller();
                for (int j = 0, len = Array.getLength(argument); j < len; j++) {
                    Object arrayElement = Array.get(argument, j);
                    if (webParamName != null) {
                        marshaller.marshal(new JAXBElement(new QName(webParamName), argumentClass, arrayElement), operationElement);
                    } else {
                        marshaller.marshal(new JAXBElement(new QName("arg0"), argumentClass, arrayElement), operationElement);
                    }
                }
            } else {
                JAXBContext jc = JAXBHelper.obtainJAXBContext(argumentClass);
                Marshaller marshaller = jc.createMarshaller();
                if (webParamName != null) {
                    marshaller.marshal(new JAXBElement(new QName(webParamName), argumentClass, argument), operationElement);
                } else {
                    marshaller.marshal(new JAXBElement(new QName("arg0"), argumentClass, argument), operationElement);
                }
            }
        }
    }
    // Save all changes to the Message
    message.saveChanges();
    if (log.isDebugEnabled()) {
        log.debug("Created SOAPMessage: " + message);
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            message.writeTo(os);
            log.debug("Contents of SOAPMessage: " + os.toString());
        } catch (Exception e) {
        // do nothing
        }
    }
    return message;
}
Also used : Marshaller(javax.xml.bind.Marshaller) MessageFactory(javax.xml.soap.MessageFactory) QName(javax.xml.namespace.QName) JAXBContext(javax.xml.bind.JAXBContext) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) Method(java.lang.reflect.Method) WebMethod(javax.jws.WebMethod) JAXBElement(javax.xml.bind.JAXBElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SOAPMessage(javax.xml.soap.SOAPMessage) Annotation(java.lang.annotation.Annotation) SOAPException(javax.xml.soap.SOAPException) JAXBException(javax.xml.bind.JAXBException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) SOAPBody(javax.xml.soap.SOAPBody) WebParam(javax.jws.WebParam) SOAPPart(javax.xml.soap.SOAPPart) SOAPElement(javax.xml.soap.SOAPElement) NodeList(org.w3c.dom.NodeList) List(java.util.List)

Example 17 with MessageFactory

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

the class SOAPHandlerInterceptorTest method prepareSOAPMessage.

private SOAPMessage prepareSOAPMessage(String resouceName) throws Exception {
    InputStream is = this.getClass().getResourceAsStream(resouceName);
    MessageFactory factory = MessageFactory.newInstance();
    MimeHeaders mhs = null;
    return factory.createMessage(mhs, is);
}
Also used : MimeHeaders(javax.xml.soap.MimeHeaders) MessageFactory(javax.xml.soap.MessageFactory) InputStream(java.io.InputStream)

Example 18 with MessageFactory

use of javax.xml.soap.MessageFactory 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 19 with MessageFactory

use of javax.xml.soap.MessageFactory 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 20 with MessageFactory

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

the class SAAJFactoryResolver method createMessageFactory.

public static MessageFactory createMessageFactory(SoapVersion version) throws SOAPException {
    MessageFactory messageFactory;
    String messageFactoryClassName = SystemPropertyAction.getPropertyOrNull(MESSAGE_FACTORY_KEY);
    if (messageFactoryClassName != null) {
        messageFactory = newInstanceCxfSAAJFactory(messageFactoryClassName, MessageFactory.class);
    } else if (version instanceof Soap11) {
        try {
            messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
        } catch (Throwable t) {
            messageFactory = MessageFactory.newInstance();
        }
    } else if (version instanceof Soap12) {
        try {
            messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
        } catch (Throwable t) {
            messageFactory = MessageFactory.newInstance();
        }
    } else {
        messageFactory = MessageFactory.newInstance();
    }
    return messageFactory;
}
Also used : Soap12(org.apache.cxf.binding.soap.Soap12) MessageFactory(javax.xml.soap.MessageFactory) Soap11(org.apache.cxf.binding.soap.Soap11)

Aggregations

MessageFactory (javax.xml.soap.MessageFactory)70 SOAPMessage (javax.xml.soap.SOAPMessage)65 URL (java.net.URL)24 InputStream (java.io.InputStream)22 QName (javax.xml.namespace.QName)22 SOAPException (javax.xml.soap.SOAPException)22 Test (org.junit.Test)21 ByteArrayInputStream (java.io.ByteArrayInputStream)19 SOAPBody (javax.xml.soap.SOAPBody)18 SOAPPart (javax.xml.soap.SOAPPart)17 StreamSource (javax.xml.transform.stream.StreamSource)14 SOAPConnection (javax.xml.soap.SOAPConnection)13 IOException (java.io.IOException)11 SOAPElement (javax.xml.soap.SOAPElement)11 RunAsClient (org.jboss.arquillian.container.test.api.RunAsClient)10 JBossWSTest (org.jboss.wsf.test.JBossWSTest)10 NodeList (org.w3c.dom.NodeList)9 AttachmentPart (javax.xml.soap.AttachmentPart)8 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)8 Element (org.w3c.dom.Element)8