src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/MIMEMessage.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.org.jvnet.mimepull;
ohair@286 27
ohair@286 28 import java.io.IOException;
ohair@286 29 import java.io.InputStream;
ohair@286 30 import java.io.UnsupportedEncodingException;
ohair@286 31 import java.net.URLDecoder;
ohair@286 32 import java.nio.ByteBuffer;
ohair@286 33 import java.util.*;
alanb@368 34 import java.util.logging.Level;
ohair@286 35 import java.util.logging.Logger;
ohair@286 36
ohair@286 37 /**
ohair@286 38 * Represents MIME message. MIME message parsing is done lazily using a
ohair@286 39 * pull parser.
ohair@286 40 *
ohair@286 41 * @author Jitendra Kotamraju
ohair@286 42 */
ohair@286 43 public class MIMEMessage {
ohair@286 44 private static final Logger LOGGER = Logger.getLogger(MIMEMessage.class.getName());
ohair@286 45
ohair@286 46 MIMEConfig config;
ohair@286 47
ohair@286 48 private final InputStream in;
ohair@286 49 private final List<MIMEPart> partsList;
ohair@286 50 private final Map<String, MIMEPart> partsMap;
ohair@286 51 private final Iterator<MIMEEvent> it;
ohair@286 52 private boolean parsed; // true when entire message is parsed
ohair@286 53 private MIMEPart currentPart;
ohair@286 54 private int currentIndex;
ohair@286 55
ohair@286 56 /**
ohair@286 57 * @see MIMEMessage(InputStream, String, MIMEConfig)
ohair@286 58 */
ohair@286 59 public MIMEMessage(InputStream in, String boundary) {
ohair@286 60 this(in, boundary, new MIMEConfig());
ohair@286 61 }
ohair@286 62
ohair@286 63 /**
ohair@286 64 * Creates a MIME message from the content's stream. The content stream
ohair@286 65 * is closed when EOF is reached.
ohair@286 66 *
ohair@286 67 * @param in MIME message stream
ohair@286 68 * @param boundary the separator for parts(pass it without --)
ohair@286 69 * @param config various configuration parameters
ohair@286 70 */
ohair@286 71 public MIMEMessage(InputStream in, String boundary, MIMEConfig config) {
ohair@286 72 this.in = in;
ohair@286 73 this.config = config;
ohair@286 74 MIMEParser parser = new MIMEParser(in, boundary, config);
ohair@286 75 it = parser.iterator();
ohair@286 76
ohair@286 77 partsList = new ArrayList<MIMEPart>();
ohair@286 78 partsMap = new HashMap<String, MIMEPart>();
ohair@286 79 if (config.isParseEagerly()) {
ohair@286 80 parseAll();
ohair@286 81 }
ohair@286 82 }
ohair@286 83
ohair@286 84 /**
ohair@286 85 * Gets all the attachments by parsing the entire MIME message. Avoid
ohair@286 86 * this if possible since it is an expensive operation.
ohair@286 87 *
ohair@286 88 * @return list of attachments.
ohair@286 89 */
ohair@286 90 public List<MIMEPart> getAttachments() {
ohair@286 91 if (!parsed) {
ohair@286 92 parseAll();
ohair@286 93 }
ohair@286 94 return partsList;
ohair@286 95 }
ohair@286 96
ohair@286 97 /**
ohair@286 98 * Creates nth attachment lazily. It doesn't validate
ohair@286 99 * if the message has so many attachments. To
ohair@286 100 * do the validation, the message needs to be parsed.
ohair@286 101 * The parsing of the message is done lazily and is done
ohair@286 102 * while reading the bytes of the part.
ohair@286 103 *
ohair@286 104 * @param index sequential order of the part. starts with zero.
ohair@286 105 * @return attachemnt part
ohair@286 106 */
ohair@286 107 public MIMEPart getPart(int index) {
alanb@368 108 LOGGER.log(Level.FINE, "index={0}", index);
ohair@286 109 MIMEPart part = (index < partsList.size()) ? partsList.get(index) : null;
ohair@286 110 if (parsed && part == null) {
ohair@286 111 throw new MIMEParsingException("There is no "+index+" attachment part ");
ohair@286 112 }
ohair@286 113 if (part == null) {
ohair@286 114 // Parsing will done lazily and will be driven by reading the part
ohair@286 115 part = new MIMEPart(this);
ohair@286 116 partsList.add(index, part);
ohair@286 117 }
alanb@368 118 LOGGER.log(Level.FINE, "Got attachment at index={0} attachment={1}", new Object[]{index, part});
ohair@286 119 return part;
ohair@286 120 }
ohair@286 121
ohair@286 122 /**
ohair@286 123 * Creates a lazy attachment for a given Content-ID. It doesn't validate
ohair@286 124 * if the message contains an attachment with the given Content-ID. To
ohair@286 125 * do the validation, the message needs to be parsed. The parsing of the
ohair@286 126 * message is done lazily and is done while reading the bytes of the part.
ohair@286 127 *
ohair@286 128 * @param contentId Content-ID of the part, expects Content-ID without <, >
ohair@286 129 * @return attachemnt part
ohair@286 130 */
ohair@286 131 public MIMEPart getPart(String contentId) {
alanb@368 132 LOGGER.log(Level.FINE, "Content-ID={0}", contentId);
ohair@286 133 MIMEPart part = getDecodedCidPart(contentId);
ohair@286 134 if (parsed && part == null) {
ohair@286 135 throw new MIMEParsingException("There is no attachment part with Content-ID = "+contentId);
ohair@286 136 }
ohair@286 137 if (part == null) {
ohair@286 138 // Parsing is done lazily and is driven by reading the part
ohair@286 139 part = new MIMEPart(this, contentId);
ohair@286 140 partsMap.put(contentId, part);
ohair@286 141 }
alanb@368 142 LOGGER.log(Level.FINE, "Got attachment for Content-ID={0} attachment={1}", new Object[]{contentId, part});
ohair@286 143 return part;
ohair@286 144 }
ohair@286 145
ohair@286 146 // this is required for Indigo interop, it writes content-id without escaping
ohair@286 147 private MIMEPart getDecodedCidPart(String cid) {
ohair@286 148 MIMEPart part = partsMap.get(cid);
ohair@286 149 if (part == null) {
ohair@286 150 if (cid.indexOf('%') != -1) {
ohair@286 151 try {
ohair@286 152 String tempCid = URLDecoder.decode(cid, "utf-8");
ohair@286 153 part = partsMap.get(tempCid);
ohair@286 154 } catch(UnsupportedEncodingException ue) {
ohair@286 155 // Ignore it
ohair@286 156 }
ohair@286 157 }
ohair@286 158 }
ohair@286 159 return part;
ohair@286 160 }
ohair@286 161
ohair@286 162
ohair@286 163 /**
ohair@286 164 * Parses the whole MIME message eagerly
ohair@286 165 */
alanb@368 166 public final void parseAll() {
ohair@286 167 while(makeProgress()) {
ohair@286 168 // Nothing to do
ohair@286 169 }
ohair@286 170 }
ohair@286 171
ohair@286 172
ohair@286 173 /**
ohair@286 174 * Parses the MIME message in a pull fashion.
ohair@286 175 *
ohair@286 176 * @return
ohair@286 177 * false if the parsing is completed.
ohair@286 178 */
ohair@286 179 public synchronized boolean makeProgress() {
ohair@286 180 if (!it.hasNext()) {
ohair@286 181 return false;
ohair@286 182 }
ohair@286 183
ohair@286 184 MIMEEvent event = it.next();
ohair@286 185
ohair@286 186 switch(event.getEventType()) {
ohair@286 187 case START_MESSAGE :
alanb@368 188 LOGGER.log(Level.FINE, "MIMEEvent={0}", MIMEEvent.EVENT_TYPE.START_MESSAGE);
ohair@286 189 break;
ohair@286 190
ohair@286 191 case START_PART :
alanb@368 192 LOGGER.log(Level.FINE, "MIMEEvent={0}", MIMEEvent.EVENT_TYPE.START_PART);
ohair@286 193 break;
ohair@286 194
ohair@286 195 case HEADERS :
alanb@368 196 LOGGER.log(Level.FINE, "MIMEEvent={0}", MIMEEvent.EVENT_TYPE.HEADERS);
ohair@286 197 MIMEEvent.Headers headers = (MIMEEvent.Headers)event;
ohair@286 198 InternetHeaders ih = headers.getHeaders();
ohair@286 199 List<String> cids = ih.getHeader("content-id");
ohair@286 200 String cid = (cids != null) ? cids.get(0) : currentIndex+"";
ohair@286 201 if (cid.length() > 2 && cid.charAt(0)=='<') {
ohair@286 202 cid = cid.substring(1,cid.length()-1);
ohair@286 203 }
ohair@286 204 MIMEPart listPart = (currentIndex < partsList.size()) ? partsList.get(currentIndex) : null;
ohair@286 205 MIMEPart mapPart = getDecodedCidPart(cid);
ohair@286 206 if (listPart == null && mapPart == null) {
ohair@286 207 currentPart = getPart(cid);
ohair@286 208 partsList.add(currentIndex, currentPart);
ohair@286 209 } else if (listPart == null) {
ohair@286 210 currentPart = mapPart;
ohair@286 211 partsList.add(currentIndex, mapPart);
ohair@286 212 } else if (mapPart == null) {
ohair@286 213 currentPart = listPart;
ohair@286 214 currentPart.setContentId(cid);
ohair@286 215 partsMap.put(cid, currentPart);
ohair@286 216 } else if (listPart != mapPart) {
ohair@286 217 throw new MIMEParsingException("Created two different attachments using Content-ID and index");
ohair@286 218 }
ohair@286 219 currentPart.setHeaders(ih);
ohair@286 220 break;
ohair@286 221
ohair@286 222 case CONTENT :
alanb@368 223 LOGGER.log(Level.FINER, "MIMEEvent={0}", MIMEEvent.EVENT_TYPE.CONTENT);
ohair@286 224 MIMEEvent.Content content = (MIMEEvent.Content)event;
ohair@286 225 ByteBuffer buf = content.getData();
ohair@286 226 currentPart.addBody(buf);
ohair@286 227 break;
ohair@286 228
ohair@286 229 case END_PART :
alanb@368 230 LOGGER.log(Level.FINE, "MIMEEvent={0}", MIMEEvent.EVENT_TYPE.END_PART);
ohair@286 231 currentPart.doneParsing();
ohair@286 232 ++currentIndex;
ohair@286 233 break;
ohair@286 234
ohair@286 235 case END_MESSAGE :
alanb@368 236 LOGGER.log(Level.FINE, "MIMEEvent={0}", MIMEEvent.EVENT_TYPE.END_MESSAGE);
ohair@286 237 parsed = true;
ohair@286 238 try {
ohair@286 239 in.close();
ohair@286 240 } catch(IOException ioe) {
ohair@286 241 throw new MIMEParsingException(ioe);
ohair@286 242 }
ohair@286 243 break;
ohair@286 244
ohair@286 245 default :
ohair@286 246 throw new MIMEParsingException("Unknown Parser state = "+event.getEventType());
ohair@286 247 }
ohair@286 248 return true;
ohair@286 249 }
ohair@286 250 }

mercurial