KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * RetrieveServlet.java
3  *
4  * Version: $Revision: 1.6 $
5  *
6  * Date: $Date: 2005/04/20 14:22:39 $
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
46 import javax.servlet.ServletException JavaDoc;
47 import javax.servlet.http.HttpServletRequest JavaDoc;
48 import javax.servlet.http.HttpServletResponse JavaDoc;
49
50 import org.apache.log4j.Logger;
51 import org.dspace.app.webui.util.JSPManager;
52 import org.dspace.authorize.AuthorizeException;
53 import org.dspace.content.Bitstream;
54 import org.dspace.core.Constants;
55 import org.dspace.core.Context;
56 import org.dspace.core.LogManager;
57 import org.dspace.core.Utils;
58
59 /**
60  * Servlet for retrieving bitstreams. The bits are simply piped to the user.
61  * <P>
62  * <code>/retrieve/bitstream-id</code>
63  *
64  * @author Robert Tansley
65  * @version $Revision: 1.6 $
66  */

67 public class RetrieveServlet extends DSpaceServlet
68 {
69     /** log4j category */
70     private static Logger log = Logger.getLogger(RetrieveServlet.class);
71
72     protected void doDSGet(Context context, HttpServletRequest JavaDoc request,
73             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
74             SQLException JavaDoc, AuthorizeException
75     {
76         Bitstream bitstream = null;
77
78         // Get the ID from the URL
79
String JavaDoc idString = request.getPathInfo();
80
81         if (idString != null)
82         {
83             // Remove leading slash
84
if (idString.startsWith("/"))
85             {
86                 idString = idString.substring(1);
87             }
88
89             // If there's a second slash, remove it and anything after it,
90
// it might be a filename
91
int slashIndex = idString.indexOf('/');
92
93             if (slashIndex != -1)
94             {
95                 idString = idString.substring(0, slashIndex);
96             }
97
98             // Find the corresponding bitstream
99
try
100             {
101                 int id = Integer.parseInt(idString);
102                 bitstream = Bitstream.find(context, id);
103             }
104             catch (NumberFormatException JavaDoc nfe)
105             {
106                 // Invalid ID - this will be dealt with below
107
}
108         }
109
110         // Did we get a bitstream?
111
if (bitstream != null)
112         {
113             log.info(LogManager.getHeader(context, "view_bitstream",
114                     "bitstream_id=" + bitstream.getID()));
115
116             // Set the response MIME type
117
response.setContentType(bitstream.getFormat().getMIMEType());
118
119             // Response length
120
response.setHeader("Content-Length", String.valueOf(bitstream
121                     .getSize()));
122
123             // Pipe the bits
124
InputStream JavaDoc is = bitstream.retrieve();
125
126             Utils.bufferedCopy(is, response.getOutputStream());
127             is.close();
128             response.getOutputStream().flush();
129         }
130         else
131         {
132             // No bitstream - we got an invalid ID
133
log.info(LogManager.getHeader(context, "view_bitstream",
134                     "invalid_bitstream_id=" + idString));
135
136             JSPManager.showInvalidIDError(request, response, idString,
137                     Constants.BITSTREAM);
138         }
139     }
140 }
141
Popular Tags