1 /*
2 *
3 * <cvs:source>$Source: /devel/astrogrid/filestore/server/src/java/org/astrogrid/filestore/server/servlet/FileStoreServlet.java,v $</cvs:source>
4 * <cvs:author>$Author: jdt $</cvs:author>
5 * <cvs:date>$Date: 2004/11/25 00:19:19 $</cvs:date>
6 * <cvs:version>$Revision: 1.5 $</cvs:version>
7 * <cvs:log>
8 * $Log: FileStoreServlet.java,v $
9 * Revision 1.5 2004/11/25 00:19:19 jdt
10 * Merge from dave-dev-200410061224-200411221626
11 *
12 * Revision 1.4.10.3 2004/10/27 11:59:43 dave
13 * Added sevlet debug ...
14 *
15 * Revision 1.4.10.2 2004/10/27 10:56:31 dave
16 * Changed inport init to save the details, and simplified tests for debug
17 *
18 * Revision 1.4.10.1 2004/10/19 14:56:16 dave
19 * Refactored config and resolver to enable multiple instances of mock implementation.
20 * Required to implement handling of multiple FileStore(s) in FileManager.
21 *
22 * Revision 1.4 2004/09/17 16:46:14 dave
23 * Fixed servlet deployment in FileStore ...
24 * Changed tabs to spaces in source code ...
25 *
26 * Revision 1.3.4.2 2004/09/17 15:10:32 dave
27 * Uncommented the call to servlet deploy in the build script.
28 * Replaced tabs with spaces in source code.
29 *
30 * Revision 1.3.4.1 2004/09/17 14:51:41 dave
31 * Added debug to servlet ....
32 *
33 * Revision 1.3 2004/09/17 06:57:10 dave
34 * Added commons logging to FileStore.
35 * Updated logging properties in Community.
36 * Fixed bug in AGINAB deployment.
37 * Removed MySpace tests with hard coded grendel address.
38 *
39 * Revision 1.2.32.1 2004/09/17 01:08:36 dave
40 * Updated debug to use commons logging API ....
41 *
42 * Revision 1.2 2004/08/18 19:00:01 dave
43 * Myspace manager modified to use remote filestore.
44 * Tested before checkin - integration tests at 91%.
45 *
46 * Revision 1.1.2.7 2004/08/04 19:48:57 dave
47 * Fixed mime type in servlet
48 *
49 * Revision 1.1.2.6 2004/08/04 19:15:28 dave
50 * Refactored servlet to get reply with file contents
51 *
52 * Revision 1.1.2.5 2004/08/04 17:03:33 dave
53 * Added container to servlet
54 *
55 * Revision 1.1.2.4 2004/08/04 16:10:19 dave
56 * Added config to servlet
57 *
58 * Revision 1.1.2.3 2004/08/04 16:06:31 dave
59 * Added config to servlet
60 *
61 * Revision 1.1.2.2 2004/08/04 15:56:44 dave
62 * Added config to servlet
63 *
64 * Revision 1.1.2.1 2004/08/04 06:35:02 dave
65 * Added initial stubs for servlet ....
66 *
67 * </cvs:log>
68 *
69 */
70 package org.astrogrid.filestore.server.servlet ;
71
72 import org.apache.commons.logging.Log ;
73 import org.apache.commons.logging.LogFactory ;
74
75 import java.io.IOException ;
76 import java.io.PrintWriter ;
77
78 import javax.servlet.ServletException ;
79
80 import javax.servlet.http.HttpServlet ;
81 import javax.servlet.http.HttpServletRequest ;
82 import javax.servlet.http.HttpServletResponse ;
83
84 import org.astrogrid.filestore.common.file.FileProperties ;
85 import org.astrogrid.filestore.common.file.FileIdentifier ;
86
87 import org.astrogrid.filestore.common.exception.FileStoreException ;
88
89 import org.astrogrid.filestore.common.FileStoreConfig ;
90 import org.astrogrid.filestore.server.FileStoreConfigImpl ;
91
92 import org.astrogrid.filestore.server.repository.Repository ;
93 import org.astrogrid.filestore.server.repository.RepositoryImpl ;
94 import org.astrogrid.filestore.server.repository.RepositoryContainer ;
95
96 /***
97 * A file server servlet to handle HTTP GET requests.
98 *
99 */
100 public class FileStoreServlet
101 extends HttpServlet
102 {
103 /***
104 * Our debug logger.
105 *
106 */
107 // private static Log log = LogFactory.getLog(FileStoreServlet.class);
108 private static Log log ;
109
110 /***
111 * The default content type (used if the type was not set when the file was stored).
112 *
113 */
114 public static final String DEFAULT_CONTENT_TYPE = "text/plain" ;
115
116 /***
117 * Reference to our repository config.
118 *
119 */
120 protected FileStoreConfig config ;
121
122 /***
123 * Reference to our local repository.
124 *
125 */
126 protected Repository repository ;
127
128 /***
129 * Initialise our servlet.
130 *
131 */
132 public void init()
133 throws ServletException
134 {
135 //
136 // Initialise our logger.
137 log = LogFactory.getLog(FileStoreServlet.class);
138 log.debug("") ;
139 log.debug("SERVLET INIT") ;
140 log.debug("FileStoreServlet.init()") ;
141
142 //
143 // Initialise our config.
144 config = new FileStoreConfigImpl() ;
145 //
146 // Initialise our repository.
147 repository = new RepositoryImpl(
148 config
149 );
150 }
151
152 /***
153 * Handle a GET request.
154 *
155 */
156 public void doGet(HttpServletRequest request, HttpServletResponse response)
157 throws IOException, ServletException
158 {
159 log.debug("") ;
160 log.debug("SERVLET GET") ;
161 log.debug("FileStoreServlet.doGet()") ;
162 //
163 // Get the request path.
164 String path = request.getPathInfo() ;
165 log.debug(" Path : " + path) ;
166 //
167 // If the path is valid.
168 if (null != path)
169 {
170 //
171 // Try locating the container.
172 try {
173 //
174 // Strip off the leading '/' and convert to an identifier.
175 // This relies on the FileIdentifier to check that the identifier is valid (which it don't at the moment).
176 FileIdentifier ident = new FileIdentifier(
177 path.substring(1)
178 ) ;
179 log.debug(" Ident : " + ident.toString()) ;
180 //
181 // Locate the container.
182 RepositoryContainer container = repository.load(
183 ident.toString()
184 ) ;
185 log.debug("PASS : Got container ....") ;
186 //
187 // Set the response content type.
188 String type = container.properties().getProperty(
189 FileProperties.MIME_TYPE_PROPERTY
190 ) ;
191 if (null != type)
192 {
193 response.setContentType(
194 type
195 );
196 }
197 else {
198 response.setContentType(
199 DEFAULT_CONTENT_TYPE
200 );
201 }
202 //
203 // Transfer the data in our response.
204 container.exportData(
205 response.getOutputStream()
206 ) ;
207 }
208 catch (FileStoreException ouch)
209 {
210 log.warn("----") ;
211 log.warn("Exception transferring data from repository") ;
212 log.warn(ouch.toString()) ;
213 log.warn("----") ;
214 response.sendError(
215 HttpServletResponse.SC_NOT_FOUND,
216 ouch.getMessage()
217 ) ;
218 }
219 }
220 }
221
222 /***
223 * Handle a PUT request.
224 *
225 */
226 public void doPut(HttpServletRequest request, HttpServletResponse response)
227 throws IOException, ServletException
228 {
229 log.debug("") ;
230 log.debug("SERVLET PUT") ;
231 log.debug("FileStoreServlet.doPut()") ;
232 //
233 // Get the request path.
234 String path = request.getPathInfo() ;
235 log.debug(" Path : " + path) ;
236 //
237 // If the path is valid.
238 if (null != path)
239 {
240 //
241 // Try locating our container.
242 try {
243 //
244 // ** Currently assumes new file, append later.
245 //
246 //
247 // Strip off the leading '/' and convert to an identifier.
248 // This relies on the FileIdentifier to check that the identifier is valid (which it don't at the moment).
249 FileIdentifier ident = new FileIdentifier(
250 path.substring(1)
251 ) ;
252 log.debug(" Ident : " + ident.toString()) ;
253 //
254 // Locate the container.
255 RepositoryContainer container = repository.load(
256 ident.toString()
257 ) ;
258 log.debug("PASS : Got container ....") ;
259 //
260 // Transfer the data in our response.
261 container.importData(
262 request.getInputStream()
263 ) ;
264 log.debug("PASS : Transfer completed ....") ;
265
266 log.debug("") ;
267 log.debug("NOTIFY MANAGER ....") ;
268 log.debug("") ;
269
270 }
271 catch (FileStoreException ouch)
272 {
273 log.warn("----") ;
274 log.warn("Exception transferring data into repository") ;
275 log.warn(ouch.toString()) ;
276 log.warn("----") ;
277 response.sendError(
278 HttpServletResponse.SC_NOT_FOUND,
279 ouch.getMessage()
280 ) ;
281 }
282
283
284 }
285 }
286 }
287