001: /*
002: * $Id: FastInfosetDataContentHandler.java,v 1.3 2007/07/16 16:41:21 ofung Exp $
003: * $Revision: 1.3 $
004: * $Date: 2007/07/16 16:41:21 $
005: */
006:
007: /*
008: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
009: *
010: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
011: *
012: * The contents of this file are subject to the terms of either the GNU
013: * General Public License Version 2 only ("GPL") or the Common Development
014: * and Distribution License("CDDL") (collectively, the "License"). You
015: * may not use this file except in compliance with the License. You can obtain
016: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
017: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
018: * language governing permissions and limitations under the License.
019: *
020: * When distributing the software, include this License Header Notice in each
021: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
022: * Sun designates this particular file as subject to the "Classpath" exception
023: * as provided by Sun in the GPL Version 2 section of the License file that
024: * accompanied this code. If applicable, add the following below the License
025: * Header, with the fields enclosed by brackets [] replaced by your own
026: * identifying information: "Portions Copyrighted [year]
027: * [name of copyright owner]"
028: *
029: * Contributor(s):
030: *
031: * If you wish your version of this file to be governed by only the CDDL or
032: * only the GPL Version 2, indicate your decision by adding "[Contributor]
033: * elects to include this software in this distribution under the [CDDL or GPL
034: * Version 2] license." If you don't indicate a single choice of license, a
035: * recipient has the option to distribute your version of this file under
036: * either the CDDL, the GPL Version 2 or to extend the choice of license to
037: * its licensees as provided above. However, if you add GPL Version 2 code
038: * and therefore, elected the GPL Version 2 license, then the option applies
039: * only if the new code is made subject to such option by the copyright
040: * holder.
041: */
042: package com.sun.xml.messaging.saaj.soap;
043:
044: import java.awt.datatransfer.DataFlavor;
045: import java.io.IOException;
046: import java.io.OutputStream;
047: import java.io.InputStream;
048:
049: import javax.activation.*;
050: import javax.xml.transform.Source;
051:
052: import com.sun.xml.messaging.saaj.util.FastInfosetReflection;
053:
054: /**
055: * JAF data handler for Fast Infoset content
056: *
057: * @author Santiago Pericas-Geertsen
058: */
059: public class FastInfosetDataContentHandler implements
060: DataContentHandler {
061: public final String STR_SRC = "org.jvnet.fastinfoset.FastInfosetSource";
062:
063: public FastInfosetDataContentHandler() {
064: }
065:
066: /**
067: * return the DataFlavors for this <code>DataContentHandler</code>
068: * @return The DataFlavors.
069: */
070: public DataFlavor[] getTransferDataFlavors() { // throws Exception;
071: DataFlavor flavors[] = new DataFlavor[1];
072: flavors[0] = new ActivationDataFlavor(FastInfosetReflection
073: .getFastInfosetSource_class(),
074: "application/fastinfoset", "Fast Infoset");
075: return flavors;
076: }
077:
078: /**
079: * return the Transfer Data of type DataFlavor from InputStream
080: * @param df The DataFlavor.
081: * @param ins The InputStream corresponding to the data.
082: * @return The constructed Object.
083: */
084: public Object getTransferData(DataFlavor flavor,
085: DataSource dataSource) throws IOException {
086: if (flavor.getMimeType().startsWith("application/fastinfoset")) {
087: try {
088: if (flavor.getRepresentationClass().getName().equals(
089: STR_SRC)) {
090: return FastInfosetReflection
091: .FastInfosetSource_new(dataSource
092: .getInputStream());
093: }
094: } catch (Exception e) {
095: throw new IOException(e.getMessage());
096: }
097: }
098: return null;
099: }
100:
101: public Object getContent(DataSource dataSource) throws IOException {
102: try {
103: return FastInfosetReflection
104: .FastInfosetSource_new(dataSource.getInputStream());
105: } catch (Exception e) {
106: throw new IOException(e.getMessage());
107: }
108: }
109:
110: /**
111: * construct an object from a byte stream
112: * (similar semantically to previous method, we are deciding
113: * which one to support)
114: */
115: public void writeTo(Object obj, String mimeType, OutputStream os)
116: throws IOException {
117: if (!mimeType.equals("application/fastinfoset")) {
118: throw new IOException("Invalid content type \"" + mimeType
119: + "\" for FastInfosetDCH");
120: }
121:
122: try {
123: InputStream is = FastInfosetReflection
124: .FastInfosetSource_getInputStream((Source) obj);
125:
126: int n;
127: byte[] buffer = new byte[4096];
128: while ((n = is.read(buffer)) != -1) {
129: os.write(buffer, 0, n);
130: }
131: } catch (Exception ex) {
132: throw new IOException(
133: "Error copying FI source to output stream "
134: + ex.getMessage());
135: }
136: }
137: }
|