1 /*
2  * Copyright (c) 1997, 2012, 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 
26 package com.sun.xml.internal.ws.encoding;
27 
28 import com.sun.xml.internal.org.jvnet.mimepull.MIMEPart;
29 
30 import javax.activation.DataSource;
31 import java.io.InputStream;
32 import java.io.IOException;
33 import java.io.OutputStream;
34 import java.io.File;
35 
36 import 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  */
57 public class MIMEPartStreamingDataHandler extends StreamingDataHandler {
58     private final StreamingDataSource ds;
59 
MIMEPartStreamingDataHandler(MIMEPart part)60     public MIMEPartStreamingDataHandler(MIMEPart part) {
61         super(new StreamingDataSource(part));
62         ds = (StreamingDataSource)getDataSource();
63     }
64 
65     @Override
readOnce()66     public InputStream readOnce() throws IOException {
67         return ds.readOnce();
68     }
69 
70     @Override
moveTo(File file)71     public void moveTo(File file) throws IOException {
72         ds.moveTo(file);
73     }
74 
75     @Override
close()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 
StreamingDataSource(MIMEPart part)83         StreamingDataSource(MIMEPart part) {
84             this.part = part;
85         }
86 
87         @Override
getInputStream()88         public InputStream getInputStream() throws IOException {
89             return part.read();             //readOnce() ??
90         }
91 
readOnce()92         InputStream readOnce() throws IOException {
93             try {
94                 return part.readOnce();
95             } catch(Exception e) {
96                 throw new MyIOException(e);
97             }
98         }
99 
moveTo(File file)100         void moveTo(File file) throws IOException {
101             part.moveTo(file);
102         }
103 
104         @Override
getOutputStream()105         public OutputStream getOutputStream() throws IOException {
106             return null;
107         }
108 
109         @Override
getContentType()110         public String getContentType() {
111             return part.getContentType();
112         }
113 
114         @Override
getName()115         public String getName() {
116             return "";
117         }
118 
close()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 
MyIOException(Exception linkedException)127         MyIOException(Exception linkedException) {
128             this.linkedException = linkedException;
129         }
130 
131         @Override
getCause()132         public Throwable getCause() {
133             return linkedException;
134         }
135     }
136 
137 }
138