01: package net.sf.regain.ui.desktop;
02:
03: import net.sf.regain.RegainToolkit;
04: import net.sf.regain.search.SearchToolkit;
05: import net.sf.regain.util.sharedtag.PageRequest;
06: import net.sf.regain.util.sharedtag.PageResponse;
07: import net.sf.regain.util.sharedtag.simple.SharedTagResource;
08: import net.sf.regain.util.sharedtag.simple.SimplePageRequest;
09: import net.sf.regain.util.sharedtag.simple.SimplePageResponse;
10: import simple.http.Request;
11: import simple.http.Response;
12: import simple.http.load.BasicService;
13: import simple.http.serve.Context;
14:
15: /**
16: * A simpleweb service providing files. For security reasons this service only
17: * provides files that are in the index.
18: *
19: * @author Til Schneider, www.murfman.de
20: */
21: public class FileService extends BasicService {
22:
23: /**
24: * Creates a new instance of FileService.
25: *
26: * @param context The context of this service.
27: */
28: public FileService(Context context) {
29: super (context);
30: }
31:
32: /**
33: * Processes a request.
34: *
35: * @param req The request.
36: * @param resp The response.
37: * @throws Exception If executing the JSP page failed.
38: */
39: public void process(Request req, Response resp) throws Exception {
40: // Check whether this request comes from localhost
41: boolean localhost = req.getInetAddress().isLoopbackAddress();
42: if (!localhost) {
43: // This request does not come from localhost -> Send 403 Forbidden
44: handle(req, resp, 403);
45: }
46:
47: // Create a shared wrapper
48: PageRequest request = new SimplePageRequest(req);
49: PageResponse response = new SimplePageResponse(this , req, resp,
50: null, null);
51:
52: // Get the request path (Without GET-Parameters)
53: // NOTE: We don't use context.getRequestPath for this, because it decodes
54: // the URL, but we want to decode it ourselves using our encoding
55: String requestPath = req.getURI();
56: int paramsStart = requestPath.indexOf('?');
57: if (paramsStart != -1) {
58: requestPath = requestPath.substring(0, paramsStart);
59: }
60:
61: // Extract the file URL
62: String fileUrl = SearchToolkit.extractFileUrl(requestPath,
63: SharedTagResource.SIMPLE_TAG_ENCODING);
64:
65: // Check the file URL
66: if (SearchToolkit.allowFileAccess(request, fileUrl)) {
67: // This file is allowed -> Send it
68: SearchToolkit.sendFile(request, response, RegainToolkit
69: .urlToFile(fileUrl));
70: } else {
71: // This file is not allowed -> Send 403 Forbidden
72: handle(req, resp, 403);
73: }
74: }
75:
76: }
|