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