Java Code Examples for javax.activation.DataSource#getContentType()

The following examples show how to use javax.activation.DataSource#getContentType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected DataSource post(DataSource req) {
    String msg;
    try {
        LOG.info("content type: " + req.getContentType());

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IOUtils.copy(req.getInputStream(), baos);
        LOG.info("body [" + new String(baos.toByteArray())  + "]");
        try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) {
            msg = "<ok/>";

            MimeMultipart multipart = DataSourceProviderTest.readAttachmentParts(req.getContentType(), bais);
            LOG.info("found " + multipart.getCount() + " parts");
        }
        return new ByteArrayDataSource(baos.toByteArray(), req.getContentType());
    } catch (Exception e) {
        e.printStackTrace();
        msg = "<fail/>";
    }
    return new ByteArrayDataSource(msg.getBytes(), "text/xml");
}
 
Example 2
Source File: LazyDataSourceTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoDataSource() throws Exception {
    DataSource ds = new LazyDataSource(ID_1,
            Collections.singleton(new AttachmentImpl(ID_1, new DataHandler((DataSource) null) {
                @Override
                public DataSource getDataSource() {
                    return null;
                }
            })));
    try {
        ds.getContentType();
        fail();
    } catch (IllegalStateException e) {
        String message = e.getMessage();
        assertTrue(message, message.contains(ID_1));
    }
}
 
Example 3
Source File: XmlDataContentHandler.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create an object from the input stream
 */
public Object getContent(DataSource ds) throws IOException {
    String ctStr = ds.getContentType();
    String charset = null;
    if (ctStr != null) {
        ContentType ct = new ContentType(ctStr);
        if (!isXml(ct)) {
            throw new IOException(
                "Cannot convert DataSource with content type \""
                        + ctStr + "\" to object in XmlDataContentHandler");
        }
        charset = ct.getParameter("charset");
    }
    return (charset != null)
            ? new StreamSource(new InputStreamReader(ds.getInputStream()), charset)
            : new StreamSource(ds.getInputStream());
}
 
Example 4
Source File: XMLHTTPBindingCodec.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private ContentType encode(MessageDataSource mds, OutputStream out) {
    try {
        final boolean isFastInfoset = XMLMessage.isFastInfoset(
                mds.getDataSource().getContentType());
        DataSource ds = transformDataSource(mds.getDataSource(),
                isFastInfoset, useFastInfosetForEncoding, features);

        InputStream is = ds.getInputStream();
        byte[] buf = new byte[1024];
        int count;
        while((count=is.read(buf)) != -1) {
            out.write(buf, 0, count);
        }
        return new ContentTypeImpl(ds.getContentType());
    } catch(IOException ioe) {
        throw new WebServiceException(ioe);
    }
}
 
Example 5
Source File: XmlDataContentHandler.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create an object from the input stream
 */
public Object getContent(DataSource ds) throws IOException {
    String ctStr = ds.getContentType();
    String charset = null;
    if (ctStr != null) {
        ContentType ct = new ContentType(ctStr);
        if (!isXml(ct)) {
            throw new IOException(
                "Cannot convert DataSource with content type \""
                        + ctStr + "\" to object in XmlDataContentHandler");
        }
        charset = ct.getParameter("charset");
    }
    return (charset != null)
            ? new StreamSource(new InputStreamReader(ds.getInputStream()), charset)
            : new StreamSource(ds.getInputStream());
}
 
Example 6
Source File: XmlDataContentHandler.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create an object from the input stream
 */
public Object getContent(DataSource ds) throws IOException {
    String ctStr = ds.getContentType();
    String charset = null;
    if (ctStr != null) {
        ContentType ct = new ContentType(ctStr);
        if (!isXml(ct)) {
            throw new IOException(
                "Cannot convert DataSource with content type \""
                        + ctStr + "\" to object in XmlDataContentHandler");
        }
        charset = ct.getParameter("charset");
    }
    return (charset != null)
            ? new StreamSource(new InputStreamReader(ds.getInputStream()), charset)
            : new StreamSource(ds.getInputStream());
}
 
Example 7
Source File: MimeUtility.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the content-transfer-encoding that should be applied
 * to the input stream of this datasource, to make it mailsafe. <p>
 *
 * The algorithm used here is: <br>
 * <ul>
 * <li>
 * If the primary type of this datasource is "text" and if all
 * the bytes in its input stream are US-ASCII, then the encoding
 * is "7bit". If more than half of the bytes are non-US-ASCII, then
 * the encoding is "base64". If less than half of the bytes are
 * non-US-ASCII, then the encoding is "quoted-printable".
 * <li>
 * If the primary type of this datasource is not "text", then if
 * all the bytes of its input stream are US-ASCII, the encoding
 * is "7bit". If there is even one non-US-ASCII character, the
 * encoding is "base64".
 * </ul>
 *
 * @param   ds      DataSource
 * @return          the encoding. This is either "7bit",
 *                  "quoted-printable" or "base64"
 */
public static String getEncoding(DataSource ds) {
    ContentType cType = null;
    InputStream is = null;
    String encoding = null;

    try {
        cType = new ContentType(ds.getContentType());
        is = ds.getInputStream();
    } catch (Exception ex) {
        return "base64"; // what else ?!
    }

    boolean isText = cType.match("text/*");
    // if not text, stop processing when we see non-ASCII
    int i = checkAscii(is, ALL, !isText);
    switch (i) {
    case ALL_ASCII:
        encoding = "7bit"; // all ascii
        break;
    case MOSTLY_ASCII:
        encoding = "quoted-printable"; // mostly ascii
        break;
    default:
        encoding = "base64"; // mostly binary
        break;
    }

    // Close the input stream
    try {
        is.close();
    } catch (IOException ioex) { }

    return encoding;
}
 
Example 8
Source File: MimePullMultipart.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public MimePullMultipart(DataSource ds, ContentType ct)
    throws MessagingException {
    parsed = false;
    if (ct==null)
        contType = new ContentType(ds.getContentType());
    else
        contType = ct;

    dataSource = ds;
    boundary = contType.getParameter("boundary");
}
 
Example 9
Source File: MimeUtility.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the content-transfer-encoding that should be applied
 * to the input stream of this datasource, to make it mailsafe. <p>
 *
 * The algorithm used here is: <br>
 * <ul>
 * <li>
 * If the primary type of this datasource is "text" and if all
 * the bytes in its input stream are US-ASCII, then the encoding
 * is "7bit". If more than half of the bytes are non-US-ASCII, then
 * the encoding is "base64". If less than half of the bytes are
 * non-US-ASCII, then the encoding is "quoted-printable".
 * <li>
 * If the primary type of this datasource is not "text", then if
 * all the bytes of its input stream are US-ASCII, the encoding
 * is "7bit". If there is even one non-US-ASCII character, the
 * encoding is "base64".
 * </ul>
 *
 * @param   ds      DataSource
 * @return          the encoding. This is either "7bit",
 *                  "quoted-printable" or "base64"
 */
public static String getEncoding(DataSource ds) {
    ContentType cType = null;
    InputStream is = null;
    String encoding = null;

    try {
        cType = new ContentType(ds.getContentType());
        is = ds.getInputStream();
    } catch (Exception ex) {
        return "base64"; // what else ?!
    }

    boolean isText = cType.match("text/*");
    // if not text, stop processing when we see non-ASCII
    int i = checkAscii(is, ALL, !isText);
    switch (i) {
    case ALL_ASCII:
        encoding = "7bit"; // all ascii
        break;
    case MOSTLY_ASCII:
        encoding = "quoted-printable"; // mostly ascii
        break;
    default:
        encoding = "base64"; // mostly binary
        break;
    }

    // Close the input stream
    try {
        is.close();
    } catch (IOException ioex) { }

    return encoding;
}
 
Example 10
Source File: MimePullMultipart.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public MimePullMultipart(DataSource ds, ContentType ct)
    throws MessagingException {
    parsed = false;
    if (ct==null)
        contType = new ContentType(ds.getContentType());
    else
        contType = ct;

    dataSource = ds;
    boundary = contType.getParameter("boundary");
}
 
Example 11
Source File: MimePullMultipart.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public MimePullMultipart(DataSource ds, ContentType ct)
    throws MessagingException {
    parsed = false;
    if (ct==null)
        contType = new ContentType(ds.getContentType());
    else
        contType = ct;

    dataSource = ds;
    boundary = contType.getParameter("boundary");
}
 
Example 12
Source File: DataSourceSource.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public DataSourceSource(DataSource source) throws MimeTypeParseException {
    this.source = source;

    String ct = source.getContentType();
    if(ct==null) {
        charset = null;
    } else {
        MimeType mimeType = new MimeType(ct);
        this.charset = mimeType.getParameter("charset");
    }
}
 
Example 13
Source File: MimePullMultipart.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public MimePullMultipart(DataSource ds, ContentType ct)
    throws MessagingException {
    parsed = false;
    if (ct==null)
        contType = new ContentType(ds.getContentType());
    else
        contType = ct;

    dataSource = ds;
    boundary = contType.getParameter("boundary");
}
 
Example 14
Source File: DataSourceSource.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public DataSourceSource(DataSource source) throws MimeTypeParseException {
    this.source = source;

    String ct = source.getContentType();
    if(ct==null) {
        charset = null;
    } else {
        MimeType mimeType = new MimeType(ct);
        this.charset = mimeType.getParameter("charset");
    }
}
 
Example 15
Source File: MimeUtility.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the content-transfer-encoding that should be applied
 * to the input stream of this datasource, to make it mailsafe. <p>
 *
 * The algorithm used here is: <br>
 * <ul>
 * <li>
 * If the primary type of this datasource is "text" and if all
 * the bytes in its input stream are US-ASCII, then the encoding
 * is "7bit". If more than half of the bytes are non-US-ASCII, then
 * the encoding is "base64". If less than half of the bytes are
 * non-US-ASCII, then the encoding is "quoted-printable".
 * <li>
 * If the primary type of this datasource is not "text", then if
 * all the bytes of its input stream are US-ASCII, the encoding
 * is "7bit". If there is even one non-US-ASCII character, the
 * encoding is "base64".
 * </ul>
 *
 * @param   ds      DataSource
 * @return          the encoding. This is either "7bit",
 *                  "quoted-printable" or "base64"
 */
public static String getEncoding(DataSource ds) {
    ContentType cType = null;
    InputStream is = null;
    String encoding = null;

    try {
        cType = new ContentType(ds.getContentType());
        is = ds.getInputStream();
    } catch (Exception ex) {
        return "base64"; // what else ?!
    }

    boolean isText = cType.match("text/*");
    // if not text, stop processing when we see non-ASCII
    int i = checkAscii(is, ALL, !isText);
    switch (i) {
    case ALL_ASCII:
        encoding = "7bit"; // all ascii
        break;
    case MOSTLY_ASCII:
        encoding = "quoted-printable"; // mostly ascii
        break;
    default:
        encoding = "base64"; // mostly binary
        break;
    }

    // Close the input stream
    try {
        is.close();
    } catch (IOException ioex) { }

    return encoding;
}
 
Example 16
Source File: MimeMultipart.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Constructs a MimeMultipart object and its bodyparts from the
 * given DataSource. <p>
 *
 * This constructor handles as a special case the situation where the
 * given DataSource is a MultipartDataSource object.
 *
 * Otherwise, the DataSource is assumed to provide a MIME multipart
 * byte stream.  The <code>parsed</code> flag is set to false.  When
 * the data for the body parts are needed, the parser extracts the
 * "boundary" parameter from the content type of this DataSource,
 * skips the 'preamble' and reads bytes till the terminating
 * boundary and creates MimeBodyParts for each part of the stream.
 *
 * @param   ds      DataSource, can be a MultipartDataSource
 * @param ct
 *      This must be the same information as {@link DataSource#getContentType()}.
 *      All the callers of this method seem to have this object handy, so
 *      for performance reason this method accepts it. Can be null.
 */
public MimeMultipart(DataSource ds, ContentType ct) throws MessagingException {
    // 'ds' was not a MultipartDataSource, we have
    // to parse this ourself.
    parsed = false;
    this.ds = ds;
    if (ct==null)
        contentType = new ContentType(ds.getContentType());
    else
        contentType = ct;
}
 
Example 17
Source File: MimeMultipart.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Constructs a MimeMultipart object and its bodyparts from the
 * given DataSource. <p>
 *
 * This constructor handles as a special case the situation where the
 * given DataSource is a MultipartDataSource object.
 *
 * Otherwise, the DataSource is assumed to provide a MIME multipart
 * byte stream.  The <code>parsed</code> flag is set to false.  When
 * the data for the body parts are needed, the parser extracts the
 * "boundary" parameter from the content type of this DataSource,
 * skips the 'preamble' and reads bytes till the terminating
 * boundary and creates MimeBodyParts for each part of the stream.
 *
 * @param   ds      DataSource, can be a MultipartDataSource
 * @param ct
 *      This must be the same information as {@link DataSource#getContentType()}.
 *      All the callers of this method seem to have this object handy, so
 *      for performance reason this method accepts it. Can be null.
 */
public MimeMultipart(DataSource ds, ContentType ct) throws MessagingException {
    // 'ds' was not a MultipartDataSource, we have
    // to parse this ourself.
    parsed = false;
    this.ds = ds;
    if (ct==null)
        contentType = new ContentType(ds.getContentType());
    else
        contentType = ct;
}
 
Example 18
Source File: MimeMultipart.java    From openjdk-8-source with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Constructs a MimeMultipart object and its bodyparts from the
 * given DataSource. <p>
 *
 * This constructor handles as a special case the situation where the
 * given DataSource is a MultipartDataSource object.
 *
 * Otherwise, the DataSource is assumed to provide a MIME multipart
 * byte stream.  The <code>parsed</code> flag is set to false.  When
 * the data for the body parts are needed, the parser extracts the
 * "boundary" parameter from the content type of this DataSource,
 * skips the 'preamble' and reads bytes till the terminating
 * boundary and creates MimeBodyParts for each part of the stream.
 *
 * @param   ds      DataSource, can be a MultipartDataSource
 * @param ct
 *      This must be the same information as {@link DataSource#getContentType()}.
 *      All the callers of this method seem to have this object handy, so
 *      for performance reason this method accepts it. Can be null.
 */
public MimeMultipart(DataSource ds, ContentType ct) throws MessagingException {
    // 'ds' was not a MultipartDataSource, we have
    // to parse this ourself.
    parsed = false;
    this.ds = ds;
    if (ct==null)
        contentType = new ContentType(ds.getContentType());
    else
        contentType = ct;
}
 
Example 19
Source File: MimeMultipart.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Constructs a MimeMultipart object and its bodyparts from the
 * given DataSource. <p>
 *
 * This constructor handles as a special case the situation where the
 * given DataSource is a MultipartDataSource object.
 *
 * Otherwise, the DataSource is assumed to provide a MIME multipart
 * byte stream.  The <code>parsed</code> flag is set to false.  When
 * the data for the body parts are needed, the parser extracts the
 * "boundary" parameter from the content type of this DataSource,
 * skips the 'preamble' and reads bytes till the terminating
 * boundary and creates MimeBodyParts for each part of the stream.
 *
 * @param   ds      DataSource, can be a MultipartDataSource
 * @param ct
 *      This must be the same information as {@link DataSource#getContentType()}.
 *      All the callers of this method seem to have this object handy, so
 *      for performance reason this method accepts it. Can be null.
 */
public MimeMultipart(DataSource ds, ContentType ct) throws MessagingException {
    // 'ds' was not a MultipartDataSource, we have
    // to parse this ourself.
    parsed = false;
    this.ds = ds;
    if (ct==null)
        contentType = new ContentType(ds.getContentType());
    else
        contentType = ct;
}
 
Example 20
Source File: MimeMultipart.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Constructs a MimeMultipart object and its bodyparts from the
 * given DataSource. <p>
 *
 * This constructor handles as a special case the situation where the
 * given DataSource is a MultipartDataSource object.
 *
 * Otherwise, the DataSource is assumed to provide a MIME multipart
 * byte stream.  The <code>parsed</code> flag is set to false.  When
 * the data for the body parts are needed, the parser extracts the
 * "boundary" parameter from the content type of this DataSource,
 * skips the 'preamble' and reads bytes till the terminating
 * boundary and creates MimeBodyParts for each part of the stream.
 *
 * @param   ds      DataSource, can be a MultipartDataSource
 * @param ct
 *      This must be the same information as {@link DataSource#getContentType()}.
 *      All the callers of this method seem to have this object handy, so
 *      for performance reason this method accepts it. Can be null.
 */
public MimeMultipart(DataSource ds, ContentType ct) throws MessagingException {
    // 'ds' was not a MultipartDataSource, we have
    // to parse this ourself.
    parsed = false;
    this.ds = ds;
    if (ct==null)
        contentType = new ContentType(ds.getContentType());
    else
        contentType = ct;
}