001package 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. 002 003import java.io.*; 004import javax.mail.*; 005import votorola.g.lang.*; 006import votorola.g.logging.*; 007 008 009/** Part utilities. 010 */ 011public final @ThreadSafe class PartX 012{ 013 014 private PartX() {} 015 016 017 018 /** Returns the value of the first header with the specified header name. 019 * 020 * @return value of header, or null if no such header exists 021 */ 022 public static String getFirstHeader( final Part part, final String name ) 023 024 throws MessagingException 025 { 026 final String[] vArray = part.getHeader( name ); 027 if( vArray == null ) return null; 028 else return vArray[0]; 029 } 030 031 032 033 /** Returns the first part of MIME type text/plain. 034 * 035 * @return the specfied part if it is plain text; 036 * or its first contained plain text part; or null if there is none 037 */ 038 public static Part getPlainTextPart( final Part part ) throws MessagingException, IOException 039 { 040 Part textPart = null; 041 if( part.isMimeType( "text/plain" )) textPart = part; 042 else if( part.isMimeType( "multipart/*" )) 043 { 044 Object nestedContent = part.getContent(); 045 if( nestedContent instanceof Multipart ) 046 { 047 final Multipart multipart = (Multipart)nestedContent; 048 for( int p = 0, pN = multipart.getCount(); p < pN; ++p ) 049 { 050 textPart = getPlainTextPart( multipart.getBodyPart( p )); 051 if( textPart != null ) break; 052 } 053 } 054 else LoggerX.i(PartX.class).info( "ignoring multipart message with improper content" ); // uncertain if this can ever occur 055 } 056 else if( part.isMimeType( "message/rfc822" )) // nested (never tested) 057 { 058 Object nestedContent = part.getContent(); 059 if( nestedContent instanceof Part ) textPart = getPlainTextPart( (Part)nestedContent ); 060 else LoggerX.i(PartX.class).info( "ignoring nested message/rfc822 with improper content" ); // uncertain if this can ever occur 061 } 062 return textPart; 063 } 064 065 066 067}