Tiny Web server

Simple REST based web service. This library is used in Use your smartphone or tablet as virtual keyboard and mouse project. This thin library gives you access to all resources. Even parameters. For that Request object has methods:

  • boolean:┬áhasParamater(String key);
    if parameter is set
  • String: getParameter(String key);
    returns parameter if exists or value is not null
  • Integer: getParameterAsInteger(String key);
    returns parameter as Integer or null if is not numeric.
  • Float: getParameterAsFloat(String key);
    returns parameter as Float or null if is not numeric.
Usage:
import java.io.IOException;
import java.net.UnknownHostException;

import ee.fj.http.Request;
import ee.fj.http.Response;
import ee.fj.http.ServerStateListener;
import ee.fj.http.Response.STATUS;
import ee.fj.http.Response.TYPE;
import ee.fj.http.ResponseHandler;
import ee.fj.http.Server;

public class Service {
	public static void main(String... args) throws UnknownHostException, IOException {
		final Server server = new Server(new ResponseHandler() {
			@Override
			public void handle(Request request, Response response) throws Exception {
				response.getWriter().append("<html><head><title></title></head><body>")
				.append("<h1>It works</h1>")
				.append("<p><img src=\"logo.png\"/></p>")
				.append("<p><a href=\"loremipsum\">Lorem ipsum</a></p>")
				.append("<p><a href=\"loremipsum?alter\">Alter Lorem ipsum</a></p>")
				.append("<p><a href=\"loremipsum?alter=new\">Alter Lorem ipsum</a></p>")
				.append("<p><a href=\"service\">Service status</a></p>")
				.append("<p><a href=\"service?method=stop\">Stop by parameter</a></p>")
				.append("<p><a href=\"service/stop\">Stop service</a></p>")
				.append("</body></html>");
				response.setHeader(true, STATUS.OK);
			}
		});

		server.root.bind("logo.png", new ResponseHandler() {
			@Override
			public void handle(Request request, Response response) throws Exception {
				response.stream("image/png", Service.class.getResourceAsStream("logo.png"));
			}
		});
		server.root.bind("loremipsum", new ResponseHandler() {
			@Override
			public void handle(Request request, Response response) throws Exception {
				response.stream(TYPE.HTML, Service.class.getResourceAsStream("text.html"));
			}
		});
		final ResponseHandler stop = new ResponseHandler() {
			@Override
			public void handle(Request request, Response response) throws Exception {
				response.getWriter().append("<html><body>Stopping sercice</body></HTML>");
				response.setHeader(true, STATUS.OK);
				server.stop();
			}
		};
		server.root.bind("service", new ResponseHandler() {
			@Override
			public void handle(Request request, Response response) throws Exception {
				if (request.hasParamater("method") && "stop".equals(request.getParameter("method"))) {
					stop.handle(request, response);
				} else {
					response.getWriter().append("<html><body>Service Running</body></HTML>");
					response.setHeader(true, STATUS.OK);
				}
			}
		}).bind("stop", stop);

		// listen specific port
		server.listen(8080);

		server.addServerStateListener(new ServerStateListener() {
			@Override
			public void stopped() {
				System.out.println("Stopped");
			}
			
			@Override
			public void started() {
				System.out.println("Started");
			}
			
			@Override
			public void shuttingDown() {
				System.out.println("Shutting down...");
				
			}
		});

		server.start();
	}
}

Note that example listens all local addresses in port 80. Alternatively you can listen only remote addresses:

java.net.InetAddress[] addresses =  ee.fj.http.NetworkInterfaces.getAvailableAddresses(false);
server.listen(8080, addresses);

Or listen specific address

java.net.InetAddress[] addresses =  ee.fj.http.NetworkInterfaces.getAvailableAddresses(false);
server.listen(8080, addresses[0]);

Or even multiple addresses and ports

java.net.InetAddress[] addresses =  ee.fj.http.NetworkInterfaces.getAvailableAddresses(false);
server.listen(9090);
server.listen(8080, addresses[0]);
server.listen(9090, addresses[1]);
server.listen(8080, addresses[1]);
server.listen(8082, addresses[0], addresses[1], addresses[2],  addresses[3]);

Now your server paths defined:

  • my.server.com/
    prints “It works”, logo.png and lorem ipsum link
  • my.sever.com/logo.png
    shows logo image
  • my.sever.com/loremipsum
    prints text.html text
  • my.server.com/service
    shows service status. Also has additional parameter “method”. If its value is “stop” shuts down service.
  • my.server.com/service/stop
    shuts down the service
If path does not exist, for instance you try my.server.com/logo.png/logo2 then it fails back to closest match. For this example it shows logo.

Source code or compiled library can be downloaded from Sourceforge.

  • Facebook
  • Twitter
  • Google
  • Google Plus
  • LinkedIn
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Comments are closed.