Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.server;
 
 import java.util.Map;
 
 
 
 /* ------------------------------------------------------------ */
Servlet RequestDispatcher.
 
 public class Dispatcher implements RequestDispatcher
 {
    
Dispatch include attribute names
 
     public final static String __INCLUDE_PREFIX="javax.servlet.include.";

    
Dispatch include attribute names
 
     public final static String __FORWARD_PREFIX="javax.servlet.forward.";

    
JSP attributes
 
     public final static String __JSP_FILE="org.apache.catalina.jsp_file";
 
     /* ------------------------------------------------------------ */
     private final ContextHandler _contextHandler;
     private final String _uri;
     private final String _path;
     private final String _dQuery;
     private final String _named;
     
     /* ------------------------------------------------------------ */
    

Parameters:
contextHandler
uri
pathInContext
query
 
     public Dispatcher(ContextHandler contextHandlerString uriString pathInContextString query)
     {
         =contextHandler;
         =uri;
         =pathInContext;
         =query;
         =null;
     }
 
 
     /* ------------------------------------------------------------ */
    
Constructor.

Parameters:
contextHandler
name
 
     public Dispatcher(ContextHandler contextHandler,String name)
         throws IllegalStateException
     {
         =contextHandler;
         =name;
         =null;
         =null;
         =null;
     }
     
     /* ------------------------------------------------------------ */
     /* 
      * @see javax.servlet.RequestDispatcher#forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
     */
    public void forward(ServletRequest requestServletResponse responsethrows ServletExceptionIOException
    {
        forward(requestresponse.);
    }
    
    /* ------------------------------------------------------------ */
    /* 
     * @see javax.servlet.RequestDispatcher#forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
     */
    public void error(ServletRequest requestServletResponse responsethrows ServletExceptionIOException
    {
        forward(requestresponse.);
    }
    
    /* ------------------------------------------------------------ */
    /* 
     * @see javax.servlet.RequestDispatcher#include(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
     */
    public void include(ServletRequest requestServletResponse responsethrows ServletExceptionIOException
    {
        Request baseRequest=(request instanceof Request)?((Request)request):AbstractHttpConnection.getCurrentConnection().getRequest();
      
        
        if (!(request instanceof HttpServletRequest))
            request = new ServletRequestHttpWrapper(request);
        if (!(response instanceof HttpServletResponse))
            response = new ServletResponseHttpWrapper(response);
            
        
        // TODO - allow stream or writer????
        
        final DispatcherType old_type = baseRequest.getDispatcherType();
        final Attributes old_attr=baseRequest.getAttributes();
        MultiMap old_params=baseRequest.getParameters();
        try
        {
            baseRequest.setDispatcherType(.);
            baseRequest.getConnection().include();
            if (!=null)
                .handle(,baseRequest, (HttpServletRequest)request, (HttpServletResponse)response);
            else 
            {
                String query=;
                
                if (query!=null)
                {
                    // force parameter extraction
                    if (old_params==null)
                    {
                        baseRequest.extractParameters();
                        old_params=baseRequest.getParameters();
                    }
                    
                    MultiMap parameters=new MultiMap();
                    UrlEncoded.decodeTo(query,parameters,baseRequest.getCharacterEncoding());
                    
                    if (old_params!=null && old_params.size()>0)
                    {
                        // Merge parameters.
                        Iterator iter = old_params.entrySet().iterator();
                        while (iter.hasNext())
                        {
                            Map.Entry entry = (Map.Entry)iter.next();
                            String name=(String)entry.getKey();
                            Object values=entry.getValue();
                            for (int i=0;i<LazyList.size(values);i++)
                                parameters.add(name, LazyList.get(valuesi));
                        }
                    }
                    baseRequest.setParameters(parameters);
                }
                
                IncludeAttributes attr = new IncludeAttributes(old_attr); 
                
                attr._requestURI=;
                attr._contextPath=.getContextPath();
                attr._servletPath=null// set by ServletHandler
                attr._pathInfo=;
                attr._query=query;
                
                baseRequest.setAttributes(attr);
                
                .handle(,baseRequest, (HttpServletRequest)request, (HttpServletResponse)response);
            }
        }
        finally
        {
            baseRequest.setAttributes(old_attr);
            baseRequest.getConnection().included();
            baseRequest.setParameters(old_params);
            baseRequest.setDispatcherType(old_type);
        }
    }
    
    /* ------------------------------------------------------------ */
    /* 
     * @see javax.servlet.RequestDispatcher#forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
     */
    protected void forward(ServletRequest requestServletResponse responseDispatcherType dispatchthrows ServletExceptionIOException
    {
        Request baseRequest=(request instanceof Request)?((Request)request):AbstractHttpConnection.getCurrentConnection().getRequest();
        Response base_response=baseRequest.getResponse();
        response.resetBuffer();
        base_response.fwdReset();
       
        if (!(request instanceof HttpServletRequest))
            request = new ServletRequestHttpWrapper(request);
        if (!(response instanceof HttpServletResponse))
            response = new ServletResponseHttpWrapper(response);
        
        final boolean old_handled=baseRequest.isHandled();
        final String old_uri=baseRequest.getRequestURI();
        final String old_context_path=baseRequest.getContextPath();
        final String old_servlet_path=baseRequest.getServletPath();
        final String old_path_info=baseRequest.getPathInfo();
        final String old_query=baseRequest.getQueryString();
        final Attributes old_attr=baseRequest.getAttributes();
        final DispatcherType old_type=baseRequest.getDispatcherType();
        MultiMap<Stringold_params=baseRequest.getParameters();
        
        try
        {
            baseRequest.setHandled(false);
            baseRequest.setDispatcherType(dispatch);
            
            if (!=null)
                .handle(,baseRequest, (HttpServletRequest)request, (HttpServletResponse)response);
            else 
            {
                
                // process any query string from the dispatch URL
                String query=;
                if (query!=null)
                {
                    // force parameter extraction
                    if (old_params==null)
                    {
                        baseRequest.extractParameters();
                        old_params=baseRequest.getParameters();
                    }
                    
                    baseRequest.mergeQueryString(query);
                }
                
                ForwardAttributes attr = new ForwardAttributes(old_attr); 
                
                //If we have already been forwarded previously, then keep using the established 
                //original value. Otherwise, this is the first forward and we need to establish the values.
                //Note: the established value on the original request for pathInfo and
                //for queryString is allowed to be null, but cannot be null for the other values.
                if (old_attr.getAttribute() != null)
                {
                    attr._pathInfo=(String)old_attr.getAttribute();
                    attr._query=(String)old_attr.getAttribute();
                    attr._requestURI=(String)old_attr.getAttribute();
                    attr._contextPath=(String)old_attr.getAttribute();
                    attr._servletPath=(String)old_attr.getAttribute();
                }
                else
                {
                    attr._pathInfo=old_path_info;
                    attr._query=old_query;
                    attr._requestURI=old_uri;
                    attr._contextPath=old_context_path;
                    attr._servletPath=old_servlet_path;
                }     
                
                baseRequest.setRequestURI();
                baseRequest.setContextPath(.getContextPath());
                baseRequest.setServletPath(null);
                baseRequest.setPathInfo();
                baseRequest.setAttributes(attr);
                
                .handle(,baseRequest, (HttpServletRequest)request, (HttpServletResponse)response);
                
                if (!baseRequest.getAsyncContinuation().isAsyncStarted())
                    commitResponse(response,baseRequest);
            }
        }
        finally
        {
            baseRequest.setHandled(old_handled);
            baseRequest.setRequestURI(old_uri);
            baseRequest.setContextPath(old_context_path);
            baseRequest.setServletPath(old_servlet_path);
            baseRequest.setPathInfo(old_path_info);
            baseRequest.setAttributes(old_attr);
            baseRequest.setParameters(old_params);
            baseRequest.setQueryString(old_query);
            baseRequest.setDispatcherType(old_type);
        }
    }
    /* ------------------------------------------------------------ */
    private void commitResponse(ServletResponse responseRequest baseRequestthrows IOException
    {
        if (baseRequest.getResponse().isWriting())
        {
            try
            {
                response.getWriter().close();
            }
            catch (IllegalStateException e)
            {
                response.getOutputStream().close();
            }
        }
        else
        {
            try
            {
                response.getOutputStream().close();
            }
            catch (IllegalStateException e)
            {
                response.getWriter().close();
            }
        }
    }
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    private class ForwardAttributes implements Attributes
    {
        final Attributes _attr;
        
        String _requestURI;
        String _contextPath;
        String _servletPath;
        String _pathInfo;
        String _query;
        
        ForwardAttributes(Attributes attributes)
        {
            =attributes;
        }
        
        /* ------------------------------------------------------------ */
        public Object getAttribute(String key)
        {
            if (Dispatcher.this.==null)
            {
                if (key.equals())    
                    return ;
                if (key.equals())  
                    return ;
                if (key.equals()) 
                    return ;
                if (key.equals()) 
                    return ;
                if (key.equals()) 
                    return ;
            }
            
            if (key.startsWith())
                return null;
            
            return .getAttribute(key);
        }
        
        /* ------------------------------------------------------------ */
        public Enumeration getAttributeNames()
        {
            HashSet set=new HashSet();
            Enumeration e=.getAttributeNames();
            while(e.hasMoreElements())
            {
                String name=(String)e.nextElement();
                if (!name.startsWith() &&
                    !name.startsWith())
                    set.add(name);
            }
            
            if (==null)
            {
                if (!=null)
                    set.add();
                else
                    set.remove();
                set.add();
                set.add();
                set.add();
                if (!=null)
                    set.add();
                else
                    set.remove();
            }
            return Collections.enumeration(set);
        }
        
        /* ------------------------------------------------------------ */
        public void setAttribute(String keyObject value)
        {
            if (==null && key.startsWith("javax.servlet."))
            {
                if (key.equals())         
                    =(String)value;
                else if (key.equals())  
                    =(String)value;
                else if (key.equals()) 
                    =(String)value;
                else if (key.equals()) 
                    =(String)value;
                else if (key.equals()) 
                    =(String)value;
                
                else if (value==null)
                    .removeAttribute(key);
                else
                    .setAttribute(key,value); 
            }
            else if (value==null)
                .removeAttribute(key);
            else
                .setAttribute(key,value);
        }
        
        /* ------------------------------------------------------------ */
        @Override
        public String toString() 
        {
            return "FORWARD+"+.toString();
        }
        /* ------------------------------------------------------------ */
        public void clearAttributes()
        {
            throw new IllegalStateException();
        }
        /* ------------------------------------------------------------ */
        public void removeAttribute(String name)
        {
            setAttribute(name,null);
        }
    }
    /* ------------------------------------------------------------ */
    private class IncludeAttributes implements Attributes
    {
        final Attributes _attr;
        
        String _requestURI;
        String _contextPath;
        String _servletPath;
        String _pathInfo;
        String _query;
        
        IncludeAttributes(Attributes attributes)
        {
            =attributes;
        }
        
        /* ------------------------------------------------------------ */
        /* ------------------------------------------------------------ */
        /* ------------------------------------------------------------ */
        public Object getAttribute(String key)
        {
            if (Dispatcher.this.==null)
            {
                if (key.equals())    return ;
                if (key.equals()) return ;
                if (key.equals()) return ;
                if (key.equals()) return ;
                if (key.equals())  return ;
            }
            else if (key.startsWith()) 
                    return null;
            
            
            return .getAttribute(key);
        }
        
        /* ------------------------------------------------------------ */
        public Enumeration getAttributeNames()
        {
            HashSet set=new HashSet();
            Enumeration e=.getAttributeNames();
            while(e.hasMoreElements())
            {
                String name=(String)e.nextElement();
                if (!name.startsWith())
                    set.add(name);
            }
            
            if (==null)
            {
                if (!=null)
                    set.add();
                else
                    set.remove();
                set.add();
                set.add();
                set.add();
                if (!=null)
                    set.add();
                else
                    set.remove();
            }
            
            return Collections.enumeration(set);
        }
        
        /* ------------------------------------------------------------ */
        public void setAttribute(String keyObject value)
        {
            if (==null && key.startsWith("javax.servlet."))
            {
                if (key.equals())         =(String)value;
                else if (key.equals())  =(String)value;
                else if (key.equals()) =(String)value;
                else if (key.equals()) =(String)value;
                else if (key.equals()) =(String)value;
                else if (value==null)
                    .removeAttribute(key);
                else
                    .setAttribute(key,value); 
            }
            else if (value==null)
                .removeAttribute(key);
            else
                .setAttribute(key,value);
        }
        
        /* ------------------------------------------------------------ */
        @Override
        public String toString() 
        {
            return "INCLUDE+"+.toString();
        }
        /* ------------------------------------------------------------ */
        public void clearAttributes()
        {
            throw new IllegalStateException();
        }
        /* ------------------------------------------------------------ */
        public void removeAttribute(String name)
        {
            setAttribute(name,null);
        }
    }
New to GrepCode? Check out our FAQ X