Sunday, April 5, 2009

A weekend With Jetty

Jetty is a pretty good HTTP server for purposes of embedding. This weekend I have been working on an application (existing in Java) that will be publishing web services for consumption by a client application that I am developing (in RoR).

First things first I tempted to set up authentication on my server because lets face it this thing will be living on the scary internet. The documentation on the jetty website shows you how to set up your real from a configuration file on the system. In my case I have the username and password set up in a different configuration and I did not want my users to set this up again in yet another configuration. In simple terms here is what I ended up using for my real setup.




server = new Server();
Connector connector=new SocketConnector();
connector.setPort(SERVER_PORT);
server.setConnectors(new Connector[]{connector});

Constraint constraint = new Constraint();
constraint.setName(Constraint.__BASIC_AUTH);;
constraint.setRoles(new String[]{"admin"});
constraint.setAuthenticate(true);

ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(constraint);
cm.setPathSpec("/*");

SecurityHandler sh = new SecurityHandler();
//read username and password from non jetty conf source
HashUserRealm realm = new HashUserRealm("admin");
realm.put("admin", "test");
realm.addUserToRole("admin", "admin");
sh.setUserRealm(realm);
sh.setConstraintMappings(new ConstraintMapping[]{cm});

server.setHandlers(new Handler[]{sh, new MyHTTPHandler()});
server.start();
server.join();



In ruby all I had to do in my model is:


self.site = "http://admin:test@localhost:8080/"

0 comments: