1 package com.atlassian.seraph.util; 2 3 import com.atlassian.seraph.config.SecurityConfig; 4 import com.atlassian.seraph.config.SecurityConfigFactory; 5 import com.atlassian.seraph.filter.SecurityFilter; 6 7 import javax.servlet.http.HttpServletRequest ; 8 import java.net.URLEncoder ; 9 10 13 public class RedirectUtils 14 { 15 16 37 public static String getLoginUrl(HttpServletRequest request) 38 { 39 SecurityConfig securityConfig = SecurityConfigFactory.getInstance(); 40 String loginURL = securityConfig.getLoginURL(); 41 return getLoginURL(loginURL, request); 42 } 43 44 51 public static String getLinkLoginURL(HttpServletRequest request) 52 { 53 SecurityConfig securityConfig = SecurityConfigFactory.getInstance(); 54 String loginURL = securityConfig.getLinkLoginURL(); 55 return getLoginURL(loginURL, request); 56 } 57 58 59 private static String getLoginURL(String loginURL, HttpServletRequest request) 60 { 61 boolean externalLoginLink = isExternalLoginLink(loginURL); 62 loginURL = replaceOriginalURL(loginURL, request, externalLoginLink); 63 if (externalLoginLink) 64 { 65 return loginURL; 66 } 67 else 68 { 69 return request.getContextPath() + loginURL; 70 } 71 } 72 73 private static boolean isExternalLoginLink(String loginURL) 74 { 75 return (loginURL.indexOf("://") != -1); 76 } 77 78 81 private static String replaceOriginalURL(final String loginURL, final HttpServletRequest request, boolean external) 82 { 83 final int i = loginURL.indexOf("${originalurl}"); 84 if (i != -1) 85 { 86 final String originalURL = getOriginalURL(request, external); 87 return loginURL.substring(0, i) + URLEncoder.encode(originalURL) + loginURL.substring(i + "${originalurl}".length()); 88 } 89 else 90 return loginURL; 91 } 92 93 96 private static String getOriginalURL(HttpServletRequest request, boolean external) 97 { 98 String originalURL = (String ) request.getAttribute(SecurityFilter.ORIGINAL_URL); 99 if (originalURL != null) 100 { 101 if (external) 102 return getServerNameAndPath(request) + originalURL; 103 else 104 return originalURL; 105 } 106 107 if (external) 108 return request.getRequestURL() + (request.getQueryString() == null ? "" : "?" + request.getQueryString()); 109 else 110 return request.getServletPath() + 111 (request.getPathInfo() == null ? "" : request.getPathInfo()) + 112 (request.getQueryString() == null ? "" : "?" + request.getQueryString()); 113 114 } 115 116 public static String getServerNameAndPath(HttpServletRequest request) 117 { 118 return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); 119 } 120 } 121 | Popular Tags |