324377 Allow dispatch of ServletRequest and ServletResponse

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2243 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2010-09-03 07:05:14 +00:00
parent 995df89b22
commit 097d770200
7 changed files with 353 additions and 9 deletions

View File

@ -24,6 +24,7 @@ jetty-7.2-SNAPSHOT
+ 324359 illegal actions on AsyncContext should not change its state.
+ 324360 validate input on getResource since loop logic obscures subclass input validation.
+ 324369 Implement draft-ietf-hybi-thewebsocketprotocol-01
+ 324377 Allow dispatch of ServletRequest and ServletResponse
+ JETTY-912 added per exchange timeout api
+ JETTY-1245 Do not use direct buffers with NIO SSL
+ JETTY-1249 Apply max idle time to all connectors

View File

@ -13,9 +13,6 @@
package org.eclipse.jetty.plus.servlet;
import javax.servlet.Filter;
import javax.servlet.Servlet;
import org.eclipse.jetty.plus.annotation.InjectionCollection;
import org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection;

View File

@ -25,6 +25,7 @@ import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.handler.ContextHandler;
@ -133,6 +134,12 @@ public class Dispatcher implements RequestDispatcher
Request baseRequest=(request instanceof Request)?((Request)request):HttpConnection.getCurrentConnection().getRequest();
request.removeAttribute(__JSP_FILE); // TODO remove when glassfish 1044 is fixed
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();
@ -158,7 +165,7 @@ public class Dispatcher implements RequestDispatcher
}
MultiMap parameters=new MultiMap();
UrlEncoded.decodeTo(query,parameters,request.getCharacterEncoding());
UrlEncoded.decodeTo(query,parameters,baseRequest.getCharacterEncoding());
if (old_params!=null && old_params.size()>0)
{
@ -211,6 +218,11 @@ public class Dispatcher implements RequestDispatcher
base_response.fwdReset();
request.removeAttribute(__JSP_FILE); // TODO remove when glassfish 1044 is fixed
if (!(request instanceof HttpServletRequest))
request = new ServletRequestHttpWrapper(request);
if (!(response instanceof HttpServletResponse))
response = new ServletResponseHttpWrapper(response);
final String old_uri=baseRequest.getRequestURI();
final String old_context_path=baseRequest.getContextPath();
final String old_servlet_path=baseRequest.getServletPath();

View File

@ -0,0 +1,162 @@
// ========================================================================
// Copyright (c) 2010 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.security.Principal;
import java.util.Enumeration;
import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestWrapper;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/* ------------------------------------------------------------ */
/** Class to tunnel a ServletRequest via a HttpServletRequest
*/
public class ServletRequestHttpWrapper extends ServletRequestWrapper implements HttpServletRequest
{
public ServletRequestHttpWrapper(ServletRequest request)
{
super(request);
}
public String getAuthType()
{
return null;
}
public Cookie[] getCookies()
{
return null;
}
public long getDateHeader(String name)
{
return 0;
}
public String getHeader(String name)
{
return null;
}
public Enumeration getHeaders(String name)
{
return null;
}
public Enumeration getHeaderNames()
{
return null;
}
public int getIntHeader(String name)
{
return 0;
}
public String getMethod()
{
return null;
}
public String getPathInfo()
{
return null;
}
public String getPathTranslated()
{
return null;
}
public String getContextPath()
{
return null;
}
public String getQueryString()
{
return null;
}
public String getRemoteUser()
{
return null;
}
public boolean isUserInRole(String role)
{
return false;
}
public Principal getUserPrincipal()
{
return null;
}
public String getRequestedSessionId()
{
return null;
}
public String getRequestURI()
{
return null;
}
public StringBuffer getRequestURL()
{
return null;
}
public String getServletPath()
{
return null;
}
public HttpSession getSession(boolean create)
{
return null;
}
public HttpSession getSession()
{
return null;
}
public boolean isRequestedSessionIdValid()
{
return false;
}
public boolean isRequestedSessionIdFromCookie()
{
return false;
}
public boolean isRequestedSessionIdFromURL()
{
return false;
}
public boolean isRequestedSessionIdFromUrl()
{
return false;
}
}

View File

@ -0,0 +1,94 @@
package org.eclipse.jetty.server;
import java.io.IOException;
import javax.servlet.ServletResponse;
import javax.servlet.ServletResponseWrapper;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
/* ------------------------------------------------------------ */
/** Wrapper to tunnel a ServletResponse via a HttpServletResponse
*/
public class ServletResponseHttpWrapper extends ServletResponseWrapper implements HttpServletResponse
{
public ServletResponseHttpWrapper(ServletResponse response)
{
super(response);
}
public void addCookie(Cookie cookie)
{
}
public boolean containsHeader(String name)
{
return false;
}
public String encodeURL(String url)
{
return null;
}
public String encodeRedirectURL(String url)
{
return null;
}
public String encodeUrl(String url)
{
return null;
}
public String encodeRedirectUrl(String url)
{
return null;
}
public void sendError(int sc, String msg) throws IOException
{
}
public void sendError(int sc) throws IOException
{
}
public void sendRedirect(String location) throws IOException
{
}
public void setDateHeader(String name, long date)
{
}
public void addDateHeader(String name, long date)
{
}
public void setHeader(String name, String value)
{
}
public void addHeader(String name, String value)
{
}
public void setIntHeader(String name, int value)
{
}
public void addIntHeader(String name, int value)
{
}
public void setStatus(int sc)
{
}
public void setStatus(int sc, String sm)
{
}
}

View File

@ -21,7 +21,6 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.jetty.server.DispatcherType;
@ -48,6 +47,8 @@ import org.eclipse.jetty.server.Dispatcher;
import org.eclipse.jetty.server.HttpConnection;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServletRequestHttpWrapper;
import org.eclipse.jetty.server.ServletResponseHttpWrapper;
import org.eclipse.jetty.server.UserIdentity;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ScopedHandler;
@ -128,6 +129,7 @@ public class ServletHandler extends ScopedHandler
}
/* ----------------------------------------------------------------- */
@Override
protected synchronized void doStart()
throws Exception
{
@ -154,6 +156,7 @@ public class ServletHandler extends ScopedHandler
}
/* ----------------------------------------------------------------- */
@Override
protected synchronized void doStop()
throws Exception
{
@ -406,6 +409,7 @@ public class ServletHandler extends ScopedHandler
/*
* @see org.eclipse.jetty.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
*/
@Override
public void doHandle(String target, Request baseRequest,HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
@ -435,19 +439,26 @@ public class ServletHandler extends ScopedHandler
try
{
// Do the filter/handling thang
if (servlet_holder==null)
{
notFound(request, response);
}
else
{
baseRequest.setHandled(true);
// unwrap any tunnelling of base Servlet request/responses
ServletRequest req = request;
if (req instanceof ServletRequestHttpWrapper)
req = ((ServletRequestHttpWrapper)req).getRequest();
ServletResponse res = response;
if (res instanceof ServletResponseHttpWrapper)
res = ((ServletResponseHttpWrapper)res).getResponse();
// Do the filter/handling thang
baseRequest.setHandled(true);
if (chain!=null)
chain.doFilter(request, response);
chain.doFilter(req, res);
else
servlet_holder.handle(baseRequest,request,response);
servlet_holder.handle(baseRequest,req,res);
}
}
catch(EofException e)

View File

@ -17,9 +17,15 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.servlet.GenericServlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestWrapper;
import javax.servlet.ServletResponse;
import javax.servlet.ServletResponseWrapper;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -131,6 +137,40 @@ public class DispatcherTest
assertEquals(expected, responses);
}
@Test
public void testServletForward() throws Exception
{
_context.addServlet(DispatchServletServlet.class, "/dispatch/*");
_context.addServlet(RogerThatServlet.class, "/roger/*");
String expected=
"HTTP/1.1 200 OK\r\n"+
"Content-Length: 11\r\n"+
"\r\n"+
"Roger That!";
String responses = _connector.getResponses("GET /context/dispatch/test?forward=/roger/that HTTP/1.0\n" + "Host: localhost\n\n");
assertEquals(expected, responses);
}
@Test
public void testServletInclude() throws Exception
{
_context.addServlet(DispatchServletServlet.class, "/dispatch/*");
_context.addServlet(RogerThatServlet.class, "/roger/*");
String expected=
"HTTP/1.1 200 OK\r\n"+
"Content-Length: 11\r\n"+
"\r\n"+
"Roger That!";
String responses = _connector.getResponses("GET /context/dispatch/test?include=/roger/that HTTP/1.0\n" + "Host: localhost\n\n");
assertEquals(expected, responses);
}
public static class ForwardServlet extends HttpServlet implements Servlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
@ -147,8 +187,26 @@ public class DispatcherTest
}
}
public static class DispatchServletServlet extends HttpServlet implements Servlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
RequestDispatcher dispatcher = null;
if(request.getParameter("include")!=null)
dispatcher = getServletContext().getRequestDispatcher(request.getParameter("include"));
else if(request.getParameter("forward")!=null)
dispatcher = getServletContext().getRequestDispatcher(request.getParameter("forward"));
dispatcher.forward(new ServletRequestWrapper(request), new ServletResponseWrapper(response));
}
}
public static class IncludeServlet extends HttpServlet implements Servlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
RequestDispatcher dispatcher = null;
@ -163,6 +221,15 @@ public class DispatcherTest
}
}
public static class RogerThatServlet extends GenericServlet
{
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
{
res.getWriter().print("Roger That!");
}
}
public static class AssertForwardServlet extends HttpServlet implements Servlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException