Example usage for org.apache.commons.codec.net QCodec decode

List of usage examples for org.apache.commons.codec.net QCodec decode

Introduction

In this page you can find the example usage for org.apache.commons.codec.net QCodec decode.

Prototype

public Object decode(Object pObject) throws DecoderException 

Source Link

Document

Decodes a quoted-printable object into its original form.

Usage

From source file:io.datenwelt.cargo.rest.utils.Rfc2047.java

public static String decodeHeader(String input) {
    StringTokenizer tokenizer = new StringTokenizer(input, " \t", true);
    StringBuilder decoded = new StringBuilder();
    while (tokenizer.hasMoreTokens()) {
        String token = tokenizer.nextToken();
        if (!token.startsWith("=?") || !token.endsWith("?=")) {
            decoded.append(token);//from w w w. j  a v a  2  s . co m
            continue;
        }
        String encodedWord = token;
        String[] parts = encodedWord.substring(2, encodedWord.length() - 2).split("\\?", 3);
        if (parts.length != 3) {
            decoded.append(token);
            continue;
        }
        Charset charset;
        try {
            charset = Charset.forName(parts[0]);
        } catch (Exception ex) {
            LOG.debug("Skipping the decoding of a header value due to an unknown charset \"{}\" found in: ",
                    parts[1], input, ex);
            decoded.append(token);
            continue;
        }
        String encoding = parts[1].toUpperCase();
        switch (encoding) {
        case "B":
            BCodec bcodec = new BCodec(charset);
            try {
                decoded.append(bcodec.decode(encodedWord));
            } catch (Exception ex) {
                LOG.debug("Skipping the decoding of BASE64 value from string \"{}\" found in: ", encodedWord,
                        input, ex);
                decoded.append(token);
            }
            break;
        case "Q":
            QCodec qcodec = new QCodec(charset);
            try {
                decoded.append(qcodec.decode(encodedWord));
            } catch (Exception ex) {
                LOG.debug("Skipping the decoding of Q encoded value from string \"{}\" found in: ", encodedWord,
                        input, ex);
                decoded.append(token);
            }
            break;
        default:
            LOG.debug("Skipping the decoding of value from unknown encoding \"{}\" found in: ", encodedWord,
                    input);
            decoded.append(token);
        }
    }
    return decoded.toString();
}