KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > httpPresentation > PageRedirect


1
2 /*-----------------------------------------------------------------------------
3  * Enhydra Java Application Server
4  * Copyright 1997-2000 Lutris Technologies, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  * must display the following acknowledgement:
17  * This product includes Enhydra software developed by Lutris
18  * Technologies, Inc. and its contributors.
19  * 4. Neither the name of Lutris Technologies nor the names of its contributors
20  * may be used to endorse or promote products derived from this software
21  * without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY LUTRIS TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL LUTRIS TECHNOLOGIES OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *-----------------------------------------------------------------------------
35  * $Id: PageRedirect.java,v 1.3 2005/03/24 10:51:16 slobodan Exp $
36  *-----------------------------------------------------------------------------
37  */

38
39
40
41
42
43 package com.lutris.appserver.server.httpPresentation;
44
45 import java.io.IOException JavaDoc;
46 import java.net.MalformedURLException JavaDoc;
47 import java.net.URL JavaDoc;
48
49 import javax.servlet.RequestDispatcher JavaDoc;
50 import javax.servlet.http.HttpServletRequest JavaDoc;
51 import javax.servlet.http.HttpUtils JavaDoc;
52
53 import com.lutris.appserver.server.StandardAppUtil;
54 import com.lutris.logging.LogChannel;
55 import com.lutris.logging.Logger;
56
57 /**
58  * Static methods for processing a page redirect.
59  *
60  * @version $Revision: 1.3 $
61  * @author Mark Diekhans
62  * @since Jolt1.3
63  */

64 class PageRedirect {
65     /**
66      * Process a page redirection.
67      *
68      * @return Path of the presentation object to invoke for a server-side
69      * redirect or null if a redirect response has been sent to the
70      * client. NOTE: Currently always returns null.
71      */

72     protected static String JavaDoc handler(HttpPresentationComms comms,
73                                     String JavaDoc presObjPath,
74                                     PageRedirectException redirectExcept,
75                     LogChannel logChannel,
76                                     long requestId)
77             throws MalformedURLException JavaDoc,
78            HttpPresentationException,
79            IOException JavaDoc {
80
81     boolean debug = logChannel.isEnabled(Logger.DEBUG);
82
83         //FIX: Break into two functions; one for client., one for server.
84
if (debug)
85         logChannel.write(Logger.DEBUG, "RID:" + requestId + ": "
86                          + redirectExcept.getClass().getName()
87                          + ": " + presObjPath + " => "
88                          + redirectExcept.getUrl());
89         String JavaDoc url = redirectExcept.getUrl();
90         if (redirectExcept instanceof ClientPageRedirectException) {
91               return clientSideRedirect(url,comms,logChannel,requestId, redirectExcept.getEncoding());
92         } else {
93             RequestDispatcher JavaDoc rd = comms.request.getHttpServletRequest().getRequestDispatcher(url);
94             try {
95                 rd.forward(comms.request.getHttpServletRequest(),
96                         comms.response.getHttpServletResponse());
97
98             } catch (Exception JavaDoc ex) {
99         logChannel.write(Logger.WARNING, "Server redirect to \"" +
100                         url + "\" failed. Reason: \""
101                         + ex.getLocalizedMessage() + "\" reattempt as client redirect.");
102                  try {
103                       clientSideRedirect(url,comms,logChannel,requestId, redirectExcept.getEncoding());
104                  } catch (Exception JavaDoc e) {
105                     throw new HttpPresentationException(e);
106                  }
107             }
108             return null;
109
110         }
111     }
112
113
114
115     private static String JavaDoc clientSideRedirect(String JavaDoc url,
116                          HttpPresentationComms comms,
117                          LogChannel logChannel,
118                                             long requestId,
119                                             String JavaDoc encoding)
120         throws HttpPresentationException {
121
122     boolean debug = logChannel.isEnabled(Logger.DEBUG);
123     url = makeAbsolute(url,comms.request.getHttpServletRequest());
124     if (comms.request.isRequestedSessionIdFromUrl() &&
125         StandardAppUtil.pointsToPO(url)) {
126         url = StandardAppUtil.encodeUrl(url, comms.session.getSessionKey());
127
128         if (debug)
129         logChannel.write(Logger.DEBUG, "RID:" + requestId + ": "
130                                  + "Adding session ID to new URL: "
131                                  + url);
132             } else {
133         logChannel.write(Logger.DEBUG, "RID:" + requestId + ": "
134                                  + "No session ID for URL: "
135                                  + url);
136             }
137             comms.response.setHeader("Location", url);
138             comms.response.setStatus(HttpPresentationResponse.SC_MOVED_TEMPORARILY,
139                                      "Redirected to new location.");
140             comms.response.setEncoding( encoding );
141             comms.response.flush();
142             return null;
143
144     }
145
146     private static String JavaDoc makeAbsolute(String JavaDoc location,
147             HttpServletRequest JavaDoc request) {
148         URL JavaDoc url = null;
149         try {
150         // Try making a URL out of the location
151
// Throws an exception if the location is relative
152
url = new URL JavaDoc(location);
153     }
154     catch (MalformedURLException JavaDoc e) {
155         String JavaDoc requrl = HttpUtils.getRequestURL(request).toString();
156         try {
157             url = new URL JavaDoc(new URL JavaDoc(requrl), location);
158         }
159         catch (MalformedURLException JavaDoc ignored) {
160             // Give up
161
return location;
162         }
163     }
164         return url.toString();
165     }
166 }
167
Popular Tags