001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.xml.ws.developer;
037:
038: import org.jvnet.mimepull.MIMEPart;
039:
040: import javax.activation.DataSource;
041: import java.io.InputStream;
042: import java.io.IOException;
043: import java.io.OutputStream;
044: import java.io.File;
045:
046: /**
047: * Implementation of {@link org.jvnet.staxex.StreamingDataHandler} to access MIME
048: * attachments efficiently. Applications can use the additional methods and decide
049: * on how to access the attachment data in JAX-WS applications.
050: *
051: * <p>
052: * for e.g.:
053: *
054: * DataHandler dh = proxy.getData();
055: * StreamingDataHandler sdh = (StreamingDataHandler)dh;
056: * // readOnce() doesn't store attachment on the disk in some cases
057: * // for e.g when only one huge attachment after soap envelope part in MIME message
058: * InputStream in = sdh.readOnce();
059: * ...
060: * in.close();
061: * sdh.close();
062: *
063: * @author Jitendra Kotamraju
064: */
065: public class StreamingDataHandler extends
066: org.jvnet.staxex.StreamingDataHandler {
067: private final StreamingDataSource ds;
068:
069: public StreamingDataHandler(MIMEPart part) {
070: super (new StreamingDataSource(part));
071: ds = (StreamingDataSource) getDataSource();
072: }
073:
074: public InputStream readOnce() throws IOException {
075: return ds.readOnce();
076: }
077:
078: public void moveTo(File file) throws IOException {
079: ds.moveTo(file);
080: }
081:
082: public void close() throws IOException {
083: ds.close();
084: }
085:
086: private static final class StreamingDataSource implements
087: DataSource {
088: private final MIMEPart part;
089:
090: StreamingDataSource(MIMEPart part) {
091: this .part = part;
092: }
093:
094: public InputStream getInputStream() throws IOException {
095: return part.read(); //readOnce() ??
096: }
097:
098: InputStream readOnce() throws IOException {
099: try {
100: return part.readOnce();
101: } catch (Exception e) {
102: throw new MyIOException(e);
103: }
104: }
105:
106: void moveTo(File file) throws IOException {
107: part.moveTo(file);
108: }
109:
110: public OutputStream getOutputStream() throws IOException {
111: return null;
112: }
113:
114: public String getContentType() {
115: return part.getContentType();
116: }
117:
118: public String getName() {
119: return "";
120: }
121:
122: public void close() throws IOException {
123: part.close();
124: }
125: }
126:
127: private static final class MyIOException extends IOException {
128: private final Exception linkedException;
129:
130: MyIOException(Exception linkedException) {
131: this .linkedException = linkedException;
132: }
133:
134: @Override
135: public Throwable getCause() {
136: return linkedException;
137: }
138: }
139:
140: }
|