src/share/jaxws_classes/com/sun/xml/internal/ws/message/jaxb/AttachmentMarshallerImpl.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/ws/message/jaxb/AttachmentMarshallerImpl.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,110 @@
     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.ws.message.jaxb;
    1.30 +
    1.31 +import com.sun.istack.internal.logging.Logger;
    1.32 +import com.sun.xml.internal.ws.api.message.Attachment;
    1.33 +import com.sun.xml.internal.ws.api.message.AttachmentSet;
    1.34 +import com.sun.xml.internal.ws.message.DataHandlerAttachment;
    1.35 +
    1.36 +import javax.activation.DataHandler;
    1.37 +import javax.xml.bind.attachment.AttachmentMarshaller;
    1.38 +import javax.xml.ws.WebServiceException;
    1.39 +import java.io.UnsupportedEncodingException;
    1.40 +import java.net.MalformedURLException;
    1.41 +import java.net.URI;
    1.42 +import java.net.URISyntaxException;
    1.43 +import java.net.URLEncoder;
    1.44 +import java.util.UUID;
    1.45 +import java.util.logging.Level;
    1.46 +
    1.47 +/**
    1.48 + * Implementation of {@link AttachmentMarshaller}, its used from JAXBMessage to marshall swaref type
    1.49 + *
    1.50 + * @author Vivek Pandey
    1.51 + * @see JAXBMessage
    1.52 + */
    1.53 +final class AttachmentMarshallerImpl extends AttachmentMarshaller {
    1.54 +
    1.55 +    private static final Logger LOGGER = Logger.getLogger(AttachmentMarshallerImpl.class);
    1.56 +
    1.57 +    private AttachmentSet attachments;
    1.58 +
    1.59 +    public AttachmentMarshallerImpl(AttachmentSet attachemnts) {
    1.60 +        this.attachments = attachemnts;
    1.61 +    }
    1.62 +
    1.63 +    /**
    1.64 +     * Release a reference to user objects to avoid keeping it in memory.
    1.65 +     */
    1.66 +    void cleanup() {
    1.67 +        attachments = null;
    1.68 +    }
    1.69 +
    1.70 +    @Override
    1.71 +    public String addMtomAttachment(DataHandler data, String elementNamespace, String elementLocalName) {
    1.72 +        // We don't use JAXB for handling XOP
    1.73 +        throw new IllegalStateException();
    1.74 +    }
    1.75 +
    1.76 +    @Override
    1.77 +    public String addMtomAttachment(byte[] data, int offset, int length, String mimeType, String elementNamespace, String elementLocalName) {
    1.78 +        // We don't use JAXB for handling XOP
    1.79 +        throw new IllegalStateException();
    1.80 +    }
    1.81 +
    1.82 +    @Override
    1.83 +    public String addSwaRefAttachment(DataHandler data) {
    1.84 +        String cid = encodeCid(null);
    1.85 +        Attachment att = new DataHandlerAttachment(cid, data);
    1.86 +        attachments.add(att);
    1.87 +        cid = "cid:" + cid;
    1.88 +        return cid;
    1.89 +    }
    1.90 +
    1.91 +    private String encodeCid(String ns) {
    1.92 +        String cid = "example.jaxws.sun.com";
    1.93 +        String name = UUID.randomUUID() + "@";
    1.94 +        if (ns != null && (ns.length() > 0)) {
    1.95 +            try {
    1.96 +                URI uri = new URI(ns);
    1.97 +                cid = uri.toURL().getHost();
    1.98 +            } catch (URISyntaxException e) {
    1.99 +                if (LOGGER.isLoggable(Level.INFO)) {
   1.100 +                    LOGGER.log(Level.INFO, null, e);
   1.101 +                }
   1.102 +                return null;
   1.103 +            } catch (MalformedURLException e) {
   1.104 +                try {
   1.105 +                    cid = URLEncoder.encode(ns, "UTF-8");
   1.106 +                } catch (UnsupportedEncodingException e1) {
   1.107 +                    throw new WebServiceException(e);
   1.108 +                }
   1.109 +            }
   1.110 +        }
   1.111 +        return name + cid;
   1.112 +    }
   1.113 +}

mercurial