protected void startServer() throws Exception
{
_server = new Server();
//SslSelectChannelConnector connector = new SslSelectChannelConnector();
SslSocketConnector connector = new SslSocketConnector();
String keystore = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator
+ "keystore";
connector.setPort(0);
connector.setKeystore(keystore);
connector.setPassword("storepwd");
connector.setKeyPassword("keypwd");
_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.err.println("passed authentication!\n"+((Request)request).getConnection().getRequestFields());
Request base_request = (request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest();
base_request.setHandled(true);
response.setStatus(200);
response.setContentType("text/plain");
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();
}