KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > servlet > multipart > Part


1 /*
2  * Copyright 1999-2005 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 package org.apache.cocoon.servlet.multipart;
17
18 import java.io.FileOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.avalon.excalibur.io.IOUtil;
25 import org.apache.avalon.framework.activity.Disposable;
26 import org.apache.excalibur.source.ModifiableSource;
27
28
29 /**
30  * This abstract class represents a file part parsed from a http post stream. The concrete
31  * class, {@link PartOnDisk} or {@link PartInMemory} that is used depends on the upload configuration
32  * in <code>web.xml</code>.
33  * <p>
34  * If uploaded data size exceeds the maximum allowed upload size (also specified in <code>web.xml</code>),
35  * then an {@link RejectedPart} is used, from which no data can be obtained, but which gives some
36  * information on the rejected uploads.
37  *
38  * @author <a HREF="mailto:j.tervoorde@home.nl">Jeroen ter Voorde</a>
39  * @version $Id: Part.java 280876 2005-09-14 15:44:29Z sylvain $
40  */

41 public abstract class Part implements Disposable {
42
43     private boolean disposeWithRequest = true;
44
45     /** Field headers */
46     protected Map JavaDoc headers;
47
48     protected Part(Map JavaDoc headers) {
49         this.headers = headers;
50     }
51
52     /**
53      * Returns the part headers
54      */

55     public Map JavaDoc getHeaders() {
56         return headers;
57     }
58
59     /**
60      * Returns the filename
61      */

62     public abstract String JavaDoc getFileName();
63     
64     /**
65      * Returns the original filename
66      */

67     public String JavaDoc getUploadName(){
68         return (String JavaDoc) headers.get("filename");
69     }
70     
71     /**
72      * Returns the length of the file content
73      */

74     public abstract int getSize();
75     
76     /**
77      * Is this part a rejected part? Provided as an alternative to <code>instanceof RejectedPart</code>
78      * in places where it's not convenient such as flowscript.
79      *
80      * @return <code>true</code> if this part was rejected
81      */

82     public boolean isRejected() {
83         return false;
84     }
85
86     /**
87      * Returns the mime type (or null if unknown)
88      */

89     public String JavaDoc getMimeType() {
90         return (String JavaDoc) headers.get("content-type");
91     }
92     
93     /**
94      * Do we want any temporary resource held by this part to be cleaned up when processing of
95      * the request that created it is finished? Default is <code>true</code>.
96      *
97      * @return <code>true</code> if the part should be disposed with the request.
98      */

99     public boolean disposeWithRequest() {
100         return this.disposeWithRequest;
101     }
102     
103     /**
104      * Set the value of the <code>disposeWithRequest</code> flag (default is <code>true</code>).
105      *
106      * @param dispose <code>true</code> if the part should be disposed after request processing
107      */

108     public void setDisposeWithRequest(boolean dispose) {
109         this.disposeWithRequest = dispose;
110     }
111     
112     /**
113      * Returns an InputStream containing the file data
114      * @throws Exception
115      */

116     public abstract InputStream JavaDoc getInputStream() throws IOException JavaDoc;
117
118     /**
119      * Convenience method to copy a part to a modifiable source.
120      *
121      * @param source the modifiable source to write to
122      * @throws IOException
123      * @since 2.1.8
124      */

125     public void copyToSource(ModifiableSource source) throws IOException JavaDoc {
126         InputStream JavaDoc is = getInputStream();
127         OutputStream JavaDoc os = source.getOutputStream();
128         IOUtil.copy(is, os);
129         is.close();
130         os.close();
131     }
132     
133     /**
134      * Convenience method to copy a part to a file.
135      *
136      * @param filename name of the file to write to
137      * @throws IOException
138      * @since 2.1.8
139      */

140     public void copyToFile(String JavaDoc filename) throws IOException JavaDoc {
141         InputStream JavaDoc is = getInputStream();
142         OutputStream JavaDoc os = new FileOutputStream JavaDoc(filename);
143         IOUtil.copy(is, os);
144         is.close();
145         os.close();
146     }
147     
148     /**
149      * Dispose any resources held by this part, such as a file or memory buffer.
150      * <p>
151      * Disposal occurs in all cases when the part is garbage collected, but calling it explicitely
152      * allows to cleanup resources more quickly.
153      */

154     public abstract void dispose();
155 }
156
Popular Tags