001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.codec.net;
018:
019: import java.io.UnsupportedEncodingException;
020:
021: import org.apache.commons.codec.DecoderException;
022: import org.apache.commons.codec.EncoderException;
023:
024: /**
025: * <p>
026: * Implements methods common to all codecs defined in RFC 1522.
027: * </p>
028: *
029: * <p>
030: * <a href="http://www.ietf.org/rfc/rfc1522.txt">RFC 1522</a>
031: * describes techniques to allow the encoding of non-ASCII text in
032: * various portions of a RFC 822 [2] message header, in a manner which
033: * is unlikely to confuse existing message handling software.
034: * </p>
035:
036: * @see <a href="http://www.ietf.org/rfc/rfc1522.txt">
037: * MIME (Multipurpose Internet Mail Extensions) Part Two:
038: * Message Header Extensions for Non-ASCII Text</a>
039: * </p>
040: *
041: * @author Apache Software Foundation
042: * @since 1.3
043: * @version $Id: RFC1522Codec.java,v 1.2 2004/04/09 22:21:43 ggregory Exp $
044: */
045: abstract class RFC1522Codec {
046:
047: /**
048: * Applies an RFC 1522 compliant encoding scheme to the given string of text with the
049: * given charset. This method constructs the "encoded-word" header common to all the
050: * RFC 1522 codecs and then invokes {@link #doEncoding(byte [])} method of a concrete
051: * class to perform the specific enconding.
052: *
053: * @param text a string to encode
054: * @param charset a charset to be used
055: *
056: * @return RFC 1522 compliant "encoded-word"
057: *
058: * @throws EncoderException thrown if there is an error conidition during the Encoding
059: * process.
060: * @throws UnsupportedEncodingException thrown if charset is not supported
061: *
062: * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
063: * encoding names</a>
064: */
065: protected String encodeText(final String text, final String charset)
066: throws EncoderException, UnsupportedEncodingException {
067: if (text == null) {
068: return null;
069: }
070: StringBuffer buffer = new StringBuffer();
071: buffer.append("=?");
072: buffer.append(charset);
073: buffer.append('?');
074: buffer.append(getEncoding());
075: buffer.append('?');
076: byte[] rawdata = doEncoding(text.getBytes(charset));
077: buffer.append(new String(rawdata, StringEncodings.US_ASCII));
078: buffer.append("?=");
079: return buffer.toString();
080: }
081:
082: /**
083: * Applies an RFC 1522 compliant decoding scheme to the given string of text. This method
084: * processes the "encoded-word" header common to all the RFC 1522 codecs and then invokes
085: * {@link #doEncoding(byte [])} method of a concrete class to perform the specific deconding.
086: *
087: * @param text a string to decode
088: *
089: * @throws DecoderException thrown if there is an error conidition during the Decoding
090: * process.
091: * @throws UnsupportedEncodingException thrown if charset specified in the "encoded-word"
092: * header is not supported
093: */
094: protected String decodeText(final String text)
095: throws DecoderException, UnsupportedEncodingException {
096: if (text == null) {
097: return null;
098: }
099: if ((!text.startsWith("=?")) || (!text.endsWith("?="))) {
100: throw new DecoderException(
101: "RFC 1522 violation: malformed encoded content");
102: }
103: int termnator = text.length() - 2;
104: int from = 2;
105: int to = text.indexOf("?", from);
106: if ((to == -1) || (to == termnator)) {
107: throw new DecoderException(
108: "RFC 1522 violation: charset token not found");
109: }
110: String charset = text.substring(from, to);
111: if (charset.equals("")) {
112: throw new DecoderException(
113: "RFC 1522 violation: charset not specified");
114: }
115: from = to + 1;
116: to = text.indexOf("?", from);
117: if ((to == -1) || (to == termnator)) {
118: throw new DecoderException(
119: "RFC 1522 violation: encoding token not found");
120: }
121: String encoding = text.substring(from, to);
122: if (!getEncoding().equalsIgnoreCase(encoding)) {
123: throw new DecoderException("This codec cannot decode "
124: + encoding + " encoded content");
125: }
126: from = to + 1;
127: to = text.indexOf("?", from);
128: byte[] data = text.substring(from, to).getBytes(
129: StringEncodings.US_ASCII);
130: data = doDecoding(data);
131: return new String(data, charset);
132: }
133:
134: /**
135: * Returns the codec name (referred to as encoding in the RFC 1522)
136: *
137: * @return name of the codec
138: */
139: protected abstract String getEncoding();
140:
141: /**
142: * Encodes an array of bytes using the defined encoding scheme
143: *
144: * @param bytes Data to be encoded
145: *
146: * @return A byte array containing the encoded data
147: *
148: * @throws EncoderException thrown if the Encoder encounters a failure condition
149: * during the encoding process.
150: */
151: protected abstract byte[] doEncoding(byte[] bytes)
152: throws EncoderException;
153:
154: /**
155: * Decodes an array of bytes using the defined encoding scheme
156: *
157: * @param bytes Data to be decoded
158: *
159: * @return a byte array that contains decoded data
160: *
161: * @throws DecoderException A decoder exception is thrown if a Decoder encounters a
162: * failure condition during the decode process.
163: */
164: protected abstract byte[] doDecoding(byte[] bytes)
165: throws DecoderException;
166: }
|