source: branches/TaskRewrite/src/plugins/acegi-0.5.1/src/groovy/org/codehaus/groovy/grails/plugins/springsecurity/RedirectUtils.groovy @ 58

Last change on this file since 58 was 58, checked in by gav, 15 years ago

Configure BootStrap? with latest concepts.
Install and setup Acegi plugin with custom views.
Test Fixture plugin in a test app but couldn't get it to work with Acegi encodePassword() so gave up.

File size: 2.4 KB
Line 
1/* Copyright 2006-2009 the original author or authors.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15package org.codehaus.groovy.grails.plugins.springsecurity
16
17import javax.servlet.http.HttpServletRequest
18import javax.servlet.http.HttpServletResponse
19
20import org.springframework.security.util.PortResolver
21import org.springframework.security.util.PortResolverImpl
22
23/**
24 * Fixes <a href='http://jira.codehaus.org/browse/GRAILSPLUGINS-273'>this redirect bug</a>.
25 * @author Tsuyoshi Yamamoto
26 */
27class RedirectUtils {
28
29        private static final PortResolver RESOLVER = new PortResolverImpl()
30
31        /**
32         * Send a redirect.
33         * @param request  the request
34         * @param response  the response
35         * @param url the target url to redirect to
36         * @throws IOException  if there's a problem
37         */
38        static void sendRedirect(
39                        HttpServletRequest request,
40                        HttpServletResponse response,
41                        String url) throws IOException {
42
43                String redirect = buildRedirectUrl(request, response, url)
44                response.sendRedirect(response.encodeRedirectURL(redirect))
45        }
46
47        /**
48         * Build a redirect url.
49         * @param request  the request
50         * @param response  the response
51         * @param url the target url to redirect to
52         * @return  the url
53         * @throws IOException  if there's a problem
54         */
55        static String buildRedirectUrl(
56                        HttpServletRequest request,
57                        HttpServletResponse response,
58                        String url) throws IOException {
59
60                if (!url.startsWith("http://") && !url.startsWith("https://")) {
61                        String scheme = request.scheme
62                        int serverPort = RESOLVER.getServerPort(request)
63                        boolean inHttp = "http".equalsIgnoreCase(scheme)
64                        boolean inHttps = "https".equalsIgnoreCase(scheme)
65                        boolean includePort = true
66                        if (inHttp && (serverPort == 80)) {
67                                includePort = false
68                        }
69                        else if (inHttps && (serverPort == 443)) {
70                                includePort = false
71                        }
72                        String port = includePort ? ":" + serverPort : ""
73                        return "${scheme}://${request.serverName}${port}${request.contextPath}${url}"
74                }
75
76                return url
77        }
78}
Note: See TracBrowser for help on using the repository browser.