Search in sources :

Example 11 with ParseException

use of javax.mail.internet.ParseException in project wso2-synapse by wso2.

the class RegistryEntryImpl method setType.

public void setType(String type) {
    try {
        new ContentType(type);
        if (log.isDebugEnabled()) {
            log.debug("Content type :" + type);
        }
        this.type = type;
    } catch (ParseException e) {
        String msg = "Invalid content-type ' " + type + " '";
        throw new SynapseException(msg, e);
    }
}
Also used : ContentType(javax.mail.internet.ContentType) SynapseException(org.apache.synapse.SynapseException) ParseException(javax.mail.internet.ParseException)

Example 12 with ParseException

use of javax.mail.internet.ParseException 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 13 with ParseException

use of javax.mail.internet.ParseException in project rest.li by linkedin.

the class RestClient method resolveContentType.

// Request content type resolution follows similar precedence order to accept type:
// 1. Request header
// 2. RestLiRequestOption
// 3. RestClient configuration
private ContentType resolveContentType(MessageHeadersBuilder<?> builder, DataMap dataMap, ContentType contentType) throws IOException {
    if (dataMap != null) {
        String header = builder.getHeader(RestConstants.HEADER_CONTENT_TYPE);
        ContentType type;
        if (header == null) {
            if (contentType != null) {
                type = contentType;
            } else if (_contentType != null) {
                type = _contentType;
            } else {
                type = DEFAULT_CONTENT_TYPE;
            }
        } else {
            javax.mail.internet.ContentType headerContentType;
            try {
                headerContentType = new javax.mail.internet.ContentType(header);
            } catch (ParseException e) {
                throw new IllegalStateException("Unable to parse Content-Type: " + header);
            }
            if (headerContentType.getBaseType().equalsIgnoreCase(RestConstants.HEADER_VALUE_APPLICATION_JSON)) {
                type = ContentType.JSON;
            } else if (headerContentType.getBaseType().equalsIgnoreCase(RestConstants.HEADER_VALUE_APPLICATION_PSON)) {
                type = ContentType.PSON;
            } else {
                throw new IllegalStateException("Unknown Content-Type: " + headerContentType.toString());
            }
        }
        return type;
    }
    return null;
}
Also used : ByteString(com.linkedin.data.ByteString) ParseException(javax.mail.internet.ParseException)

Example 14 with ParseException

use of javax.mail.internet.ParseException in project rest.li by linkedin.

the class RestLiServer method verifyAttachmentSupportNotNeeded.

private boolean verifyAttachmentSupportNotNeeded(final Request request, final Callback<RestResponse> callback) {
    final Map<String, String> requestHeaders = request.getHeaders();
    try {
        final String contentTypeString = requestHeaders.get(RestConstants.HEADER_CONTENT_TYPE);
        if (contentTypeString != null) {
            final ContentType contentType = new ContentType(contentTypeString);
            if (contentType.getBaseType().equalsIgnoreCase(RestConstants.HEADER_VALUE_MULTIPART_RELATED)) {
                callback.onError(RestException.forError(415, "This server cannot handle requests with a content type of multipart/related"));
                return true;
            }
        }
        final String acceptTypeHeader = requestHeaders.get(RestConstants.HEADER_ACCEPT);
        if (acceptTypeHeader != null) {
            final List<String> acceptTypes = MIMEParse.parseAcceptType(acceptTypeHeader);
            for (final String acceptType : acceptTypes) {
                if (acceptType.equalsIgnoreCase(RestConstants.HEADER_VALUE_MULTIPART_RELATED)) {
                    callback.onError(RestException.forError(406, "This server cannot handle requests with an accept type of multipart/related"));
                    return true;
                }
            }
        }
    } catch (ParseException parseException) {
        callback.onError(RestException.forError(400, "Unable to parse content or accept types."));
        return true;
    }
    return false;
}
Also used : ContentType(javax.mail.internet.ContentType) ByteString(com.linkedin.data.ByteString) ParseException(javax.mail.internet.ParseException)

Example 15 with ParseException

use of javax.mail.internet.ParseException in project wso2-axis2-transports by wso2.

the class RabbitMQUtils method setSOAPEnvelope.

public static void setSOAPEnvelope(RabbitMQMessage message, MessageContext msgContext, String contentType) throws AxisFault {
    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;
    String charSetEnc = null;
    try {
        if (contentType != null) {
            charSetEnc = new ContentType(contentType).getParameter("charset");
        }
    } catch (ParseException ex) {
        log.debug("Parse error", ex);
    }
    msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc);
    documentElement = builder.processDocument(new ByteArrayInputStream(message.getBody()), contentType, msgContext);
    msgContext.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement));
}
Also used : ContentType(javax.mail.internet.ContentType) ByteArrayInputStream(java.io.ByteArrayInputStream) SOAPBuilder(org.apache.axis2.builder.SOAPBuilder) Builder(org.apache.axis2.builder.Builder) SOAPBuilder(org.apache.axis2.builder.SOAPBuilder) OMElement(org.apache.axiom.om.OMElement) LongString(com.rabbitmq.client.LongString) ParseException(javax.mail.internet.ParseException)

Aggregations

ParseException (javax.mail.internet.ParseException)29 ContentType (javax.mail.internet.ContentType)25 ByteString (com.linkedin.data.ByteString)6 Map (java.util.Map)6 IOException (java.io.IOException)5 List (java.util.List)5 MimeHeaders (javax.xml.soap.MimeHeaders)5 SOAPMessage (javax.xml.soap.SOAPMessage)5 InputStream (java.io.InputStream)4 WebServiceException (javax.xml.ws.WebServiceException)4 SOAPMessageContext (javax.xml.ws.handler.soap.SOAPMessageContext)4 Builder (org.apache.axis2.builder.Builder)4 SOAPBuilder (org.apache.axis2.builder.SOAPBuilder)4 MultiPartMIMEReader (com.linkedin.multipart.MultiPartMIMEReader)3 BodyPart (javax.mail.BodyPart)3 MessagingException (javax.mail.MessagingException)3 MimeBodyPart (javax.mail.internet.MimeBodyPart)3 MimeMultipart (javax.mail.internet.MimeMultipart)3 Callback (com.linkedin.common.callback.Callback)2 MultiPartMIMEWriter (com.linkedin.multipart.MultiPartMIMEWriter)2