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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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

mercurial