src/share/jaxws_classes/com/sun/xml/internal/ws/message/jaxb/AttachmentMarshallerImpl.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

     1 /*
     2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.xml.internal.ws.message.jaxb;
    28 import com.sun.istack.internal.logging.Logger;
    29 import com.sun.xml.internal.ws.api.message.Attachment;
    30 import com.sun.xml.internal.ws.api.message.AttachmentSet;
    31 import com.sun.xml.internal.ws.message.DataHandlerAttachment;
    33 import javax.activation.DataHandler;
    34 import javax.xml.bind.attachment.AttachmentMarshaller;
    35 import javax.xml.ws.WebServiceException;
    36 import java.io.UnsupportedEncodingException;
    37 import java.net.MalformedURLException;
    38 import java.net.URI;
    39 import java.net.URISyntaxException;
    40 import java.net.URLEncoder;
    41 import java.util.UUID;
    42 import java.util.logging.Level;
    44 /**
    45  * Implementation of {@link AttachmentMarshaller}, its used from JAXBMessage to marshall swaref type
    46  *
    47  * @author Vivek Pandey
    48  * @see JAXBMessage
    49  */
    50 final class AttachmentMarshallerImpl extends AttachmentMarshaller {
    52     private static final Logger LOGGER = Logger.getLogger(AttachmentMarshallerImpl.class);
    54     private AttachmentSet attachments;
    56     public AttachmentMarshallerImpl(AttachmentSet attachemnts) {
    57         this.attachments = attachemnts;
    58     }
    60     /**
    61      * Release a reference to user objects to avoid keeping it in memory.
    62      */
    63     void cleanup() {
    64         attachments = null;
    65     }
    67     @Override
    68     public String addMtomAttachment(DataHandler data, String elementNamespace, String elementLocalName) {
    69         // We don't use JAXB for handling XOP
    70         throw new IllegalStateException();
    71     }
    73     @Override
    74     public String addMtomAttachment(byte[] data, int offset, int length, String mimeType, String elementNamespace, String elementLocalName) {
    75         // We don't use JAXB for handling XOP
    76         throw new IllegalStateException();
    77     }
    79     @Override
    80     public String addSwaRefAttachment(DataHandler data) {
    81         String cid = encodeCid(null);
    82         Attachment att = new DataHandlerAttachment(cid, data);
    83         attachments.add(att);
    84         cid = "cid:" + cid;
    85         return cid;
    86     }
    88     private String encodeCid(String ns) {
    89         String cid = "example.jaxws.sun.com";
    90         String name = UUID.randomUUID() + "@";
    91         if (ns != null && (ns.length() > 0)) {
    92             try {
    93                 URI uri = new URI(ns);
    94                 cid = uri.toURL().getHost();
    95             } catch (URISyntaxException e) {
    96                 if (LOGGER.isLoggable(Level.INFO)) {
    97                     LOGGER.log(Level.INFO, null, e);
    98                 }
    99                 return null;
   100             } catch (MalformedURLException e) {
   101                 try {
   102                     cid = URLEncoder.encode(ns, "UTF-8");
   103                 } catch (UnsupportedEncodingException e1) {
   104                     throw new WebServiceException(e);
   105                 }
   106             }
   107         }
   108         return name + cid;
   109     }
   110 }

mercurial