aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.messaging.saaj.soap; aoqi@0: aoqi@0: import java.awt.datatransfer.DataFlavor; aoqi@0: import java.io.IOException; aoqi@0: import java.io.OutputStream; aoqi@0: import java.io.InputStream; aoqi@0: aoqi@0: import javax.activation.*; aoqi@0: import javax.xml.transform.Source; aoqi@0: aoqi@0: import com.sun.xml.internal.messaging.saaj.util.FastInfosetReflection; aoqi@0: aoqi@0: /** aoqi@0: * JAF data handler for Fast Infoset content aoqi@0: * aoqi@0: * @author Santiago Pericas-Geertsen aoqi@0: */ aoqi@0: public class FastInfosetDataContentHandler implements DataContentHandler { aoqi@0: public static final String STR_SRC = "com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetSource"; aoqi@0: aoqi@0: public FastInfosetDataContentHandler() { aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * return the DataFlavors for this DataContentHandler aoqi@0: * @return The DataFlavors. aoqi@0: */ aoqi@0: public DataFlavor[] getTransferDataFlavors() { // throws Exception; aoqi@0: DataFlavor flavors[] = new DataFlavor[1]; aoqi@0: flavors[0] = new ActivationDataFlavor( aoqi@0: FastInfosetReflection.getFastInfosetSource_class(), aoqi@0: "application/fastinfoset", "Fast Infoset"); aoqi@0: return flavors; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * return the Transfer Data of type DataFlavor from InputStream aoqi@0: * @param df The DataFlavor. aoqi@0: * @param ins The InputStream corresponding to the data. aoqi@0: * @return The constructed Object. aoqi@0: */ aoqi@0: public Object getTransferData(DataFlavor flavor, DataSource dataSource) aoqi@0: throws IOException aoqi@0: { aoqi@0: if (flavor.getMimeType().startsWith("application/fastinfoset")) { aoqi@0: try { aoqi@0: if (flavor.getRepresentationClass().getName().equals(STR_SRC)) { aoqi@0: return FastInfosetReflection.FastInfosetSource_new( aoqi@0: dataSource.getInputStream()); aoqi@0: } aoqi@0: } aoqi@0: catch (Exception e) { aoqi@0: throw new IOException(e.getMessage()); aoqi@0: } aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public Object getContent(DataSource dataSource) throws IOException { aoqi@0: try { aoqi@0: return FastInfosetReflection.FastInfosetSource_new( aoqi@0: dataSource.getInputStream()); aoqi@0: } aoqi@0: catch (Exception e) { aoqi@0: throw new IOException(e.getMessage()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * construct an object from a byte stream aoqi@0: * (similar semantically to previous method, we are deciding aoqi@0: * which one to support) aoqi@0: */ aoqi@0: public void writeTo(Object obj, String mimeType, OutputStream os) aoqi@0: throws IOException aoqi@0: { aoqi@0: if (!mimeType.equals("application/fastinfoset")) { aoqi@0: throw new IOException("Invalid content type \"" + mimeType aoqi@0: + "\" for FastInfosetDCH"); aoqi@0: } aoqi@0: aoqi@0: try { aoqi@0: InputStream is = FastInfosetReflection.FastInfosetSource_getInputStream( aoqi@0: (Source) obj); aoqi@0: aoqi@0: int n; byte[] buffer = new byte[4096]; aoqi@0: while ((n = is.read(buffer)) != -1) { aoqi@0: os.write(buffer, 0, n); aoqi@0: } aoqi@0: } aoqi@0: catch (Exception ex) { aoqi@0: throw new IOException( aoqi@0: "Error copying FI source to output stream " + ex.getMessage()); aoqi@0: } aoqi@0: } aoqi@0: }