KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > impl > PartSource


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 package org.apache.cocoon.components.source.impl;
17
18 import org.apache.excalibur.source.Source;
19 import org.apache.excalibur.source.SourceException;
20 import org.apache.excalibur.source.SourceValidity;
21 import org.apache.excalibur.source.SourceNotFoundException;
22
23 import org.apache.cocoon.servlet.multipart.Part;
24 import org.apache.cocoon.environment.ObjectModelHelper;
25 import org.apache.cocoon.environment.Request;
26 import java.net.MalformedURLException JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30
31
32 /**
33  * Implementation of a {@link Source} that gets its content
34  * from a PartOnDisk or PartInMemory held in the Request when
35  * a file is uploaded.
36  *
37  * @author <a HREF="mailto:paul.crabtree@dna.co.uk">Paul Crabtree</a>
38  * @version CVS $Id: PartSource.java 30932 2004-07-29 17:35:38Z vgritsenko $
39  */

40 public class PartSource implements Source
41 {
42     /* hold a private ref to the protocol used to call the Source */
43     private String JavaDoc protocol;
44
45     /* hold a private ref to the full uri */
46     private String JavaDoc uri;
47
48     /* hold a private ref to the Part which has been uploaded. */
49     private Part part;
50
51     /**
52      * Builds a PartSource given an URI.
53      * @param uri e.g., upload://formField1
54      * @throws SourceException
55      * @throws MalformedURLException
56      */

57     public PartSource(String JavaDoc uri, Map JavaDoc objectModel) throws MalformedURLException JavaDoc, SourceException
58     {
59         // set the uri for use in getURI()
60
this.uri = uri;
61
62         int position = uri.indexOf(':') + 1;
63         if (position != 0)
64         {
65             // set the protocol for use in getScheme()
66
this.protocol = uri.substring(0, position-1);
67         }
68         else
69         {
70             // if the URI is not correctly formatted then throw an excpetion
71
throw new MalformedURLException JavaDoc("No protocol found for part source in " + uri);
72         }
73
74         // get the request parameter name: the bit after ://
75
String JavaDoc location = uri.substring(position + 2);
76
77         // get the cocoon request from the object model.
78
Request request = ObjectModelHelper.getRequest(objectModel);
79
80         // try and cast the request object to a Part
81
Object JavaDoc obj = request.get(location);
82         if (obj instanceof Part)
83         {
84              part = (Part) obj;
85         }
86         else
87         {
88              throw new SourceException("Request object " + location + " is not an uploaded Part");
89         }
90     }
91
92     /**
93      * @see org.apache.excalibur.source.Source#getInputStream()
94      */

95     public InputStream JavaDoc getInputStream() throws IOException JavaDoc, SourceNotFoundException
96     {
97         try
98         {
99             return part.getInputStream();
100         }
101         catch (Exception JavaDoc ex)
102         {
103             throw new SourceNotFoundException("The part source can not be found.");
104         }
105     }
106
107     /**
108      * @see org.apache.excalibur.source.Source#getMimeType()
109      */

110     public String JavaDoc getMimeType()
111     {
112         return part.getMimeType();
113     }
114
115     /**
116       * @return true if the resource exists.
117       */

118     public boolean exists()
119     {
120         return part != null;
121     }
122
123     /*
124      * @see org.apache.excalibur.source.Source#getURI()
125      */

126     public String JavaDoc getURI()
127     {
128         return uri;
129     }
130
131     /*
132      * @see org.apache.excalibur.source.Source#getScheme()
133      */

134     public String JavaDoc getScheme()
135     {
136         return this.protocol;
137     }
138
139     /*
140      * Not used, Parts are not cacheable.
141      */

142     public SourceValidity getValidity()
143     {
144         // not sure what happens here.
145
return null;
146     }
147
148     /**
149       * @see org.apache.excalibur.source.Source#refresh()
150       */

151     public void refresh()
152     {
153     }
154
155     /**
156      * @see org.apache.excalibur.source.Source#getContentLength()
157      */

158     public long getContentLength()
159     {
160         return part.getSize();
161     }
162
163     /**
164      * @see org.apache.excalibur.source.Source#getLastModified()
165      */

166     public long getLastModified()
167     {
168         return 0;
169     }
170     
171     public Part getPart() {
172         return this.part;
173     }
174 }
175
Popular Tags