View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.codec.net;
19  
20  import java.io.UnsupportedEncodingException;
21  import java.nio.charset.Charset;
22  import java.nio.charset.StandardCharsets;
23  import java.nio.charset.UnsupportedCharsetException;
24  import java.util.BitSet;
25  
26  import org.apache.commons.codec.DecoderException;
27  import org.apache.commons.codec.EncoderException;
28  import org.apache.commons.codec.StringDecoder;
29  import org.apache.commons.codec.StringEncoder;
30  
31  /**
32   * Similar to the Quoted-Printable content-transfer-encoding defined in
33   * <a href="https://www.ietf.org/rfc/rfc1521.txt">RFC 1521</a> and designed to allow text containing mostly ASCII
34   * characters to be decipherable on an ASCII terminal without decoding.
35   * <p>
36   * <a href="https://www.ietf.org/rfc/rfc1522.txt">RFC 1522</a> describes techniques to allow the encoding of non-ASCII
37   * text in various portions of a RFC 822 [2] message header, in a manner which is unlikely to confuse existing message
38   * handling software.
39   * </p>
40   * <p>
41   * This class is conditionally thread-safe.
42   * The instance field for encoding blanks is mutable {@link #setEncodeBlanks(boolean)}
43   * but is not volatile, and accesses are not synchronized.
44   * If an instance of the class is shared between threads, the caller needs to ensure that suitable synchronization
45   * is used to ensure safe publication of the value between threads, and must not invoke
46   * {@link #setEncodeBlanks(boolean)} after initial setup.
47   * </p>
48   *
49   * @see <a href="https://www.ietf.org/rfc/rfc1522.txt">MIME (Multipurpose Internet Mail Extensions) Part Two: Message
50   *          Header Extensions for Non-ASCII Text</a>
51   *
52   * @since 1.3
53   */
54  public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder {
55  
56      /**
57       * BitSet of printable characters as defined in RFC 1522.
58       */
59      private static final BitSet PRINTABLE_CHARS = new BitSet(256);
60  
61      // Static initializer for printable chars collection
62      static {
63          // alpha characters
64          PRINTABLE_CHARS.set(' ');
65          PRINTABLE_CHARS.set('!');
66          PRINTABLE_CHARS.set('"');
67          PRINTABLE_CHARS.set('#');
68          PRINTABLE_CHARS.set('$');
69          PRINTABLE_CHARS.set('%');
70          PRINTABLE_CHARS.set('&');
71          PRINTABLE_CHARS.set('\'');
72          PRINTABLE_CHARS.set('(');
73          PRINTABLE_CHARS.set(')');
74          PRINTABLE_CHARS.set('*');
75          PRINTABLE_CHARS.set('+');
76          PRINTABLE_CHARS.set(',');
77          PRINTABLE_CHARS.set('-');
78          PRINTABLE_CHARS.set('.');
79          PRINTABLE_CHARS.set('/');
80          for (int i = '0'; i <= '9'; i++) {
81              PRINTABLE_CHARS.set(i);
82          }
83          PRINTABLE_CHARS.set(':');
84          PRINTABLE_CHARS.set(';');
85          PRINTABLE_CHARS.set('<');
86          PRINTABLE_CHARS.set('>');
87          PRINTABLE_CHARS.set('@');
88          for (int i = 'A'; i <= 'Z'; i++) {
89              PRINTABLE_CHARS.set(i);
90          }
91          PRINTABLE_CHARS.set('[');
92          PRINTABLE_CHARS.set('\\');
93          PRINTABLE_CHARS.set(']');
94          PRINTABLE_CHARS.set('^');
95          PRINTABLE_CHARS.set('`');
96          for (int i = 'a'; i <= 'z'; i++) {
97              PRINTABLE_CHARS.set(i);
98          }
99          PRINTABLE_CHARS.set('{');
100         PRINTABLE_CHARS.set('|');
101         PRINTABLE_CHARS.set('}');
102         PRINTABLE_CHARS.set('~');
103     }
104     private static final byte SPACE = 32;
105 
106     private static final byte UNDERSCORE = 95;
107 
108     private boolean encodeBlanks;
109 
110     /**
111      * Default constructor.
112      */
113     public QCodec() {
114         this(StandardCharsets.UTF_8);
115     }
116 
117     /**
118      * Constructor which allows for the selection of a default Charset.
119      *
120      * @param charset
121      *            the default string Charset to use.
122      *
123      * @see Charset
124      * @since 1.7
125      */
126     public QCodec(final Charset charset) {
127         super(charset);
128     }
129 
130     /**
131      * Constructor which allows for the selection of a default Charset.
132      *
133      * @param charsetName
134      *            the Charset to use.
135      * @throws java.nio.charset.UnsupportedCharsetException
136      *             If the named Charset is unavailable.
137      * @since 1.7 throws UnsupportedCharsetException if the named Charset is unavailable
138      * @see Charset
139      */
140     public QCodec(final String charsetName) {
141         this(Charset.forName(charsetName));
142     }
143 
144     /**
145      * Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original
146      * representation.
147      *
148      * @param obj
149      *            quoted-printable object to convert into its original form.
150      * @return original object.
151      * @throws DecoderException
152      *             Thrown if the argument is not a {@code String}. Thrown if a failure condition is encountered
153      *             during the decode process.
154      */
155     @Override
156     public Object decode(final Object obj) throws DecoderException {
157         if (obj == null) {
158             return null;
159         }
160         if (obj instanceof String) {
161             return decode((String) obj);
162         }
163         throw new DecoderException("Objects of type " + obj.getClass().getName() + " cannot be decoded using Q codec");
164     }
165 
166     /**
167      * Decodes a quoted-printable string into its original form. Escaped characters are converted back to their original
168      * representation.
169      *
170      * @param str
171      *            quoted-printable string to convert into its original form.
172      * @return original string.
173      * @throws DecoderException
174      *             A decoder exception is thrown if a failure condition is encountered during the decode process.
175      */
176     @Override
177     public String decode(final String str) throws DecoderException {
178         try {
179             return decodeText(str);
180         } catch (final UnsupportedEncodingException e) {
181             throw new DecoderException(e.getMessage(), e);
182         }
183     }
184 
185     @Override
186     protected byte[] doDecoding(final byte[] bytes) throws DecoderException {
187         if (bytes == null) {
188             return null;
189         }
190         boolean hasUnderscores = false;
191         for (final byte b : bytes) {
192             if (b == UNDERSCORE) {
193                 hasUnderscores = true;
194                 break;
195             }
196         }
197         if (hasUnderscores) {
198             final byte[] tmp = new byte[bytes.length];
199             for (int i = 0; i < bytes.length; i++) {
200                 final byte b = bytes[i];
201                 if (b != UNDERSCORE) {
202                     tmp[i] = b;
203                 } else {
204                     tmp[i] = SPACE;
205                 }
206             }
207             return QuotedPrintableCodec.decodeQuotedPrintable(tmp);
208         }
209         return QuotedPrintableCodec.decodeQuotedPrintable(bytes);
210     }
211 
212     @Override
213     protected byte[] doEncoding(final byte[] bytes) {
214         if (bytes == null) {
215             return null;
216         }
217         final byte[] data = QuotedPrintableCodec.encodeQuotedPrintable(PRINTABLE_CHARS, bytes);
218         if (this.encodeBlanks) {
219             for (int i = 0; i < data.length; i++) {
220                 if (data[i] == SPACE) {
221                     data[i] = UNDERSCORE;
222                 }
223             }
224         }
225         return data;
226     }
227 
228     /**
229      * Encodes an object into its quoted-printable form using the default Charset. Unsafe characters are escaped.
230      *
231      * @param obj
232      *            object to convert to quoted-printable form.
233      * @return quoted-printable object.
234      * @throws EncoderException
235      *             thrown if a failure condition is encountered during the encoding process.
236      */
237     @Override
238     public Object encode(final Object obj) throws EncoderException {
239         if (obj == null) {
240             return null;
241         }
242         if (obj instanceof String) {
243             return encode((String) obj);
244         }
245         throw new EncoderException("Objects of type " + obj.getClass().getName() + " cannot be encoded using Q codec");
246     }
247 
248     /**
249      * Encodes a string into its quoted-printable form using the default Charset. Unsafe characters are escaped.
250      *
251      * @param sourceStr
252      *            string to convert to quoted-printable form.
253      * @return quoted-printable string.
254      * @throws EncoderException
255      *             thrown if a failure condition is encountered during the encoding process.
256      */
257     @Override
258     public String encode(final String sourceStr) throws EncoderException {
259         return encode(sourceStr, getCharset());
260     }
261 
262     /**
263      * Encodes a string into its quoted-printable form using the specified Charset. Unsafe characters are escaped.
264      *
265      * @param sourceStr
266      *            string to convert to quoted-printable form.
267      * @param sourceCharset
268      *            the Charset for sourceStr.
269      * @return quoted-printable string.
270      * @throws EncoderException
271      *             thrown if a failure condition is encountered during the encoding process.
272      * @since 1.7
273      */
274     public String encode(final String sourceStr, final Charset sourceCharset) throws EncoderException {
275         return encodeText(sourceStr, sourceCharset);
276     }
277 
278     /**
279      * Encodes a string into its quoted-printable form using the specified Charset. Unsafe characters are escaped.
280      *
281      * @param sourceStr
282      *            string to convert to quoted-printable form.
283      * @param sourceCharset
284      *            the Charset for sourceStr.
285      * @return quoted-printable string.
286      * @throws EncoderException
287      *             thrown if a failure condition is encountered during the encoding process.
288      */
289     public String encode(final String sourceStr, final String sourceCharset) throws EncoderException {
290         try {
291             return encodeText(sourceStr, sourceCharset);
292         } catch (final UnsupportedCharsetException e) {
293             throw new EncoderException(e.getMessage(), e);
294         }
295     }
296 
297     @Override
298     protected String getEncoding() {
299         return "Q";
300     }
301 
302     /**
303      * Tests if optional transformation of SPACE characters is to be used
304      *
305      * @return {@code true} if SPACE characters are to be transformed, {@code false} otherwise.
306      */
307     public boolean isEncodeBlanks() {
308         return this.encodeBlanks;
309     }
310 
311     /**
312      * Defines whether optional transformation of SPACE characters is to be used
313      *
314      * @param b
315      *            {@code true} if SPACE characters are to be transformed, {@code false} otherwise.
316      */
317     public void setEncodeBlanks(final boolean b) {
318         this.encodeBlanks = b;
319     }
320 }