ohair@286: /* ohair@286: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.org.jvnet.mimepull; ohair@286: ohair@286: import java.io.IOException; ohair@286: import java.io.InputStream; ohair@286: import java.io.UnsupportedEncodingException; ohair@286: import java.net.URLDecoder; ohair@286: import java.nio.ByteBuffer; ohair@286: import java.util.*; ohair@286: import java.util.logging.Logger; ohair@286: ohair@286: /** ohair@286: * Represents MIME message. MIME message parsing is done lazily using a ohair@286: * pull parser. ohair@286: * ohair@286: * @author Jitendra Kotamraju ohair@286: */ ohair@286: public class MIMEMessage { ohair@286: private static final Logger LOGGER = Logger.getLogger(MIMEMessage.class.getName()); ohair@286: ohair@286: MIMEConfig config; ohair@286: ohair@286: private final InputStream in; ohair@286: private final List partsList; ohair@286: private final Map partsMap; ohair@286: private final Iterator it; ohair@286: private boolean parsed; // true when entire message is parsed ohair@286: private MIMEPart currentPart; ohair@286: private int currentIndex; ohair@286: ohair@286: /** ohair@286: * @see MIMEMessage(InputStream, String, MIMEConfig) ohair@286: */ ohair@286: public MIMEMessage(InputStream in, String boundary) { ohair@286: this(in, boundary, new MIMEConfig()); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Creates a MIME message from the content's stream. The content stream ohair@286: * is closed when EOF is reached. ohair@286: * ohair@286: * @param in MIME message stream ohair@286: * @param boundary the separator for parts(pass it without --) ohair@286: * @param config various configuration parameters ohair@286: */ ohair@286: public MIMEMessage(InputStream in, String boundary, MIMEConfig config) { ohair@286: this.in = in; ohair@286: this.config = config; ohair@286: MIMEParser parser = new MIMEParser(in, boundary, config); ohair@286: it = parser.iterator(); ohair@286: ohair@286: partsList = new ArrayList(); ohair@286: partsMap = new HashMap(); ohair@286: if (config.isParseEagerly()) { ohair@286: parseAll(); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Gets all the attachments by parsing the entire MIME message. Avoid ohair@286: * this if possible since it is an expensive operation. ohair@286: * ohair@286: * @return list of attachments. ohair@286: */ ohair@286: public List getAttachments() { ohair@286: if (!parsed) { ohair@286: parseAll(); ohair@286: } ohair@286: return partsList; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Creates nth attachment lazily. It doesn't validate ohair@286: * if the message has so many attachments. To ohair@286: * do the validation, the message needs to be parsed. ohair@286: * The parsing of the message is done lazily and is done ohair@286: * while reading the bytes of the part. ohair@286: * ohair@286: * @param index sequential order of the part. starts with zero. ohair@286: * @return attachemnt part ohair@286: */ ohair@286: public MIMEPart getPart(int index) { ohair@286: LOGGER.fine("index="+index); ohair@286: MIMEPart part = (index < partsList.size()) ? partsList.get(index) : null; ohair@286: if (parsed && part == null) { ohair@286: throw new MIMEParsingException("There is no "+index+" attachment part "); ohair@286: } ohair@286: if (part == null) { ohair@286: // Parsing will done lazily and will be driven by reading the part ohair@286: part = new MIMEPart(this); ohair@286: partsList.add(index, part); ohair@286: } ohair@286: LOGGER.fine("Got attachment at index="+index+" attachment="+part); ohair@286: return part; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Creates a lazy attachment for a given Content-ID. It doesn't validate ohair@286: * if the message contains an attachment with the given Content-ID. To ohair@286: * do the validation, the message needs to be parsed. The parsing of the ohair@286: * message is done lazily and is done while reading the bytes of the part. ohair@286: * ohair@286: * @param contentId Content-ID of the part, expects Content-ID without <, > ohair@286: * @return attachemnt part ohair@286: */ ohair@286: public MIMEPart getPart(String contentId) { ohair@286: LOGGER.fine("Content-ID="+contentId); ohair@286: MIMEPart part = getDecodedCidPart(contentId); ohair@286: if (parsed && part == null) { ohair@286: throw new MIMEParsingException("There is no attachment part with Content-ID = "+contentId); ohair@286: } ohair@286: if (part == null) { ohair@286: // Parsing is done lazily and is driven by reading the part ohair@286: part = new MIMEPart(this, contentId); ohair@286: partsMap.put(contentId, part); ohair@286: } ohair@286: LOGGER.fine("Got attachment for Content-ID="+contentId+" attachment="+part); ohair@286: return part; ohair@286: } ohair@286: ohair@286: // this is required for Indigo interop, it writes content-id without escaping ohair@286: private MIMEPart getDecodedCidPart(String cid) { ohair@286: MIMEPart part = partsMap.get(cid); ohair@286: if (part == null) { ohair@286: if (cid.indexOf('%') != -1) { ohair@286: try { ohair@286: String tempCid = URLDecoder.decode(cid, "utf-8"); ohair@286: part = partsMap.get(tempCid); ohair@286: } catch(UnsupportedEncodingException ue) { ohair@286: // Ignore it ohair@286: } ohair@286: } ohair@286: } ohair@286: return part; ohair@286: } ohair@286: ohair@286: ohair@286: /** ohair@286: * Parses the whole MIME message eagerly ohair@286: */ ohair@286: public void parseAll() { ohair@286: while(makeProgress()) { ohair@286: // Nothing to do ohair@286: } ohair@286: } ohair@286: ohair@286: ohair@286: /** ohair@286: * Parses the MIME message in a pull fashion. ohair@286: * ohair@286: * @return ohair@286: * false if the parsing is completed. ohair@286: */ ohair@286: public synchronized boolean makeProgress() { ohair@286: if (!it.hasNext()) { ohair@286: return false; ohair@286: } ohair@286: ohair@286: MIMEEvent event = it.next(); ohair@286: ohair@286: switch(event.getEventType()) { ohair@286: case START_MESSAGE : ohair@286: LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.START_MESSAGE); ohair@286: break; ohair@286: ohair@286: case START_PART : ohair@286: LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.START_PART); ohair@286: break; ohair@286: ohair@286: case HEADERS : ohair@286: LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.HEADERS); ohair@286: MIMEEvent.Headers headers = (MIMEEvent.Headers)event; ohair@286: InternetHeaders ih = headers.getHeaders(); ohair@286: List cids = ih.getHeader("content-id"); ohair@286: String cid = (cids != null) ? cids.get(0) : currentIndex+""; ohair@286: if (cid.length() > 2 && cid.charAt(0)=='<') { ohair@286: cid = cid.substring(1,cid.length()-1); ohair@286: } ohair@286: MIMEPart listPart = (currentIndex < partsList.size()) ? partsList.get(currentIndex) : null; ohair@286: MIMEPart mapPart = getDecodedCidPart(cid); ohair@286: if (listPart == null && mapPart == null) { ohair@286: currentPart = getPart(cid); ohair@286: partsList.add(currentIndex, currentPart); ohair@286: } else if (listPart == null) { ohair@286: currentPart = mapPart; ohair@286: partsList.add(currentIndex, mapPart); ohair@286: } else if (mapPart == null) { ohair@286: currentPart = listPart; ohair@286: currentPart.setContentId(cid); ohair@286: partsMap.put(cid, currentPart); ohair@286: } else if (listPart != mapPart) { ohair@286: throw new MIMEParsingException("Created two different attachments using Content-ID and index"); ohair@286: } ohair@286: currentPart.setHeaders(ih); ohair@286: break; ohair@286: ohair@286: case CONTENT : ohair@286: LOGGER.finer("MIMEEvent="+MIMEEvent.EVENT_TYPE.CONTENT); ohair@286: MIMEEvent.Content content = (MIMEEvent.Content)event; ohair@286: ByteBuffer buf = content.getData(); ohair@286: currentPart.addBody(buf); ohair@286: break; ohair@286: ohair@286: case END_PART : ohair@286: LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.END_PART); ohair@286: currentPart.doneParsing(); ohair@286: ++currentIndex; ohair@286: break; ohair@286: ohair@286: case END_MESSAGE : ohair@286: LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.END_MESSAGE); ohair@286: parsed = true; ohair@286: try { ohair@286: in.close(); ohair@286: } catch(IOException ioe) { ohair@286: throw new MIMEParsingException(ioe); ohair@286: } ohair@286: break; ohair@286: ohair@286: default : ohair@286: throw new MIMEParsingException("Unknown Parser state = "+event.getEventType()); ohair@286: } ohair@286: return true; ohair@286: } ohair@286: }