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 */ 25 26package com.sun.xml.internal.ws.message.stream; 27 28import com.sun.xml.internal.ws.api.message.Attachment; 29import com.sun.xml.internal.ws.util.ByteArrayDataSource; 30import com.sun.xml.internal.ws.util.ByteArrayBuffer; 31import com.sun.xml.internal.ws.encoding.DataSourceStreamingDataHandler; 32 33import javax.activation.DataHandler; 34import javax.xml.transform.Source; 35import javax.xml.transform.stream.StreamSource; 36import javax.xml.soap.SOAPMessage; 37import javax.xml.soap.AttachmentPart; 38import javax.xml.soap.SOAPException; 39import java.io.InputStream; 40import java.io.OutputStream; 41import java.io.IOException; 42import java.io.ByteArrayInputStream; 43 44import com.sun.xml.internal.org.jvnet.staxex.Base64Data; 45 46/** 47 * Attachment created from raw bytes. 48 * 49 * @author Vivek Pandey 50 */ 51public class StreamAttachment implements Attachment { 52 private final String contentId; 53 private final String contentType; 54 private final ByteArrayBuffer byteArrayBuffer; 55 private final byte[] data; 56 private final int len; 57 58 public StreamAttachment(ByteArrayBuffer buffer, String contentId, String contentType) { 59 this.contentId = contentId; 60 this.contentType = contentType; 61 this.byteArrayBuffer = buffer; 62 this.data = byteArrayBuffer.getRawData(); 63 this.len = byteArrayBuffer.size(); 64 } 65 66 public String getContentId() { 67 return contentId; 68 } 69 70 public String getContentType() { 71 return contentType; 72 } 73 74 75 public byte[] asByteArray() { 76 //we got to reallocate and give the exact byte[] 77 return byteArrayBuffer.toByteArray(); 78 } 79 80 public DataHandler asDataHandler() { 81 return new DataSourceStreamingDataHandler(new ByteArrayDataSource(data,0,len,getContentType())); 82 } 83 84 public Source asSource() { 85 return new StreamSource(new ByteArrayInputStream(data,0,len)); 86 } 87 88 public InputStream asInputStream() { 89 return byteArrayBuffer.newInputStream(); 90 } 91 92 public Base64Data asBase64Data(){ 93 Base64Data base64Data = new Base64Data(); 94 base64Data.set(data, len, contentType); 95 return base64Data; 96 } 97 98 public void writeTo(OutputStream os) throws IOException { 99 byteArrayBuffer.writeTo(os); 100 } 101 102 public void writeTo(SOAPMessage saaj) throws SOAPException { 103 AttachmentPart part = saaj.createAttachmentPart(); 104 part.setRawContentBytes(data,0,len,getContentType()); 105 part.setContentId(contentId); 106 saaj.addAttachmentPart(part); 107 } 108} 109