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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2010, 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.*;
ohair@286 34 import java.util.logging.Logger;
ohair@286 35
ohair@286 36 /**
ohair@286 37 * Represents MIME message. MIME message parsing is done lazily using a
ohair@286 38 * pull parser.
ohair@286 39 *
ohair@286 40 * @author Jitendra Kotamraju
ohair@286 41 */
ohair@286 42 public class MIMEMessage {
ohair@286 43 private static final Logger LOGGER = Logger.getLogger(MIMEMessage.class.getName());
ohair@286 44
ohair@286 45 MIMEConfig config;
ohair@286 46
ohair@286 47 private final InputStream in;
ohair@286 48 private final List<MIMEPart> partsList;
ohair@286 49 private final Map<String, MIMEPart> partsMap;
ohair@286 50 private final Iterator<MIMEEvent> it;
ohair@286 51 private boolean parsed; // true when entire message is parsed
ohair@286 52 private MIMEPart currentPart;
ohair@286 53 private int currentIndex;
ohair@286 54
ohair@286 55 /**
ohair@286 56 * @see MIMEMessage(InputStream, String, MIMEConfig)
ohair@286 57 */
ohair@286 58 public MIMEMessage(InputStream in, String boundary) {
ohair@286 59 this(in, boundary, new MIMEConfig());
ohair@286 60 }
ohair@286 61
ohair@286 62 /**
ohair@286 63 * Creates a MIME message from the content's stream. The content stream
ohair@286 64 * is closed when EOF is reached.
ohair@286 65 *
ohair@286 66 * @param in MIME message stream
ohair@286 67 * @param boundary the separator for parts(pass it without --)
ohair@286 68 * @param config various configuration parameters
ohair@286 69 */
ohair@286 70 public MIMEMessage(InputStream in, String boundary, MIMEConfig config) {
ohair@286 71 this.in = in;
ohair@286 72 this.config = config;
ohair@286 73 MIMEParser parser = new MIMEParser(in, boundary, config);
ohair@286 74 it = parser.iterator();
ohair@286 75
ohair@286 76 partsList = new ArrayList<MIMEPart>();
ohair@286 77 partsMap = new HashMap<String, MIMEPart>();
ohair@286 78 if (config.isParseEagerly()) {
ohair@286 79 parseAll();
ohair@286 80 }
ohair@286 81 }
ohair@286 82
ohair@286 83 /**
ohair@286 84 * Gets all the attachments by parsing the entire MIME message. Avoid
ohair@286 85 * this if possible since it is an expensive operation.
ohair@286 86 *
ohair@286 87 * @return list of attachments.
ohair@286 88 */
ohair@286 89 public List<MIMEPart> getAttachments() {
ohair@286 90 if (!parsed) {
ohair@286 91 parseAll();
ohair@286 92 }
ohair@286 93 return partsList;
ohair@286 94 }
ohair@286 95
ohair@286 96 /**
ohair@286 97 * Creates nth attachment lazily. It doesn't validate
ohair@286 98 * if the message has so many attachments. To
ohair@286 99 * do the validation, the message needs to be parsed.
ohair@286 100 * The parsing of the message is done lazily and is done
ohair@286 101 * while reading the bytes of the part.
ohair@286 102 *
ohair@286 103 * @param index sequential order of the part. starts with zero.
ohair@286 104 * @return attachemnt part
ohair@286 105 */
ohair@286 106 public MIMEPart getPart(int index) {
ohair@286 107 LOGGER.fine("index="+index);
ohair@286 108 MIMEPart part = (index < partsList.size()) ? partsList.get(index) : null;
ohair@286 109 if (parsed && part == null) {
ohair@286 110 throw new MIMEParsingException("There is no "+index+" attachment part ");
ohair@286 111 }
ohair@286 112 if (part == null) {
ohair@286 113 // Parsing will done lazily and will be driven by reading the part
ohair@286 114 part = new MIMEPart(this);
ohair@286 115 partsList.add(index, part);
ohair@286 116 }
ohair@286 117 LOGGER.fine("Got attachment at index="+index+" attachment="+part);
ohair@286 118 return part;
ohair@286 119 }
ohair@286 120
ohair@286 121 /**
ohair@286 122 * Creates a lazy attachment for a given Content-ID. It doesn't validate
ohair@286 123 * if the message contains an attachment with the given Content-ID. To
ohair@286 124 * do the validation, the message needs to be parsed. The parsing of the
ohair@286 125 * message is done lazily and is done while reading the bytes of the part.
ohair@286 126 *
ohair@286 127 * @param contentId Content-ID of the part, expects Content-ID without <, >
ohair@286 128 * @return attachemnt part
ohair@286 129 */
ohair@286 130 public MIMEPart getPart(String contentId) {
ohair@286 131 LOGGER.fine("Content-ID="+contentId);
ohair@286 132 MIMEPart part = getDecodedCidPart(contentId);
ohair@286 133 if (parsed && part == null) {
ohair@286 134 throw new MIMEParsingException("There is no attachment part with Content-ID = "+contentId);
ohair@286 135 }
ohair@286 136 if (part == null) {
ohair@286 137 // Parsing is done lazily and is driven by reading the part
ohair@286 138 part = new MIMEPart(this, contentId);
ohair@286 139 partsMap.put(contentId, part);
ohair@286 140 }
ohair@286 141 LOGGER.fine("Got attachment for Content-ID="+contentId+" attachment="+part);
ohair@286 142 return part;
ohair@286 143 }
ohair@286 144
ohair@286 145 // this is required for Indigo interop, it writes content-id without escaping
ohair@286 146 private MIMEPart getDecodedCidPart(String cid) {
ohair@286 147 MIMEPart part = partsMap.get(cid);
ohair@286 148 if (part == null) {
ohair@286 149 if (cid.indexOf('%') != -1) {
ohair@286 150 try {
ohair@286 151 String tempCid = URLDecoder.decode(cid, "utf-8");
ohair@286 152 part = partsMap.get(tempCid);
ohair@286 153 } catch(UnsupportedEncodingException ue) {
ohair@286 154 // Ignore it
ohair@286 155 }
ohair@286 156 }
ohair@286 157 }
ohair@286 158 return part;
ohair@286 159 }
ohair@286 160
ohair@286 161
ohair@286 162 /**
ohair@286 163 * Parses the whole MIME message eagerly
ohair@286 164 */
ohair@286 165 public void parseAll() {
ohair@286 166 while(makeProgress()) {
ohair@286 167 // Nothing to do
ohair@286 168 }
ohair@286 169 }
ohair@286 170
ohair@286 171
ohair@286 172 /**
ohair@286 173 * Parses the MIME message in a pull fashion.
ohair@286 174 *
ohair@286 175 * @return
ohair@286 176 * false if the parsing is completed.
ohair@286 177 */
ohair@286 178 public synchronized boolean makeProgress() {
ohair@286 179 if (!it.hasNext()) {
ohair@286 180 return false;
ohair@286 181 }
ohair@286 182
ohair@286 183 MIMEEvent event = it.next();
ohair@286 184
ohair@286 185 switch(event.getEventType()) {
ohair@286 186 case START_MESSAGE :
ohair@286 187 LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.START_MESSAGE);
ohair@286 188 break;
ohair@286 189
ohair@286 190 case START_PART :
ohair@286 191 LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.START_PART);
ohair@286 192 break;
ohair@286 193
ohair@286 194 case HEADERS :
ohair@286 195 LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.HEADERS);
ohair@286 196 MIMEEvent.Headers headers = (MIMEEvent.Headers)event;
ohair@286 197 InternetHeaders ih = headers.getHeaders();
ohair@286 198 List<String> cids = ih.getHeader("content-id");
ohair@286 199 String cid = (cids != null) ? cids.get(0) : currentIndex+"";
ohair@286 200 if (cid.length() > 2 && cid.charAt(0)=='<') {
ohair@286 201 cid = cid.substring(1,cid.length()-1);
ohair@286 202 }
ohair@286 203 MIMEPart listPart = (currentIndex < partsList.size()) ? partsList.get(currentIndex) : null;
ohair@286 204 MIMEPart mapPart = getDecodedCidPart(cid);
ohair@286 205 if (listPart == null && mapPart == null) {
ohair@286 206 currentPart = getPart(cid);
ohair@286 207 partsList.add(currentIndex, currentPart);
ohair@286 208 } else if (listPart == null) {
ohair@286 209 currentPart = mapPart;
ohair@286 210 partsList.add(currentIndex, mapPart);
ohair@286 211 } else if (mapPart == null) {
ohair@286 212 currentPart = listPart;
ohair@286 213 currentPart.setContentId(cid);
ohair@286 214 partsMap.put(cid, currentPart);
ohair@286 215 } else if (listPart != mapPart) {
ohair@286 216 throw new MIMEParsingException("Created two different attachments using Content-ID and index");
ohair@286 217 }
ohair@286 218 currentPart.setHeaders(ih);
ohair@286 219 break;
ohair@286 220
ohair@286 221 case CONTENT :
ohair@286 222 LOGGER.finer("MIMEEvent="+MIMEEvent.EVENT_TYPE.CONTENT);
ohair@286 223 MIMEEvent.Content content = (MIMEEvent.Content)event;
ohair@286 224 ByteBuffer buf = content.getData();
ohair@286 225 currentPart.addBody(buf);
ohair@286 226 break;
ohair@286 227
ohair@286 228 case END_PART :
ohair@286 229 LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.END_PART);
ohair@286 230 currentPart.doneParsing();
ohair@286 231 ++currentIndex;
ohair@286 232 break;
ohair@286 233
ohair@286 234 case END_MESSAGE :
ohair@286 235 LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.END_MESSAGE);
ohair@286 236 parsed = true;
ohair@286 237 try {
ohair@286 238 in.close();
ohair@286 239 } catch(IOException ioe) {
ohair@286 240 throw new MIMEParsingException(ioe);
ohair@286 241 }
ohair@286 242 break;
ohair@286 243
ohair@286 244 default :
ohair@286 245 throw new MIMEParsingException("Unknown Parser state = "+event.getEventType());
ohair@286 246 }
ohair@286 247 return true;
ohair@286 248 }
ohair@286 249 }

mercurial