Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 1997-2012 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 
 package com.sun.enterprise.web;
 
 
 import java.net.URL;
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.
 
 public class VirtualServerPipeline extends StandardPipeline {
 
     private static final Logger logger = .....;
 
     private static final ResourceBundle rb = .getResourceBundle();
 
     @LogMessageInfo(
             message = "Virtual server {0} has been turned off",
             level = "FINE")
     public static final String VS_VALVE_OFF = "AS-WEB-00320";
 
     @LogMessageInfo(
             message = "Virtual server {0} has been disabled",
             level = "FINE")
     public static final String VS_VALVE_DISABLED = "AS-WEB-00321";
 
     @LogMessageInfo(
             message = "Invalid redirect URL [{0}]: Impossible to URL encode",
             level = "WARNING")
     public static final String INVALID_REDIRECTION_LOCATION = "AS-WEB-00322";
 
     private VirtualServer vs;
 
     private boolean isOff;
     private boolean isDisabled;
 
 
     private ConcurrentLinkedQueue<CharChunklocations;

    
Constructor.

Parameters:
vs Virtual server with which this VirtualServerPipeline is being associated
    public VirtualServerPipeline(VirtualServer vs) {
        super(vs);
        this. = vs;
         = new ConcurrentLinkedQueue<CharChunk>();
    }

    
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.

Parameters:
request The request to process
response The response to return
    public void invoke(Request requestResponse response)
            throws IOExceptionServletException {
        if () {
            String msg = .getString();
            msg = MessageFormat.format(msgnew Object[] { .getName() });
            if (.isLoggable(.)) {
                .log(.msg);
            }
            ((HttpServletResponseresponse.getResponse()).sendError(
                                            .,
                                            msg);
        } else if () {
            String msg = .getString();
            msg = MessageFormat.format(msgnew Object[] { .getName() });
            if (.isLoggable(.)) {
                .log(.msg);
            }
            ((HttpServletResponseresponse.getResponse()).sendError(
                                            .,
                                            msg);
        } else {
            boolean redirect = false;
            if ( != null) {
                redirect = redirectIfNecessary(requestresponse);
            }
            if (!redirect) {
                super.invoke(requestresponse);
            }
        }
    }


    
Sets the disabled state of this VirtualServerPipeline.

Parameters:
isDisabled true if the associated virtual server has been disabled, false otherwise
    void setIsDisabled(boolean isDisabled) {
        this. = isDisabled;
    }


    
Sets the off state of this VirtualServerPipeline.

Parameters:
isOff true if the associated virtual server is off, false otherwise
    void setIsOff(boolean isOff) {
        this. = isOff;
    }


    
Adds the given redirect instruction to this VirtualServerPipeline.

Parameters:
from URI prefix to match
url Redirect URL to return to the client
urlPrefix New URL prefix to return to the client
escape true if redirect URL returned to the client is to be escaped, false otherwise
    void addRedirect(String fromString urlString urlPrefix,
                     boolean escape) {
        if ( == null) {
             = new ArrayList<RedirectParameters>();
        }
        .add(new RedirectParameters(fromurlurlPrefixescape));
    }


    

Returns:
true if this VirtualServerPipeline has any redirects configured, and false otherwise.
    boolean hasRedirects() {
        return (( != null) && (.size() > 0));
    }


    
Clears all redirects.
    void clearRedirects() {
        if ( != null) {
            .clear();
        }
    }


    
Checks to see if the given request needs to be redirected.

Parameters:
request The request to process
response The response to return
Returns:
true if redirect has occurred, false otherwise
    private boolean redirectIfNecessary(Request requestResponse response)
            throws IOException {
        if ( == null) {
            return false;
        }
   
        HttpServletRequest hreq = (HttpServletRequestrequest.getRequest();
        HttpServletResponse hres = (HttpServletResponserequest.getResponse();
        String requestURI = hreq.getRequestURI();
        RedirectParameters redirectMatch = null;
        // Determine the longest 'from' URI prefix match
        int size = .size();
        for (int i=0; i<sizei++) {
            RedirectParameters elem = .get(i);
            String elemFromWithTrailingSlash = elem.from;
            if (!elemFromWithTrailingSlash.endsWith("/")) {
                elemFromWithTrailingSlash += "/";
            }
            if (requestURI.equals(elem.from) ||
                    requestURI.startsWith(elemFromWithTrailingSlash)) {
                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;
                // START 6810361
                if (redirectMatch.urlPrefixPath != null &&
                        uriSuffix.startsWith(redirectMatch.urlPrefixPath)) {
                    return false;
                }
                // END 6810361
            }
            // START 6810361
            // Implements welcome page only redirection
            if ("".equals(redirectMatch.from)) {
                if (!("/".equals(requestURI))) return false;
            }
            // END 6810361
            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;
            if (redirectMatch.isEscape) {
                try {
                    URL url = new URL(location);
                    locationCC = .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()));
                    }
                    locationCC.append(response.encode(url.getPath()));
                    if (queryString != null) {
                        locationCC.append("?");
                        locationCC.append(url.getQuery());
                    }
                    location = locationCC.toString();
                } catch (MalformedURLException mue) {
                    if (redirectMatch.validURI) {
                        .log(.,
                            ,
                            location);
                    } else {
                        if (.isLoggable(.)) {
                            .log(.,
                                ,
                                location);
                        }
                    }
                } finally {
                    if (locationCC != null) {
                        locationCC.recycle();
                        .offer(locationCC);
                    }
                }
            }
            hres.sendRedirect(location);
            return true;
        }
        return false;
    }


    
Class representing redirect parameters
    static class RedirectParameters {
        private String from;
        private String url;
        private String urlPrefix;
        // START 6810361
        /*
         * The path portion of the urlPrefix, in case urlPrefix is
         * specified as an absolute URL (including protocol etc.)
         */
        private String urlPrefixPath;
        // END 6810361
        private boolean validURI;
        private boolean isEscape;
        RedirectParameters(String fromString urlString urlPrefix,
                           boolean isEscape) {
            this. = from;
            this. = url;
            this. = urlPrefix;
            this. = isEscape;
            this. = true;
            // START 6810361
            try {
                URL u = new URL(urlPrefix);
                 = u.getPath();
            } catch (MalformedURLException e) {
                 = urlPrefix;
                this. = false;
            }
            // END 6810361
        }
    }
New to GrepCode? Check out our FAQ X