KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > attachments > PartOnFile


1 /**
2  * Copyright 2001-2004 The Apache Software Foundation.
3  * <p/>
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  * <p/>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p/>
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  * <p/>
16  */

17 package org.apache.axis2.attachments;
18
19 import java.io.FileInputStream JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.Enumeration JavaDoc;
28
29 import javax.activation.DataHandler JavaDoc;
30 import javax.mail.BodyPart JavaDoc;
31 import javax.mail.MessagingException JavaDoc;
32 import javax.mail.Multipart JavaDoc;
33 import javax.mail.Part JavaDoc;
34 import javax.mail.internet.MimeBodyPart JavaDoc;
35
36 import org.apache.axis2.om.OMException;
37
38 /**
39  * @author <a HREF="mailto:thilina@opensource.lk"> Thilina Gunarathne </a>
40  */

41 public class PartOnFile implements Part JavaDoc {
42     
43     String JavaDoc fileName;
44     
45     Part JavaDoc bodyPart;
46     
47     int size;
48     
49     String JavaDoc contentType;
50     
51     Enumeration JavaDoc headers;
52     
53     String JavaDoc contentID;
54     
55     public PartOnFile(Part JavaDoc bodyPart,String JavaDoc contentID, String JavaDoc repoDir) throws Exception JavaDoc {
56         super();
57         size = bodyPart.getSize();
58         contentType = bodyPart.getContentType();
59         headers = bodyPart.getAllHeaders();
60         // TODO Find a better naming algorithm
61
if (repoDir==null)
62         {
63             repoDir=".";
64         }
65         fileName = repoDir+(new Date JavaDoc()).getTime() + ".tmp";
66         FileOutputStream JavaDoc outFileStream;
67         outFileStream = new FileOutputStream JavaDoc(fileName);
68         bodyPart.writeTo(outFileStream);
69         outFileStream.close();
70     }
71     
72     private Part JavaDoc getPartOnFile() {
73         FileInputStream JavaDoc inFileStream;
74         Part JavaDoc part=null;
75         try {
76             inFileStream = new FileInputStream JavaDoc(fileName);
77             
78             part = new MimeBodyPart JavaDoc(inFileStream);
79         } catch (FileNotFoundException JavaDoc e) {
80             throw new OMException("File Not Found"+e.toString());
81         } catch (MessagingException JavaDoc e1) {
82             throw new OMException("Cannot create MimePart from the Part read from file"+e1.toString());
83         }
84         return part;
85     }
86     
87     public String JavaDoc getContentID()
88     {
89         return contentID;
90     }
91     public int getSize() throws MessagingException JavaDoc {
92         return size;
93     }
94     
95     
96     /*
97      * (non-Javadoc)
98      *
99      * @see javax.mail.Part#getLineCount()
100      */

101     public int getLineCount() throws MessagingException JavaDoc {
102         throw new UnsupportedOperationException JavaDoc();
103     }
104     
105     /*
106      * (non-Javadoc)
107      *
108      * @see javax.mail.Part#getContentType()
109      */

110     public String JavaDoc getContentType() throws MessagingException JavaDoc {
111         // TODO Auto-generated method stub
112
return contentType;
113     }
114     
115     /*
116      * (non-Javadoc)
117      *
118      * @see javax.mail.Part#isMimeType(java.lang.String)
119      */

120     public boolean isMimeType(String JavaDoc arg0) throws MessagingException JavaDoc {
121         throw new UnsupportedOperationException JavaDoc();
122     }
123     
124     /*
125      * (non-Javadoc)
126      *
127      * @see javax.mail.Part#getDisposition()
128      */

129     public String JavaDoc getDisposition() throws MessagingException JavaDoc {
130         throw new UnsupportedOperationException JavaDoc();
131     }
132     
133     /*
134      * (non-Javadoc)
135      *
136      * @see javax.mail.Part#setDisposition(java.lang.String)
137      */

138     public void setDisposition(String JavaDoc arg0) throws MessagingException JavaDoc {
139         throw new UnsupportedOperationException JavaDoc();
140     }
141     
142     /*
143      * (non-Javadoc)
144      *
145      * @see javax.mail.Part#getDescription()
146      */

147     public String JavaDoc getDescription() throws MessagingException JavaDoc {
148         throw new UnsupportedOperationException JavaDoc();
149     }
150     
151     /*
152      * (non-Javadoc)
153      *
154      * @see javax.mail.Part#setDescription(java.lang.String)
155      */

156     public void setDescription(String JavaDoc arg0) throws MessagingException JavaDoc {
157         throw new UnsupportedOperationException JavaDoc();
158     }
159     
160     /*
161      * (non-Javadoc)
162      *
163      * @see javax.mail.Part#getFileName()
164      */

165     public String JavaDoc getFileName() throws MessagingException JavaDoc {
166         throw new UnsupportedOperationException JavaDoc();
167     }
168     
169     /*
170      * (non-Javadoc)
171      *
172      * @see javax.mail.Part#setFileName(java.lang.String)
173      */

174     public void setFileName(String JavaDoc arg0) throws MessagingException JavaDoc {
175         throw new UnsupportedOperationException JavaDoc();
176     }
177     
178     /*
179      * (non-Javadoc)
180      *
181      * @see javax.mail.Part#getInputStream()
182      */

183     public InputStream JavaDoc getInputStream() throws IOException JavaDoc, MessagingException JavaDoc {
184         Part JavaDoc part = getPartOnFile();
185         return part.getInputStream();
186     }
187     
188     /*
189      * (non-Javadoc)
190      *
191      * @see javax.mail.Part#getDataHandler()
192      */

193     public DataHandler JavaDoc getDataHandler() throws MessagingException JavaDoc {
194         Part JavaDoc part = getPartOnFile();
195         return part.getDataHandler();
196     }
197     
198     /*
199      * (non-Javadoc)
200      *
201      * @see javax.mail.Part#getContent()
202      */

203     public Object JavaDoc getContent() throws IOException JavaDoc, MessagingException JavaDoc {
204         Part JavaDoc part = getPartOnFile();
205         return part.getContent();
206     }
207     
208     /*
209      * (non-Javadoc)
210      *
211      * @see javax.mail.Part#setDataHandler(javax.activation.DataHandler)
212      */

213     public void setDataHandler(DataHandler JavaDoc arg0) throws MessagingException JavaDoc {
214         throw new UnsupportedOperationException JavaDoc();
215     }
216     
217     /*
218      * (non-Javadoc)
219      *
220      * @see javax.mail.Part#setContent(java.lang.Object, java.lang.String)
221      */

222     public void setContent(Object JavaDoc arg0, String JavaDoc arg1) throws MessagingException JavaDoc {
223         throw new UnsupportedOperationException JavaDoc();
224     }
225     
226     /*
227      * (non-Javadoc)
228      *
229      * @see javax.mail.Part#setText(java.lang.String)
230      */

231     public void setText(String JavaDoc arg0) throws MessagingException JavaDoc {
232         throw new UnsupportedOperationException JavaDoc();
233     }
234     
235     /*
236      * (non-Javadoc)
237      *
238      * @see javax.mail.Part#setContent(javax.mail.Multipart)
239      */

240     public void setContent(Multipart JavaDoc arg0) throws MessagingException JavaDoc {
241         throw new UnsupportedOperationException JavaDoc();
242         
243     }
244     
245     /*
246      * (non-Javadoc)
247      *
248      * @see javax.mail.Part#writeTo(java.io.OutputStream)
249      */

250     public void writeTo(OutputStream JavaDoc outStream) throws IOException JavaDoc,
251     MessagingException JavaDoc {
252         Part JavaDoc part = getPartOnFile();
253         part.writeTo(outStream);
254     }
255     
256     /*
257      * (non-Javadoc)
258      *
259      * @see javax.mail.Part#getHeader(java.lang.String)
260      */

261     public String JavaDoc[] getHeader(String JavaDoc arg0) throws MessagingException JavaDoc {
262         ArrayList JavaDoc selectedHeader = null;
263         while (headers.hasMoreElements()) {
264             String JavaDoc header = (String JavaDoc) headers.nextElement();
265             if (arg0.equals(header)) {
266                 selectedHeader.add(header);
267             }
268         }
269         String JavaDoc[] headerStrings = (String JavaDoc[]) selectedHeader.toArray();
270         return headerStrings;
271     }
272     
273     /*
274      * (non-Javadoc)
275      *
276      * @see javax.mail.Part#setHeader(java.lang.String, java.lang.String)
277      */

278     public void setHeader(String JavaDoc arg0, String JavaDoc arg1) throws MessagingException JavaDoc {
279         throw new UnsupportedOperationException JavaDoc();
280         
281     }
282     
283     /*
284      * (non-Javadoc)
285      *
286      * @see javax.mail.Part#addHeader(java.lang.String, java.lang.String)
287      */

288     public void addHeader(String JavaDoc arg0, String JavaDoc arg1) throws MessagingException JavaDoc {
289         throw new UnsupportedOperationException JavaDoc();
290         
291     }
292     
293     /*
294      * (non-Javadoc)
295      *
296      * @see javax.mail.Part#removeHeader(java.lang.String)
297      */

298     public void removeHeader(String JavaDoc arg0) throws MessagingException JavaDoc {
299         throw new UnsupportedOperationException JavaDoc();
300         
301     }
302     
303     /*
304      * (non-Javadoc)
305      *
306      * @see javax.mail.Part#getAllHeaders()
307      */

308     public Enumeration JavaDoc getAllHeaders() throws MessagingException JavaDoc {
309         return headers;
310     }
311     
312     /*
313      * (non-Javadoc)
314      *
315      * @see javax.mail.Part#getMatchingHeaders(java.lang.String[])
316      */

317     public Enumeration JavaDoc getMatchingHeaders(String JavaDoc[] arg0)
318     throws MessagingException JavaDoc {
319         throw new UnsupportedOperationException JavaDoc();
320     }
321     
322     /*
323      * (non-Javadoc)
324      *
325      * @see javax.mail.Part#getNonMatchingHeaders(java.lang.String[])
326      */

327     public Enumeration JavaDoc getNonMatchingHeaders(String JavaDoc[] arg0)
328     throws MessagingException JavaDoc {
329         throw new UnsupportedOperationException JavaDoc();
330     }
331     
332 }
Popular Tags