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

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/MIMEMessage.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,249 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.org.jvnet.mimepull;
    1.30 +
    1.31 +import java.io.IOException;
    1.32 +import java.io.InputStream;
    1.33 +import java.io.UnsupportedEncodingException;
    1.34 +import java.net.URLDecoder;
    1.35 +import java.nio.ByteBuffer;
    1.36 +import java.util.*;
    1.37 +import java.util.logging.Logger;
    1.38 +
    1.39 +/**
    1.40 + * Represents MIME message. MIME message parsing is done lazily using a
    1.41 + * pull parser.
    1.42 + *
    1.43 + * @author Jitendra Kotamraju
    1.44 + */
    1.45 +public class MIMEMessage {
    1.46 +    private static final Logger LOGGER = Logger.getLogger(MIMEMessage.class.getName());
    1.47 +
    1.48 +    MIMEConfig config;
    1.49 +
    1.50 +    private final InputStream in;
    1.51 +    private final List<MIMEPart> partsList;
    1.52 +    private final Map<String, MIMEPart> partsMap;
    1.53 +    private final Iterator<MIMEEvent> it;
    1.54 +    private boolean parsed;     // true when entire message is parsed
    1.55 +    private MIMEPart currentPart;
    1.56 +    private int currentIndex;
    1.57 +
    1.58 +    /**
    1.59 +     * @see MIMEMessage(InputStream, String, MIMEConfig)
    1.60 +     */
    1.61 +    public MIMEMessage(InputStream in, String boundary) {
    1.62 +        this(in, boundary, new MIMEConfig());
    1.63 +    }
    1.64 +
    1.65 +    /**
    1.66 +     * Creates a MIME message from the content's stream. The content stream
    1.67 +     * is closed when EOF is reached.
    1.68 +     *
    1.69 +     * @param in MIME message stream
    1.70 +     * @param boundary the separator for parts(pass it without --)
    1.71 +     * @param config various configuration parameters
    1.72 +     */
    1.73 +    public MIMEMessage(InputStream in, String boundary, MIMEConfig config) {
    1.74 +        this.in = in;
    1.75 +        this.config = config;
    1.76 +        MIMEParser parser = new MIMEParser(in, boundary, config);
    1.77 +        it = parser.iterator();
    1.78 +
    1.79 +        partsList = new ArrayList<MIMEPart>();
    1.80 +        partsMap = new HashMap<String, MIMEPart>();
    1.81 +        if (config.isParseEagerly()) {
    1.82 +            parseAll();
    1.83 +        }
    1.84 +    }
    1.85 +
    1.86 +    /**
    1.87 +     * Gets all the attachments by parsing the entire MIME message. Avoid
    1.88 +     * this if possible since it is an expensive operation.
    1.89 +     *
    1.90 +     * @return list of attachments.
    1.91 +     */
    1.92 +    public List<MIMEPart> getAttachments() {
    1.93 +        if (!parsed) {
    1.94 +            parseAll();
    1.95 +        }
    1.96 +        return partsList;
    1.97 +    }
    1.98 +
    1.99 +    /**
   1.100 +     * Creates nth attachment lazily. It doesn't validate
   1.101 +     * if the message has so many attachments. To
   1.102 +     * do the validation, the message needs to be parsed.
   1.103 +     * The parsing of the message is done lazily and is done
   1.104 +     * while reading the bytes of the part.
   1.105 +     *
   1.106 +     * @param index sequential order of the part. starts with zero.
   1.107 +     * @return attachemnt part
   1.108 +     */
   1.109 +    public MIMEPart getPart(int index) {
   1.110 +        LOGGER.fine("index="+index);
   1.111 +        MIMEPart part = (index < partsList.size()) ? partsList.get(index) : null;
   1.112 +        if (parsed && part == null) {
   1.113 +            throw new MIMEParsingException("There is no "+index+" attachment part ");
   1.114 +        }
   1.115 +        if (part == null) {
   1.116 +            // Parsing will done lazily and will be driven by reading the part
   1.117 +            part = new MIMEPart(this);
   1.118 +            partsList.add(index, part);
   1.119 +        }
   1.120 +        LOGGER.fine("Got attachment at index="+index+" attachment="+part);
   1.121 +        return part;
   1.122 +    }
   1.123 +
   1.124 +    /**
   1.125 +     * Creates a lazy attachment for a given Content-ID. It doesn't validate
   1.126 +     * if the message contains an attachment with the given Content-ID. To
   1.127 +     * do the validation, the message needs to be parsed. The parsing of the
   1.128 +     * message is done lazily and is done while reading the bytes of the part.
   1.129 +     *
   1.130 +     * @param contentId Content-ID of the part, expects Content-ID without <, >
   1.131 +     * @return attachemnt part
   1.132 +     */
   1.133 +    public MIMEPart getPart(String contentId) {
   1.134 +        LOGGER.fine("Content-ID="+contentId);
   1.135 +        MIMEPart part = getDecodedCidPart(contentId);
   1.136 +        if (parsed && part == null) {
   1.137 +            throw new MIMEParsingException("There is no attachment part with Content-ID = "+contentId);
   1.138 +        }
   1.139 +        if (part == null) {
   1.140 +            // Parsing is done lazily and is driven by reading the part
   1.141 +            part = new MIMEPart(this, contentId);
   1.142 +            partsMap.put(contentId, part);
   1.143 +        }
   1.144 +        LOGGER.fine("Got attachment for Content-ID="+contentId+" attachment="+part);
   1.145 +        return part;
   1.146 +    }
   1.147 +
   1.148 +    // this is required for Indigo interop, it writes content-id without escaping
   1.149 +    private MIMEPart getDecodedCidPart(String cid) {
   1.150 +        MIMEPart part = partsMap.get(cid);
   1.151 +        if (part == null) {
   1.152 +            if (cid.indexOf('%') != -1) {
   1.153 +                try {
   1.154 +                    String tempCid = URLDecoder.decode(cid, "utf-8");
   1.155 +                    part = partsMap.get(tempCid);
   1.156 +                } catch(UnsupportedEncodingException ue) {
   1.157 +                    // Ignore it
   1.158 +                }
   1.159 +            }
   1.160 +        }
   1.161 +        return part;
   1.162 +    }
   1.163 +
   1.164 +
   1.165 +    /**
   1.166 +     * Parses the whole MIME message eagerly
   1.167 +     */
   1.168 +    public void parseAll() {
   1.169 +        while(makeProgress()) {
   1.170 +            // Nothing to do
   1.171 +        }
   1.172 +    }
   1.173 +
   1.174 +
   1.175 +    /**
   1.176 +     * Parses the MIME message in a pull fashion.
   1.177 +     *
   1.178 +     * @return
   1.179 +     *      false if the parsing is completed.
   1.180 +     */
   1.181 +    public synchronized boolean makeProgress() {
   1.182 +        if (!it.hasNext()) {
   1.183 +            return false;
   1.184 +        }
   1.185 +
   1.186 +        MIMEEvent event = it.next();
   1.187 +
   1.188 +        switch(event.getEventType()) {
   1.189 +            case START_MESSAGE :
   1.190 +                LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.START_MESSAGE);
   1.191 +                break;
   1.192 +
   1.193 +            case START_PART :
   1.194 +                LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.START_PART);
   1.195 +                break;
   1.196 +
   1.197 +            case HEADERS :
   1.198 +                LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.HEADERS);
   1.199 +                MIMEEvent.Headers headers = (MIMEEvent.Headers)event;
   1.200 +                InternetHeaders ih = headers.getHeaders();
   1.201 +                List<String> cids = ih.getHeader("content-id");
   1.202 +                String cid = (cids != null) ? cids.get(0) : currentIndex+"";
   1.203 +                if (cid.length() > 2 && cid.charAt(0)=='<') {
   1.204 +                    cid = cid.substring(1,cid.length()-1);
   1.205 +                }
   1.206 +                MIMEPart listPart = (currentIndex < partsList.size()) ? partsList.get(currentIndex) : null;
   1.207 +                MIMEPart mapPart = getDecodedCidPart(cid);
   1.208 +                if (listPart == null && mapPart == null) {
   1.209 +                    currentPart = getPart(cid);
   1.210 +                    partsList.add(currentIndex, currentPart);
   1.211 +                } else if (listPart == null) {
   1.212 +                    currentPart = mapPart;
   1.213 +                    partsList.add(currentIndex, mapPart);
   1.214 +                } else if (mapPart == null) {
   1.215 +                    currentPart = listPart;
   1.216 +                    currentPart.setContentId(cid);
   1.217 +                    partsMap.put(cid, currentPart);
   1.218 +                } else if (listPart != mapPart) {
   1.219 +                    throw new MIMEParsingException("Created two different attachments using Content-ID and index");
   1.220 +                }
   1.221 +                currentPart.setHeaders(ih);
   1.222 +                break;
   1.223 +
   1.224 +            case CONTENT :
   1.225 +                LOGGER.finer("MIMEEvent="+MIMEEvent.EVENT_TYPE.CONTENT);
   1.226 +                MIMEEvent.Content content = (MIMEEvent.Content)event;
   1.227 +                ByteBuffer buf = content.getData();
   1.228 +                currentPart.addBody(buf);
   1.229 +                break;
   1.230 +
   1.231 +            case END_PART :
   1.232 +                LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.END_PART);
   1.233 +                currentPart.doneParsing();
   1.234 +                ++currentIndex;
   1.235 +                break;
   1.236 +
   1.237 +            case END_MESSAGE :
   1.238 +                LOGGER.fine("MIMEEvent="+MIMEEvent.EVENT_TYPE.END_MESSAGE);
   1.239 +                parsed = true;
   1.240 +                try {
   1.241 +                    in.close();
   1.242 +                } catch(IOException ioe) {
   1.243 +                    throw new MIMEParsingException(ioe);
   1.244 +                }
   1.245 +                break;
   1.246 +
   1.247 +            default :
   1.248 +                throw new MIMEParsingException("Unknown Parser state = "+event.getEventType());
   1.249 +        }
   1.250 +        return true;
   1.251 +    }
   1.252 +}

mercurial