src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/AttachmentPartImpl.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/messaging/saaj/soap/AttachmentPartImpl.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,607 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, 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.messaging.saaj.soap;
    1.30 +
    1.31 +import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
    1.32 +
    1.33 +import com.sun.xml.internal.messaging.saaj.packaging.mime.util.ASCIIUtility;
    1.34 +
    1.35 +import com.sun.xml.internal.messaging.saaj.packaging.mime.Header;
    1.36 +import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimePartDataSource;
    1.37 +import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.InternetHeaders;
    1.38 +import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeBodyPart;
    1.39 +import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;
    1.40 +import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
    1.41 +import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants;
    1.42 +
    1.43 +import java.io.IOException;
    1.44 +import java.io.InputStream;
    1.45 +import java.io.ByteArrayOutputStream;
    1.46 +import java.io.ByteArrayInputStream;
    1.47 +import java.io.OutputStream;
    1.48 +import java.util.Iterator;
    1.49 +import java.util.List;
    1.50 +import java.util.logging.Level;
    1.51 +import java.util.logging.Logger;
    1.52 +
    1.53 +import javax.activation.*;
    1.54 +import javax.xml.soap.*;
    1.55 +import com.sun.xml.internal.org.jvnet.mimepull.MIMEPart;
    1.56 +
    1.57 +/**
    1.58 + * Implementation of attachments.
    1.59 + *
    1.60 + * @author Anil Vijendran (akv@eng.sun.com)
    1.61 + */
    1.62 +public class AttachmentPartImpl extends AttachmentPart {
    1.63 +
    1.64 +    protected static final Logger log =
    1.65 +        Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
    1.66 +                         "com.sun.xml.internal.messaging.saaj.soap.LocalStrings");
    1.67 +
    1.68 +    private final MimeHeaders headers;
    1.69 +    private MimeBodyPart rawContent = null;
    1.70 +    private DataHandler dataHandler = null;
    1.71 +
    1.72 +    //alternate impl that uses a MIMEPart
    1.73 +    private MIMEPart mimePart = null;
    1.74 +
    1.75 +    public AttachmentPartImpl() {
    1.76 +        headers = new MimeHeaders();
    1.77 +
    1.78 +        // initialization from here should cover most of cases;
    1.79 +        // if not, it would be necessary to call
    1.80 +        //   AttachmentPartImpl.initializeJavaActivationHandlers()
    1.81 +        // explicitly by programmer
    1.82 +        initializeJavaActivationHandlers();
    1.83 +    }
    1.84 +
    1.85 +    public AttachmentPartImpl(MIMEPart part) {
    1.86 +        headers = new MimeHeaders();
    1.87 +        mimePart = part;
    1.88 +        List<? extends com.sun.xml.internal.org.jvnet.mimepull.Header> hdrs = part.getAllHeaders();
    1.89 +        for (com.sun.xml.internal.org.jvnet.mimepull.Header hd : hdrs) {
    1.90 +            headers.addHeader(hd.getName(), hd.getValue());
    1.91 +        }
    1.92 +    }
    1.93 +
    1.94 +    public int getSize() throws SOAPException {
    1.95 +        byte[] bytes;
    1.96 +        if (mimePart != null) {
    1.97 +            try {
    1.98 +                return mimePart.read().available();
    1.99 +            } catch (IOException e) {
   1.100 +                return -1;
   1.101 +            }
   1.102 +        }
   1.103 +        if ((rawContent == null) && (dataHandler == null))
   1.104 +            return 0;
   1.105 +
   1.106 +        if (rawContent != null) {
   1.107 +            try {
   1.108 +                return rawContent.getSize();
   1.109 +            } catch (Exception ex) {
   1.110 +                log.log(
   1.111 +                    Level.SEVERE,
   1.112 +                    "SAAJ0573.soap.attachment.getrawbytes.ioexception",
   1.113 +                    new String[] { ex.getLocalizedMessage()});
   1.114 +                throw new SOAPExceptionImpl("Raw InputStream Error: " + ex);
   1.115 +            }
   1.116 +        } else {
   1.117 +            ByteOutputStream bout = new ByteOutputStream();
   1.118 +            try {
   1.119 +                dataHandler.writeTo(bout);
   1.120 +            } catch (IOException ex) {
   1.121 +                log.log(
   1.122 +                    Level.SEVERE,
   1.123 +                    "SAAJ0501.soap.data.handler.err",
   1.124 +                    new String[] { ex.getLocalizedMessage()});
   1.125 +                throw new SOAPExceptionImpl("Data handler error: " + ex);
   1.126 +            }
   1.127 +            return bout.size();
   1.128 +        }
   1.129 +    }
   1.130 +
   1.131 +    public void clearContent() {
   1.132 +        if (mimePart != null) {
   1.133 +            mimePart.close();
   1.134 +            mimePart = null;
   1.135 +        }
   1.136 +        dataHandler = null;
   1.137 +        rawContent = null;
   1.138 +    }
   1.139 +
   1.140 +    public Object getContent() throws SOAPException {
   1.141 +        try {
   1.142 +            if (mimePart != null) {
   1.143 +                //return an inputstream
   1.144 +                return mimePart.read();
   1.145 +            }
   1.146 +            if (dataHandler != null) {
   1.147 +                return getDataHandler().getContent();
   1.148 +            } else if (rawContent != null) {
   1.149 +                return rawContent.getContent();
   1.150 +            } else {
   1.151 +                log.severe("SAAJ0572.soap.no.content.for.attachment");
   1.152 +                throw new SOAPExceptionImpl("No data handler/content associated with this attachment");
   1.153 +            }
   1.154 +        } catch (Exception ex) {
   1.155 +            log.log(Level.SEVERE, "SAAJ0575.soap.attachment.getcontent.exception", ex);
   1.156 +            throw new SOAPExceptionImpl(ex.getLocalizedMessage());
   1.157 +        }
   1.158 +    }
   1.159 +
   1.160 +    public void setContent(Object object, String contentType)
   1.161 +        throws IllegalArgumentException {
   1.162 +        if (mimePart != null) {
   1.163 +            mimePart.close();
   1.164 +            mimePart = null;
   1.165 +        }
   1.166 +        DataHandler dh = new DataHandler(object, contentType);
   1.167 +
   1.168 +        setDataHandler(dh);
   1.169 +    }
   1.170 +
   1.171 +
   1.172 +    public DataHandler getDataHandler() throws SOAPException {
   1.173 +        if (mimePart != null) {
   1.174 +            //return an inputstream
   1.175 +            return new DataHandler(new DataSource() {
   1.176 +
   1.177 +                public InputStream getInputStream() throws IOException {
   1.178 +                    return mimePart.read();
   1.179 +                }
   1.180 +
   1.181 +                public OutputStream getOutputStream() throws IOException {
   1.182 +                    throw new UnsupportedOperationException("getOutputStream cannot be supported : You have enabled LazyAttachments Option");
   1.183 +                }
   1.184 +
   1.185 +                public String getContentType() {
   1.186 +                    return mimePart.getContentType();
   1.187 +                }
   1.188 +
   1.189 +                public String getName() {
   1.190 +                    return "MIMEPart Wrapper DataSource";
   1.191 +                }
   1.192 +            });
   1.193 +        }
   1.194 +        if (dataHandler == null) {
   1.195 +            if (rawContent != null) {
   1.196 +                return new DataHandler(new MimePartDataSource(rawContent));
   1.197 +            }
   1.198 +            log.severe("SAAJ0502.soap.no.handler.for.attachment");
   1.199 +            throw new SOAPExceptionImpl("No data handler associated with this attachment");
   1.200 +        }
   1.201 +        return dataHandler;
   1.202 +    }
   1.203 +
   1.204 +    public void setDataHandler(DataHandler dataHandler)
   1.205 +        throws IllegalArgumentException {
   1.206 +        if (mimePart != null) {
   1.207 +            mimePart.close();
   1.208 +            mimePart = null;
   1.209 +        }
   1.210 +        if (dataHandler == null) {
   1.211 +            log.severe("SAAJ0503.soap.no.null.to.dataHandler");
   1.212 +            throw new IllegalArgumentException("Null dataHandler argument to setDataHandler");
   1.213 +        }
   1.214 +        this.dataHandler = dataHandler;
   1.215 +        rawContent = null;
   1.216 +
   1.217 +        if (log.isLoggable(Level.FINE))
   1.218 +            log.log(Level.FINE, "SAAJ0580.soap.set.Content-Type",
   1.219 +                    new String[] { dataHandler.getContentType() });
   1.220 +        setMimeHeader("Content-Type", dataHandler.getContentType());
   1.221 +    }
   1.222 +
   1.223 +    public void removeAllMimeHeaders() {
   1.224 +        headers.removeAllHeaders();
   1.225 +    }
   1.226 +
   1.227 +    public void removeMimeHeader(String header) {
   1.228 +        headers.removeHeader(header);
   1.229 +    }
   1.230 +
   1.231 +    public String[] getMimeHeader(String name) {
   1.232 +        return headers.getHeader(name);
   1.233 +    }
   1.234 +
   1.235 +    public void setMimeHeader(String name, String value) {
   1.236 +        headers.setHeader(name, value);
   1.237 +    }
   1.238 +
   1.239 +    public void addMimeHeader(String name, String value) {
   1.240 +        headers.addHeader(name, value);
   1.241 +    }
   1.242 +
   1.243 +    public Iterator getAllMimeHeaders() {
   1.244 +        return headers.getAllHeaders();
   1.245 +    }
   1.246 +
   1.247 +    public Iterator getMatchingMimeHeaders(String[] names) {
   1.248 +        return headers.getMatchingHeaders(names);
   1.249 +    }
   1.250 +
   1.251 +    public Iterator getNonMatchingMimeHeaders(String[] names) {
   1.252 +        return headers.getNonMatchingHeaders(names);
   1.253 +    }
   1.254 +
   1.255 +    boolean hasAllHeaders(MimeHeaders hdrs) {
   1.256 +        if (hdrs != null) {
   1.257 +            Iterator i = hdrs.getAllHeaders();
   1.258 +            while (i.hasNext()) {
   1.259 +                MimeHeader hdr = (MimeHeader) i.next();
   1.260 +                String[] values = headers.getHeader(hdr.getName());
   1.261 +                boolean found = false;
   1.262 +
   1.263 +                if (values != null) {
   1.264 +                    for (int j = 0; j < values.length; j++)
   1.265 +                        if (hdr.getValue().equalsIgnoreCase(values[j])) {
   1.266 +                            found = true;
   1.267 +                            break;
   1.268 +                        }
   1.269 +                }
   1.270 +
   1.271 +                if (!found) {
   1.272 +                    return false;
   1.273 +                }
   1.274 +            }
   1.275 +        }
   1.276 +        return true;
   1.277 +    }
   1.278 +
   1.279 +    MimeBodyPart getMimePart() throws SOAPException {
   1.280 +        try {
   1.281 +            if (this.mimePart != null) {
   1.282 +                return new MimeBodyPart(mimePart);
   1.283 +            }
   1.284 +            if (rawContent != null) {
   1.285 +                copyMimeHeaders(headers, rawContent);
   1.286 +                return rawContent;
   1.287 +            }
   1.288 +
   1.289 +            MimeBodyPart envelope = new MimeBodyPart();
   1.290 +
   1.291 +            envelope.setDataHandler(dataHandler);
   1.292 +            copyMimeHeaders(headers, envelope);
   1.293 +
   1.294 +            return envelope;
   1.295 +        } catch (Exception ex) {
   1.296 +            log.severe("SAAJ0504.soap.cannot.externalize.attachment");
   1.297 +            throw new SOAPExceptionImpl("Unable to externalize attachment", ex);
   1.298 +        }
   1.299 +    }
   1.300 +
   1.301 +    public static void copyMimeHeaders(MimeHeaders headers, MimeBodyPart mbp)
   1.302 +        throws SOAPException {
   1.303 +
   1.304 +        Iterator i = headers.getAllHeaders();
   1.305 +
   1.306 +        while (i.hasNext())
   1.307 +            try {
   1.308 +                MimeHeader mh = (MimeHeader) i.next();
   1.309 +
   1.310 +                mbp.setHeader(mh.getName(), mh.getValue());
   1.311 +            } catch (Exception ex) {
   1.312 +                log.severe("SAAJ0505.soap.cannot.copy.mime.hdr");
   1.313 +                throw new SOAPExceptionImpl("Unable to copy MIME header", ex);
   1.314 +            }
   1.315 +    }
   1.316 +
   1.317 +    public static void copyMimeHeaders(MimeBodyPart mbp, AttachmentPartImpl ap)
   1.318 +        throws SOAPException {
   1.319 +        try {
   1.320 +            List hdr = mbp.getAllHeaders();
   1.321 +            int sz = hdr.size();
   1.322 +            for( int i=0; i<sz; i++ ) {
   1.323 +                Header h = (Header)hdr.get(i);
   1.324 +                if(h.getName().equalsIgnoreCase("Content-Type"))
   1.325 +                    continue;   // skip
   1.326 +                ap.addMimeHeader(h.getName(), h.getValue());
   1.327 +            }
   1.328 +        } catch (Exception ex) {
   1.329 +            log.severe("SAAJ0506.soap.cannot.copy.mime.hdrs.into.attachment");
   1.330 +            throw new SOAPExceptionImpl(
   1.331 +                "Unable to copy MIME headers into attachment",
   1.332 +                ex);
   1.333 +        }
   1.334 +    }
   1.335 +
   1.336 +    public  void setBase64Content(InputStream content, String contentType)
   1.337 +        throws SOAPException {
   1.338 +
   1.339 +        if (mimePart != null) {
   1.340 +            mimePart.close();
   1.341 +            mimePart = null;
   1.342 +        }
   1.343 +        dataHandler = null;
   1.344 +        InputStream decoded = null;
   1.345 +        try {
   1.346 +            decoded = MimeUtility.decode(content, "base64");
   1.347 +            InternetHeaders hdrs = new InternetHeaders();
   1.348 +            hdrs.setHeader("Content-Type", contentType);
   1.349 +            //TODO: reading the entire attachment here is ineffcient. Somehow the MimeBodyPart
   1.350 +            // Ctor with inputStream causes problems based on the InputStream
   1.351 +            // has markSupported()==true
   1.352 +            ByteOutputStream bos = new ByteOutputStream();
   1.353 +            bos.write(decoded);
   1.354 +            rawContent = new MimeBodyPart(hdrs, bos.getBytes(), bos.getCount());
   1.355 +            setMimeHeader("Content-Type", contentType);
   1.356 +        } catch (Exception e) {
   1.357 +            log.log(Level.SEVERE, "SAAJ0578.soap.attachment.setbase64content.exception", e);
   1.358 +            throw new SOAPExceptionImpl(e.getLocalizedMessage());
   1.359 +        } finally {
   1.360 +            try {
   1.361 +                    decoded.close();
   1.362 +            } catch (IOException ex) {
   1.363 +                throw new SOAPException(ex);
   1.364 +            }
   1.365 +        }
   1.366 +    }
   1.367 +
   1.368 +    public  InputStream getBase64Content() throws SOAPException {
   1.369 +        InputStream stream;
   1.370 +        if (mimePart != null) {
   1.371 +            stream = mimePart.read();
   1.372 +        } else if (rawContent != null) {
   1.373 +            try {
   1.374 +                 stream = rawContent.getInputStream();
   1.375 +            } catch (Exception e) {
   1.376 +                log.log(Level.SEVERE,"SAAJ0579.soap.attachment.getbase64content.exception", e);
   1.377 +                throw new SOAPExceptionImpl(e.getLocalizedMessage());
   1.378 +            }
   1.379 +        } else if (dataHandler != null) {
   1.380 +            try {
   1.381 +                stream = dataHandler.getInputStream();
   1.382 +            } catch (IOException e) {
   1.383 +                log.severe("SAAJ0574.soap.attachment.datahandler.ioexception");
   1.384 +                throw new SOAPExceptionImpl("DataHandler error" + e);
   1.385 +            }
   1.386 +        } else {
   1.387 +            log.severe("SAAJ0572.soap.no.content.for.attachment");
   1.388 +            throw new SOAPExceptionImpl("No data handler/content associated with this attachment");
   1.389 +        }
   1.390 +
   1.391 +        //TODO: Write a BASE64EncoderInputStream instead,
   1.392 +        // this code below is inefficient
   1.393 +        // where we are trying to read the whole attachment first
   1.394 +        int len;
   1.395 +        int size = 1024;
   1.396 +        byte [] buf;
   1.397 +        if (stream != null) {
   1.398 +            try {
   1.399 +                ByteArrayOutputStream bos = new ByteArrayOutputStream(size);
   1.400 +                //TODO: try and optimize this on the same lines as
   1.401 +                // ByteOutputStream : to eliminate the temp buffer here
   1.402 +                OutputStream ret = MimeUtility.encode(bos, "base64");
   1.403 +                buf = new byte[size];
   1.404 +                while ((len = stream.read(buf, 0, size)) != -1) {
   1.405 +                    ret.write(buf, 0, len);
   1.406 +                }
   1.407 +                ret.flush();
   1.408 +                buf = bos.toByteArray();
   1.409 +                return new ByteArrayInputStream(buf);
   1.410 +            } catch (Exception e) {
   1.411 +                // throw new SOAPException
   1.412 +                log.log(Level.SEVERE,"SAAJ0579.soap.attachment.getbase64content.exception", e);
   1.413 +                throw new SOAPExceptionImpl(e.getLocalizedMessage());
   1.414 +            } finally {
   1.415 +                try {
   1.416 +                    stream.close();
   1.417 +                } catch (IOException ex) {
   1.418 +                  //close the stream
   1.419 +                }
   1.420 +            }
   1.421 +        } else {
   1.422 +          //throw  new SOAPException
   1.423 +          log.log(Level.SEVERE,"SAAJ0572.soap.no.content.for.attachment");
   1.424 +          throw new SOAPExceptionImpl("No data handler/content associated with this attachment");
   1.425 +        }
   1.426 +    }
   1.427 +
   1.428 +    public void setRawContent(InputStream content, String contentType)
   1.429 +        throws SOAPException {
   1.430 +        if (mimePart != null) {
   1.431 +            mimePart.close();
   1.432 +            mimePart = null;
   1.433 +        }
   1.434 +        dataHandler = null;
   1.435 +        try {
   1.436 +            InternetHeaders hdrs = new InternetHeaders();
   1.437 +            hdrs.setHeader("Content-Type", contentType);
   1.438 +            //TODO: reading the entire attachment here is ineffcient. Somehow the MimeBodyPart
   1.439 +            // Ctor with inputStream causes problems based on whether the InputStream has
   1.440 +            // markSupported()==true or false
   1.441 +            ByteOutputStream bos = new ByteOutputStream();
   1.442 +            bos.write(content);
   1.443 +            rawContent = new MimeBodyPart(hdrs, bos.getBytes(), bos.getCount());
   1.444 +            setMimeHeader("Content-Type", contentType);
   1.445 +        } catch (Exception e) {
   1.446 +            log.log(Level.SEVERE, "SAAJ0576.soap.attachment.setrawcontent.exception", e);
   1.447 +            throw new SOAPExceptionImpl(e.getLocalizedMessage());
   1.448 +        } finally {
   1.449 +            try {
   1.450 +                content.close();
   1.451 +            } catch (IOException ex) {
   1.452 +                throw new SOAPException(ex);
   1.453 +            }
   1.454 +        }
   1.455 +    }
   1.456 +
   1.457 +   /*
   1.458 +    public void setRawContentBytes(byte[] content, String contentType)
   1.459 +        throws SOAPException {
   1.460 +        if (content == null) {
   1.461 +            throw new SOAPExceptionImpl("Null content passed to setRawContentBytes");
   1.462 +        }
   1.463 +        dataHandler = null;
   1.464 +        try {
   1.465 +            InternetHeaders hdrs = new InternetHeaders();
   1.466 +            hdrs.setHeader("Content-Type", contentType);
   1.467 +            rawContent = new MimeBodyPart(hdrs, content, content.length);
   1.468 +            setMimeHeader("Content-Type", contentType);
   1.469 +        } catch (Exception e) {
   1.470 +            log.log(Level.SEVERE, "SAAJ0576.soap.attachment.setrawcontent.exception", e);
   1.471 +            throw new SOAPExceptionImpl(e.getLocalizedMessage());
   1.472 +        }
   1.473 +    } */
   1.474 +
   1.475 +    public void setRawContentBytes(
   1.476 +        byte[] content, int off, int len, String contentType)
   1.477 +        throws SOAPException {
   1.478 +        if (mimePart != null) {
   1.479 +            mimePart.close();
   1.480 +            mimePart = null;
   1.481 +        }
   1.482 +        if (content == null) {
   1.483 +            throw new SOAPExceptionImpl("Null content passed to setRawContentBytes");
   1.484 +        }
   1.485 +        dataHandler = null;
   1.486 +        try {
   1.487 +            InternetHeaders hdrs = new InternetHeaders();
   1.488 +            hdrs.setHeader("Content-Type", contentType);
   1.489 +            rawContent = new MimeBodyPart(hdrs, content, off, len);
   1.490 +            setMimeHeader("Content-Type", contentType);
   1.491 +        } catch (Exception e) {
   1.492 +            log.log(Level.SEVERE,
   1.493 +                "SAAJ0576.soap.attachment.setrawcontent.exception", e);
   1.494 +            throw new SOAPExceptionImpl(e.getLocalizedMessage());
   1.495 +        }
   1.496 +    }
   1.497 +
   1.498 +    public  InputStream getRawContent() throws SOAPException {
   1.499 +        if (mimePart != null) {
   1.500 +            return mimePart.read();
   1.501 +        }
   1.502 +        if (rawContent != null) {
   1.503 +            try {
   1.504 +                return rawContent.getInputStream();
   1.505 +            } catch (Exception e) {
   1.506 +                log.log(Level.SEVERE,"SAAJ0577.soap.attachment.getrawcontent.exception", e);
   1.507 +                throw new SOAPExceptionImpl(e.getLocalizedMessage());
   1.508 +            }
   1.509 +        } else if (dataHandler != null) {
   1.510 +            try {
   1.511 +                return dataHandler.getInputStream();
   1.512 +            } catch (IOException e) {
   1.513 +                log.severe("SAAJ0574.soap.attachment.datahandler.ioexception");
   1.514 +                throw new SOAPExceptionImpl("DataHandler error" + e);
   1.515 +            }
   1.516 +        } else {
   1.517 +            log.severe("SAAJ0572.soap.no.content.for.attachment");
   1.518 +            throw new SOAPExceptionImpl("No data handler/content associated with this attachment");
   1.519 +        }
   1.520 +    }
   1.521 +
   1.522 +    public  byte[] getRawContentBytes() throws SOAPException {
   1.523 +        InputStream ret;
   1.524 +        if (mimePart != null) {
   1.525 +            try {
   1.526 +                ret = mimePart.read();
   1.527 +                return ASCIIUtility.getBytes(ret);
   1.528 +            } catch (IOException ex) {
   1.529 +                log.log(Level.SEVERE,"SAAJ0577.soap.attachment.getrawcontent.exception", ex);
   1.530 +                throw new SOAPExceptionImpl(ex);
   1.531 +            }
   1.532 +        }
   1.533 +        if (rawContent != null) {
   1.534 +            try {
   1.535 +                ret = rawContent.getInputStream();
   1.536 +                return ASCIIUtility.getBytes(ret);
   1.537 +            } catch (Exception e) {
   1.538 +                log.log(Level.SEVERE,"SAAJ0577.soap.attachment.getrawcontent.exception", e);
   1.539 +                throw new SOAPExceptionImpl(e);
   1.540 +            }
   1.541 +        } else if (dataHandler != null) {
   1.542 +            try {
   1.543 +                ret = dataHandler.getInputStream();
   1.544 +                return ASCIIUtility.getBytes(ret);
   1.545 +            } catch (IOException e) {
   1.546 +                log.severe("SAAJ0574.soap.attachment.datahandler.ioexception");
   1.547 +                throw new SOAPExceptionImpl("DataHandler error" + e);
   1.548 +            }
   1.549 +        } else {
   1.550 +            log.severe("SAAJ0572.soap.no.content.for.attachment");
   1.551 +            throw new SOAPExceptionImpl("No data handler/content associated with this attachment");
   1.552 +        }
   1.553 +    }
   1.554 +
   1.555 +    // attachments are equal if they are the same reference
   1.556 +    public boolean equals(Object o) {
   1.557 +        return (this == o);
   1.558 +    }
   1.559 +
   1.560 +    // In JDK 8 we get a warning if we implement equals() but not hashCode().
   1.561 +    // There is no intuitive value for this, the default one in Object is fine.
   1.562 +    public int hashCode() {
   1.563 +        return super.hashCode();
   1.564 +    }
   1.565 +
   1.566 +    public MimeHeaders getMimeHeaders() {
   1.567 +        return headers;
   1.568 +    }
   1.569 +
   1.570 +    public static void initializeJavaActivationHandlers() {
   1.571 +        // DataHandler.writeTo() may search for DCH. So adding some default ones.
   1.572 +        try {
   1.573 +            CommandMap map = CommandMap.getDefaultCommandMap();
   1.574 +            if (map instanceof MailcapCommandMap) {
   1.575 +                MailcapCommandMap mailMap = (MailcapCommandMap) map;
   1.576 +
   1.577 +                // registering our DCH since javamail's DCH doesn't handle
   1.578 +                if (!cmdMapInitialized(mailMap)) {
   1.579 +                    mailMap.addMailcap("text/xml;;x-java-content-handler=com.sun.xml.internal.messaging.saaj.soap.XmlDataContentHandler");
   1.580 +                    mailMap.addMailcap("application/xml;;x-java-content-handler=com.sun.xml.internal.messaging.saaj.soap.XmlDataContentHandler");
   1.581 +                    mailMap.addMailcap("application/fastinfoset;;x-java-content-handler=com.sun.xml.internal.messaging.saaj.soap.FastInfosetDataContentHandler");
   1.582 +                    // this handler seems to be not used according VCS history ...
   1.583 +                    // mailMap.addMailcap("multipart/*;;x-java-content-handler=com.sun.xml.internal.messaging.saaj.soap.MultipartDataContentHandler");
   1.584 +                    mailMap.addMailcap("image/*;;x-java-content-handler=com.sun.xml.internal.messaging.saaj.soap.ImageDataContentHandler");
   1.585 +                    mailMap.addMailcap("text/plain;;x-java-content-handler=com.sun.xml.internal.messaging.saaj.soap.StringDataContentHandler");
   1.586 +                }
   1.587 +            }
   1.588 +        } catch (Throwable t) {
   1.589 +            // ignore the exception.
   1.590 +        }
   1.591 +    }
   1.592 +
   1.593 +    private static boolean cmdMapInitialized(MailcapCommandMap mailMap) {
   1.594 +
   1.595 +        // checking fastinfoset handler, since this one is specific to SAAJ
   1.596 +        CommandInfo[] commands = mailMap.getAllCommands("application/fastinfoset");
   1.597 +        if (commands == null || commands.length == 0) {
   1.598 +            return false;
   1.599 +        }
   1.600 +
   1.601 +        String saajClassName = "com.sun.xml.internal.ws.binding.FastInfosetDataContentHandler";
   1.602 +        for (CommandInfo command : commands) {
   1.603 +            String commandClass = command.getCommandClass();
   1.604 +            if (saajClassName.equals(commandClass)) {
   1.605 +                return true;
   1.606 +            }
   1.607 +        }
   1.608 +        return false;
   1.609 +    }
   1.610 +}

mercurial