Example usage for javax.mail.internet ContentDisposition ContentDisposition

List of usage examples for javax.mail.internet ContentDisposition ContentDisposition

Introduction

In this page you can find the example usage for javax.mail.internet ContentDisposition ContentDisposition.

Prototype

public ContentDisposition(String s) throws ParseException 

Source Link

Document

Constructor that takes a ContentDisposition string.

Usage

From source file:immf.Util.java

public static void setFileName(Part part, String filename, String charset, String lang)
        throws MessagingException {

    ContentDisposition disposition;// w  w w. java 2 s .  c  o m
    String[] strings = part.getHeader("Content-Disposition");
    if (strings == null || strings.length < 1) {
        disposition = new ContentDisposition(Part.ATTACHMENT);
    } else {
        disposition = new ContentDisposition(strings[0]);
        disposition.getParameterList().remove("filename");
    }

    part.setHeader("Content-Disposition",
            disposition.toString() + encodeParameter("filename", filename, charset, lang));

    ContentType cType;
    strings = part.getHeader("Content-Type");
    if (strings == null || strings.length < 1) {
        cType = new ContentType(part.getDataHandler().getContentType());
    } else {
        cType = new ContentType(strings[0]);
    }

    try {
        // I want to public the MimeUtility#doEncode()!!!
        String mimeString = MimeUtility.encodeWord(filename, charset, "B");
        // cut <CRLF>...
        StringBuffer sb = new StringBuffer();
        int i;
        while ((i = mimeString.indexOf('\r')) != -1) {
            sb.append(mimeString.substring(0, i));
            mimeString = mimeString.substring(i + 2);
        }
        sb.append(mimeString);

        cType.setParameter("name", new String(sb));
    } catch (UnsupportedEncodingException e) {
        throw new MessagingException("Encoding error", e);
    }
    part.setHeader("Content-Type", cType.toString());
}

From source file:com.adaptris.core.mail.attachment.MimeMailCreator.java

private String getAttachmentFileName(MimeBodyPart p) throws Exception {
    String filename = null;//from www.ja v a 2 s .  c  om
    String[] hdr = p.getHeader("Content-Disposition");
    if (hdr != null) {
        ContentDisposition cd = new ContentDisposition(hdr[0]);
        filename = cd.getParameter("filename");
    }
    if (filename == null) {
        hdr = p.getHeader("Content-Type");
        if (hdr != null) {
            ContentType ct = new ContentType(hdr[0]);
            filename = ct.getParameter("name");
        }
    }
    if (filename == null) {
        filename = idGenerator.create(p);
        logR.warn("Could not determine filename for MimeBodyPart, assigning unique filename of {}", filename);

    }
    return filename;
}

From source file:org.apache.tika.server.resource.TikaResource.java

public static String detectFilename(MultivaluedMap<String, String> httpHeaders) {

    String disposition = httpHeaders.getFirst("Content-Disposition");
    if (disposition != null) {
        try {/*w  w w .  ja va2s.co m*/
            ContentDisposition c = new ContentDisposition(disposition);

            // only support "attachment" dispositions
            if ("attachment".equals(c.getDisposition())) {
                String fn = c.getParameter("filename");
                if (fn != null) {
                    return fn;
                }
            }
        } catch (ParseException e) {
            // not a valid content-disposition field
            LOG.warn("Parse exception {} determining content disposition", e.getMessage(), e);
        }
    }

    // this really should not be used, since it's not an official field
    return httpHeaders.getFirst("File-Name");
}

From source file:uk.co.techblue.alfresco.resteasy.providers.DocumentContentProvider.java

/**
 * Gets the content disposition./*from w  w  w  .  ja v a 2 s . c  om*/
 * 
 * @param dispositionHeader the disposition header
 * @return the content disposition
 * @throws ParseException the parse exception
 */
private ContentDisposition getContentDisposition(final String dispositionHeader) throws ParseException {
    if (StringUtils.isBlank(dispositionHeader)) {
        return null;
    }
    return new ContentDisposition(dispositionHeader);
}

From source file:uk.co.techblue.docusign.resteasy.providers.DocumentFileProvider.java

private ContentDisposition getContentDisposition(String dispositionHeader) throws ParseException {
    if (StringUtils.isBlank(dispositionHeader)) {
        return null;
    }//from  w w  w  . j  av  a 2  s .co m
    return new ContentDisposition(dispositionHeader);
}