001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.mail.datasource;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023:
024: import org.apache.excalibur.source.SourceUtil;
025:
026: /**
027: * The InputStreamDataSource class provides an object, that wraps an
028: * {@link InputStream} object in a DataSource interface.
029: *
030: * @see javax.activation.DataSource
031: * @version $Id: InputStreamDataSource.java 469261 2006-10-30 20:29:14Z vgritsenko $
032: */
033: public class InputStreamDataSource extends AbstractDataSource {
034:
035: private byte[] data;
036:
037: /**
038: * Creates a new instance of FilePartDataSource from an
039: * {@link InputStream} object.
040: *
041: * @param in An {@link InputStream} object.
042: */
043: public InputStreamDataSource(InputStream in) throws IOException {
044: this (in, null, null);
045: }
046:
047: /**
048: * Creates a new instance of FilePartDataSource from a byte array.
049: */
050: public InputStreamDataSource(byte[] data, String type, String name) {
051: super (getName(name), getType(type));
052:
053: if (data == null) {
054: this .data = new byte[0];
055: } else {
056: this .data = data;
057: }
058: }
059:
060: /**
061: * Creates a new instance of FilePartDataSource from an
062: * {@link InputStream} object.
063: *
064: * @param in An {@link InputStream} object.
065: */
066: public InputStreamDataSource(InputStream in, String type,
067: String name) throws IOException {
068: super (getName(name), getType(type));
069:
070: // Need to copy contents of InputStream into byte array since getInputStream
071: // method is called more than once by JavaMail API.
072: if (in == null) {
073: data = new byte[0];
074: } else {
075: ByteArrayOutputStream out = new ByteArrayOutputStream();
076: SourceUtil.copy(in, out);
077: data = out.toByteArray();
078: }
079: }
080:
081: /**
082: * Determines the name for this <code>DataSource</code> object.
083: * It is first non empty value from the list:
084: * <ul>
085: * <li>The value of <code>name</code> argument.
086: * <li>"attachment".
087: * </ul>
088: *
089: * @return the name for this <code>DataSource</code>
090: */
091: private static String getName(String name) {
092: if (isNullOrEmpty(name)) {
093: name = "attachment";
094: }
095:
096: return name;
097: }
098:
099: /**
100: * Determines the mime type for this <code>DataSource</code> object.
101: * It is first non empty value from the list:
102: * <ul>
103: * <li>The value of <code>type</code> argument.
104: * <li>"application/octet-stream".
105: * </ul>
106: *
107: * @return The content type (mime type) of this <code>DataSource</code> object.
108: */
109: private static String getType(String type) {
110: if (isNullOrEmpty(type)) {
111: type = "application/octet-stream";
112: }
113:
114: return type;
115: }
116:
117: /**
118: * The InputStream object passed into contructor.
119: *
120: * @return The InputStream object for this <code>DataSource</code> object.
121: */
122: public InputStream getInputStream() {
123: return new ByteArrayInputStream(data);
124: }
125: }
|