1 23 24 package com.sun.enterprise.web; 25 26 import java.io.IOException ; 27 import java.util.ArrayList ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 import java.net.URL ; 31 import java.net.MalformedURLException ; 32 import java.util.concurrent.ConcurrentLinkedQueue ; 33 34 import javax.servlet.ServletException ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 38 import org.apache.catalina.Request; 39 import org.apache.catalina.Response; 40 import org.apache.catalina.core.StandardPipeline; 41 import org.apache.catalina.util.StringManager; 42 import org.apache.tomcat.util.buf.UEncoder; 43 import org.apache.tomcat.util.buf.CharChunk; 44 45 import com.sun.logging.LogDomains; 46 47 54 public class VirtualServerPipeline extends StandardPipeline { 55 56 private static final StringManager sm = 57 StringManager.getManager("com.sun.enterprise.web"); 58 59 private static final Logger logger = 60 LogDomains.getLogger(LogDomains.WEB_LOGGER); 61 62 private VirtualServer vs; 63 64 private boolean isOff; 65 private boolean isDisabled; 66 67 private ArrayList <RedirectParameters> redirects; 68 69 private ConcurrentLinkedQueue <CharChunk> locations; 70 private ConcurrentLinkedQueue <UEncoder> urlEncoders; 71 72 78 public VirtualServerPipeline(VirtualServer vs) { 79 super(vs); 80 this.vs = vs; 81 locations = new ConcurrentLinkedQueue <CharChunk>(); 82 urlEncoders = new ConcurrentLinkedQueue <UEncoder>(); 83 } 84 85 93 public void invoke(Request request, Response response) 94 throws IOException , ServletException { 95 96 if (isOff) { 97 String msg = sm.getString("virtualServerValve.vsOff", 98 vs.getName()); 99 if (logger.isLoggable(Level.FINE)) { 100 logger.fine(msg); 101 } 102 ((HttpServletResponse ) response.getResponse()).sendError( 103 HttpServletResponse.SC_NOT_FOUND, 104 msg); 105 } else if (isDisabled) { 106 String msg = sm.getString("virtualServerValve.vsDisabled", 107 vs.getName()); 108 if (logger.isLoggable(Level.FINE)) { 109 logger.fine(msg); 110 } 111 ((HttpServletResponse ) response.getResponse()).sendError( 112 HttpServletResponse.SC_FORBIDDEN, 113 msg); 114 } else { 115 boolean redirect = false; 116 if (redirects != null) { 117 redirect = redirectIfNecessary(request, response); 118 } 119 if (!redirect) { 120 doInvoke(request, response); 121 } 122 } 123 } 124 125 126 132 void setIsDisabled(boolean isDisabled) { 133 this.isDisabled = isDisabled; 134 } 135 136 137 143 void setIsOff(boolean isOff) { 144 this.isOff = isOff; 145 } 146 147 148 157 void addRedirect(String from, String url, String urlPrefix, 158 boolean escape) { 159 160 if (redirects == null) { 161 redirects = new ArrayList <RedirectParameters>(); 162 } 163 164 redirects.add(new RedirectParameters(from, url, urlPrefix, escape)); 165 } 166 167 168 176 private boolean redirectIfNecessary(Request request, Response response) 177 throws IOException { 178 179 if (redirects == null) { 180 return false; 181 } 182 183 HttpServletRequest hreq = (HttpServletRequest ) request.getRequest(); 184 HttpServletResponse hres = (HttpServletResponse ) request.getResponse(); 185 String requestURI = hreq.getRequestURI(); 186 RedirectParameters redirectMatch = null; 187 188 int size = redirects.size(); 190 for (int i=0; i<size; i++) { 191 RedirectParameters elem = redirects.get(i); 192 if (requestURI.startsWith(elem.from)) { 193 if (redirectMatch != null) { 194 if (elem.from.length() > redirectMatch.from.length()) { 195 redirectMatch = elem; 196 } 197 } else { 198 redirectMatch = elem; 199 } 200 } 201 } 202 203 if (redirectMatch != null) { 204 String location = null; 206 String uriSuffix = requestURI.substring( 207 redirectMatch.from.length()); 208 if ("/".equals(redirectMatch.from)) { 209 uriSuffix = "/" + uriSuffix; 210 } 211 if (redirectMatch.urlPrefix != null) { 212 location = redirectMatch.urlPrefix + uriSuffix; 214 } else { 215 location = redirectMatch.url; 217 } 218 219 String queryString = hreq.getQueryString(); 220 if (queryString != null) { 221 location += queryString; 222 } 223 224 CharChunk locationCC = null; 225 UEncoder urlEncoder = null; 226 227 if (redirectMatch.isEscape) { 228 try { 229 URL url = new URL (location); 230 locationCC = locations.poll(); 231 if (locationCC == null) { 232 locationCC = new CharChunk(); 233 } 234 locationCC.append(url.getProtocol()); 235 locationCC.append("://"); 236 locationCC.append(url.getHost()); 237 if (url.getPort() != -1) { 238 locationCC.append(":"); 239 locationCC.append(String.valueOf(url.getPort())); 240 } 241 urlEncoder = urlEncoders.poll(); 242 if (urlEncoder == null){ 243 urlEncoder = new UEncoder(); 244 urlEncoder.addSafeCharacter('/'); 245 } 246 locationCC.append(urlEncoder.encodeURL(url.getPath())); 247 location = locationCC.toString(); 248 } catch (MalformedURLException mue) { 249 logger.log(Level.WARNING, 250 "virtualServerPipeline.invalidRedirectLocation", 251 location); 252 } finally { 253 if (urlEncoder != null) { 254 urlEncoders.offer(urlEncoder); 255 } 256 if (locationCC != null) { 257 locationCC.recycle(); 258 locations.offer(locationCC); 259 } 260 } 261 } 262 263 hres.sendRedirect(location); 264 return true; 265 } 266 267 return false; 268 } 269 270 271 274 static class RedirectParameters { 275 276 private String from; 277 private String url; 278 private String urlPrefix; 279 private boolean isEscape; 280 281 RedirectParameters(String from, String url, String urlPrefix, 282 boolean isEscape) { 283 this.from = from; 284 this.url = url; 285 this.urlPrefix = urlPrefix; 286 this.isEscape = isEscape; 287 } 288 } 289 290 } 291 | Popular Tags |