RFC1522Codec.java revision 069490a5ca2fd1988d29daf45d892f47ad665115
1/*
2 * Copyright 2001-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.commons.codec.net;
18
19import java.io.UnsupportedEncodingException;
20
21import org.apache.commons.codec.DecoderException;
22import org.apache.commons.codec.EncoderException;
23
24/**
25 * <p>
26 * Implements methods common to all codecs defined in RFC 1522.
27 * </p>
28 *
29 * <p>
30 * <a href="http://www.ietf.org/rfc/rfc1522.txt">RFC 1522</a>
31 * describes techniques to allow the encoding of non-ASCII text in
32 * various portions of a RFC 822 [2] message header, in a manner which
33 * is unlikely to confuse existing message handling software.
34 * </p>
35
36 * @see <a href="http://www.ietf.org/rfc/rfc1522.txt">
37 * MIME (Multipurpose Internet Mail Extensions) Part Two:
38 * Message Header Extensions for Non-ASCII Text</a>
39 * </p>
40 *
41 * @author Apache Software Foundation
42 * @since 1.3
43 * @version $Id: RFC1522Codec.java,v 1.2 2004/04/09 22:21:43 ggregory Exp $
44 */
45abstract class RFC1522Codec {
46
47    /**
48     * Applies an RFC 1522 compliant encoding scheme to the given string of text with the
49     * given charset. This method constructs the "encoded-word" header common to all the
50     * RFC 1522 codecs and then invokes {@link #doEncoding(byte [])} method of a concrete
51     * class to perform the specific enconding.
52     *
53     * @param text a string to encode
54     * @param charset a charset to be used
55     *
56     * @return RFC 1522 compliant "encoded-word"
57     *
58     * @throws EncoderException thrown if there is an error conidition during the Encoding
59     *  process.
60     * @throws UnsupportedEncodingException thrown if charset is not supported
61     *
62     * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
63     *          encoding names</a>
64     */
65    protected String encodeText(final String text, final String charset)
66     throws EncoderException, UnsupportedEncodingException
67    {
68        if (text == null) {
69            return null;
70        }
71        StringBuffer buffer = new StringBuffer();
72        buffer.append("=?");
73        buffer.append(charset);
74        buffer.append('?');
75        buffer.append(getEncoding());
76        buffer.append('?');
77        byte [] rawdata = doEncoding(text.getBytes(charset));
78        buffer.append(new String(rawdata, StringEncodings.US_ASCII));
79        buffer.append("?=");
80        return buffer.toString();
81    }
82
83    /**
84     * Applies an RFC 1522 compliant decoding scheme to the given string of text. This method
85     * processes the "encoded-word" header common to all the RFC 1522 codecs and then invokes
86     * {@link #doEncoding(byte [])} method of a concrete class to perform the specific deconding.
87     *
88     * @param text a string to decode
89     *
90     * @throws DecoderException thrown if there is an error conidition during the Decoding
91     *  process.
92     * @throws UnsupportedEncodingException thrown if charset specified in the "encoded-word"
93     *  header is not supported
94     */
95    protected String decodeText(final String text)
96     throws DecoderException, UnsupportedEncodingException
97    {
98        if (text == null) {
99            return null;
100        }
101        if ((!text.startsWith("=?")) || (!text.endsWith("?="))) {
102            throw new DecoderException("RFC 1522 violation: malformed encoded content");
103        }
104        int termnator = text.length() - 2;
105        int from = 2;
106        int to = text.indexOf("?", from);
107        if ((to == -1) || (to == termnator)) {
108            throw new DecoderException("RFC 1522 violation: charset token not found");
109        }
110        String charset = text.substring(from, to);
111        if (charset.equals("")) {
112            throw new DecoderException("RFC 1522 violation: charset not specified");
113        }
114        from = to + 1;
115        to = text.indexOf("?", from);
116        if ((to == -1) || (to == termnator)) {
117            throw new DecoderException("RFC 1522 violation: encoding token not found");
118        }
119        String encoding = text.substring(from, to);
120        if (!getEncoding().equalsIgnoreCase(encoding)) {
121            throw new DecoderException("This codec cannot decode " +
122                encoding + " encoded content");
123        }
124        from = to + 1;
125        to = text.indexOf("?", from);
126        byte[] data = text.substring(from, to).getBytes(StringEncodings.US_ASCII);
127        data = doDecoding(data);
128        return new String(data, charset);
129    }
130
131    /**
132     * Returns the codec name (referred to as encoding in the RFC 1522)
133     *
134     * @return name of the codec
135     */
136    protected abstract String getEncoding();
137
138    /**
139     * Encodes an array of bytes using the defined encoding scheme
140     *
141     * @param bytes Data to be encoded
142     *
143     * @return A byte array containing the encoded data
144     *
145     * @throws EncoderException thrown if the Encoder encounters a failure condition
146     *  during the encoding process.
147     */
148    protected abstract byte[] doEncoding(byte[] bytes) throws EncoderException;
149
150    /**
151     * Decodes an array of bytes using the defined encoding scheme
152     *
153     * @param bytes Data to be decoded
154     *
155     * @return a byte array that contains decoded data
156     *
157     * @throws DecoderException A decoder exception is thrown if a Decoder encounters a
158     *  failure condition during the decode process.
159     */
160    protected abstract byte[] doDecoding(byte[] bytes) throws DecoderException;
161}
162