001: /*-----------------------------------------------------------------------------
002: * Enhydra Java Application Server
003: * Copyright 1997-2000 Lutris Technologies, Inc.
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the distribution.
014: * 3. All advertising materials mentioning features or use of this software
015: * must display the following acknowledgement:
016: * This product includes Enhydra software developed by Lutris
017: * Technologies, Inc. and its contributors.
018: * 4. Neither the name of Lutris Technologies nor the names of its contributors
019: * may be used to endorse or promote products derived from this software
020: * without specific prior written permission.
021: *
022: * THIS SOFTWARE IS PROVIDED BY LUTRIS TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
023: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
024: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
025: * ARE DISCLAIMED. IN NO EVENT SHALL LUTRIS TECHNOLOGIES OR CONTRIBUTORS BE
026: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
027: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
028: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
029: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
030: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
031: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
032: * POSSIBILITY OF SUCH DAMAGE.
033: *-----------------------------------------------------------------------------
034: * $Id: PageRedirect.java,v 1.5 2007-05-17 12:52:55 sinisa Exp $
035: *-----------------------------------------------------------------------------
036: */
037:
038: package com.lutris.appserver.server.httpPresentation;
039:
040: import java.io.IOException;
041: import java.net.MalformedURLException;
042: import java.net.URL;
043:
044: import javax.servlet.RequestDispatcher;
045: import javax.servlet.http.HttpServletRequest;
046: import javax.servlet.http.HttpServletResponse;
047:
048: import com.lutris.appserver.server.StandardAppUtil;
049: import com.lutris.logging.LogChannel;
050: import com.lutris.logging.Logger;
051: import com.lutris.util.ConfigException;
052:
053: /**
054: * Static methods for processing a page redirect.
055: *
056: * @version $Revision: 1.5 $
057: * @author Mark Diekhans, Slobodan Vujasinovic
058: * @since Jolt1.3
059: */
060: class PageRedirect {
061: /**
062: * Process a page redirection.
063: *
064: * @return Path of the presentation object to invoke for a server-side
065: * redirect or null if a redirect response has been sent to the
066: * client. NOTE: Currently always returns null.
067: */
068: protected static String handler(HttpPresentationComms comms,
069: String presObjPath, PageRedirectException redirectExcept,
070: LogChannel logChannel, long requestId)
071: throws MalformedURLException, HttpPresentationException,
072: IOException {
073:
074: boolean debug = logChannel.isEnabled(Logger.DEBUG);
075:
076: // FIX: Break into two functions; one for client., one for server.
077: if (debug)
078: logChannel.write(Logger.DEBUG, "RID:" + requestId + ": "
079: + redirectExcept.getClass().getName() + ": "
080: + presObjPath + " => " + redirectExcept.getUrl());
081: String url = redirectExcept.getUrl();
082: if (redirectExcept instanceof ClientPageRedirectException) {
083: return clientSideRedirect(url, comms, logChannel,
084: requestId, redirectExcept.getEncoding());
085: } else {
086: try {
087: HttpServletRequest hsreq = comms.request
088: .getHttpServletRequest();
089: HttpServletResponse hsres = comms.response
090: .getHttpServletResponse();
091:
092: if (((ServerPageRedirectException) redirectExcept)
093: .getHttpServletRequest() != null)
094: hsreq = ((ServerPageRedirectException) redirectExcept)
095: .getHttpServletRequest();
096: if (((ServerPageRedirectException) redirectExcept)
097: .getHttpServletResponse() != null)
098: hsres = ((ServerPageRedirectException) redirectExcept)
099: .getHttpServletResponse();
100:
101: RequestDispatcher rd = hsreq.getRequestDispatcher(url);
102:
103: rd.forward(hsreq, hsres);
104:
105: } catch (Exception ex) {
106: logChannel.write(Logger.WARNING,
107: "Server redirect to \"" + url
108: + "\" failed. Reason: \""
109: + ex.getLocalizedMessage()
110: + "\" reattempt as client redirect.");
111: try {
112: clientSideRedirect(url, comms, logChannel,
113: requestId, redirectExcept.getEncoding());
114: } catch (Exception e) {
115: throw new HttpPresentationException(e);
116: }
117: }
118: return null;
119:
120: }
121: }
122:
123: private static String clientSideRedirect(String url,
124: HttpPresentationComms comms, LogChannel logChannel,
125: long requestId, String encoding)
126: throws HttpPresentationException {
127:
128: boolean debug = logChannel.isEnabled(Logger.DEBUG);
129: url = makeAbsolute(url, comms.request.getHttpServletRequest());
130: if (comms.request.isRequestedSessionIdFromUrl()
131: && StandardAppUtil.pointsToPO(url)) {
132: url = StandardAppUtil.encodeUrl(url, comms.session
133: .getSessionKey());
134:
135: if (debug)
136: logChannel
137: .write(Logger.DEBUG, "RID:" + requestId + ": "
138: + "Adding session ID to new URL: "
139: + url);
140: } else {
141: logChannel.write(Logger.DEBUG, "RID:" + requestId + ": "
142: + "No session ID for URL: " + url);
143: }
144:
145: boolean metaRedirect = false;
146: try {
147: metaRedirect = comms.application.getConfig().getBoolean(
148: "Application.ForceHtmlMetaRedirect", false);
149: } catch (ConfigException ex) {
150: metaRedirect = false;
151: }
152:
153: if (metaRedirect) {
154: String response = "<html><head><meta HTTP-EQUIV=\"REFRESH\" CONTENT=\"0; URL="
155: + url
156: + "\">"
157: + "<title>Redirected to new location</title></head>"
158: + "<body><p> </p></body></html>";
159: try {
160: comms.response.getOutputStream().write(
161: response.getBytes());
162: } catch (IOException e) {
163: logChannel.write(Logger.WARNING, "RID:" + requestId
164: + ": "
165: + "Not able to write redirect HTML content: "
166: + url);
167: }
168: } else {
169: comms.response.setHeader("Location", url);
170: comms.response.setStatus(
171: HttpPresentationResponse.SC_MOVED_TEMPORARILY,
172: "Redirected to new location.");
173: }
174:
175: comms.response.setContentType("text/html");
176: comms.response.setEncoding(encoding);
177: comms.response.flush();
178: return null;
179: }
180:
181: private static String makeAbsolute(String location,
182: HttpServletRequest request) {
183: URL url = null;
184: try {
185: // Try making a URL out of the location
186: // Throws an exception if the location is relative
187: url = new URL(location);
188: } catch (MalformedURLException e) {
189: // Slobodan Vujasinovic - old implementation deprecated (Servlet API
190: // 2.3)
191: // String requrl = HttpUtils.getRequestURL(request).toString();
192: String requrl = request.getRequestURL().toString();
193: try {
194: url = new URL(new URL(requrl), location);
195: } catch (MalformedURLException ignored) {
196: // Give up
197: return location;
198: }
199: }
200: return url.toString();
201: }
202: }
|