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

The following examples show how to use javax.activation.DataSource#getName() . 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: MimeMessageParser.java    From commons-email with Apache License 2.0 6 votes vote down vote up
/**
 * Determines the name of the data source if it is not already set.
 *
 * @param part the mail part
 * @param dataSource the data source
 * @return the name of the data source or {@code null} if no name can be determined
 * @throws MessagingException accessing the part failed
 * @throws UnsupportedEncodingException decoding the text failed
 */
protected String getDataSourceName(final Part part, final DataSource dataSource)
    throws MessagingException, UnsupportedEncodingException
{
    String result = dataSource.getName();

    if (result == null || result.length() == 0)
    {
        result = part.getFileName();
    }

    if (result != null && result.length() > 0)
    {
        result = MimeUtility.decodeText(result);
    }
    else
    {
        result = null;
    }

    return result;
}
 
Example 2
Source File: ByteArrayDataSource.java    From unitime with Apache License 2.0 5 votes vote down vote up
public ByteArrayDataSource(DataSource ds) throws IOException {
	iName = ds.getName();
	iContentType = ds.getContentType();
	InputStream is = ds.getInputStream();
	try {
		iData = IOUtils.toByteArray(is);
	} finally {
		 is.close();
	}			
}
 
Example 3
Source File: LazyDataSourceTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoAttachment() throws Exception {
    DataSource ds = new LazyDataSource(ID_1, Collections.singleton(new AttachmentImpl(ID_2)));
    try {
        ds.getName();
        fail();
    } catch (IllegalStateException e) {
        String message = e.getMessage();
        assertTrue(message, message.contains(ID_1));
        assertTrue(message, message.contains(ID_2));
    }
}
 
Example 4
Source File: MultiPartEmail.java    From commons-email with Apache License 2.0 5 votes vote down vote up
/**
 * Attach a file specified as a DataSource interface.
 *
 * @param ds A DataSource interface for the file.
 * @param name The name field for the attachment.
 * @param description A description for the attachment.
 * @param disposition Either mixed or inline.
 * @return A MultiPartEmail.
 * @throws EmailException see javax.mail.internet.MimeBodyPart
 *  for definitions
 * @since 1.0
 */
public MultiPartEmail attach(
    final DataSource ds,
    String name,
    final String description,
    final String disposition)
    throws EmailException
{
    if (EmailUtils.isEmpty(name))
    {
        name = ds.getName();
    }
    final BodyPart bodyPart = createBodyPart();
    try
    {
        bodyPart.setDisposition(disposition);
        bodyPart.setFileName(MimeUtility.encodeText(name));
        bodyPart.setDescription(description);
        bodyPart.setDataHandler(new DataHandler(ds));

        getContainer().addBodyPart(bodyPart);
    }
    catch (final UnsupportedEncodingException uee)
    {
        // in case the file name could not be encoded
        throw new EmailException(uee);
    }
    catch (final MessagingException me)
    {
        throw new EmailException(me);
    }
    setBoolHasAttachments(true);

    return this;
}
 
Example 5
Source File: ImageHtmlEmail.java    From commons-email with Apache License 2.0 4 votes vote down vote up
/**
 * Replace the regexp matching resource locations with "cid:..." references.
 *
 * @param htmlMessage the HTML message to analyze
 * @param pattern the regular expression to find resources
 * @return the HTML message containing "cid" references
 * @throws EmailException creating the email failed
 * @throws IOException resolving the resources failed
 */
private String replacePattern(final String htmlMessage, final Pattern pattern)
        throws EmailException, IOException
{
    DataSource dataSource;
    final StringBuffer stringBuffer = new StringBuffer();

    // maps "cid" --> name
    final Map<String, String> cidCache = new HashMap<String, String>();

    // maps "name" --> dataSource
    final Map<String, DataSource> dataSourceCache = new HashMap<String, DataSource>();

    // in the String, replace all "img src" with a CID and embed the related
    // image file if we find it.
    final Matcher matcher = pattern.matcher(htmlMessage);

    // the matcher returns all instances one by one
    while (matcher.find())
    {
        // in the RegEx we have the <src> element as second "group"
        final String resourceLocation = matcher.group(2);

        // avoid loading the same data source more than once
        if (dataSourceCache.get(resourceLocation) == null)
        {
            // in lenient mode we might get a 'null' data source if the resource was not found
            dataSource = getDataSourceResolver().resolve(resourceLocation);

            if (dataSource != null)
            {
                dataSourceCache.put(resourceLocation, dataSource);
            }
        }
        else
        {
            dataSource = dataSourceCache.get(resourceLocation);
        }

        if (dataSource != null)
        {
            String name = dataSource.getName();
            if (EmailUtils.isEmpty(name))
            {
                name = resourceLocation;
            }

            String cid = cidCache.get(name);

            if (cid == null)
            {
                cid = embed(dataSource, name);
                cidCache.put(name, cid);
            }

            // if we embedded something, then we need to replace the URL with
            // the CID, otherwise the Matcher takes care of adding the
            // non-replaced text afterwards, so no else is necessary here!
            matcher.appendReplacement(stringBuffer,
                    Matcher.quoteReplacement(matcher.group(1) + "cid:" + cid + matcher.group(3)));
        }
    }

    // append the remaining items...
    matcher.appendTail(stringBuffer);

    cidCache.clear();
    dataSourceCache.clear();

    return stringBuffer.toString();
}