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

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     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/MIMEPart.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,250 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, 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.File;
    1.32 +import java.io.InputStream;
    1.33 +import java.nio.ByteBuffer;
    1.34 +import java.util.List;
    1.35 +import java.util.logging.Level;
    1.36 +import java.util.logging.Logger;
    1.37 +
    1.38 +/**
    1.39 + * Represents an attachment part in a MIME message. MIME message parsing is done
    1.40 + * lazily using a pull parser, so the part may not have all the data. {@link #read}
    1.41 + * and {@link #readOnce} may trigger the actual parsing the message. In fact,
    1.42 + * parsing of an attachment part may be triggered by calling {@link #read} methods
    1.43 + * on some other attachment parts. All this happens behind the scenes so the
    1.44 + * application developer need not worry about these details.
    1.45 + *
    1.46 + * @author Jitendra Kotamraju, Martin Grebac
    1.47 + */
    1.48 +public class MIMEPart {
    1.49 +
    1.50 +    private static final Logger LOGGER = Logger.getLogger(MIMEPart.class.getName());
    1.51 +
    1.52 +    private volatile InternetHeaders headers;
    1.53 +    private volatile String contentId;
    1.54 +    private String contentType;
    1.55 +    private String contentTransferEncoding;
    1.56 +
    1.57 +    volatile boolean parsed;    // part is parsed or not
    1.58 +    final MIMEMessage msg;
    1.59 +    private final DataHead dataHead;
    1.60 +
    1.61 +    MIMEPart(MIMEMessage msg) {
    1.62 +        this.msg = msg;
    1.63 +        this.dataHead = new DataHead(this);
    1.64 +    }
    1.65 +
    1.66 +    MIMEPart(MIMEMessage msg, String contentId) {
    1.67 +        this(msg);
    1.68 +        this.contentId = contentId;
    1.69 +    }
    1.70 +
    1.71 +    /**
    1.72 +     * Can get the attachment part's content multiple times. That means
    1.73 +     * the full content needs to be there in memory or on the file system.
    1.74 +     * Calling this method would trigger parsing for the part's data. So
    1.75 +     * do not call this unless it is required(otherwise, just wrap MIMEPart
    1.76 +     * into a object that returns InputStream for e.g DataHandler)
    1.77 +     *
    1.78 +     * @return data for the part's content
    1.79 +     */
    1.80 +    public InputStream read() {
    1.81 +        InputStream is = null;
    1.82 +        try {
    1.83 +            is = MimeUtility.decode(dataHead.read(), contentTransferEncoding);
    1.84 +        } catch (DecodingException ex) { //ignore
    1.85 +            if (LOGGER.isLoggable(Level.WARNING)) {
    1.86 +                LOGGER.log(Level.WARNING, null, ex);
    1.87 +            }
    1.88 +        }
    1.89 +        return is;
    1.90 +    }
    1.91 +
    1.92 +    /**
    1.93 +     * Cleans up any resources that are held by this part (for e.g. deletes
    1.94 +     * the temp file that is used to serve this part's content). After
    1.95 +     * calling this, one shouldn't call {@link #read()} or {@link #readOnce()}
    1.96 +     */
    1.97 +    public void close() {
    1.98 +        dataHead.close();
    1.99 +    }
   1.100 +
   1.101 +    /**
   1.102 +     * Can get the attachment part's content only once. The content
   1.103 +     * will be lost after the method. Content data is not be stored
   1.104 +     * on the file system or is not kept in the memory for the
   1.105 +     * following case:
   1.106 +     *   - Attachement parts contents are accessed sequentially
   1.107 +     *
   1.108 +     * In general, take advantage of this when the data is used only
   1.109 +     * once.
   1.110 +     *
   1.111 +     * @return data for the part's content
   1.112 +     */
   1.113 +    public InputStream readOnce() {
   1.114 +        InputStream is = null;
   1.115 +        try {
   1.116 +            is = MimeUtility.decode(dataHead.readOnce(), contentTransferEncoding);
   1.117 +        } catch (DecodingException ex) { //ignore
   1.118 +            if (LOGGER.isLoggable(Level.WARNING)) {
   1.119 +                LOGGER.log(Level.WARNING, null, ex);
   1.120 +            }
   1.121 +        }
   1.122 +        return is;
   1.123 +    }
   1.124 +
   1.125 +    public void moveTo(File f) {
   1.126 +        dataHead.moveTo(f);
   1.127 +    }
   1.128 +
   1.129 +    /**
   1.130 +     * Returns Content-ID MIME header for this attachment part
   1.131 +     *
   1.132 +     * @return Content-ID of the part
   1.133 +     */
   1.134 +    public String getContentId() {
   1.135 +        if (contentId == null) {
   1.136 +            getHeaders();
   1.137 +        }
   1.138 +        return contentId;
   1.139 +    }
   1.140 +
   1.141 +    /**
   1.142 +     * Returns Content-Transfer-Encoding MIME header for this attachment part
   1.143 +     *
   1.144 +     * @return Content-Transfer-Encoding of the part
   1.145 +     */
   1.146 +    public String getContentTransferEncoding() {
   1.147 +        if (contentTransferEncoding == null) {
   1.148 +            getHeaders();
   1.149 +        }
   1.150 +        return contentTransferEncoding;
   1.151 +    }
   1.152 +
   1.153 +    /**
   1.154 +     * Returns Content-Type MIME header for this attachment part
   1.155 +     *
   1.156 +     * @return Content-Type of the part
   1.157 +     */
   1.158 +    public String getContentType() {
   1.159 +        if (contentType == null) {
   1.160 +            getHeaders();
   1.161 +        }
   1.162 +        return contentType;
   1.163 +    }
   1.164 +
   1.165 +    private void getHeaders() {
   1.166 +        // Trigger parsing for the part headers
   1.167 +        while(headers == null) {
   1.168 +            if (!msg.makeProgress()) {
   1.169 +                if (headers == null) {
   1.170 +                    throw new IllegalStateException("Internal Error. Didn't get Headers even after complete parsing.");
   1.171 +                }
   1.172 +            }
   1.173 +        }
   1.174 +    }
   1.175 +
   1.176 +    /**
   1.177 +     * Return all the values for the specified header.
   1.178 +     * Returns <code>null</code> if no headers with the
   1.179 +     * specified name exist.
   1.180 +     *
   1.181 +     * @param   name header name
   1.182 +     * @return  list of header values, or null if none
   1.183 +     */
   1.184 +    public List<String> getHeader(String name) {
   1.185 +        getHeaders();
   1.186 +        assert headers != null;
   1.187 +        return headers.getHeader(name);
   1.188 +    }
   1.189 +
   1.190 +    /**
   1.191 +     * Return all the headers
   1.192 +     *
   1.193 +     * @return list of Header objects
   1.194 +     */
   1.195 +    public List<? extends Header> getAllHeaders() {
   1.196 +        getHeaders();
   1.197 +        assert headers != null;
   1.198 +        return headers.getAllHeaders();
   1.199 +    }
   1.200 +
   1.201 +    /**
   1.202 +     * Callback to set headers
   1.203 +     *
   1.204 +     * @param headers MIME headers for the part
   1.205 +     */
   1.206 +    void setHeaders(InternetHeaders headers) {
   1.207 +        this.headers = headers;
   1.208 +        List<String> ct = getHeader("Content-Type");
   1.209 +        this.contentType = (ct == null) ? "application/octet-stream" : ct.get(0);
   1.210 +        List<String> cte = getHeader("Content-Transfer-Encoding");
   1.211 +        this.contentTransferEncoding = (cte == null) ? "binary" : cte.get(0);
   1.212 +    }
   1.213 +
   1.214 +    /**
   1.215 +     * Callback to notify that there is a partial content for the part
   1.216 +     *
   1.217 +     * @param buf content data for the part
   1.218 +     */
   1.219 +    void addBody(ByteBuffer buf) {
   1.220 +        dataHead.addBody(buf);
   1.221 +    }
   1.222 +
   1.223 +    /**
   1.224 +     * Callback to indicate that parsing is done for this part
   1.225 +     * (no more update events for this part)
   1.226 +     */
   1.227 +    void doneParsing() {
   1.228 +        parsed = true;
   1.229 +        dataHead.doneParsing();
   1.230 +    }
   1.231 +
   1.232 +    /**
   1.233 +     * Callback to set Content-ID for this part
   1.234 +     * @param cid Content-ID of the part
   1.235 +     */
   1.236 +    void setContentId(String cid) {
   1.237 +        this.contentId = cid;
   1.238 +    }
   1.239 +
   1.240 +    /**
   1.241 +     * Callback to set Content-Transfer-Encoding for this part
   1.242 +     * @param cte Content-Transfer-Encoding of the part
   1.243 +     */
   1.244 +    void setContentTransferEncoding(String cte) {
   1.245 +        this.contentTransferEncoding = cte;
   1.246 +    }
   1.247 +
   1.248 +    @Override
   1.249 +    public String toString() {
   1.250 +        return "Part="+contentId+":"+contentTransferEncoding;
   1.251 +    }
   1.252 +
   1.253 +}

mercurial