| 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 | */ |
|---|
| 15 | package org.codehaus.groovy.grails.plugins.springsecurity |
|---|
| 16 | |
|---|
| 17 | import javax.servlet.http.HttpServletRequest |
|---|
| 18 | import javax.servlet.http.HttpServletResponse |
|---|
| 19 | |
|---|
| 20 | import org.springframework.security.util.PortResolver |
|---|
| 21 | import 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 | */ |
|---|
| 27 | class 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 | } |
|---|