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 Emil Ong
028: */
029:
030: package com.caucho.xml.saaj;
031:
032: import java.io.*;
033: import java.util.*;
034:
035: import javax.activation.*;
036: import javax.imageio.*;
037: import javax.mail.util.ByteArrayDataSource;
038: import javax.xml.soap.*;
039: import javax.xml.transform.stream.StreamSource;
040:
041: import com.caucho.util.Base64;
042:
043: public class AttachmentPartImpl extends AttachmentPart {
044: private static final String CONTENT_TYPE = "Content-Type";
045:
046: static {
047: try {
048: // Make sure the data handler gets its DataContentHandlers from us.
049: // Setting the command map is not sufficient.
050: DataHandler
051: .setDataContentHandlerFactory(SAAJCommandMap.COMMAND_MAP);
052: } catch (Error e) {
053: }
054: }
055:
056: private DataHandler _dataHandler;
057: private MimeHeaders _headers = new MimeHeaders();
058:
059: public void addMimeHeader(String name, String value) {
060: _headers.addHeader(name, value);
061: }
062:
063: public Iterator getAllMimeHeaders() {
064: return _headers.getAllHeaders();
065: }
066:
067: public Iterator getMatchingMimeHeaders(String[] names) {
068: return _headers.getMatchingHeaders(names);
069: }
070:
071: public String[] getMimeHeader(String name) {
072: return _headers.getHeader(name);
073: }
074:
075: public Iterator getNonMatchingMimeHeaders(String[] names) {
076: return _headers.getNonMatchingHeaders(names);
077: }
078:
079: public void removeAllMimeHeaders() {
080: _headers.removeAllHeaders();
081: }
082:
083: public void removeMimeHeader(String header) {
084: _headers.removeHeader(header);
085: }
086:
087: public void setMimeHeader(String name, String value) {
088: try {
089: if (CONTENT_TYPE.equals(name)) {
090: if (_dataHandler != null
091: && !value.equals(_dataHandler.getContentType())) {
092: Object o = _dataHandler.getContent();
093: _dataHandler = new DataHandler(o, value);
094: }
095: }
096: } catch (IOException e) {
097: // do nothing?
098: }
099:
100: _headers.setHeader(name, value);
101: }
102:
103: //////////////////
104:
105: public void clearContent() {
106: _dataHandler = null;
107: }
108:
109: public InputStream getBase64Content() throws SOAPException {
110: try {
111: InputStream is = getRawContent();
112: ByteArrayOutputStream os = new ByteArrayOutputStream();
113: OutputStreamWriter w = new OutputStreamWriter(os);
114:
115: Base64.encode(w, is);
116:
117: return new ByteArrayInputStream(os.toByteArray());
118: } catch (IOException e) {
119: throw new SOAPException(e);
120: }
121: }
122:
123: public Object getContent() throws SOAPException {
124: if (_dataHandler == null)
125: throw new SOAPException("No content available");
126:
127: try {
128: return _dataHandler.getContent();
129: } catch (IOException e) {
130: return new SOAPException(e);
131: }
132: }
133:
134: public DataHandler getDataHandler() throws SOAPException {
135: if (_dataHandler == null)
136: throw new SOAPException("DataHandler not set");
137:
138: return _dataHandler;
139: }
140:
141: public InputStream getRawContent() throws SOAPException {
142: if (_dataHandler == null)
143: throw new SOAPException("Content not set");
144:
145: try {
146: return _dataHandler.getInputStream();
147: } catch (IOException e) {
148: throw new SOAPException(e);
149: }
150: }
151:
152: public byte[] getRawContentBytes() throws SOAPException {
153: try {
154: ByteArrayOutputStream os = new ByteArrayOutputStream();
155: _dataHandler.writeTo(os);
156:
157: return os.toByteArray();
158: } catch (IOException e) {
159: throw new SOAPException(e);
160: }
161: }
162:
163: public int getSize() throws SOAPException {
164: if (_dataHandler == null)
165: return 0;
166:
167: byte[] buffer = getRawContentBytes();
168:
169: _dataHandler = new DataHandler(new ByteArrayDataSource(buffer,
170: getContentType()));
171: _dataHandler.setCommandMap(SAAJCommandMap.COMMAND_MAP);
172:
173: return buffer.length;
174: }
175:
176: public void setBase64Content(InputStream content, String contentType)
177: throws SOAPException {
178: if (content == null)
179: throw new SOAPException(
180: "Content InputStream cannot be null");
181:
182: try {
183: ByteArrayOutputStream os = new ByteArrayOutputStream();
184: Base64.decode(new InputStreamReader(content), os);
185:
186: byte[] buffer = os.toByteArray();
187: setRawContentBytes(buffer, 0, buffer.length, contentType);
188: } catch (Exception e) {
189: // IOException and any Exceptions thrown by Base64 (e.g. ArrayOutOfBounds)
190: throw new SOAPException(e);
191: }
192: }
193:
194: public void setContent(Object object, String contentType) {
195: setContentType(contentType);
196: _dataHandler = new DataHandler(object, contentType);
197: _dataHandler.setCommandMap(SAAJCommandMap.COMMAND_MAP);
198: }
199:
200: public void setDataHandler(DataHandler dataHandler) {
201: if (dataHandler == null)
202: throw new IllegalArgumentException(
203: "DataHandler cannot be null");
204:
205: _dataHandler = dataHandler;
206: }
207:
208: public void setRawContent(InputStream content, String contentType)
209: throws SOAPException {
210: if (content == null)
211: throw new SOAPException("InputStream cannot be null");
212:
213: setContentType(contentType);
214:
215: try {
216: DataSource source = new ByteArrayDataSource(content,
217: contentType);
218: _dataHandler = new DataHandler(source);
219: _dataHandler.setCommandMap(SAAJCommandMap.COMMAND_MAP);
220: } catch (IOException e) {
221: throw new SOAPException(e);
222: }
223: }
224:
225: public void setRawContentBytes(byte[] content, int offset, int len,
226: String contentType) throws SOAPException {
227: setContentType(contentType);
228:
229: try {
230: InputStream stream = new ByteArrayInputStream(content,
231: offset, len);
232: DataSource source = new ByteArrayDataSource(stream,
233: contentType);
234: _dataHandler = new DataHandler(source);
235: _dataHandler.setCommandMap(SAAJCommandMap.COMMAND_MAP);
236: } catch (IOException e) {
237: throw new SOAPException(e);
238: }
239: }
240: }
|