KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > mail > datasource > FilePartDataSource


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.cocoon.mail.datasource;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22
23 import javax.activation.DataSource JavaDoc;
24
25 import org.apache.cocoon.servlet.multipart.Part;
26
27 /** The FilePartDataSource class provides an object, that wraps a
28  * Cocoon {@link Part}
29  * object in a DataSource interface.
30  * @see javax.activation.DataSource
31  *
32  * @author <a HREF="mailto:frank.ridderbusch@gmx.de">Frank Ridderbusch</a>
33  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
34  * @version CVS $Id: FilePartDataSource.java 232592 2005-08-14 08:28:57Z antonio $
35  */

36 public class FilePartDataSource implements DataSource JavaDoc {
37     private Part part;
38     private String JavaDoc contentType = null;
39     private String JavaDoc name = null;
40
41     /** Creates a new instance of FilePartDataSource from an
42      * {@link Part} object.
43      * @param part An {@link Part} object.
44      */

45     public FilePartDataSource(Part part) {
46         this(part,null,null);
47     }
48
49     /** Creates a new instance of FilePartDataSource from an
50      * {@link Part} object.
51      * @param part An {@link Part} object.
52      */

53     public FilePartDataSource(Part part, String JavaDoc type, String JavaDoc name) {
54         this.part = part;
55         this.contentType = type;
56         this.name = name;
57         if (isNullOrEmpty(this.name)) this.name = null;
58         if (isNullOrEmpty(this.contentType)) this.contentType = null;
59     }
60
61     /**
62       * Check String for null or empty.
63       * @param str
64       * @return true if str is null, empty string, or equals "null"
65       */

66      private boolean isNullOrEmpty(String JavaDoc str) {
67          return (str == null || "".equals(str) || "null".equals(str));
68      }
69
70     /** Return the content type (mime type) obtained from
71      * {@link Part#getMimeType()}.
72      * Return <CODE>application/octet-stream</CODE> if <CODE>getMimeType()</CODE>
73      * returns <CODE>null</CODE>.
74      * @return The content type (mime type) for this <CODE>DataSource</CODE> object.
75      */

76     public String JavaDoc getContentType() {
77         if (this.contentType != null) {
78             return this.contentType;
79         }
80         String JavaDoc mimeType = part.getMimeType();
81         if (isNullOrEmpty(mimeType)) {
82             mimeType = "application/octet-stream";
83         }
84         return mimeType;
85     }
86
87     /** The InputStream object obtained from
88      * {@link Part#getInputStream()}
89      * object.
90      * @throws java.io.IOException if an I/O error occurs.
91      * @return The InputStream object for this <CODE>DataSource</CODE> object.
92      */

93     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
94         InputStream JavaDoc inp;
95         try {
96             inp = part.getInputStream();
97         } catch (Exception JavaDoc e) {
98             throw new IOException JavaDoc(e.getMessage());
99         }
100         return inp;
101     }
102
103     /** Returns the name for this <CODE>DataSource</CODE> object. This is
104      * what is returned by
105      * {@link Part#getFileName()}.
106      * @return the name for this <CODE>DataSource</CODE> object.
107      */

108     public String JavaDoc getName() {
109         String JavaDoc name = (this.name != null ? this.name : part.getFileName());
110         if (isNullOrEmpty(name)) name="attachment";
111         return name;
112     }
113
114     /** Unimplemented. Directly throws <CODE>IOException</CODE>.
115      * @throws java.io.IOException since unimplemented
116      * @return nothing
117      */

118     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
119         throw new IOException JavaDoc("no data sink available");
120     }
121 }
122
Popular Tags