1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/MimeUtility.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,171 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.org.jvnet.mimepull; 1.30 + 1.31 +import java.io.*; 1.32 + 1.33 + 1.34 +/** 1.35 + * This is a utility class that provides various MIME related 1.36 + * functionality. <p> 1.37 + * 1.38 + * There are a set of methods to encode and decode MIME headers as 1.39 + * per RFC 2047. Note that, in general, these methods are 1.40 + * <strong>not</strong> needed when using methods such as 1.41 + * <code>setSubject</code> and <code>setRecipients</code>; JavaMail 1.42 + * will automatically encode and decode data when using these "higher 1.43 + * level" methods. The methods below are only needed when maniuplating 1.44 + * raw MIME headers using <code>setHeader</code> and <code>getHeader</code> 1.45 + * methods. A brief description on handling such headers is given below: <p> 1.46 + * 1.47 + * RFC 822 mail headers <strong>must</strong> contain only US-ASCII 1.48 + * characters. Headers that contain non US-ASCII characters must be 1.49 + * encoded so that they contain only US-ASCII characters. Basically, 1.50 + * this process involves using either BASE64 or QP to encode certain 1.51 + * characters. RFC 2047 describes this in detail. <p> 1.52 + * 1.53 + * In Java, Strings contain (16 bit) Unicode characters. ASCII is a 1.54 + * subset of Unicode (and occupies the range 0 - 127). A String 1.55 + * that contains only ASCII characters is already mail-safe. If the 1.56 + * String contains non US-ASCII characters, it must be encoded. An 1.57 + * additional complexity in this step is that since Unicode is not 1.58 + * yet a widely used charset, one might want to first charset-encode 1.59 + * the String into another charset and then do the transfer-encoding. 1.60 + * <p> 1.61 + * Note that to get the actual bytes of a mail-safe String (say, 1.62 + * for sending over SMTP), one must do 1.63 + * <p><blockquote><pre> 1.64 + * 1.65 + * byte[] bytes = string.getBytes("iso-8859-1"); 1.66 + * 1.67 + * </pre></blockquote><p> 1.68 + * 1.69 + * The <code>setHeader</code> and <code>addHeader</code> methods 1.70 + * on MimeMessage and MimeBodyPart assume that the given header values 1.71 + * are Unicode strings that contain only US-ASCII characters. Hence 1.72 + * the callers of those methods must insure that the values they pass 1.73 + * do not contain non US-ASCII characters. The methods in this class 1.74 + * help do this. <p> 1.75 + * 1.76 + * The <code>getHeader</code> family of methods on MimeMessage and 1.77 + * MimeBodyPart return the raw header value. These might be encoded 1.78 + * as per RFC 2047, and if so, must be decoded into Unicode Strings. 1.79 + * The methods in this class help to do this. <p> 1.80 + * 1.81 + * Several System properties control strict conformance to the MIME 1.82 + * spec. Note that these are not session properties but must be set 1.83 + * globally as System properties. <p> 1.84 + * 1.85 + * The <code>mail.mime.decodetext.strict</code> property controls 1.86 + * decoding of MIME encoded words. The MIME spec requires that encoded 1.87 + * words start at the beginning of a whitespace separated word. Some 1.88 + * mailers incorrectly include encoded words in the middle of a word. 1.89 + * If the <code>mail.mime.decodetext.strict</code> System property is 1.90 + * set to <code>"false"</code>, an attempt will be made to decode these 1.91 + * illegal encoded words. The default is true. <p> 1.92 + * 1.93 + * The <code>mail.mime.encodeeol.strict</code> property controls the 1.94 + * choice of Content-Transfer-Encoding for MIME parts that are not of 1.95 + * type "text". Often such parts will contain textual data for which 1.96 + * an encoding that allows normal end of line conventions is appropriate. 1.97 + * In rare cases, such a part will appear to contain entirely textual 1.98 + * data, but will require an encoding that preserves CR and LF characters 1.99 + * without change. If the <code>mail.mime.encodeeol.strict</code> 1.100 + * System property is set to <code>"true"</code>, such an encoding will 1.101 + * be used when necessary. The default is false. <p> 1.102 + * 1.103 + * In addition, the <code>mail.mime.charset</code> System property can 1.104 + * be used to specify the default MIME charset to use for encoded words 1.105 + * and text parts that don't otherwise specify a charset. Normally, the 1.106 + * default MIME charset is derived from the default Java charset, as 1.107 + * specified in the <code>file.encoding</code> System property. Most 1.108 + * applications will have no need to explicitly set the default MIME 1.109 + * charset. In cases where the default MIME charset to be used for 1.110 + * mail messages is different than the charset used for files stored on 1.111 + * the system, this property should be set. <p> 1.112 + * 1.113 + * The current implementation also supports the following System property. 1.114 + * <p> 1.115 + * The <code>mail.mime.ignoreunknownencoding</code> property controls 1.116 + * whether unknown values in the <code>Content-Transfer-Encoding</code> 1.117 + * header, as passed to the <code>decode</code> method, cause an exception. 1.118 + * If set to <code>"true"</code>, unknown values are ignored and 8bit 1.119 + * encoding is assumed. Otherwise, unknown values cause a MessagingException 1.120 + * to be thrown. 1.121 + * 1.122 + * @author John Mani 1.123 + * @author Bill Shannon 1.124 + */ 1.125 + 1.126 +/* FROM mail.jar */ 1.127 +final class MimeUtility { 1.128 + 1.129 + // This class cannot be instantiated 1.130 + private MimeUtility() { } 1.131 + 1.132 + private static final boolean ignoreUnknownEncoding = 1.133 + PropUtil.getBooleanSystemProperty( 1.134 + "mail.mime.ignoreunknownencoding", false); 1.135 + 1.136 + /** 1.137 + * Decode the given input stream. The Input stream returned is 1.138 + * the decoded input stream. All the encodings defined in RFC 2045 1.139 + * are supported here. They include "base64", "quoted-printable", 1.140 + * "7bit", "8bit", and "binary". In addition, "uuencode" is also 1.141 + * supported. <p> 1.142 + * 1.143 + * In the current implementation, if the 1.144 + * <code>mail.mime.ignoreunknownencoding</code> system property is set to 1.145 + * <code>"true"</code>, unknown encoding values are ignored and the 1.146 + * original InputStream is returned. 1.147 + * 1.148 + * @param is input stream 1.149 + * @param encoding the encoding of the stream. 1.150 + * @return decoded input stream. 1.151 + * @exception MessagingException if the encoding is unknown 1.152 + */ 1.153 + public static InputStream decode(InputStream is, String encoding) 1.154 + throws DecodingException { 1.155 + if (encoding.equalsIgnoreCase("base64")) 1.156 + return new BASE64DecoderStream(is); 1.157 + else if (encoding.equalsIgnoreCase("quoted-printable")) 1.158 + return new QPDecoderStream(is); 1.159 + else if (encoding.equalsIgnoreCase("uuencode") || 1.160 + encoding.equalsIgnoreCase("x-uuencode") || 1.161 + encoding.equalsIgnoreCase("x-uue")) 1.162 + return new UUDecoderStream(is); 1.163 + else if (encoding.equalsIgnoreCase("binary") || 1.164 + encoding.equalsIgnoreCase("7bit") || 1.165 + encoding.equalsIgnoreCase("8bit")) 1.166 + return is; 1.167 + else { 1.168 + if (!ignoreUnknownEncoding) { 1.169 + throw new DecodingException("Unknown encoding: " + encoding); 1.170 + } 1.171 + return is; 1.172 + } 1.173 + } 1.174 +}