325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.ws.encoding;
325N/A
325N/Aimport com.sun.xml.internal.org.jvnet.mimepull.MIMEPart;
325N/A
325N/Aimport javax.activation.DataSource;
325N/Aimport java.io.InputStream;
325N/Aimport java.io.IOException;
325N/Aimport java.io.OutputStream;
325N/Aimport java.io.File;
325N/A
325N/Aimport com.sun.xml.internal.ws.developer.StreamingDataHandler;
325N/A
325N/A/**
325N/A * Implementation of {@link StreamingDataHandler} to access MIME
325N/A * attachments efficiently. Applications can use the additional methods and decide
325N/A * on how to access the attachment data in JAX-WS applications.
325N/A *
325N/A * <p>
325N/A * for e.g.:
325N/A *
325N/A * DataHandler dh = proxy.getData();
325N/A * StreamingDataHandler sdh = (StreamingDataHandler)dh;
325N/A * // readOnce() doesn't store attachment on the disk in some cases
325N/A * // for e.g when only one huge attachment after soap envelope part in MIME message
325N/A * InputStream in = sdh.readOnce();
325N/A * ...
325N/A * in.close();
325N/A * sdh.close();
325N/A *
325N/A * @author Jitendra Kotamraju
325N/A */
325N/Apublic class MIMEPartStreamingDataHandler extends StreamingDataHandler {
325N/A private final StreamingDataSource ds;
325N/A
325N/A public MIMEPartStreamingDataHandler(MIMEPart part) {
325N/A super(new StreamingDataSource(part));
325N/A ds = (StreamingDataSource)getDataSource();
325N/A }
325N/A
325N/A public InputStream readOnce() throws IOException {
325N/A return ds.readOnce();
325N/A }
325N/A
325N/A public void moveTo(File file) throws IOException {
325N/A ds.moveTo(file);
325N/A }
325N/A
325N/A public void close() throws IOException {
325N/A ds.close();
325N/A }
325N/A
325N/A private static final class StreamingDataSource implements DataSource {
325N/A private final MIMEPart part;
325N/A
325N/A StreamingDataSource(MIMEPart part) {
325N/A this.part = part;
325N/A }
325N/A
325N/A public InputStream getInputStream() throws IOException {
325N/A return part.read(); //readOnce() ??
325N/A }
325N/A
325N/A InputStream readOnce() throws IOException {
325N/A try {
325N/A return part.readOnce();
325N/A } catch(Exception e) {
325N/A throw new MyIOException(e);
325N/A }
325N/A }
325N/A
325N/A void moveTo(File file) throws IOException {
325N/A part.moveTo(file);
325N/A }
325N/A
325N/A public OutputStream getOutputStream() throws IOException {
325N/A return null;
325N/A }
325N/A
325N/A public String getContentType() {
325N/A return part.getContentType();
325N/A }
325N/A
325N/A public String getName() {
325N/A return "";
325N/A }
325N/A
325N/A public void close() throws IOException {
325N/A part.close();
325N/A }
325N/A }
325N/A
325N/A private static final class MyIOException extends IOException {
325N/A private final Exception linkedException;
325N/A
325N/A MyIOException(Exception linkedException) {
325N/A this.linkedException = linkedException;
325N/A }
325N/A
325N/A @Override
325N/A public Throwable getCause() {
325N/A return linkedException;
325N/A }
325N/A }
325N/A
325N/A}