KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > controller > RedirectController


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.web.controller;
25
26 import java.io.IOException JavaDoc;
27 import java.io.UnsupportedEncodingException JavaDoc;
28 import java.net.URLEncoder JavaDoc;
29 import java.util.Enumeration JavaDoc;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.riotfamily.common.web.util.ServletUtils;
35 import org.springframework.web.servlet.ModelAndView;
36 import org.springframework.web.servlet.mvc.Controller;
37
38 /**
39  * Controller that sends a redirect to a configurable URL.
40  */

41 public class RedirectController implements Controller {
42
43     private boolean http10Compatible = true;
44     
45     private boolean addContextPath = false;
46     
47     private boolean addServletMapping = false;
48     
49     private String JavaDoc encodingScheme = "UTF-8";
50         
51     private String JavaDoc url;
52     
53     public RedirectController(String JavaDoc url) {
54         this.url = url;
55     }
56     
57     public RedirectController(String JavaDoc url, boolean addContextPath,
58             boolean addServletMapping) {
59         
60         this.url = url;
61         this.addContextPath = addContextPath;
62         this.addServletMapping = addServletMapping;
63     }
64
65     protected RedirectController() {
66     }
67     
68     /**
69      * Set whether to stay compatible with HTTP 1.0 clients.
70      * <p>In the default implementation, this will enforce HTTP status code 302
71      * in any case, i.e. delegate to <code>HttpServletResponse.sendRedirect</code>.
72      * Turning this off will send HTTP status code 303, which is the correct
73      * code for HTTP 1.1 clients, but not understood by HTTP 1.0 clients.
74      * <p>Many HTTP 1.1 clients treat 302 just like 303, not making any
75      * difference. However, some clients depend on 303 when redirecting
76      * after a POST request; turn this flag off in such a scenario.
77      * @see javax.servlet.http.HttpServletResponse#sendRedirect
78      */

79     public void setHttp10Compatible(boolean http10Compatible) {
80         this.http10Compatible = http10Compatible;
81     }
82
83     
84     public void setAddContextPath(boolean contextRelative) {
85         this.addContextPath = contextRelative;
86     }
87
88     public void setAddServletMapping(boolean addServletMapping) {
89         this.addServletMapping = addServletMapping;
90     }
91
92     /**
93      * Set the encoding to be used for parameter values.
94      */

95     public void setEncodingScheme(String JavaDoc encodingScheme) {
96         this.encodingScheme = encodingScheme;
97     }
98
99     public ModelAndView handleRequest(HttpServletRequest JavaDoc request,
100             HttpServletResponse JavaDoc response) throws Exception JavaDoc {
101
102         String JavaDoc destination = getDestination(request);
103         if (destination == null) {
104             response.sendError(HttpServletResponse.SC_NOT_FOUND);
105             return null;
106         }
107         
108         StringBuffer JavaDoc url = new StringBuffer JavaDoc();
109         
110         if (addContextPath && destination.startsWith("/")) {
111             url.append(request.getContextPath());
112         }
113         if (addServletMapping) {
114             url.append(ServletUtils.getServletPrefix(request));
115         }
116         
117         url.append(destination);
118         
119         if (addServletMapping) {
120             url.append(ServletUtils.getServletSuffix(request));
121         }
122         appendParameters(url, request);
123         sendRedirect(request, response, url.toString());
124         return null;
125     }
126     
127     protected String JavaDoc getDestination(HttpServletRequest JavaDoc request) {
128         return url;
129     }
130     
131     protected void appendParameters(StringBuffer JavaDoc targetUrl,
132             HttpServletRequest JavaDoc request) throws UnsupportedEncodingException JavaDoc {
133
134         boolean first = (targetUrl.indexOf("?") == -1);
135         Enumeration JavaDoc paramNames = request.getParameterNames();
136         while (paramNames.hasMoreElements()) {
137             String JavaDoc name = (String JavaDoc) paramNames.nextElement();
138             String JavaDoc[] values = request.getParameterValues(name);
139             for (int i = 0; i < values.length; i++) {
140                 if (first) {
141                     targetUrl.append('?');
142                     first = false;
143                 }
144                 else {
145                     targetUrl.append('&');
146                 }
147                 targetUrl.append(name).append('=');
148                 if (values[i] != null) {
149                     targetUrl.append(URLEncoder.encode(values[i], encodingScheme));
150                 }
151             }
152         }
153     }
154     
155     /**
156      * Send a redirect back to the HTTP client
157      * @param request current HTTP request (allows for reacting to request method)
158      * @param response current HTTP response (for sending response headers)
159      * @param targetUrl the target URL to redirect to
160      * @throws IOException if thrown by response methods
161      */

162     protected void sendRedirect(HttpServletRequest JavaDoc request,
163             HttpServletResponse JavaDoc response, String JavaDoc targetUrl) throws IOException JavaDoc {
164         
165         if (http10Compatible) {
166             // always send status code 302
167
response.sendRedirect(response.encodeRedirectURL(targetUrl));
168         }
169         else {
170             // correct HTTP status code is 303, in particular for POST requests
171
response.setStatus(303);
172             response.setHeader("Location", response.encodeRedirectURL(targetUrl));
173         }
174     }
175         
176 }
177
Popular Tags