KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * DSpaceServlet.java
3  *
4  * Version: $Revision: 1.13 $
5  *
6  * Date: $Date: 2005/10/17 03:35:45 $
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.sql.SQLException JavaDoc;
44
45 import javax.servlet.ServletException JavaDoc;
46 import javax.servlet.http.HttpServlet 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.Authenticate;
52 import org.dspace.app.webui.util.JSPManager;
53 import org.dspace.app.webui.util.UIUtil;
54 import org.dspace.authorize.AuthorizeException;
55 import org.dspace.core.Context;
56 import org.dspace.core.LogManager;
57
58 /**
59  * Base class for DSpace servlets. This manages the initialisation of a context,
60  * if a context has not already been initialised by an authentication filter. It
61  * also grabs the original request URL for later use.
62  * <P>
63  * Unlike regular servlets, DSpace servlets should override the
64  * <code>doDSGet</code> and <code>doDSPost</code> methods, which provide a
65  * DSpace context to work with, and handle the common exceptions
66  * <code>SQLException</code> and <code>AuthorizeException</code>.
67  * <code>doGet</code> and <code>doPost</code> should be overridden only in
68  * special circumstances.
69  * <P>
70  * Note that if all goes well, the context object passed in to
71  * <code>doDSGet</code> and <code>doDSPut</code> must be completed
72  * <em>after</em> a JSP has been displayed, but <code>before</code> the
73  * method returns. If an error occurs (an exception is thrown, or the context is
74  * not completed) the context is aborted after <code>doDSGet</code> or
75  * <code>doDSPut</code> return.
76  *
77  * @see org.dspace.core.Context
78  *
79  * @author Robert Tansley
80  * @version $Revision: 1.13 $
81  */

82 public class DSpaceServlet extends HttpServlet JavaDoc
83 {
84     /*
85      * Because the error handling is indentical for GET and POST requests, to
86      * make things easier, doGet and doPost are folded into one method which
87      * then "re-separates" to GET and POST. We do not override "service" since
88      * we wish to allow doGet and doPost to be overridden if necessary (for
89      * example, by lightweight servlets that do not need a DSpace context
90      * object).
91      */

92
93     /** log4j category */
94     private static Logger log = Logger.getLogger(DSpaceServlet.class);
95
96     protected void doGet(HttpServletRequest JavaDoc request,
97             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc
98     {
99         processRequest(request, response);
100     }
101
102     protected void doPost(HttpServletRequest JavaDoc request,
103             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc
104     {
105         processRequest(request, response);
106     }
107
108     /**
109      * Process an incoming request
110      *
111      * @param request
112      * the request object
113      * @param response
114      * the response object
115      */

116     private void processRequest(HttpServletRequest JavaDoc request,
117             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc
118     {
119         Context context = null;
120
121         // set all incoming encoding to UTF-8
122
request.setCharacterEncoding("UTF-8");
123
124         // Get the URL from the request immediately, since forwarding
125
// loses that information
126
UIUtil.storeOriginalURL(request);
127
128         try
129         {
130             // Obtain a context - either create one, or get the one created by
131
// an authentication filter
132
context = UIUtil.obtainContext(request);
133
134             // Are we resuming a previous request that was interrupted for
135
// authentication?
136
request = Authenticate.getRealRequest(request);
137
138             if (log.isDebugEnabled())
139             {
140                 log.debug(LogManager.getHeader(context, "http_request", UIUtil
141                         .getRequestLogInfo(request)));
142             }
143
144             // Invoke the servlet code
145
if (request.getMethod().equals("POST"))
146             {
147                 doDSPost(context, request, response);
148             }
149             else
150             {
151                 doDSGet(context, request, response);
152             }
153         }
154         catch (SQLException JavaDoc se)
155         {
156             // For database errors, we log the exception and show a suitably
157
// apologetic error
158
log.warn(LogManager.getHeader(context, "database_error", se
159                     .toString()), se);
160
161             // Also email an alert
162
UIUtil.sendAlert(request, se);
163
164             JSPManager.showInternalError(request, response);
165         }
166         catch (AuthorizeException ae)
167         {
168             /*
169              * If no user is logged in, we will start authentication, since if
170              * they authenticate, they might be allowed to do what they tried to
171              * do. If someone IS logged in, and we got this exception, we know
172              * they tried to do something they aren't allowed to, so we display
173              * an error in that case.
174              */

175             if (context.getCurrentUser() != null ||
176                 Authenticate.startAuthentication(context, request, response))
177             {
178                 // FIXME: Log the right info?
179
// Log the error
180
log.info(LogManager.getHeader(context, "authorize_error", ae
181                         .toString()));
182
183                 JSPManager.showAuthorizeError(request, response, ae);
184             }
185         }
186         catch (RuntimeException JavaDoc e)
187         {
188             // Catch and re-throw to ensure context aborted (via "finally")
189
throw e;
190         }
191         catch (IOException JavaDoc ioe)
192         {
193             // Catch and re-throw to ensure context aborted (via "finally")
194
throw ioe;
195         }
196         catch (ServletException JavaDoc sve)
197         {
198             // Catch and re-throw to ensure context aborted (via "finally")
199
throw sve;
200         }
201         finally
202         {
203             // Abort the context if it's still valid
204
if ((context != null) && context.isValid())
205             {
206                 context.abort();
207             }
208         }
209     }
210
211     /**
212      * Process an incoming HTTP GET. If an exception is thrown, or for some
213      * other reason the passed in context is not completed, it will be aborted
214      * and any changes made by this method discarded when this method returns.
215      *
216      * @param context
217      * a DSpace Context object
218      * @param request
219      * the HTTP request
220      * @param response
221      * the HTTP response
222      *
223      * @throws SQLException
224      * if a database error occurs
225      * @throws AuthorizeException
226      * if some authorization error occurs
227      */

228     protected void doDSGet(Context context, HttpServletRequest JavaDoc request,
229             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
230             SQLException JavaDoc, AuthorizeException
231     {
232         // If this is not overridden, we invoke the raw HttpServlet "doGet" to
233
// indicate that GET is not supported by this servlet.
234
super.doGet(request, response);
235     }
236
237     /**
238      * Process an incoming HTTP POST. If an exception is thrown, or for some
239      * other reason the passed in context is not completed, it will be aborted
240      * and any changes made by this method discarded when this method returns.
241      *
242      * @param context
243      * a DSpace Context object
244      * @param request
245      * the HTTP request
246      * @param response
247      * the HTTP response
248      *
249      * @throws SQLException
250      * if a database error occurs
251      * @throws AuthorizeException
252      * if some authorization error occurs
253      */

254     protected void doDSPost(Context context, HttpServletRequest JavaDoc request,
255             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
256             SQLException JavaDoc, AuthorizeException
257     {
258         // If this is not overridden, we invoke the raw HttpServlet "doGet" to
259
// indicate that POST is not supported by this servlet.
260
super.doGet(request, response);
261     }
262 }
263
Popular Tags