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