aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.ws.message.jaxb; aoqi@0: aoqi@0: import com.sun.istack.internal.logging.Logger; aoqi@0: import com.sun.xml.internal.ws.api.message.Attachment; aoqi@0: import com.sun.xml.internal.ws.api.message.AttachmentSet; aoqi@0: import com.sun.xml.internal.ws.message.DataHandlerAttachment; aoqi@0: aoqi@0: import javax.activation.DataHandler; aoqi@0: import javax.xml.bind.attachment.AttachmentMarshaller; aoqi@0: import javax.xml.ws.WebServiceException; aoqi@0: import java.io.UnsupportedEncodingException; aoqi@0: import java.net.MalformedURLException; aoqi@0: import java.net.URI; aoqi@0: import java.net.URISyntaxException; aoqi@0: import java.net.URLEncoder; aoqi@0: import java.util.UUID; aoqi@0: import java.util.logging.Level; aoqi@0: aoqi@0: /** aoqi@0: * Implementation of {@link AttachmentMarshaller}, its used from JAXBMessage to marshall swaref type aoqi@0: * aoqi@0: * @author Vivek Pandey aoqi@0: * @see JAXBMessage aoqi@0: */ aoqi@0: final class AttachmentMarshallerImpl extends AttachmentMarshaller { aoqi@0: aoqi@0: private static final Logger LOGGER = Logger.getLogger(AttachmentMarshallerImpl.class); aoqi@0: aoqi@0: private AttachmentSet attachments; aoqi@0: aoqi@0: public AttachmentMarshallerImpl(AttachmentSet attachemnts) { aoqi@0: this.attachments = attachemnts; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Release a reference to user objects to avoid keeping it in memory. aoqi@0: */ aoqi@0: void cleanup() { aoqi@0: attachments = null; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public String addMtomAttachment(DataHandler data, String elementNamespace, String elementLocalName) { aoqi@0: // We don't use JAXB for handling XOP aoqi@0: throw new IllegalStateException(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public String addMtomAttachment(byte[] data, int offset, int length, String mimeType, String elementNamespace, String elementLocalName) { aoqi@0: // We don't use JAXB for handling XOP aoqi@0: throw new IllegalStateException(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public String addSwaRefAttachment(DataHandler data) { aoqi@0: String cid = encodeCid(null); aoqi@0: Attachment att = new DataHandlerAttachment(cid, data); aoqi@0: attachments.add(att); aoqi@0: cid = "cid:" + cid; aoqi@0: return cid; aoqi@0: } aoqi@0: aoqi@0: private String encodeCid(String ns) { aoqi@0: String cid = "example.jaxws.sun.com"; aoqi@0: String name = UUID.randomUUID() + "@"; aoqi@0: if (ns != null && (ns.length() > 0)) { aoqi@0: try { aoqi@0: URI uri = new URI(ns); aoqi@0: cid = uri.toURL().getHost(); aoqi@0: } catch (URISyntaxException e) { aoqi@0: if (LOGGER.isLoggable(Level.INFO)) { aoqi@0: LOGGER.log(Level.INFO, null, e); aoqi@0: } aoqi@0: return null; aoqi@0: } catch (MalformedURLException e) { aoqi@0: try { aoqi@0: cid = URLEncoder.encode(ns, "UTF-8"); aoqi@0: } catch (UnsupportedEncodingException e1) { aoqi@0: throw new WebServiceException(e); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return name + cid; aoqi@0: } aoqi@0: }