mimeutils.java

来自「spam source codejasen-0.9jASEN - java An」· Java 代码 · 共 739 行 · 第 1/2 页

JAVA
739
字号
	        return FORGERY_UNKNOWN;
	    }
	}

	/**
	 * Returns the domain component of an email address
	 * @param emailAddress
	 * @return The domain ofthe address (everything after the @)
	 */
	public static String getDomainFromAddress(String emailAddress) {

		if (emailAddress != null) {
			return emailAddress.substring(emailAddress.indexOf("@") + 1, emailAddress.length());
		}
		else {
			return null;
		}

	}

    public static boolean isAttachment(String disposition) {
    
    	if(disposition != null) {
    		return (Arrays.binarySearch(ATTACHMENT_DISPOSITIONS, disposition) > -1);
    	}
    	else
    	{
    		return false;
    	}
    }

    public static void getParts(List parts, Part p) throws MessagingException, IOException {
    	getParts(parts, p, null, null);
    }

    /**
     * Gets the parts from the MimeMessage<BR>
     * <BR>
     * Use null disposition to ignore<BR>
     * @param parts A new list to hold the parts
     * @param p The current Part
     * @param contentType If specified, returns only parts matching the given content type.  Use null to ignore
     * @param disposition If specified, returns only parts matching the given disposition.  Use null to ignore
     */
    public static void getParts(List parts, Part p, String contentType, String disposition) throws MessagingException, IOException {
    
    	Object content = getPartContent (p);
    
    	String currentDisposition = p.getDisposition();
    	String currentContentType = p.getContentType();
    
    	// now we need to check if the part was a multipart...
    	if (content instanceof Multipart) {
    		Multipart mp = (Multipart) content;
    
    		// Now we need to delve into the parts
    
    		// This call to getCount seems to throw ParseExceptions occasionally
    		//  so we will catch that exception here so we don't lose the rest of the email...
    		int count = 0;
    
    		try
            {
                count = mp.getCount();
            }
            catch (Exception e)
            {
                // We weren't able to determine the number of parts in the multipart.
                // This shouldn't ever really happen, and is usually caused when
                // the MIME message is incorrectly formatted.
                // For now we are just going to record, but ignore the error
                ErrorHandlerBroker.getInstance().getErrorHandler().handleException(e);
            }
    
    		for (int i = 0; i < count; i++) {
    			getParts(parts, mp.getBodyPart(i), contentType, disposition);
    		}
    
    	}
    	else
    	{
    		// check to the contentType
    
    		if (contentType == null) {
    
    			// we don't need to check
    			if (disposition == null) {
    				// no need to check
    				// add the part
    				parts.add(p);
    				//index++;
    			}
    			else if (currentDisposition != null && currentDisposition.startsWith(disposition)) {
    				// add the part
    				parts.add(p);
    			}
    		}
    		else if (p.isMimeType(contentType)) {
    			// Check the disposition
    
    			if (disposition == null) {
    				// no need to check
    				// add the part
    				parts.add(p);
    			}
    			else if (currentDisposition != null && currentDisposition.startsWith(disposition)) {
    				// add the part
    				parts.add(p);
    			}
    		}
    	}
    }

    /**
     * Gets the parts which match any of the dispositions
     * @param parts A list of parts
     * @param contentType The content type required
     * @param dispositions The content dispositions required
     * @return All the parts in the message which match the given content type and disposition
     */
    public static List getMultiplePartsFromList(List parts, String contentType, String[] dispositions) throws IOException, MessagingException {
    
    	List matchedParts = null;
    
    	for (int i = 0; i < dispositions.length; i++) {
    		if (matchedParts == null) {
    			matchedParts = getPartsFromList(parts, contentType, dispositions[i]);
    		}
    		else {
    			matchedParts.addAll(getPartsFromList(parts, contentType, dispositions[i]));
    		}
    	}
    
    	return matchedParts;
    
    }

    /**
     * Gets a list of all parts which are themselves MimeMessages
     * @param parts
     * @return
     */
    public static List getSubMessagePartsFromList(List parts) throws IOException, MessagingException {
    	List subMessages = null;
    
    	if(parts != null) {
    
    		Iterator m = parts.iterator();
    		Part part = null;
    		Object content = null;
    
    		while(m.hasNext()) {
    			part = (Part) m.next();
    			content = getPartContent(part);
    
    
    			if(content instanceof MimeMessage) {
    				if(subMessages == null) {
    					subMessages = new LinkedList();
    				}
    
    				subMessages.add(content);
    			}
    		}
    	}
    
    	return subMessages;
    }

    /**
     * Gets all parts which are unknown.  An unknown part is one which has no disposition, and is not text or html
     * @param parts
     * @return
     */
    public static List getUnknownPartsFromList(List parts) throws IOException, MessagingException {
    
    	List unknownParts = null;
    
    	if(parts != null) {
    
    		Iterator m = parts.iterator();
    		Part part = null;
    
    		while(m.hasNext()) {
    			part = (Part) m.next();
    
    			if(!(getPartContent(part)  instanceof MimeMessage)) {
    				if(part.getDisposition() == null && !part.isMimeType(MimeType.TEXT_PLAIN) && !part.isMimeType(MimeType.TEXT_HTML)) {
    					// its unknown
    					if(unknownParts == null) {
    						unknownParts = new LinkedList();
    					}
    
    					unknownParts.add(part);
    				}
    			}
    		}
    	}
    
    	return unknownParts;
    
    }

    /**
     * Gets all parts which can be considered an attachment.  This includes sub messages and unknown parts
     * @param parts
     * @return A List of Part objects
     */
    public static List getAllAttachmentParts(List parts) throws IOException, MessagingException {
    
    	List attachments = null;
    
    	if(parts != null) {
    
    		Iterator m = parts.iterator();
    		Part part = null;
    
    		while(m.hasNext()) {
    			part = (Part) m.next();
    
    			if(getPartContent(part) instanceof MimeMessage) {
    
    				// Its a sub message
    				if(attachments == null) {
    					attachments = new LinkedList();
    				}
    
    				attachments.add(part);
    			}
    			else if(part.getDisposition() == null && !part.isMimeType(MimeType.TEXT_PLAIN) && !part.isMimeType(MimeType.TEXT_HTML)) {
    				// its unknown
    				if(attachments == null) {
    					attachments = new LinkedList();
    				}
    
    				attachments.add(part);
    			}
    			else if(part.getDisposition() != null && (
    					part.getDisposition().startsWith(MimeMessage.INLINE) ||
    					part.getDisposition().startsWith(MimeMessage.ATTACHMENT))) {
    
    				if(attachments == null) {
    					attachments = new LinkedList();
    				}
    
    				// It's an attachment
    				attachments.add(part);
    			}
    		}
    	}
    
    	return attachments;
    }

    public static Part getFirstPartFromList(List parts, String contentType, String disposition) throws MessagingException, IOException {
        List list = getPartsFromList(parts, contentType, disposition);
        if(list != null && list.size() > 0) {
            return (Part)list.iterator().next();
        }
        else
        {
            return null;
        }
    }

    public static List getPartsFromList(List parts, String contentType, String disposition) throws MessagingException, IOException {
    
    	LinkedList matchedParts = new LinkedList();
    	Part p = null;
    	Object content = null;
    	String currentDisposition = null;
    	String currentContentType = null;
    
    	for (int i = 0; i < parts.size(); i++) {
    
    		p = (Part) parts.get(i);
    
    		content = getPartContent (p);
    		//content = p.getContent();
    		currentDisposition = p.getDisposition();
    		currentContentType = p.getContentType();
    
    		if (contentType == null) {
    
    			// we don't need to check
    			if (disposition == null) {
    				// no need to check
    				matchedParts.add(p);
    			}
    			else if (currentDisposition != null && currentDisposition.startsWith(disposition)) {
    				matchedParts.add(p);
    			}
    		}
    		else if (p.isMimeType(contentType)) {
    			// Check the disposition
    
    			if (disposition == null) {
    				// no need to check
    				matchedParts.add(p);
    			}
    			else if (currentDisposition != null && currentDisposition.startsWith(disposition)) {
    				matchedParts.add(p);
    			}
    		}
    	}
    
    	return matchedParts;
    }

    public static Object getPartContent(Part part) throws IOException, MessagingException {
        Object content = null;
    
        if(part != null) {
    
            try
            {
                String strContentType = part.getContentType();
    	        ContentType contentType = null;
    	        String specifiedCharset = null;
    	        try
                {
    	            contentType = new ContentType(strContentType);
    	            specifiedCharset = contentType.getParameter("charset");
                }
                catch (ParseException ignore)
                {
                    // Ignore
                }
    
    			if(specifiedCharset != null) {
    
    				specifiedCharset = specifiedCharset.toLowerCase();
    
    				if (specifiedCharset.indexOf("utf-7") > -1) {
    					InputStream in = part.getInputStream();
    					ByteArrayOutputStream out = new ByteArrayOutputStream();
    		
    					IOUtils.pipe(in, out, 1024);
    
    					ByteToCharUTF7Converter btc = new ByteToCharUTF7Converter();
    
    					byte[] bytes = out.toByteArray();
    
    					char[] chars = new char[bytes.length];
    
    					btc.convert(bytes, 0, bytes.length,	chars, 0, chars.length);
    
    					content = new String(chars);
    				}
    				else
    				{
    					content = part.getContent();
    				}
    			}
    			else
    			{
    				content = part.getContent();
    			}
            }
            catch (IOException e)
            {
                ErrorHandlerBroker.getInstance().getErrorHandler().handleException(e);
            }
        }
    
    	return content;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?