001: /*
002: * JSPManager.java
003: *
004: * Version: $Revision: 1671 $
005: *
006: * Date: $Date: 2006-11-10 16:26:30 -0600 (Fri, 10 Nov 2006) $
007: *
008: * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040: package org.dspace.app.webui.util;
041:
042: import java.io.IOException;
043:
044: import javax.servlet.ServletException;
045: import javax.servlet.http.HttpServletRequest;
046: import javax.servlet.http.HttpServletResponse;
047:
048: import org.apache.log4j.Logger;
049: import org.dspace.authorize.AuthorizeException;
050: import org.dspace.core.Context;
051: import org.dspace.core.LogManager;
052:
053: /**
054: * Methods for displaying UI pages to the user.
055: *
056: * @author Robert Tansley
057: * @version $Revision: 1671 $
058: */
059: public class JSPManager {
060: /*
061: * All displaying of UI pages should be performed using this manager for
062: * future-proofing, since any future localisation effort will probably use
063: * this manager.
064: */
065:
066: /** log4j logger */
067: private static Logger log = Logger.getLogger(JSPManager.class);
068:
069: /**
070: * Forwards control of the request to the display JSP passed in.
071: *
072: * @param request
073: * current servlet request object
074: * @param response
075: * current servlet response object
076: * @param jsp
077: * the JSP page to display, relative to the webapps directory
078: */
079: public static void showJSP(HttpServletRequest request,
080: HttpServletResponse response, String jsp)
081: throws ServletException, IOException {
082: if (log.isDebugEnabled()) {
083: log.debug(LogManager.getHeader((Context) request
084: .getAttribute("dspace.context"), "view_jsp", jsp));
085: }
086:
087: // For the moment, a simple forward
088: request.getRequestDispatcher(jsp).forward(request, response);
089: }
090:
091: /**
092: * Display an internal server error message - for example, a database error
093: *
094: * @param request
095: * the HTTP request
096: * @param response
097: * the HTTP response
098: */
099: public static void showInternalError(HttpServletRequest request,
100: HttpServletResponse response) throws ServletException,
101: IOException {
102: response
103: .setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
104: showJSP(request, response, "/error/internal.jsp");
105: }
106:
107: /**
108: * Display an integrity error message. Use when the POSTed data from a
109: * request doesn't make sense.
110: *
111: * @param request
112: * the HTTP request
113: * @param response
114: * the HTTP response
115: */
116: public static void showIntegrityError(HttpServletRequest request,
117: HttpServletResponse response) throws ServletException,
118: IOException {
119: response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
120: showJSP(request, response, "/error/integrity.jsp");
121: }
122:
123: /**
124: * Display an authorization failed error message. The exception should be
125: * passed in if possible so that the error message can be descriptive.
126: *
127: * @param request
128: * the HTTP request
129: * @param response
130: * the HTTP response
131: * @param exception
132: * the AuthorizeException leading to this error, passing in
133: * <code>null</code> will display default error message
134: */
135: public static void showAuthorizeError(HttpServletRequest request,
136: HttpServletResponse response, AuthorizeException exception)
137: throws ServletException, IOException {
138: // FIXME: Need to work out which error message to display?
139: response.setStatus(HttpServletResponse.SC_FORBIDDEN);
140: showJSP(request, response, "/error/authorize.jsp");
141: }
142:
143: /**
144: * Display an "invalid ID" error message. Passing in information about the
145: * bad ID and what the ID was supposed to represent (collection etc.) should
146: * result in a more descriptive and helpful error message.
147: *
148: * @param request
149: * the HTTP request
150: * @param response
151: * the HTTP response
152: * @param badID
153: * the bad identifier, or <code>null</code>
154: * @param type
155: * the type of object, from
156: * <code>org.dspace.core.Constants</code>, or <code>-1</code>
157: * for a default message
158: */
159: public static void showInvalidIDError(HttpServletRequest request,
160: HttpServletResponse response, String badID, int type)
161: throws ServletException, IOException {
162: request.setAttribute("bad.id", badID);
163: response.setStatus(HttpServletResponse.SC_NOT_FOUND);
164:
165: if (type != -1) {
166: request.setAttribute("bad.type", new Integer(type));
167: }
168:
169: showJSP(request, response, "/error/invalid-id.jsp");
170: }
171: }
|