package votorola.g.mail; // Copyright 2007, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import java.io.*; import javax.mail.*; import votorola.g.lang.*; import votorola.g.logging.*; /** Part utilities. */ public final @ThreadSafe class PartX { private PartX() {} /** Returns the value of the first header with the specified header name. * * @return value of header, or null if no such header exists */ public static String getFirstHeader( final Part part, final String name ) throws MessagingException { final String[] vArray = part.getHeader( name ); if( vArray == null ) return null; else return vArray[0]; } /** Returns the first part of MIME type text/plain. * * @return the specfied part if it is plain text; * or its first contained plain text part; or null if there is none */ public static Part getPlainTextPart( final Part part ) throws MessagingException, IOException { Part textPart = null; if( part.isMimeType( "text/plain" )) textPart = part; else if( part.isMimeType( "multipart/*" )) { Object nestedContent = part.getContent(); if( nestedContent instanceof Multipart ) { final Multipart multipart = (Multipart)nestedContent; for( int p = 0, pN = multipart.getCount(); p < pN; ++p ) { textPart = getPlainTextPart( multipart.getBodyPart( p )); if( textPart != null ) break; } } else LoggerX.i(PartX.class).info( "ignoring multipart message with improper content" ); // uncertain if this can ever occur } else if( part.isMimeType( "message/rfc822" )) // nested (never tested) { Object nestedContent = part.getContent(); if( nestedContent instanceof Part ) textPart = getPlainTextPart( (Part)nestedContent ); else LoggerX.i(PartX.class).info( "ignoring nested message/rfc822 with improper content" ); // uncertain if this can ever occur } return textPart; } }