001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * $Id: StringDataContentHandler.java,v 1.2 2007/07/16 16:41:22 ofung Exp $
022: * $Revision: 1.2 $
023: * $Date: 2007/07/16 16:41:22 $
024: */
025:
026: /*
027: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
028: *
029: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
030: *
031: * The contents of this file are subject to the terms of either the GNU
032: * General Public License Version 2 only ("GPL") or the Common Development
033: * and Distribution License("CDDL") (collectively, the "License"). You
034: * may not use this file except in compliance with the License. You can obtain
035: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
036: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
037: * language governing permissions and limitations under the License.
038: *
039: * When distributing the software, include this License Header Notice in each
040: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
041: * Sun designates this particular file as subject to the "Classpath" exception
042: * as provided by Sun in the GPL Version 2 section of the License file that
043: * accompanied this code. If applicable, add the following below the License
044: * Header, with the fields enclosed by brackets [] replaced by your own
045: * identifying information: "Portions Copyrighted [year]
046: * [name of copyright owner]"
047: *
048: * Contributor(s):
049: *
050: * If you wish your version of this file to be governed by only the CDDL or
051: * only the GPL Version 2, indicate your decision by adding "[Contributor]
052: * elects to include this software in this distribution under the [CDDL or GPL
053: * Version 2] license." If you don't indicate a single choice of license, a
054: * recipient has the option to distribute your version of this file under
055: * either the CDDL, the GPL Version 2 or to extend the choice of license to
056: * its licensees as provided above. However, if you add GPL Version 2 code
057: * and therefore, elected the GPL Version 2 license, then the option applies
058: * only if the new code is made subject to such option by the copyright
059: * holder.
060: */
061: package com.sun.xml.messaging.saaj.soap;
062:
063: import java.awt.datatransfer.DataFlavor;
064: import java.io.*;
065:
066: import javax.activation.*;
067:
068: /**
069: * JAF data content handler for text/plain --> String
070: *
071: * @author Anil Vijendran
072: */
073: public class StringDataContentHandler implements DataContentHandler {
074: /**
075: * return the DataFlavors for this <code>DataContentHandler</code>
076: * @return The DataFlavors.
077: */
078: public DataFlavor[] getTransferDataFlavors() { // throws Exception;
079: DataFlavor flavors[] = new DataFlavor[2];
080:
081: try {
082: flavors[0] = new ActivationDataFlavor(Class
083: .forName("java.lang.String"), "text/plain",
084: "text string");
085: } catch (Exception e) {
086: }
087:
088: flavors[1] = new DataFlavor("text/plain", "Plain Text");
089: return flavors;
090: }
091:
092: /**
093: * return the Transfer Data of type DataFlavor from InputStream
094: * @param df The DataFlavor.
095: * @param ins The InputStream corresponding to the data.
096: * @return The constructed Object.
097: */
098: public Object getTransferData(DataFlavor df, DataSource ds) {
099:
100: // this is sort of hacky, but will work for the
101: // sake of testing...
102: if (df.getMimeType().startsWith("text/plain")) {
103: if (df.getRepresentationClass().getName().equals(
104: "java.lang.String")) {
105: // spit out String
106: StringBuffer buf = new StringBuffer();
107: char data[] = new char[1024];
108: // InputStream is = null;
109: InputStreamReader isr = null;
110: int bytes_read = 0;
111: int total_bytes = 0;
112:
113: try {
114: isr = new InputStreamReader(ds.getInputStream());
115:
116: while (true) {
117: bytes_read = isr.read(data);
118: if (bytes_read > 0)
119: buf.append(data, 0, bytes_read);
120: else
121: break;
122: total_bytes += bytes_read;
123: }
124: } catch (Exception e) {
125: }
126:
127: return buf.toString();
128:
129: } else if (df.getRepresentationClass().getName().equals(
130: "java.io.InputStream")) {
131: // spit out InputStream
132: try {
133: return ds.getInputStream();
134: } catch (Exception e) {
135: }
136: }
137:
138: }
139: return null;
140: }
141:
142: /**
143: *
144: */
145: public Object getContent(DataSource ds) { // throws Exception;
146: StringBuffer buf = new StringBuffer();
147: char data[] = new char[1024];
148: // InputStream is = null;
149: InputStreamReader isr = null;
150: int bytes_read = 0;
151: int total_bytes = 0;
152:
153: try {
154: isr = new InputStreamReader(ds.getInputStream());
155:
156: while (true) {
157: bytes_read = isr.read(data);
158: if (bytes_read > 0)
159: buf.append(data, 0, bytes_read);
160: else
161: break;
162: total_bytes += bytes_read;
163: }
164: } catch (Exception e) {
165: }
166:
167: return buf.toString();
168: }
169:
170: /**
171: * construct an object from a byte stream
172: * (similar semantically to previous method, we are deciding
173: * which one to support)
174: */
175: public void writeTo(Object obj, String mimeType, OutputStream os)
176: throws IOException {
177: if (!mimeType.startsWith("text/plain"))
178: throw new IOException("Invalid type \"" + mimeType
179: + "\" on StringDCH");
180:
181: Writer out = new OutputStreamWriter(os);
182: out.write((String) obj);
183: out.flush();
184: }
185:
186: }
|