KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > webui > util > JSPManager


1 /*
2  * JSPManager.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.util;
41
42 import java.io.IOException JavaDoc;
43
44 import javax.servlet.ServletException JavaDoc;
45 import javax.servlet.http.HttpServletRequest JavaDoc;
46 import javax.servlet.http.HttpServletResponse JavaDoc;
47
48 import org.apache.log4j.Logger;
49 import org.dspace.authorize.AuthorizeException;
50 import org.dspace.core.Context;
51 import org.dspace.core.LogManager;
52
53 /**
54  * Methods for displaying UI pages to the user.
55  *
56  * @author Robert Tansley
57  * @version $Revision: 1.11 $
58  */

59 public class JSPManager
60 {
61     /*
62      * All displaying of UI pages should be performed using this manager for
63      * future-proofing, since any future localisation effort will probably use
64      * this manager.
65      */

66
67     /** log4j logger */
68     private static Logger log = Logger.getLogger(JSPManager.class);
69
70     /**
71      * Forwards control of the request to the display JSP passed in.
72      *
73      * @param request
74      * current servlet request object
75      * @param response
76      * current servlet response object
77      * @param jsp
78      * the JSP page to display, relative to the webapps directory
79      */

80     public static void showJSP(HttpServletRequest JavaDoc request,
81             HttpServletResponse JavaDoc response, String JavaDoc jsp) throws ServletException JavaDoc,
82             IOException JavaDoc
83     {
84         if (log.isDebugEnabled())
85         {
86             log.debug(LogManager.getHeader((Context) request
87                     .getAttribute("dspace.context"), "view_jsp", jsp));
88         }
89
90         // For the moment, a simple forward
91
request.getRequestDispatcher(jsp).forward(request, response);
92     }
93
94     /**
95      * Display an internal server error message - for example, a database error
96      *
97      * @param request
98      * the HTTP request
99      * @param response
100      * the HTTP response
101      */

102     public static void showInternalError(HttpServletRequest JavaDoc request,
103             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc
104     {
105         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
106         showJSP(request, response, "/error/internal.jsp");
107     }
108
109     /**
110      * Display an integrity error message. Use when the POSTed data from a
111      * request doesn't make sense.
112      *
113      * @param request
114      * the HTTP request
115      * @param response
116      * the HTTP response
117      */

118     public static void showIntegrityError(HttpServletRequest JavaDoc request,
119             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc
120     {
121         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
122         showJSP(request, response, "/error/integrity.jsp");
123     }
124
125     /**
126      * Display an authorization failed error message. The exception should be
127      * passed in if possible so that the error message can be descriptive.
128      *
129      * @param request
130      * the HTTP request
131      * @param response
132      * the HTTP response
133      * @param exception
134      * the AuthorizeException leading to this error, passing in
135      * <code>null</code> will display default error message
136      */

137     public static void showAuthorizeError(HttpServletRequest JavaDoc request,
138             HttpServletResponse JavaDoc response, AuthorizeException exception)
139             throws ServletException JavaDoc, IOException JavaDoc
140     {
141         // FIXME: Need to work out which error message to display?
142
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
143         showJSP(request, response, "/error/authorize.jsp");
144     }
145
146     /**
147      * Display an "invalid ID" error message. Passing in information about the
148      * bad ID and what the ID was supposed to represent (collection etc.) should
149      * result in a more descriptive and helpful error message.
150      *
151      * @param request
152      * the HTTP request
153      * @param response
154      * the HTTP response
155      * @param badID
156      * the bad identifier, or <code>null</code>
157      * @param type
158      * the type of object, from
159      * <code>org.dspace.core.Constants</code>, or <code>-1</code>
160      * for a default message
161      */

162     public static void showInvalidIDError(HttpServletRequest JavaDoc request,
163             HttpServletResponse JavaDoc response, String JavaDoc badID, int type)
164             throws ServletException JavaDoc, IOException JavaDoc
165     {
166         request.setAttribute("bad.id", badID);
167         response.setStatus(HttpServletResponse.SC_NOT_FOUND);
168
169         if (type != -1)
170         {
171             request.setAttribute("bad.type", new Integer JavaDoc(type));
172         }
173
174         showJSP(request, response, "/error/invalid-id.jsp");
175     }
176 }
177
Popular Tags