private void startServer() throws Exception
{
_server = new Server();
_server.setGracefulShutdown(500);
Connector connector = new SelectChannelConnector();
connector.setPort(0);
_server.setConnectors(new Connector[]{connector});
UserRealm userRealm = new HashUserRealm("MyRealm", "src/test/resources/realm.properties");
Constraint constraint = new Constraint();
constraint.setName("Need User or Admin");
constraint.setRoles(new String[]{"user", "admin"});
constraint.setAuthenticate(true);
ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(constraint);
cm.setPathSpec("/*");
SecurityHandler sh = new SecurityHandler();
_server.setHandler(sh);
sh.setUserRealm(userRealm);
sh.setConstraintMappings(new ConstraintMapping[]{cm});
sh.setAuthenticator(new BasicAuthenticator());
Handler testHandler = new AbstractHandler()
{
public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException
{
System.out.println("passed authentication!");
Request base_request=(request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest();
base_request.setHandled(true);
response.setStatus(200);
if (request.getServerName().equals("jetty.mortbay.org"))
{
response.getOutputStream().println("Proxy request: "+request.getRequestURL());
}
else if (request.getMethod().equalsIgnoreCase("GET"))
{
response.getOutputStream().println("<hello>");
for (int i=0; i<100; i++)
{
response.getOutputStream().println(" <world>"+i+"</world>");
if (i%20==0)
response.getOutputStream().flush();
}
response.getOutputStream().println("</hello>");
}
else
{
copyStream(request.getInputStream(),response.getOutputStream());
}
}
};
sh.setHandler(testHandler);
_server.start();
_port = connector.getLocalPort();
}