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:
018: package org.apache.cocoon.mail.datasource;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022:
023: import org.apache.cocoon.servlet.multipart.Part;
024:
025: /**
026: * The FilePartDataSource class provides an object, that wraps a
027: * Cocoon {@link Part} object in a DataSource interface.
028: *
029: * @see javax.activation.DataSource
030: * @version $Id: FilePartDataSource.java 469258 2006-10-30 20:21:40Z vgritsenko $
031: */
032: public class FilePartDataSource extends AbstractDataSource {
033:
034: private Part part;
035:
036: /**
037: * Creates a new instance of FilePartDataSource from an
038: * {@link Part} object.
039: * @param part An {@link Part} object.
040: */
041: public FilePartDataSource(Part part) {
042: this (part, null, null);
043: }
044:
045: /**
046: * Creates a new instance of FilePartDataSource from an
047: * {@link Part} object.
048: * @param part An {@link Part} object.
049: */
050: public FilePartDataSource(Part part, String type, String name) {
051: super (getName(name, part), getType(type, part));
052: this .part = part;
053: }
054:
055: /**
056: * Determines the name for this <code>DataSource</code> object.
057: * It is first non empty value from the list:
058: * <ul>
059: * <li>The value of <code>name</code> argument.
060: * <li>The value returned by the {@link Part#getFileName()}.
061: * <li>"attachment".
062: * </ul>
063: *
064: * @return the name for this <code>DataSource</code>
065: */
066: private static String getName(String name, Part part) {
067: if (isNullOrEmpty(name)) {
068: name = part.getFileName();
069: if (isNullOrEmpty(name)) {
070: name = "attachment";
071: }
072: }
073:
074: return name;
075: }
076:
077: /**
078: * Determines the mime type for this <code>DataSource</code> object.
079: * It is first non empty value from the list:
080: * <ul>
081: * <li>The value of <code>type</code> argument.
082: * <li>The value returned by {@link Part#getMimeType()}.
083: * <li>"application/octet-stream".
084: * </ul>
085: *
086: * @return The content type (mime type) of this <code>DataSource</code> object.
087: */
088: private static String getType(String type, Part part) {
089: if (isNullOrEmpty(type)) {
090: type = part.getMimeType();
091: if (isNullOrEmpty(type)) {
092: type = "application/octet-stream";
093: }
094: }
095:
096: return type;
097: }
098:
099: /**
100: * The InputStream object obtained from {@link Part#getInputStream()}
101: * object.
102: *
103: * @throws java.io.IOException if an I/O error occurs.
104: * @return The InputStream object for this <code>DataSource</code> object.
105: */
106: public InputStream getInputStream() throws IOException {
107: try {
108: return part.getInputStream();
109: } catch (IOException e) {
110: // Sun's SMTPTransport looses cause exception. Log it now.
111: if (getLogger() != null) {
112: getLogger().warn(
113: "Unable to obtain input stream for '"
114: + part.getUploadName() + "'", e);
115: }
116: throw e;
117: }
118: }
119: }
|