FileDocCategorySizeDatePackage
VirtualServerPipeline.javaAPI DocGlassfish v2 API11274Fri May 04 22:36:02 BST 2007com.sun.enterprise.web

VirtualServerPipeline

public class VirtualServerPipeline extends org.apache.catalina.core.StandardPipeline
Pipeline associated with a virtual server. This pipeline inherits the state (off/disabled) of its associated virtual server, and will abort execution and return an appropriate response error code if its associated virtual server is off or disabled.

Fields Summary
private static final org.apache.catalina.util.StringManager
sm
private static final Logger
logger
private VirtualServer
vs
private boolean
isOff
private boolean
isDisabled
private ArrayList
redirects
private ConcurrentLinkedQueue
locations
private ConcurrentLinkedQueue
urlEncoders
Constructors Summary
public VirtualServerPipeline(VirtualServer vs)
Constructor.

param
vs Virtual server with which this VirtualServerPipeline is being associated


                     
       
        super(vs);
        this.vs = vs;
        locations = new ConcurrentLinkedQueue<CharChunk>();
        urlEncoders = new ConcurrentLinkedQueue<UEncoder>();
    
Methods Summary
voidaddRedirect(java.lang.String from, java.lang.String url, java.lang.String urlPrefix, boolean escape)
Adds the given redirect instruction to this VirtualServerPipeline.

param
from URI prefix to match
param
url Redirect URL to return to the client
param
urlPrefix New URL prefix to return to the client
param
escape true if redirect URL returned to the client is to be escaped, false otherwise


        if (redirects == null) {
            redirects = new ArrayList<RedirectParameters>();
        }

        redirects.add(new RedirectParameters(from, url, urlPrefix, escape));
    
voidclearRedirects()
Clears all redirects.

        if (redirects != null) {
            redirects.clear();
        }
    
booleanhasRedirects()

return
true if this VirtualServerPipeline has any redirects configured, and false otherwise.

        return ((redirects != null) && (redirects.size() > 0));
    
public voidinvoke(org.apache.catalina.Request request, org.apache.catalina.Response response)
Processes the specified request, and produces the appropriate response, by invoking the first valve (if any) of this pipeline, or the pipeline's basic valve.

param
request The request to process
param
response The response to return


        if (isOff) {
            String msg = sm.getString("virtualServerValve.vsOff",
                                      vs.getName());
            if (logger.isLoggable(Level.FINE)) {
                logger.fine(msg);
            }
            ((HttpServletResponse) response.getResponse()).sendError(
                                            HttpServletResponse.SC_NOT_FOUND,
                                            msg);
        } else if (isDisabled) {
            String msg = sm.getString("virtualServerValve.vsDisabled",
                                      vs.getName());
            if (logger.isLoggable(Level.FINE)) {
                logger.fine(msg);
            }
            ((HttpServletResponse) response.getResponse()).sendError(
                                            HttpServletResponse.SC_FORBIDDEN,
                                            msg);
        } else {
            boolean redirect = false;
            if (redirects != null) {
                redirect = redirectIfNecessary(request, response);
            }
            if (!redirect) {
                doInvoke(request, response);
            }
        }
    
private booleanredirectIfNecessary(org.apache.catalina.Request request, org.apache.catalina.Response response)
Checks to see if the given request needs to be redirected.

param
request The request to process
param
response The response to return
return
true if redirect has occurred, false otherwise


        if (redirects == null) {
            return false;
        }
   
        HttpServletRequest hreq = (HttpServletRequest) request.getRequest();
        HttpServletResponse hres = (HttpServletResponse) request.getResponse();
        String requestURI = hreq.getRequestURI();
        RedirectParameters redirectMatch = null;

        // Determine the longest 'from' URI prefix match
        int size = redirects.size();
        for (int i=0; i<size; i++) {
            RedirectParameters elem = redirects.get(i);
            if (requestURI.startsWith(elem.from)) {
                if (redirectMatch != null) {
                    if (elem.from.length() > redirectMatch.from.length()) {
                        redirectMatch = elem;
                    }
                } else {
                    redirectMatch = elem;
                }
            }
        }

        if (redirectMatch != null) {
            // Redirect prefix match found, need to redirect
            String location = null;
            String uriSuffix = requestURI.substring(
                            redirectMatch.from.length());
            if ("/".equals(redirectMatch.from)) {
                uriSuffix = "/" + uriSuffix;
            }
            if (redirectMatch.urlPrefix != null) {
                // Replace 'from' URI prefix with URL prefix
                location = redirectMatch.urlPrefix + uriSuffix;
            } else {
                // Replace 'from' URI prefix with complete URL
                location = redirectMatch.url;
            }
  
            String queryString = hreq.getQueryString();
            if (queryString != null) {
                location += queryString;
            }
     
            CharChunk locationCC = null;
            UEncoder urlEncoder = null;

            if (redirectMatch.isEscape) {
                try {
                    URL url = new URL(location);
                    locationCC = locations.poll();
                    if (locationCC == null) {
                        locationCC = new CharChunk();
                    }
                    locationCC.append(url.getProtocol());
                    locationCC.append("://");
                    locationCC.append(url.getHost());
                    if (url.getPort() != -1) {
                        locationCC.append(":");
                        locationCC.append(String.valueOf(url.getPort()));
                    }
                    urlEncoder = urlEncoders.poll();
                    if (urlEncoder == null){
                        urlEncoder = new UEncoder();
                        urlEncoder.addSafeCharacter('/");
                    }
                    locationCC.append(urlEncoder.encodeURL(url.getPath()));
                    location = locationCC.toString();
                } catch (MalformedURLException mue) {
                    logger.log(Level.WARNING,
                               "virtualServerPipeline.invalidRedirectLocation",
                               location);
                } finally {
                    if (urlEncoder != null) {
                        urlEncoders.offer(urlEncoder);
                    }
                    if (locationCC != null) {
                        locationCC.recycle();
                        locations.offer(locationCC);
                    }
                }
            }

            hres.sendRedirect(location);
            return true;
        }

        return false;
    
voidsetIsDisabled(boolean isDisabled)
Sets the disabled state of this VirtualServerPipeline.

param
isDisabled true if the associated virtual server has been disabled, false otherwise

        this.isDisabled = isDisabled;
    
voidsetIsOff(boolean isOff)
Sets the off state of this VirtualServerPipeline.

param
isOff true if the associated virtual server is off, false otherwise

        this.isOff = isOff;