Example usage for javax.mail.internet HeaderTokenizer MIME

List of usage examples for javax.mail.internet HeaderTokenizer MIME

Introduction

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

Prototype

String MIME

To view the source code for javax.mail.internet HeaderTokenizer MIME.

Click Source Link

Document

MIME specials

Usage

From source file:com.linuxbox.enkive.web.AttachmentRetrieveServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    final MessageRetrieverService retriever = getMessageRetrieverService();
    final String attachmentUUID = req.getParameter(PARAM_ATTACHMENT_ID);

    try {//ww w  .  java 2  s  . c o  m
        EncodedContentReadData attachment = retriever.retrieveAttachment(attachmentUUID);

        String filename = req.getParameter(PARAM_FILE_NAME);
        if (filename == null || filename.isEmpty()) {
            filename = attachment.getFilename();
        }
        if (filename == null || filename.isEmpty()) {
            filename = DEFAULT_FILE_NAME;
        }

        String mimeType = req.getParameter(PARAM_MIME_TYPE);
        if (mimeType == null || mimeType.isEmpty()) {
            mimeType = attachment.getMimeType();
        }

        // is there a purpose to the nested trys?
        try {
            if (mimeType != null) {
                resp.setContentType(mimeType);
            }
            resp.setCharacterEncoding("utf-8");
            resp.setHeader("Content-disposition",
                    "attachment;  filename=" + MimeUtility.quote(filename, HeaderTokenizer.MIME));

            final InputStream in = attachment.getBinaryContent();
            final OutputStream out = resp.getOutputStream();

            IOUtils.copy(in, out);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        } catch (ContentException e) {
            LOGGER.error("error transferring attachment  " + attachmentUUID, e);
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                    "error transferring attachment " + attachmentUUID + "; see server logs");
        }
    } catch (CannotRetrieveException e) {
        LOGGER.error("error retrieving attachment " + attachmentUUID, e);
        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                "error retrieving attachment " + attachmentUUID + "; see server logs");
    }
}

From source file:immf.Util.java

public static String encodeParameter(String name, String value, String encoding, String lang) {
    StringBuffer result = new StringBuffer();
    StringBuffer encodedPart = new StringBuffer();

    boolean needWriteCES = !isAllAscii(value);
    boolean CESWasWritten = false;
    boolean encoded;
    boolean needFolding = false;
    int sequenceNo = 0;
    int column;//from ww w  .  ja  v  a  2  s.  c  o m

    while (value.length() > 0) {
        // index of boundary of ascii/non ascii
        int lastIndex;
        boolean isAscii = value.charAt(0) < 0x80;
        for (lastIndex = 1; lastIndex < value.length(); lastIndex++) {
            if (value.charAt(lastIndex) < 0x80) {
                if (!isAscii)
                    break;
            } else {
                if (isAscii)
                    break;
            }
        }
        if (lastIndex != value.length())
            needFolding = true;

        RETRY: while (true) {
            encodedPart.delete(0, encodedPart.length());
            String target = value.substring(0, lastIndex);

            byte[] bytes;
            try {
                if (isAscii) {
                    bytes = target.getBytes("us-ascii");
                } else {
                    bytes = target.getBytes(encoding);
                }
            } catch (UnsupportedEncodingException e) {
                bytes = target.getBytes(); // use default encoding
                encoding = MimeUtility.mimeCharset(MimeUtility.getDefaultJavaCharset());
            }

            encoded = false;
            // It is not strict.
            column = name.length() + 7; // size of " " and "*nn*=" and ";"

            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] > ' ' && bytes[i] < 'z' && HeaderTokenizer.MIME.indexOf((char) bytes[i]) < 0) {
                    encodedPart.append((char) bytes[i]);
                    column++;
                } else {
                    encoded = true;
                    encodedPart.append('%');
                    String hex = Integer.toString(bytes[i] & 0xff, 16);
                    if (hex.length() == 1) {
                        encodedPart.append('0');
                    }
                    encodedPart.append(hex);
                    column += 3;
                }
                if (column > 76) {
                    needFolding = true;
                    lastIndex /= 2;
                    continue RETRY;
                }
            }

            result.append(";\r\n ").append(name);
            if (needFolding) {
                result.append('*').append(sequenceNo);
                sequenceNo++;
            }
            if (!CESWasWritten && needWriteCES) {
                result.append("*=");
                CESWasWritten = true;
                result.append(encoding).append('\'');
                if (lang != null)
                    result.append(lang);
                result.append('\'');
            } else if (encoded) {
                result.append("*=");
            } else {
                result.append('=');
            }
            result.append(new String(encodedPart));
            value = value.substring(lastIndex);
            break;
        }
    }
    return new String(result);
}

From source file:jenkins.plugins.mailer.tasks.MimeMessageBuilder.java

private String contentType() {
    return String.format("%s; charset=%s", mimeType, MimeUtility.quote(charset, HeaderTokenizer.MIME));
}

From source file:immf.Util.java

public static String getParameter(String header, String name) {

    HeaderTokenizer tokenizer = new HeaderTokenizer(header, HeaderTokenizer.MIME, true);
    HeaderTokenizer.Token token;/*  w  ww .jav  a  2  s .  co  m*/
    StringBuffer sb = new StringBuffer();
    // It is specified in first encoded-part.
    Encoding encoding = new Encoding();

    String n;
    String v;

    try {
        while (true) {
            token = tokenizer.next();
            if (token.getType() == HeaderTokenizer.Token.EOF)
                break;
            if (token.getType() != ';')
                continue;

            token = tokenizer.next();
            checkType(token);
            n = token.getValue();

            token = tokenizer.next();
            if (token.getType() != '=') {
                throw new ParseException("Illegal token : " + token.getValue());
            }

            token = tokenizer.next();
            checkType(token);
            v = token.getValue();

            if (n.equalsIgnoreCase(name)) {
                // It is not divided and is not encoded.
                return v;
            }

            int index = name.length();

            if (!n.startsWith(name) || n.charAt(index) != '*') {
                // another parameter
                continue;
            }
            // be folded, or be encoded
            int lastIndex = n.length() - 1;
            if (n.charAt(lastIndex) == '*') {
                sb.append(decodeRFC2231(v, encoding));
            } else {
                sb.append(v);
            }
            if (index == lastIndex) {
                // not folding
                break;
            }
        }
        return new String(sb);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    throw new InternalError();
}

From source file:org.apache.axiom.attachments.impl.AbstractPart.java

/**
 * @return contentTransferEncoding/*  ww w. j  a v  a2 s.  c  om*/
 * @throws MessagingException
 */
public String getContentTransferEncoding() throws MessagingException {
    if (log.isDebugEnabled()) {
        log.debug("getContentTransferEncoding()");
    }
    String cte = getHeader("content-transfer-encoding");

    if (log.isDebugEnabled()) {
        log.debug(" CTE =" + cte);
    }

    if (cte != null) {
        cte = cte.trim();

        if (cte.equalsIgnoreCase("7bit") || cte.equalsIgnoreCase("8bit")
                || cte.equalsIgnoreCase("quoted-printable") || cte.equalsIgnoreCase("base64")) {

            return cte;
        }

        HeaderTokenizer ht = new HeaderTokenizer(cte, HeaderTokenizer.MIME);
        boolean done = false;
        while (!done) {
            HeaderTokenizer.Token token = ht.next();
            switch (token.getType()) {
            case HeaderTokenizer.Token.EOF:
                if (log.isDebugEnabled()) {
                    log.debug("HeaderTokenizer EOF");
                }
                done = true;
                break;
            case HeaderTokenizer.Token.ATOM:
                return token.getValue();
            }
        }
        return cte;
    }
    return null;

}

From source file:org.kuali.coeus.sys.framework.controller.KcTransactionalDocumentActionBase.java

/**
 * Quotes a string that follows RFC 822 and is valid to include in an http header.
 * /*from   www .  ja  v  a2s .  c  om*/
 * <p>
 * This really should be a part of {@link org.kuali.rice.kns.util.WebUtils WebUtils}.
 * <p>
 * 
 * For example: without this method, file names with spaces will not show up to the client correctly.
 * 
 * <p>
 * This method is not doing a Base64 encode just a quoted printable character otherwise we would have
 * to set the encoding type on the header.
 * <p>
 * 
 * @param s the original string
 * @return the modified header string
 */
protected static String getValidHeaderString(String s) {
    return MimeUtility.quote(s, HeaderTokenizer.MIME);
}

From source file:org.kuali.kra.coi.personfinancialentity.FinancialEntityAction.java

protected static String getValidHeaderString(String s) {
    return MimeUtility.quote(s, HeaderTokenizer.MIME);
}