Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2010-2016 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.sun.mail.util;
import java.io.*;
import java.lang.reflect.*;
import java.security.*;
import javax.mail.MessagingException;
import javax.mail.internet.*;
/**
* General MIME-related utility methods.
*
* @author Bill Shannon
* @since JavaMail 1.4.4
*/
public class MimeUtil {
private static final Method cleanContentType;
static {
Method meth = null;
try {
String cth = System.getProperty("mail.mime.contenttypehandler");
if (cth != null) {
ClassLoader cl = getContextClassLoader();
Class> clsHandler = null;
if (cl != null) {
try {
clsHandler = Class.forName(cth, false, cl);
} catch (ClassNotFoundException cex) { }
}
if (clsHandler == null)
clsHandler = Class.forName(cth);
meth = clsHandler.getMethod("cleanContentType",
new Class>[] { MimePart.class, String.class });
}
} catch (ClassNotFoundException ex) {
// ignore it
} catch (NoSuchMethodException ex) {
// ignore it
} catch (RuntimeException ex) {
// ignore it
} finally {
cleanContentType = meth;
}
}
// No one should instantiate this class.
private MimeUtil() {
}
/**
* If a Content-Type handler has been specified,
* call it to clean up the Content-Type value.
*
* @param mp the MimePart
* @param contentType the Content-Type value
* @return the cleaned Content-Type value
*/
public static String cleanContentType(MimePart mp, String contentType) {
if (cleanContentType != null) {
try {
return (String)cleanContentType.invoke(null,
new Object[] { mp, contentType });
} catch (Exception ex) {
return contentType;
}
} else
return contentType;
}
/**
* If the MimePart is a text part and has a Content-Transfer-Encoding
* of "quoted-printable" or "base64", and it obeys the rules for
* "8bit" encoding, change the encoding to "8bit". If the part is
* a multipart or a message/rfc822, recursively process all its parts.
*
* @return true if any changes were made
* @since JavaMail 1.5.6
*/
// XXX - This is really quite a hack.
public static boolean convertTo8Bit(MimePart part) {
boolean changed = false;
try {
if (part.isMimeType("text/*")) {
String enc = part.getEncoding();
if (enc != null && (enc.equalsIgnoreCase("quoted-printable") ||
enc.equalsIgnoreCase("base64"))) {
InputStream is = null;
try {
is = part.getInputStream();
if (is8Bit(is)) {
/*
* If the message was created using an InputStream
* then we have to extract the content as an object
* and set it back as an object so that the content
* will be re-encoded.
*
* If the message was not created using an
* InputStream, the following should have no effect.
*/
part.setContent(part.getContent(),
part.getContentType());
part.setHeader("Content-Transfer-Encoding", "8bit");
changed = true;
}
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ex2) {
// ignore it
}
}
}
}
} else if (part.isMimeType("multipart/*")) {
MimeMultipart mp = (MimeMultipart)part.getContent();
int count = mp.getCount();
for (int i = 0; i < count; i++) {
if (convertTo8Bit((MimePart)mp.getBodyPart(i)))
changed = true;
}
} else if (part.isMimeType("message/rfc822")) {
MimeMessage msg = (MimeMessage)part.getContent();
if (convertTo8Bit(msg))
changed = true;
}
} catch (IOException ioex) {
// any exception causes us to give up
} catch (MessagingException mex) {
// any exception causes us to give up
}
return changed;
}
/**
* Check whether the data in the given InputStream follows the
* rules for 8bit text. Lines have to be 998 characters or less
* and no NULs are allowed. CR and LF must occur in pairs but we
* don't check that because we assume this is text and we convert
* all CR/LF combinations into canonical CRLF later.
*/
private static boolean is8Bit(InputStream is) {
int b;
int linelen = 0;
boolean need8bit = false;
try {
while ((b = is.read()) >= 0) {
b &= 0xff;
if (b == '\r' || b == '\n')
linelen = 0;
else if (b == 0)
return false;
else {
linelen++;
if (linelen > 998) // 1000 - CRLF
return false;
}
if (b > 0x7f)
need8bit = true;
}
} catch (IOException ex) {
return false;
}
return need8bit;
}
/**
* If the MimePart is a text part and has a Content-Transfer-Encoding
* of "8bit", change the encoding to "7bit". If the part is
* a multipart or a message/rfc822, recursively process all its parts.
*
* @return true if any changes were made
* @since JavaMail 1.5.6
*/
// XXX - This is really quite a hack.
public static boolean convertTo7Bit(MimePart part) {
// XXX XXX XXX XXX XXX XXX XXX XXX
boolean changed = false;
try {
if (part.isMimeType("text/*")) {
String enc = part.getEncoding();
if (enc != null && (enc.equalsIgnoreCase("quoted-printable") ||
enc.equalsIgnoreCase("base64"))) {
InputStream is = null;
try {
is = part.getInputStream();
if (is8Bit(is)) {
/*
* If the message was created using an InputStream
* then we have to extract the content as an object
* and set it back as an object so that the content
* will be re-encoded.
*
* If the message was not created using an
* InputStream, the following should have no effect.
*/
part.setContent(part.getContent(),
part.getContentType());
part.setHeader("Content-Transfer-Encoding", "8bit");
changed = true;
}
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ex2) {
// ignore it
}
}
}
}
} else if (part.isMimeType("multipart/*")) {
MimeMultipart mp = (MimeMultipart)part.getContent();
int count = mp.getCount();
for (int i = 0; i < count; i++) {
if (convertTo8Bit((MimePart)mp.getBodyPart(i)))
changed = true;
}
} else if (part.isMimeType("message/rfc822")) {
MimeMessage msg = (MimeMessage)part.getContent();
if (convertTo8Bit(msg))
changed = true;
}
} catch (IOException ioex) {
// any exception causes us to give up
} catch (MessagingException mex) {
// any exception causes us to give up
}
return changed;
}
/**
* Convenience method to get our context class loader.
* Assert any privileges we might have and then call the
* Thread.getContextClassLoader method.
*/
private static ClassLoader getContextClassLoader() {
return
AccessController.doPrivileged(new PrivilegedAction() {
public ClassLoader run() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
} catch (SecurityException ex) { }
return cl;
}
});
}
}