KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > webui > servlet > HTMLServlet


1 /*
2  * HTMLServlet.java
3  *
4  * Version: $Revision: 1.10 $
5  *
6  * Date: $Date: 2006/11/21 00:19:53 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.app.webui.servlet;
41
42 import java.io.IOException JavaDoc;
43 import java.io.InputStream JavaDoc;
44 import java.net.URLDecoder JavaDoc;
45 import java.sql.SQLException JavaDoc;
46
47 import javax.servlet.ServletException JavaDoc;
48 import javax.servlet.http.HttpServletRequest JavaDoc;
49 import javax.servlet.http.HttpServletResponse JavaDoc;
50
51 import org.apache.log4j.Logger;
52 import org.dspace.app.webui.util.JSPManager;
53 import org.dspace.authorize.AuthorizeException;
54 import org.dspace.content.Bitstream;
55 import org.dspace.content.Bundle;
56 import org.dspace.content.Item;
57 import org.dspace.core.ConfigurationManager;
58 import org.dspace.core.Constants;
59 import org.dspace.core.Context;
60 import org.dspace.core.LogManager;
61 import org.dspace.core.Utils;
62 import org.dspace.handle.HandleManager;
63
64 /**
65  * Servlet for HTML bitstream support.
66  * <P>
67  * If we receive a request like this:
68  * <P>
69  * <code>http://dspace.foo.edu/html/123.456/789/foo/bar/index.html</code>
70  * <P>
71  * we first check for a bitstream with the *exact* filename
72  * <code>foo/bar/index.html</code>. Otherwise, we strip the path information
73  * (up to three levels deep to prevent infinite URL spaces occurring) and see if
74  * we have a bitstream with the filename <code>index.html</code> (with no
75  * path). If this exists, it is served up. This is because if an end user
76  * uploads a composite HTML document with the submit UI, we will not have
77  * accurate path information, and so we assume that if the browser is requesting
78  * foo/bar/index.html but we only have index.html, that this is the desired file
79  * but we lost the path information on upload.
80  *
81  * @author Austin Kim, Robert Tansley
82  * @version $Revision: 1.10 $
83  */

84 public class HTMLServlet extends DSpaceServlet
85 {
86     /** log4j category */
87     private static Logger log = Logger.getLogger(HTMLServlet.class);
88
89     /**
90      * Default maximum number of path elements to strip when testing if a
91      * bitstream called "foo.html" should be served when "xxx/yyy/zzz/foo.html"
92      * is requested.
93      */

94     private int maxDepthGuess;
95
96     /**
97      * Create an HTML Servlet
98      */

99     public HTMLServlet()
100     {
101         super();
102
103         if (ConfigurationManager.getProperty("webui.html.max-depth-guess") != null)
104         {
105             maxDepthGuess = ConfigurationManager
106                     .getIntProperty("webui.html.max-depth-guess");
107         }
108         else
109         {
110             maxDepthGuess = 3;
111         }
112     }
113     
114     // Return bitstream whose name matches the target bitstream-name
115
// bsName, or null if there is no match. Match must be exact.
116
// NOTE: This does not detect duplicate bitstream names, just returns first.
117
private static Bitstream getItemBitstreamByName(Item item, String JavaDoc bsName)
118                             throws SQLException JavaDoc
119     {
120         Bundle[] bundles = item.getBundles();
121
122         for (int i = 0; i < bundles.length; i++)
123         {
124             Bitstream[] bitstreams = bundles[i].getBitstreams();
125
126             for (int k = 0; k < bitstreams.length; k++)
127             {
128                 if (bsName.equals(bitstreams[k].getName()))
129                     return bitstreams[k];
130             }
131         }
132         return null;
133     }
134
135     // On the surface it doesn't make much sense for this servlet to
136
// handle POST requests, but in practice some HTML pages which
137
// are actually JSP get called on with a POST, so it's needed.
138
protected void doDSPost(Context context, HttpServletRequest JavaDoc request,
139                 HttpServletResponse JavaDoc response)
140         throws ServletException JavaDoc, IOException JavaDoc, SQLException JavaDoc, AuthorizeException
141     {
142         doDSGet(context, request, response);
143     }
144
145     protected void doDSGet(Context context, HttpServletRequest JavaDoc request,
146             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
147             SQLException JavaDoc, AuthorizeException
148     {
149         Item item = null;
150         Bitstream bitstream = null;
151
152         String JavaDoc idString = request.getPathInfo();
153         String JavaDoc filenameNoPath = null;
154         String JavaDoc fullpath = null;
155         String JavaDoc handle = null;
156
157         // Parse URL
158
if (idString != null)
159         {
160             // Remove leading slash
161
if (idString.startsWith("/"))
162             {
163                 idString = idString.substring(1);
164             }
165
166             // Get handle and full file path
167
int slashIndex = idString.indexOf('/');
168             if (slashIndex != -1)
169             {
170                 slashIndex = idString.indexOf('/', slashIndex + 1);
171                 if (slashIndex != -1)
172                 {
173                     handle = idString.substring(0, slashIndex);
174                     fullpath = URLDecoder.decode(idString
175                             .substring(slashIndex + 1),
176                             Constants.DEFAULT_ENCODING);
177
178                     // Get filename with no path
179
slashIndex = fullpath.indexOf('/');
180                     if (slashIndex != -1)
181                     {
182                         String JavaDoc[] pathComponents = fullpath.split("/");
183                         if (pathComponents.length <= maxDepthGuess + 1)
184                         {
185                             filenameNoPath = pathComponents[pathComponents.length - 1];
186                         }
187                     }
188                 }
189             }
190         }
191
192         if (handle != null && fullpath != null)
193         {
194             // Find the item
195
try
196             {
197                 /*
198                  * If the original item doesn't have a Handle yet (because it's
199                  * in the workflow) what we actually have is a fake Handle in
200                  * the form: db-id/1234 where 1234 is the database ID of the
201                  * item.
202                  */

203                 if (handle.startsWith("db-id"))
204                 {
205                     String JavaDoc dbIDString = handle
206                             .substring(handle.indexOf('/') + 1);
207                     int dbID = Integer.parseInt(dbIDString);
208                     item = Item.find(context, dbID);
209                 }
210                 else
211                 {
212                     item = (Item) HandleManager
213                             .resolveToObject(context, handle);
214                 }
215             }
216             catch (NumberFormatException JavaDoc nfe)
217             {
218                 // Invalid ID - this will be dealt with below
219
}
220         }
221
222         if (item != null)
223         {
224             // Try to find bitstream with exactly matching name + path
225
bitstream = getItemBitstreamByName(item, fullpath);
226             
227             if (bitstream == null && filenameNoPath != null)
228             {
229                 // No match with the full path, but we can try again with
230
// only the filename
231
bitstream = getItemBitstreamByName(item, filenameNoPath);
232             }
233         }
234
235         // Did we get a bitstream?
236
if (bitstream != null)
237         {
238             log.info(LogManager.getHeader(context, "view_html", "handle="
239                     + handle + ",bitstream_id=" + bitstream.getID()));
240
241             // Set the response MIME type
242
response.setContentType(bitstream.getFormat().getMIMEType());
243
244             // Response length
245
response.setHeader("Content-Length", String.valueOf(bitstream
246                     .getSize()));
247
248             // Pipe the bits
249
InputStream JavaDoc is = bitstream.retrieve();
250
251             Utils.bufferedCopy(is, response.getOutputStream());
252             is.close();
253             response.getOutputStream().flush();
254         }
255         else
256         {
257             // No bitstream - we got an invalid ID
258
log.info(LogManager.getHeader(context, "view_html",
259                     "invalid_bitstream_id=" + idString));
260
261             JSPManager.showInvalidIDError(request, response, idString,
262                     Constants.BITSTREAM);
263         }
264     }
265 }
266
Popular Tags