1 2 38 39 40 41 42 43 package com.lutris.appserver.server.httpPresentation; 44 45 import java.io.IOException ; 46 import java.net.MalformedURLException ; 47 import java.net.URL ; 48 49 import javax.servlet.RequestDispatcher ; 50 import javax.servlet.http.HttpServletRequest ; 51 import javax.servlet.http.HttpUtils ; 52 53 import com.lutris.appserver.server.StandardAppUtil; 54 import com.lutris.logging.LogChannel; 55 import com.lutris.logging.Logger; 56 57 64 class PageRedirect { 65 72 protected static String handler(HttpPresentationComms comms, 73 String presObjPath, 74 PageRedirectException redirectExcept, 75 LogChannel logChannel, 76 long requestId) 77 throws MalformedURLException , 78 HttpPresentationException, 79 IOException { 80 81 boolean debug = logChannel.isEnabled(Logger.DEBUG); 82 83 if (debug) 85 logChannel.write(Logger.DEBUG, "RID:" + requestId + ": " 86 + redirectExcept.getClass().getName() 87 + ": " + presObjPath + " => " 88 + redirectExcept.getUrl()); 89 String url = redirectExcept.getUrl(); 90 if (redirectExcept instanceof ClientPageRedirectException) { 91 return clientSideRedirect(url,comms,logChannel,requestId, redirectExcept.getEncoding()); 92 } else { 93 RequestDispatcher rd = comms.request.getHttpServletRequest().getRequestDispatcher(url); 94 try { 95 rd.forward(comms.request.getHttpServletRequest(), 96 comms.response.getHttpServletResponse()); 97 98 } catch (Exception 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 e) { 105 throw new HttpPresentationException(e); 106 } 107 } 108 return null; 109 110 } 111 } 112 113 114 115 private static String clientSideRedirect(String url, 116 HttpPresentationComms comms, 117 LogChannel logChannel, 118 long requestId, 119 String 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 makeAbsolute(String location, 147 HttpServletRequest request) { 148 URL url = null; 149 try { 150 url = new URL (location); 153 } 154 catch (MalformedURLException e) { 155 String requrl = HttpUtils.getRequestURL(request).toString(); 156 try { 157 url = new URL (new URL (requrl), location); 158 } 159 catch (MalformedURLException ignored) { 160 return location; 162 } 163 } 164 return url.toString(); 165 } 166 } 167 | Popular Tags |