Fri, 04 Oct 2013 16:21:34 +0100
8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.org.jvnet.mimepull; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.*; |
aoqi@0 | 29 | |
aoqi@0 | 30 | |
aoqi@0 | 31 | /** |
aoqi@0 | 32 | * This is a utility class that provides various MIME related |
aoqi@0 | 33 | * functionality. <p> |
aoqi@0 | 34 | * |
aoqi@0 | 35 | * There are a set of methods to encode and decode MIME headers as |
aoqi@0 | 36 | * per RFC 2047. Note that, in general, these methods are |
aoqi@0 | 37 | * <strong>not</strong> needed when using methods such as |
aoqi@0 | 38 | * <code>setSubject</code> and <code>setRecipients</code>; JavaMail |
aoqi@0 | 39 | * will automatically encode and decode data when using these "higher |
aoqi@0 | 40 | * level" methods. The methods below are only needed when maniuplating |
aoqi@0 | 41 | * raw MIME headers using <code>setHeader</code> and <code>getHeader</code> |
aoqi@0 | 42 | * methods. A brief description on handling such headers is given below: <p> |
aoqi@0 | 43 | * |
aoqi@0 | 44 | * RFC 822 mail headers <strong>must</strong> contain only US-ASCII |
aoqi@0 | 45 | * characters. Headers that contain non US-ASCII characters must be |
aoqi@0 | 46 | * encoded so that they contain only US-ASCII characters. Basically, |
aoqi@0 | 47 | * this process involves using either BASE64 or QP to encode certain |
aoqi@0 | 48 | * characters. RFC 2047 describes this in detail. <p> |
aoqi@0 | 49 | * |
aoqi@0 | 50 | * In Java, Strings contain (16 bit) Unicode characters. ASCII is a |
aoqi@0 | 51 | * subset of Unicode (and occupies the range 0 - 127). A String |
aoqi@0 | 52 | * that contains only ASCII characters is already mail-safe. If the |
aoqi@0 | 53 | * String contains non US-ASCII characters, it must be encoded. An |
aoqi@0 | 54 | * additional complexity in this step is that since Unicode is not |
aoqi@0 | 55 | * yet a widely used charset, one might want to first charset-encode |
aoqi@0 | 56 | * the String into another charset and then do the transfer-encoding. |
aoqi@0 | 57 | * <p> |
aoqi@0 | 58 | * Note that to get the actual bytes of a mail-safe String (say, |
aoqi@0 | 59 | * for sending over SMTP), one must do |
aoqi@0 | 60 | * <p><blockquote><pre> |
aoqi@0 | 61 | * |
aoqi@0 | 62 | * byte[] bytes = string.getBytes("iso-8859-1"); |
aoqi@0 | 63 | * |
aoqi@0 | 64 | * </pre></blockquote><p> |
aoqi@0 | 65 | * |
aoqi@0 | 66 | * The <code>setHeader</code> and <code>addHeader</code> methods |
aoqi@0 | 67 | * on MimeMessage and MimeBodyPart assume that the given header values |
aoqi@0 | 68 | * are Unicode strings that contain only US-ASCII characters. Hence |
aoqi@0 | 69 | * the callers of those methods must insure that the values they pass |
aoqi@0 | 70 | * do not contain non US-ASCII characters. The methods in this class |
aoqi@0 | 71 | * help do this. <p> |
aoqi@0 | 72 | * |
aoqi@0 | 73 | * The <code>getHeader</code> family of methods on MimeMessage and |
aoqi@0 | 74 | * MimeBodyPart return the raw header value. These might be encoded |
aoqi@0 | 75 | * as per RFC 2047, and if so, must be decoded into Unicode Strings. |
aoqi@0 | 76 | * The methods in this class help to do this. <p> |
aoqi@0 | 77 | * |
aoqi@0 | 78 | * Several System properties control strict conformance to the MIME |
aoqi@0 | 79 | * spec. Note that these are not session properties but must be set |
aoqi@0 | 80 | * globally as System properties. <p> |
aoqi@0 | 81 | * |
aoqi@0 | 82 | * The <code>mail.mime.decodetext.strict</code> property controls |
aoqi@0 | 83 | * decoding of MIME encoded words. The MIME spec requires that encoded |
aoqi@0 | 84 | * words start at the beginning of a whitespace separated word. Some |
aoqi@0 | 85 | * mailers incorrectly include encoded words in the middle of a word. |
aoqi@0 | 86 | * If the <code>mail.mime.decodetext.strict</code> System property is |
aoqi@0 | 87 | * set to <code>"false"</code>, an attempt will be made to decode these |
aoqi@0 | 88 | * illegal encoded words. The default is true. <p> |
aoqi@0 | 89 | * |
aoqi@0 | 90 | * The <code>mail.mime.encodeeol.strict</code> property controls the |
aoqi@0 | 91 | * choice of Content-Transfer-Encoding for MIME parts that are not of |
aoqi@0 | 92 | * type "text". Often such parts will contain textual data for which |
aoqi@0 | 93 | * an encoding that allows normal end of line conventions is appropriate. |
aoqi@0 | 94 | * In rare cases, such a part will appear to contain entirely textual |
aoqi@0 | 95 | * data, but will require an encoding that preserves CR and LF characters |
aoqi@0 | 96 | * without change. If the <code>mail.mime.encodeeol.strict</code> |
aoqi@0 | 97 | * System property is set to <code>"true"</code>, such an encoding will |
aoqi@0 | 98 | * be used when necessary. The default is false. <p> |
aoqi@0 | 99 | * |
aoqi@0 | 100 | * In addition, the <code>mail.mime.charset</code> System property can |
aoqi@0 | 101 | * be used to specify the default MIME charset to use for encoded words |
aoqi@0 | 102 | * and text parts that don't otherwise specify a charset. Normally, the |
aoqi@0 | 103 | * default MIME charset is derived from the default Java charset, as |
aoqi@0 | 104 | * specified in the <code>file.encoding</code> System property. Most |
aoqi@0 | 105 | * applications will have no need to explicitly set the default MIME |
aoqi@0 | 106 | * charset. In cases where the default MIME charset to be used for |
aoqi@0 | 107 | * mail messages is different than the charset used for files stored on |
aoqi@0 | 108 | * the system, this property should be set. <p> |
aoqi@0 | 109 | * |
aoqi@0 | 110 | * The current implementation also supports the following System property. |
aoqi@0 | 111 | * <p> |
aoqi@0 | 112 | * The <code>mail.mime.ignoreunknownencoding</code> property controls |
aoqi@0 | 113 | * whether unknown values in the <code>Content-Transfer-Encoding</code> |
aoqi@0 | 114 | * header, as passed to the <code>decode</code> method, cause an exception. |
aoqi@0 | 115 | * If set to <code>"true"</code>, unknown values are ignored and 8bit |
aoqi@0 | 116 | * encoding is assumed. Otherwise, unknown values cause a MessagingException |
aoqi@0 | 117 | * to be thrown. |
aoqi@0 | 118 | * |
aoqi@0 | 119 | * @author John Mani |
aoqi@0 | 120 | * @author Bill Shannon |
aoqi@0 | 121 | */ |
aoqi@0 | 122 | |
aoqi@0 | 123 | /* FROM mail.jar */ |
aoqi@0 | 124 | final class MimeUtility { |
aoqi@0 | 125 | |
aoqi@0 | 126 | // This class cannot be instantiated |
aoqi@0 | 127 | private MimeUtility() { } |
aoqi@0 | 128 | |
aoqi@0 | 129 | private static final boolean ignoreUnknownEncoding = |
aoqi@0 | 130 | PropUtil.getBooleanSystemProperty( |
aoqi@0 | 131 | "mail.mime.ignoreunknownencoding", false); |
aoqi@0 | 132 | |
aoqi@0 | 133 | /** |
aoqi@0 | 134 | * Decode the given input stream. The Input stream returned is |
aoqi@0 | 135 | * the decoded input stream. All the encodings defined in RFC 2045 |
aoqi@0 | 136 | * are supported here. They include "base64", "quoted-printable", |
aoqi@0 | 137 | * "7bit", "8bit", and "binary". In addition, "uuencode" is also |
aoqi@0 | 138 | * supported. <p> |
aoqi@0 | 139 | * |
aoqi@0 | 140 | * In the current implementation, if the |
aoqi@0 | 141 | * <code>mail.mime.ignoreunknownencoding</code> system property is set to |
aoqi@0 | 142 | * <code>"true"</code>, unknown encoding values are ignored and the |
aoqi@0 | 143 | * original InputStream is returned. |
aoqi@0 | 144 | * |
aoqi@0 | 145 | * @param is input stream |
aoqi@0 | 146 | * @param encoding the encoding of the stream. |
aoqi@0 | 147 | * @return decoded input stream. |
aoqi@0 | 148 | * @exception MessagingException if the encoding is unknown |
aoqi@0 | 149 | */ |
aoqi@0 | 150 | public static InputStream decode(InputStream is, String encoding) |
aoqi@0 | 151 | throws DecodingException { |
aoqi@0 | 152 | if (encoding.equalsIgnoreCase("base64")) |
aoqi@0 | 153 | return new BASE64DecoderStream(is); |
aoqi@0 | 154 | else if (encoding.equalsIgnoreCase("quoted-printable")) |
aoqi@0 | 155 | return new QPDecoderStream(is); |
aoqi@0 | 156 | else if (encoding.equalsIgnoreCase("uuencode") || |
aoqi@0 | 157 | encoding.equalsIgnoreCase("x-uuencode") || |
aoqi@0 | 158 | encoding.equalsIgnoreCase("x-uue")) |
aoqi@0 | 159 | return new UUDecoderStream(is); |
aoqi@0 | 160 | else if (encoding.equalsIgnoreCase("binary") || |
aoqi@0 | 161 | encoding.equalsIgnoreCase("7bit") || |
aoqi@0 | 162 | encoding.equalsIgnoreCase("8bit")) |
aoqi@0 | 163 | return is; |
aoqi@0 | 164 | else { |
aoqi@0 | 165 | if (!ignoreUnknownEncoding) { |
aoqi@0 | 166 | throw new DecodingException("Unknown encoding: " + encoding); |
aoqi@0 | 167 | } |
aoqi@0 | 168 | return is; |
aoqi@0 | 169 | } |
aoqi@0 | 170 | } |
aoqi@0 | 171 | } |