Search in sources :

Example 1 with SOAPBuilder

use of org.apache.axis2.builder.SOAPBuilder in project wso2-axis2-transports by wso2.

the class JMSUtils method setSOAPEnvelope.

/**
 * Set the SOAPEnvelope to the Axis2 MessageContext, from the JMS Message passed in
 * @param message the JMS message read
 * @param msgContext the Axis2 MessageContext to be populated
 * @param contentType content type for the message
 * @throws AxisFault
 * @throws JMSException
 */
public static void setSOAPEnvelope(Message message, MessageContext msgContext, String contentType) throws AxisFault, JMSException {
    if (contentType == null) {
        if (message instanceof TextMessage) {
            contentType = "text/plain";
        } else {
            contentType = "application/octet-stream";
        }
        if (log.isDebugEnabled()) {
            log.debug("No content type specified; assuming " + contentType);
        }
    }
    int index = contentType.indexOf(';');
    String type = index > 0 ? contentType.substring(0, index) : contentType;
    Builder builder = BuilderUtil.getBuilderFromSelector(type, msgContext);
    if (builder == null) {
        if (log.isDebugEnabled()) {
            log.debug("No message builder found for type '" + type + "'. Falling back to SOAP.");
        }
        builder = new SOAPBuilder();
    }
    OMElement documentElement;
    if (message instanceof BytesMessage) {
        // Extract the charset encoding from the content type and
        // set the CHARACTER_SET_ENCODING property as e.g. SOAPBuilder relies on this.
        String charSetEnc = null;
        try {
            if (contentType != null) {
                charSetEnc = new ContentType(contentType).getParameter("charset");
            }
        } catch (ParseException ex) {
        // ignore
        }
        msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc);
        if (builder instanceof DataSourceMessageBuilder) {
            documentElement = ((DataSourceMessageBuilder) builder).processDocument(new BytesMessageDataSource((BytesMessage) message), contentType, msgContext);
        } else {
            documentElement = builder.processDocument(new BytesMessageInputStream((BytesMessage) message), contentType, msgContext);
        }
    } else if (message instanceof TextMessage) {
        TextMessageBuilder textMessageBuilder;
        if (builder instanceof TextMessageBuilder) {
            textMessageBuilder = (TextMessageBuilder) builder;
        } else {
            textMessageBuilder = new TextMessageBuilderAdapter(builder);
        }
        String content = ((TextMessage) message).getText();
        documentElement = textMessageBuilder.processDocument(content, contentType, msgContext);
    } else if (message instanceof MapMessage) {
        documentElement = convertJMSMapToXML((MapMessage) message);
    } else {
        handleException("Unsupported JMS message type " + message.getClass().getName());
        // Make compiler happy
        return;
    }
    msgContext.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement));
}
Also used : ContentType(javax.mail.internet.ContentType) DataSourceMessageBuilder(org.apache.axis2.format.DataSourceMessageBuilder) Builder(org.apache.axis2.builder.Builder) TextMessageBuilder(org.apache.axis2.format.TextMessageBuilder) SOAPBuilder(org.apache.axis2.builder.SOAPBuilder) MapMessage(javax.jms.MapMessage) OMElement(org.apache.axiom.om.OMElement) BytesMessage(javax.jms.BytesMessage) TextMessageBuilder(org.apache.axis2.format.TextMessageBuilder) TextMessageBuilderAdapter(org.apache.axis2.format.TextMessageBuilderAdapter) BytesMessageDataSource(org.apache.axis2.transport.jms.iowrappers.BytesMessageDataSource) SOAPBuilder(org.apache.axis2.builder.SOAPBuilder) ParseException(javax.mail.internet.ParseException) BytesMessageInputStream(org.apache.axis2.transport.jms.iowrappers.BytesMessageInputStream) TextMessage(javax.jms.TextMessage) DataSourceMessageBuilder(org.apache.axis2.format.DataSourceMessageBuilder)

Example 2 with SOAPBuilder

use of org.apache.axis2.builder.SOAPBuilder in project wso2-synapse by wso2.

the class VFSTransportListener method processFile.

/**
 * Process a single file through Axis2
 * @param entry the PollTableEntry for the file (or its parent directory or archive)
 * @param file the file that contains the actual message pumped into Axis2
 * @throws AxisFault on error
 */
private void processFile(PollTableEntry entry, FileObject file) throws AxisFault {
    try {
        FileContent content = file.getContent();
        String fileName = file.getName().getBaseName();
        String filePath = file.getName().getPath();
        String fileURI = file.getName().getURI();
        metrics.incrementBytesReceived(content.getSize());
        Map<String, Object> transportHeaders = new HashMap<String, Object>();
        transportHeaders.put(VFSConstants.FILE_PATH, filePath);
        transportHeaders.put(VFSConstants.FILE_NAME, fileName);
        transportHeaders.put(VFSConstants.FILE_URI, fileURI);
        try {
            transportHeaders.put(VFSConstants.FILE_LENGTH, content.getSize());
            transportHeaders.put(VFSConstants.LAST_MODIFIED, content.getLastModifiedTime());
        } catch (FileSystemException ignore) {
        }
        MessageContext msgContext = entry.createMessageContext();
        String contentType = entry.getContentType();
        if (BaseUtils.isBlank(contentType)) {
            if (file.getName().getExtension().toLowerCase().endsWith(".xml")) {
                contentType = "text/xml";
            } else if (file.getName().getExtension().toLowerCase().endsWith(".txt")) {
                contentType = "text/plain";
            }
        } else {
            // Extract the charset encoding from the configured content type and
            // set the CHARACTER_SET_ENCODING property as e.g. SOAPBuilder relies on this.
            String charSetEnc = null;
            try {
                if (contentType != null) {
                    charSetEnc = new ContentType(contentType).getParameter("charset");
                }
            } catch (ParseException ex) {
            // ignore
            }
            msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc);
        }
        // if the content type was not found, but the service defined it.. use it
        if (contentType == null) {
            if (entry.getContentType() != null) {
                contentType = entry.getContentType();
            } else if (VFSUtils.getProperty(content, BaseConstants.CONTENT_TYPE) != null) {
                contentType = VFSUtils.getProperty(content, BaseConstants.CONTENT_TYPE);
            }
        }
        // does the service specify a default reply file URI ?
        String replyFileURI = entry.getReplyFileURI();
        if (replyFileURI != null) {
            msgContext.setProperty(Constants.OUT_TRANSPORT_INFO, new VFSOutTransportInfo(replyFileURI, entry.isFileLockingEnabled()));
        }
        // Determine the message builder to use
        Builder builder;
        if (contentType == null) {
            if (log.isDebugEnabled()) {
                log.debug("No content type specified. Using SOAP builder.");
            }
            builder = new SOAPBuilder();
        } else {
            int index = contentType.indexOf(';');
            String type = index > 0 ? contentType.substring(0, index) : contentType;
            builder = BuilderUtil.getBuilderFromSelector(type, msgContext);
            if (builder == null) {
                if (log.isDebugEnabled()) {
                    log.debug("No message builder found for type '" + type + "'. Falling back to SOAP.");
                }
                builder = new SOAPBuilder();
            }
        }
        // set the message payload to the message context
        InputStream in;
        ManagedDataSource dataSource;
        if (builder instanceof DataSourceMessageBuilder && entry.isStreaming()) {
            in = null;
            dataSource = ManagedDataSourceFactory.create(new FileObjectDataSource(file, contentType));
        } else {
            in = new AutoCloseInputStream(content.getInputStream());
            dataSource = null;
        }
        try {
            OMElement documentElement;
            if (in != null) {
                documentElement = builder.processDocument(in, contentType, msgContext);
            } else {
                documentElement = ((DataSourceMessageBuilder) builder).processDocument(dataSource, contentType, msgContext);
            }
            msgContext.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement));
            handleIncomingMessage(msgContext, transportHeaders, // * SOAP Action - not applicable *//
            null, contentType);
        } finally {
            if (dataSource != null) {
                dataSource.destroy();
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Processed file : " + VFSUtils.maskURLPassword(file.toString()) + " of Content-type : " + contentType);
        }
    } catch (FileSystemException e) {
        handleException("Error reading file content or attributes : " + VFSUtils.maskURLPassword(file.toString()), e);
    } finally {
        try {
            if (file != null) {
                if (fsManager != null && file.getName() != null && file.getName().getScheme() != null && file.getName().getScheme().startsWith("file")) {
                    fsManager.closeFileSystem(file.getParent().getFileSystem());
                }
                file.close();
            }
        } catch (FileSystemException warn) {
        // ignore the warning,  since we handed over the stream close job to
        // AutocloseInputstream..
        }
    }
}
Also used : ContentType(javax.mail.internet.ContentType) HashMap(java.util.HashMap) AutoCloseInputStream(org.apache.commons.io.input.AutoCloseInputStream) InputStream(java.io.InputStream) DataSourceMessageBuilder(org.apache.axis2.format.DataSourceMessageBuilder) Builder(org.apache.axis2.builder.Builder) SOAPBuilder(org.apache.axis2.builder.SOAPBuilder) OMElement(org.apache.axiom.om.OMElement) FileContent(org.apache.commons.vfs2.FileContent) FileSystemException(org.apache.commons.vfs2.FileSystemException) FileObject(org.apache.commons.vfs2.FileObject) SOAPBuilder(org.apache.axis2.builder.SOAPBuilder) FileObjectDataSource(org.apache.synapse.commons.vfs.FileObjectDataSource) MessageContext(org.apache.axis2.context.MessageContext) ParseException(javax.mail.internet.ParseException) VFSOutTransportInfo(org.apache.synapse.commons.vfs.VFSOutTransportInfo) AutoCloseInputStream(org.apache.commons.io.input.AutoCloseInputStream) ManagedDataSource(org.apache.axis2.format.ManagedDataSource) DataSourceMessageBuilder(org.apache.axis2.format.DataSourceMessageBuilder)

Example 3 with SOAPBuilder

use of org.apache.axis2.builder.SOAPBuilder in project wso2-synapse by wso2.

the class DeferredMessageBuilder method getDocument.

public OMElement getDocument(MessageContext msgCtx, InputStream in) throws XMLStreamException, IOException {
    /**
     * HTTP Delete requests may contain entity body or not. Hence if the request is a HTTP DELETE, we have to verify
     * that the payload stream is empty or not.
     */
    if (HTTPConstants.HEADER_DELETE.equals(msgCtx.getProperty(Constants.Configuration.HTTP_METHOD)) && RelayUtils.isEmptyPayloadStream(in)) {
        msgCtx.setProperty(PassThroughConstants.NO_ENTITY_BODY, Boolean.TRUE);
        return TransportUtils.createSOAPEnvelope(null);
    }
    String contentType = (String) msgCtx.getProperty(Constants.Configuration.CONTENT_TYPE);
    String _contentType = getContentType(contentType, msgCtx);
    in = HTTPTransportUtils.handleGZip(msgCtx, in);
    AxisConfiguration configuration = msgCtx.getConfigurationContext().getAxisConfiguration();
    Parameter useFallbackParameter = configuration.getParameter(Constants.Configuration.USE_DEFAULT_FALLBACK_BUILDER);
    boolean useFallbackBuilder = false;
    if (useFallbackParameter != null) {
        useFallbackBuilder = JavaUtils.isTrueExplicitly(useFallbackParameter.getValue(), useFallbackBuilder);
    }
    Map transportHeaders = (Map) msgCtx.getProperty(MessageContext.TRANSPORT_HEADERS);
    String contentLength = null;
    String trasferEncoded = null;
    if (transportHeaders != null) {
        contentLength = (String) transportHeaders.get(HTTP.CONTENT_LEN);
        trasferEncoded = (String) transportHeaders.get(HTTP.TRANSFER_ENCODING);
        if (contentType.equals(PassThroughConstants.DEFAULT_CONTENT_TYPE) && (contentLength == null || Integer.valueOf(contentLength) == 0) && trasferEncoded == null) {
            msgCtx.setProperty(PassThroughConstants.NO_ENTITY_BODY, true);
            msgCtx.setProperty(Constants.Configuration.CONTENT_TYPE, "");
            msgCtx.setProperty(PassThroughConstants.RELAY_EARLY_BUILD, true);
            return new SOAP11Factory().getDefaultEnvelope();
        }
    }
    OMElement element = null;
    Builder builder;
    if (contentType != null) {
        // loading builder from externally..
        // builder = configuration.getMessageBuilder(_contentType,useFallbackBuilder);
        builder = MessageProcessorSelector.getMessageBuilder(_contentType, msgCtx);
        if (builder != null) {
            try {
                if (contentLength != null && "0".equals(contentLength)) {
                    element = new org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory().getDefaultEnvelope();
                    // since we are setting an empty envelop to achieve the empty body, we have to set a different
                    // content-type other than text/xml, application/soap+xml or any other content-type which will
                    // invoke the soap builder, otherwise soap builder will get hit and an empty envelope
                    // will be send out
                    msgCtx.setProperty(Constants.Configuration.MESSAGE_TYPE, "application/xml");
                } else {
                    element = builder.processDocument(in, contentType, msgCtx);
                }
            } catch (AxisFault axisFault) {
                log.error("Error building message", axisFault);
                throw axisFault;
            }
        }
    }
    if (element == null) {
        if (msgCtx.isDoingREST()) {
            try {
                element = BuilderUtil.getPOXBuilder(in, null).getDocumentElement();
            } catch (XMLStreamException e) {
                log.error("Error building message using POX Builder", e);
                throw e;
            }
        } else {
            // switch to default
            builder = new SOAPBuilder();
            try {
                if (contentLength != null && "0".equals(contentLength)) {
                    element = new SOAP11Factory().getDefaultEnvelope();
                    // since we are setting an empty envelop to achieve the empty body, we have to set a different
                    // content-type other than text/xml, application/soap+xml or any other content-type which will
                    // invoke the soap builder, otherwise soap builder will get hit and an empty envelope
                    // will be send out
                    msgCtx.setProperty(Constants.Configuration.MESSAGE_TYPE, "application/xml");
                } else {
                    element = builder.processDocument(in, contentType, msgCtx);
                }
            } catch (AxisFault axisFault) {
                log.error("Error building message using SOAP builder");
                throw axisFault;
            }
        }
    }
    // build the soap headers and body
    if (element instanceof SOAPEnvelope) {
        SOAPEnvelope env = (SOAPEnvelope) element;
        env.hasFault();
    }
    // setting up original contentType (resetting the content type)
    if (contentType != null && !contentType.isEmpty()) {
        msgCtx.setProperty(Constants.Configuration.CONTENT_TYPE, contentType);
    }
    return element;
}
Also used : AxisFault(org.apache.axis2.AxisFault) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) XMLStreamException(javax.xml.stream.XMLStreamException) SOAP11Factory(org.apache.axiom.soap.impl.dom.soap11.SOAP11Factory) Parameter(org.apache.axis2.description.Parameter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with SOAPBuilder

use of org.apache.axis2.builder.SOAPBuilder in project wso2-axis2-transports by wso2.

the class MqttUtils method invoke.

public static void invoke(MqttMessage mqttMessage, MessageContext msgContext, String contentType) throws AxisFault, AxisMqttException {
    if (contentType == null) {
        contentType = "text/plain";
    }
    Builder builder = BuilderUtil.getBuilderFromSelector(contentType, msgContext);
    if (builder == null) {
        if (log.isDebugEnabled()) {
            log.debug("No message builder found for type '" + contentType + "'. Falling back to SOAP.");
        }
        builder = new SOAPBuilder();
    }
    msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, "UTF-8");
    OMElement documentElement = null;
    byte[] bytes = mqttMessage.getPayload();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    documentElement = builder.processDocument(byteArrayInputStream, contentType, msgContext);
    msgContext.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SOAPBuilder(org.apache.axis2.builder.SOAPBuilder) Builder(org.apache.axis2.builder.Builder) TextMessageBuilder(org.apache.axis2.format.TextMessageBuilder) SOAPBuilder(org.apache.axis2.builder.SOAPBuilder) OMElement(org.apache.axiom.om.OMElement)

Example 5 with SOAPBuilder

use of org.apache.axis2.builder.SOAPBuilder in project wso2-axis2-transports by wso2.

the class MailTransportListener method processMail.

/**
 * Process a mail message through Axis2
 *
 * @param message the email message
 * @param entry   the poll table entry
 * @throws MessagingException on error
 * @throws IOException        on error
 */
private void processMail(Message message, PollTableEntry entry) throws MessagingException, IOException {
    updateMetrics(message);
    // populate transport headers using the mail headers
    Map trpHeaders = getTransportHeaders(message, entry);
    // Allow the content type to be overridden by configuration.
    String contentType = entry.getContentType();
    Part messagePart;
    if (contentType != null) {
        messagePart = message;
    } else {
        messagePart = getMessagePart(message, cfgCtx.getAxisConfiguration());
        contentType = messagePart.getContentType();
    }
    // FIXME: remove this ugly hack when Axis2 has learned that content types are case insensitive...
    int idx = contentType.indexOf(';');
    if (idx == -1) {
        contentType = contentType.toLowerCase();
    } else {
        contentType = contentType.substring(0, idx).toLowerCase() + contentType.substring(idx);
    }
    // if the content type was not found, we have an error
    if (contentType == null) {
        processFailure("Unable to determine Content-type for message : " + message.getMessageNumber() + " :: " + message.getSubject(), null, entry);
        return;
    } else if (log.isDebugEnabled()) {
        log.debug("Processing message as Content-Type : " + contentType);
    }
    MessageContext msgContext = entry.createMessageContext();
    // Extract the charset encoding from the configured content type and
    // set the CHARACTER_SET_ENCODING property as e.g. SOAPBuilder relies on this.
    String charSetEnc;
    try {
        charSetEnc = new ContentType(contentType).getParameter("charset");
    } catch (ParseException ex) {
        // ignore
        charSetEnc = null;
    }
    msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc);
    MailOutTransportInfo outInfo = buildOutTransportInfo(message, entry);
    // save out transport information
    msgContext.setProperty(Constants.OUT_TRANSPORT_INFO, outInfo);
    // this property only useful for supporting smtp with Sandesha2.
    msgContext.setProperty(RequestResponseTransport.TRANSPORT_CONTROL, new MailRequestResponseTransport());
    // set message context From
    if (outInfo.getFromAddress() != null) {
        msgContext.setFrom(new EndpointReference(MailConstants.TRANSPORT_PREFIX + outInfo.getFromAddress().getAddress()));
    }
    // save original mail message id message context MessageID
    msgContext.setMessageID(outInfo.getRequestMessageID());
    // set the message payload to the message context
    InputStream in = messagePart.getInputStream();
    try {
        try {
            msgContext.setEnvelope(TransportUtils.createSOAPMessage(msgContext, in, contentType));
        } catch (XMLStreamException ex) {
            handleException("Error parsing message", ex);
        }
        String soapAction = (String) trpHeaders.get(BaseConstants.SOAPACTION);
        if (soapAction == null && message.getSubject() != null && message.getSubject().startsWith(BaseConstants.SOAPACTION)) {
            soapAction = message.getSubject().substring(BaseConstants.SOAPACTION.length());
            if (soapAction.startsWith(":")) {
                soapAction = soapAction.substring(1).trim();
            }
        }
        handleIncomingMessage(msgContext, trpHeaders, soapAction, contentType);
    } finally {
        in.close();
    }
    if (log.isDebugEnabled()) {
        log.debug("Processed message : " + message.getMessageNumber() + " :: " + message.getSubject());
    }
}
Also used : ContentType(javax.mail.internet.ContentType) XMLStreamException(javax.xml.stream.XMLStreamException) MimeBodyPart(javax.mail.internet.MimeBodyPart) InputStream(java.io.InputStream) MessageContext(org.apache.axis2.context.MessageContext) ParseException(javax.mail.internet.ParseException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Aggregations

OMElement (org.apache.axiom.om.OMElement)6 Builder (org.apache.axis2.builder.Builder)5 SOAPBuilder (org.apache.axis2.builder.SOAPBuilder)5 ContentType (javax.mail.internet.ContentType)4 ParseException (javax.mail.internet.ParseException)4 TextMessageBuilder (org.apache.axis2.format.TextMessageBuilder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 MessageContext (org.apache.axis2.context.MessageContext)2 DataSourceMessageBuilder (org.apache.axis2.format.DataSourceMessageBuilder)2 TextMessageBuilderAdapter (org.apache.axis2.format.TextMessageBuilderAdapter)2 LongString (com.rabbitmq.client.LongString)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 BytesMessage (javax.jms.BytesMessage)1 MapMessage (javax.jms.MapMessage)1 TextMessage (javax.jms.TextMessage)1