Example usage for javax.mail.internet HeaderTokenizer next

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

Introduction

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

Prototype

public Token next() throws ParseException 

Source Link

Document

Parses the next token from this String.

Usage

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 w w  . java 2s  .  c o  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//from  w  ww  .  j  a  v  a 2  s  .co  m
 * @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;

}