Example usage for javax.mail.internet HeaderTokenizer HeaderTokenizer

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

Introduction

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

Prototype

public HeaderTokenizer(String header, String delimiters, boolean skipComments) 

Source Link

Document

Constructor that takes a rfc822 style header.

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 ww. ja v a  2 s . 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();
}