001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.xml.saaj;
031:
032: import java.awt.datatransfer.*;
033: import java.awt.image.*;
034: import java.io.*;
035: import java.util.*;
036:
037: import javax.activation.*;
038: import javax.imageio.*;
039: import javax.imageio.stream.*;
040: import javax.xml.namespace.*;
041: import javax.xml.soap.*;
042: import javax.xml.transform.stream.*;
043:
044: public class SAAJCommandMap extends MailcapCommandMap implements
045: DataContentHandlerFactory {
046: private static final DataContentHandler _plainDataContentHandler = new PlainDataContentHandler();
047:
048: private static final DataContentHandler _xmlDataContentHandler = new XmlDataContentHandler();
049:
050: private static final DataContentHandler _imageDataContentHandler = new ImageDataContentHandler();
051:
052: public static final SAAJCommandMap COMMAND_MAP = new SAAJCommandMap();
053:
054: public DataContentHandler createDataContentHandler(String mimeType) {
055: if ("text/plain".equals(mimeType))
056: return _plainDataContentHandler;
057: else if ("text/xml".equals(mimeType))
058: return _xmlDataContentHandler;
059: else if ("image/gif".equals(mimeType))
060: return _imageDataContentHandler;
061: else if ("image/jpeg".equals(mimeType))
062: return _imageDataContentHandler;
063: else
064: return super .createDataContentHandler(mimeType);
065: }
066:
067: static class PlainDataContentHandler implements DataContentHandler {
068: static final DataFlavor[] flavors = new DataFlavor[] { new DataFlavor(
069: String.class, "text/plain") };
070:
071: public Object getContent(DataSource ds) throws IOException {
072: InputStream is = ds.getInputStream();
073: ByteArrayOutputStream os = new ByteArrayOutputStream();
074:
075: for (int b = is.read(); b >= 0; b = is.read())
076: os.write(b);
077:
078: return new String(os.toByteArray());
079: }
080:
081: public Object getTransferData(DataFlavor df, DataSource ds)
082: throws UnsupportedFlavorException, IOException {
083: if (df.equals(flavors[0]))
084: return getContent(ds);
085:
086: throw new UnsupportedFlavorException(df);
087: }
088:
089: public DataFlavor[] getTransferDataFlavors() {
090: return flavors;
091: }
092:
093: public void writeTo(Object obj, String mimeType, OutputStream os)
094: throws IOException {
095: os.write(obj.toString().getBytes());
096: }
097: }
098:
099: static class XmlDataContentHandler implements DataContentHandler {
100: static final DataFlavor[] flavors = new DataFlavor[] { new DataFlavor(
101: StreamSource.class, "text/xml") };
102:
103: public Object getContent(DataSource ds) throws IOException {
104: return new StreamSource(ds.getInputStream());
105: }
106:
107: public Object getTransferData(DataFlavor df, DataSource ds)
108: throws UnsupportedFlavorException, IOException {
109: if (df.equals(flavors[0]))
110: return getContent(ds);
111:
112: throw new UnsupportedFlavorException(df);
113: }
114:
115: public DataFlavor[] getTransferDataFlavors() {
116: return flavors;
117: }
118:
119: public void writeTo(Object obj, String mimeType, OutputStream os)
120: throws IOException {
121: StreamSource source = (StreamSource) obj;
122:
123: InputStream is = source.getInputStream();
124:
125: if (is != null) {
126: // copy the input stream and then reset it
127: ByteArrayOutputStream baos = new ByteArrayOutputStream();
128:
129: for (int b = is.read(); b >= 0; b = is.read()) {
130: os.write(b);
131: baos.write(b);
132: }
133:
134: source.setInputStream(new ByteArrayInputStream(baos
135: .toByteArray()));
136: } else {
137: Reader r = source.getReader();
138:
139: if (r != null) {
140: CharArrayWriter copy = new CharArrayWriter();
141:
142: // XXX set encoding
143: Writer w = new OutputStreamWriter(os);
144:
145: for (int ch = r.read(); ch >= 0; ch = r.read()) {
146: w.write(ch);
147: copy.write(ch);
148: }
149:
150: w.flush();
151:
152: source.setReader(new CharArrayReader(copy
153: .toCharArray()));
154: } else
155: throw new IOException(
156: "No data available from StreamSource");
157: }
158: }
159: }
160:
161: static class ImageDataContentHandler implements DataContentHandler {
162: static final DataFlavor[] flavors = new DataFlavor[] {
163: new DataFlavor(BufferedImage.class, "image/gif"),
164: new DataFlavor(BufferedImage.class, "image/jpeg") };
165:
166: public Object getContent(DataSource ds) throws IOException {
167: return ImageIO.read(ds.getInputStream());
168: }
169:
170: public Object getTransferData(DataFlavor df, DataSource ds)
171: throws UnsupportedFlavorException, IOException {
172: if (df.equals(flavors[0]) || df.equals(flavors[1]))
173: return getContent(ds);
174:
175: throw new UnsupportedFlavorException(df);
176: }
177:
178: public DataFlavor[] getTransferDataFlavors() {
179: return flavors;
180: }
181:
182: public void writeTo(Object obj, String mimeType, OutputStream os)
183: throws IOException {
184: int slash = mimeType.indexOf('/');
185:
186: if (slash < 0)
187: throw new IOException("Unrecognized MIME type : "
188: + mimeType);
189:
190: String formatName = mimeType.substring(slash + 1);
191:
192: Iterator writers = ImageIO
193: .getImageWritersByFormatName(formatName);
194:
195: if (!writers.hasNext())
196: throw new IOException("Unsupported image type : "
197: + mimeType);
198:
199: ImageWriter writer = (ImageWriter) writers.next();
200:
201: MemoryCacheImageOutputStream out = new MemoryCacheImageOutputStream(
202: os);
203:
204: writer.setOutput(out);
205: writer.write((BufferedImage) obj);
206:
207: out.flush();
208: }
209: }
210: }
|