• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package org.apache.commons.codec.net;
18 
19 import java.io.UnsupportedEncodingException;
20 
21 import org.apache.commons.codec.DecoderException;
22 import 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  *
45  * @deprecated Please use {@link java.net.URL#openConnection} instead.
46  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
47  *     for further details.
48  */
49 @Deprecated
50 abstract class RFC1522Codec {
51 
52     /**
53      * Applies an RFC 1522 compliant encoding scheme to the given string of text with the
54      * given charset. This method constructs the "encoded-word" header common to all the
55      * RFC 1522 codecs and then invokes {@link #doEncoding(byte [])} method of a concrete
56      * class to perform the specific enconding.
57      *
58      * @param text a string to encode
59      * @param charset a charset to be used
60      *
61      * @return RFC 1522 compliant "encoded-word"
62      *
63      * @throws EncoderException thrown if there is an error conidition during the Encoding
64      *  process.
65      * @throws UnsupportedEncodingException thrown if charset is not supported
66      *
67      * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
68      *          encoding names</a>
69      */
encodeText(final String text, final String charset)70     protected String encodeText(final String text, final String charset)
71      throws EncoderException, UnsupportedEncodingException
72     {
73         if (text == null) {
74             return null;
75         }
76         StringBuffer buffer = new StringBuffer();
77         buffer.append("=?");
78         buffer.append(charset);
79         buffer.append('?');
80         buffer.append(getEncoding());
81         buffer.append('?');
82         byte [] rawdata = doEncoding(text.getBytes(charset));
83         buffer.append(new String(rawdata, StringEncodings.US_ASCII));
84         buffer.append("?=");
85         return buffer.toString();
86     }
87 
88     /**
89      * Applies an RFC 1522 compliant decoding scheme to the given string of text. This method
90      * processes the "encoded-word" header common to all the RFC 1522 codecs and then invokes
91      * {@link #doEncoding(byte [])} method of a concrete class to perform the specific deconding.
92      *
93      * @param text a string to decode
94      *
95      * @throws DecoderException thrown if there is an error conidition during the Decoding
96      *  process.
97      * @throws UnsupportedEncodingException thrown if charset specified in the "encoded-word"
98      *  header is not supported
99      */
decodeText(final String text)100     protected String decodeText(final String text)
101      throws DecoderException, UnsupportedEncodingException
102     {
103         if (text == null) {
104             return null;
105         }
106         if ((!text.startsWith("=?")) || (!text.endsWith("?="))) {
107             throw new DecoderException("RFC 1522 violation: malformed encoded content");
108         }
109         int termnator = text.length() - 2;
110         int from = 2;
111         int to = text.indexOf("?", from);
112         if ((to == -1) || (to == termnator)) {
113             throw new DecoderException("RFC 1522 violation: charset token not found");
114         }
115         String charset = text.substring(from, to);
116         if (charset.equals("")) {
117             throw new DecoderException("RFC 1522 violation: charset not specified");
118         }
119         from = to + 1;
120         to = text.indexOf("?", from);
121         if ((to == -1) || (to == termnator)) {
122             throw new DecoderException("RFC 1522 violation: encoding token not found");
123         }
124         String encoding = text.substring(from, to);
125         if (!getEncoding().equalsIgnoreCase(encoding)) {
126             throw new DecoderException("This codec cannot decode " +
127                 encoding + " encoded content");
128         }
129         from = to + 1;
130         to = text.indexOf("?", from);
131         byte[] data = text.substring(from, to).getBytes(StringEncodings.US_ASCII);
132         data = doDecoding(data);
133         return new String(data, charset);
134     }
135 
136     /**
137      * Returns the codec name (referred to as encoding in the RFC 1522)
138      *
139      * @return name of the codec
140      */
getEncoding()141     protected abstract String getEncoding();
142 
143     /**
144      * Encodes an array of bytes using the defined encoding scheme
145      *
146      * @param bytes Data to be encoded
147      *
148      * @return A byte array containing the encoded data
149      *
150      * @throws EncoderException thrown if the Encoder encounters a failure condition
151      *  during the encoding process.
152      */
doEncoding(byte[] bytes)153     protected abstract byte[] doEncoding(byte[] bytes) throws EncoderException;
154 
155     /**
156      * Decodes an array of bytes using the defined encoding scheme
157      *
158      * @param bytes Data to be decoded
159      *
160      * @return a byte array that contains decoded data
161      *
162      * @throws DecoderException A decoder exception is thrown if a Decoder encounters a
163      *  failure condition during the decode process.
164      */
doDecoding(byte[] bytes)165     protected abstract byte[] doDecoding(byte[] bytes) throws DecoderException;
166 }
167