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.encoding; 27 28import com.sun.xml.internal.org.jvnet.mimepull.MIMEPart; 29 30import javax.activation.DataSource; 31import java.io.InputStream; 32import java.io.IOException; 33import java.io.OutputStream; 34import java.io.File; 35 36import com.sun.xml.internal.ws.developer.StreamingDataHandler; 37 38/** 39 * Implementation of {@link StreamingDataHandler} to access MIME 40 * attachments efficiently. Applications can use the additional methods and decide 41 * on how to access the attachment data in JAX-WS applications. 42 * 43 * <p> 44 * for e.g.: 45 * 46 * DataHandler dh = proxy.getData(); 47 * StreamingDataHandler sdh = (StreamingDataHandler)dh; 48 * // readOnce() doesn't store attachment on the disk in some cases 49 * // for e.g when only one huge attachment after soap envelope part in MIME message 50 * InputStream in = sdh.readOnce(); 51 * ... 52 * in.close(); 53 * sdh.close(); 54 * 55 * @author Jitendra Kotamraju 56 */ 57public class MIMEPartStreamingDataHandler extends StreamingDataHandler { 58 private final StreamingDataSource ds; 59 60 public MIMEPartStreamingDataHandler(MIMEPart part) { 61 super(new StreamingDataSource(part)); 62 ds = (StreamingDataSource)getDataSource(); 63 } 64 65 @Override 66 public InputStream readOnce() throws IOException { 67 return ds.readOnce(); 68 } 69 70 @Override 71 public void moveTo(File file) throws IOException { 72 ds.moveTo(file); 73 } 74 75 @Override 76 public void close() throws IOException { 77 ds.close(); 78 } 79 80 private static final class StreamingDataSource implements DataSource { 81 private final MIMEPart part; 82 83 StreamingDataSource(MIMEPart part) { 84 this.part = part; 85 } 86 87 @Override 88 public InputStream getInputStream() throws IOException { 89 return part.read(); //readOnce() ?? 90 } 91 92 InputStream readOnce() throws IOException { 93 try { 94 return part.readOnce(); 95 } catch(Exception e) { 96 throw new MyIOException(e); 97 } 98 } 99 100 void moveTo(File file) throws IOException { 101 part.moveTo(file); 102 } 103 104 @Override 105 public OutputStream getOutputStream() throws IOException { 106 return null; 107 } 108 109 @Override 110 public String getContentType() { 111 return part.getContentType(); 112 } 113 114 @Override 115 public String getName() { 116 return ""; 117 } 118 119 public void close() throws IOException { 120 part.close(); 121 } 122 } 123 124 private static final class MyIOException extends IOException { 125 private final Exception linkedException; 126 127 MyIOException(Exception linkedException) { 128 this.linkedException = linkedException; 129 } 130 131 @Override 132 public Throwable getCause() { 133 return linkedException; 134 } 135 } 136 137} 138