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 Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,101 @@ 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.ws.message.jaxb; 1.30 + 1.31 +import com.sun.xml.internal.ws.api.message.Attachment; 1.32 +import com.sun.xml.internal.ws.api.message.AttachmentSet; 1.33 +import com.sun.xml.internal.ws.message.AttachmentSetImpl; 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 + 1.46 +/** 1.47 + * Implementation of {@link AttachmentMarshaller}, its used from JAXBMessage to marshall swaref type 1.48 + * 1.49 + * @author Vivek Pandey 1.50 + * @see JAXBMessage 1.51 + */ 1.52 +final class AttachmentMarshallerImpl extends AttachmentMarshaller { 1.53 + private AttachmentSet attachments; 1.54 + 1.55 + public AttachmentMarshallerImpl(AttachmentSet attachemnts) { 1.56 + this.attachments = attachemnts; 1.57 + } 1.58 + 1.59 + /** 1.60 + * Release a reference to user objects to avoid keeping it in memory. 1.61 + */ 1.62 + void cleanup() { 1.63 + attachments = null; 1.64 + } 1.65 + 1.66 + public String addMtomAttachment(DataHandler data, String elementNamespace, String elementLocalName) { 1.67 + // We don't use JAXB for handling XOP 1.68 + throw new IllegalStateException(); 1.69 + } 1.70 + 1.71 + public String addMtomAttachment(byte[] data, int offset, int length, String mimeType, 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 + public String addSwaRefAttachment(DataHandler data) { 1.77 + String cid = encodeCid(null); 1.78 + Attachment att = new DataHandlerAttachment(cid, data); 1.79 + attachments.add(att); 1.80 + cid = "cid:" + cid; 1.81 + return cid; 1.82 + } 1.83 + 1.84 + private String encodeCid(String ns) { 1.85 + String cid = "example.jaxws.sun.com"; 1.86 + String name = UUID.randomUUID() + "@"; 1.87 + if (ns != null && (ns.length() > 0)) { 1.88 + try { 1.89 + URI uri = new URI(ns); 1.90 + cid = uri.toURL().getHost(); 1.91 + } catch (URISyntaxException e) { 1.92 + e.printStackTrace(); 1.93 + return null; 1.94 + } catch (MalformedURLException e) { 1.95 + try { 1.96 + cid = URLEncoder.encode(ns, "UTF-8"); 1.97 + } catch (UnsupportedEncodingException e1) { 1.98 + throw new WebServiceException(e); 1.99 + } 1.100 + } 1.101 + } 1.102 + return name + cid; 1.103 + } 1.104 +}