KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * BitstreamServlet.java
3  *
4  * Version: $Revision: 1.11 $
5  *
6  * Date: $Date: 2006/11/10 22:26:29 $
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.sql.SQLException JavaDoc;
45 import java.text.DateFormat JavaDoc;
46 import java.text.ParseException JavaDoc;
47 import java.text.SimpleDateFormat JavaDoc;
48 import java.util.Date JavaDoc;
49 import java.util.Locale JavaDoc;
50 import java.util.TimeZone JavaDoc;
51
52 import javax.servlet.ServletException JavaDoc;
53 import javax.servlet.http.HttpServletRequest JavaDoc;
54 import javax.servlet.http.HttpServletResponse JavaDoc;
55
56 import org.apache.log4j.Logger;
57 import org.dspace.app.webui.util.JSPManager;
58 import org.dspace.authorize.AuthorizeException;
59 import org.dspace.content.Bitstream;
60 import org.dspace.content.Bundle;
61 import org.dspace.content.DSpaceObject;
62 import org.dspace.content.Item;
63 import org.dspace.core.Constants;
64 import org.dspace.core.Context;
65 import org.dspace.core.LogManager;
66 import org.dspace.core.Utils;
67 import org.dspace.handle.HandleManager;
68
69 /**
70  * Servlet for retrieving bitstreams. The bits are simply piped to the user. If
71  * there is an <code>If-Modified-Since</code> header, only a 304 status code
72  * is returned if the containing item has not been modified since that date.
73  * <P>
74  * <code>/bitstream/handle/sequence_id/filename</code>
75  *
76  * @author Robert Tansley
77  * @version $Revision: 1.11 $
78  */

79 public class BitstreamServlet extends DSpaceServlet
80 {
81     /** log4j category */
82     private static Logger log = Logger.getLogger(BitstreamServlet.class);
83
84     protected void doDSGet(Context context, HttpServletRequest JavaDoc request,
85             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
86             SQLException JavaDoc, AuthorizeException
87     {
88         Item item = null;
89         Bitstream bitstream = null;
90
91         // Get the ID from the URL
92
String JavaDoc idString = request.getPathInfo();
93         String JavaDoc handle = "";
94         String JavaDoc sequenceText = "";
95         String JavaDoc filename = null;
96         int sequenceID;
97
98         // Parse 'handle' and 'sequence' (bitstream seq. number) out
99
// of remaining URL path, which is typically of the format:
100
// {handle}/{sequence}/{bitstream-name}
101
// But since the bitstream name MAY have any number of "/"s in
102
// it, and the handle is guaranteed to have one slash, we
103
// scan from the start to pick out handle and sequence:
104

105         // Remove leading slash if any:
106
if (idString.startsWith("/"))
107         {
108             idString = idString.substring(1);
109         }
110
111         // skip first slash within handle
112
int slashIndex = idString.indexOf('/');
113         if (slashIndex != -1)
114         {
115             slashIndex = idString.indexOf('/', slashIndex + 1);
116             if (slashIndex != -1)
117             {
118                 handle = idString.substring(0, slashIndex);
119                 int slash2 = idString.indexOf('/', slashIndex + 1);
120                 if (slash2 != -1)
121                 {
122                     sequenceText = idString.substring(slashIndex+1,slash2);
123                     filename = idString.substring(slash2+1);
124                 }
125             }
126         }
127
128         try
129         {
130             sequenceID = Integer.parseInt(sequenceText);
131         }
132         catch (NumberFormatException JavaDoc nfe)
133         {
134             sequenceID = -1;
135         }
136         
137         // Now try and retrieve the item
138
DSpaceObject dso = HandleManager.resolveToObject(context, handle);
139         
140         // Make sure we have valid item and sequence number
141
if (dso != null && dso.getType() == Constants.ITEM && sequenceID >= 0)
142         {
143             item = (Item) dso;
144         
145             if (item.isWithdrawn())
146             {
147                 log.info(LogManager.getHeader(context, "view_bitstream",
148                         "handle=" + handle + ",withdrawn=true"));
149                 JSPManager.showJSP(request, response, "/tombstone.jsp");
150                 return;
151             }
152
153             boolean found = false;
154
155             Bundle[] bundles = item.getBundles();
156
157             for (int i = 0; (i < bundles.length) && !found; i++)
158             {
159                 Bitstream[] bitstreams = bundles[i].getBitstreams();
160
161                 for (int k = 0; (k < bitstreams.length) && !found; k++)
162                 {
163                     if (sequenceID == bitstreams[k].getSequenceID())
164                     {
165                         bitstream = bitstreams[k];
166                         found = true;
167                     }
168                 }
169             }
170         }
171
172         if (bitstream == null || filename == null
173                 || !filename.equals(bitstream.getName()))
174         {
175             // No bitstream found or filename was wrong -- ID invalid
176
log.info(LogManager.getHeader(context, "invalid_id", "path="
177                     + idString));
178             JSPManager.showInvalidIDError(request, response, idString,
179                     Constants.BITSTREAM);
180
181             return;
182         }
183
184         log.info(LogManager.getHeader(context, "view_bitstream",
185                 "bitstream_id=" + bitstream.getID()));
186
187         // Modification date
188
// TODO: Currently the date of the item, since we don't have dates
189
// for files
190
response.setDateHeader("Last-Modified", item.getLastModified()
191                 .getTime());
192         
193         // Check for if-modified-since header
194
long modSince = request.getDateHeader("If-Modified-Since");
195
196         if (modSince != -1 && item.getLastModified().getTime() < modSince)
197         {
198             // Item has not been modified since requested date,
199
// hence bitstream has not; return 304
200
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
201             return;
202         }
203         
204         // Pipe the bits
205
InputStream JavaDoc is = bitstream.retrieve();
206      
207                 // Set the response MIME type
208
response.setContentType(bitstream.getFormat().getMIMEType());
209
210         // Response length
211
response.setHeader("Content-Length", String
212                 .valueOf(bitstream.getSize()));
213
214         Utils.bufferedCopy(is, response.getOutputStream());
215         is.close();
216         response.getOutputStream().flush();
217     }
218 }
219
Popular Tags