Simple HTTP Logging Server in Java


This little server accepts connections and log the request details into a logging queue. It consumes the request and only returns “1” since it’s meant for fast logging. It can be used for collecting analytics from CURL requests or in-domain AJAX log requests.

import com.sun.net.httpserver.HttpServer;

// Initializing and starting the server
InetSocketAddress addr = new InetSocketAddress(8080);
HttpServer httpServer = HttpServer.create(addr, 0);
httpServer.createContext("/", new HttpLogHandler()); 
httpServer.setExecutor(Executors.newCachedThreadPool());


// Creating the handler
public class HttpLogHandler implements HttpHandler {
    @Override
    public void handle(HttpExchange exchange) throws IOException {
        String requestMethod = exchange.getRequestMethod();
        try {
            
            URI uri = null;
            
            if (requestMethod.equalsIgnoreCase("POST")) {
                Headers responseHeaders = exchange.getResponseHeaders();
                responseHeaders.set("Content-Type", "text/plain");

                InputStream bodyStream = exchange.getRequestBody();
                String requestBody = IOUtils.toString(bodyStream, "UTF-8");
                //requestBody = URLDecoder.decode(requestBody,"UTF-8");
                uri = URIUtils.createURI("http", "somehost", -1, "somepath", requestBody, null);
                
            } else if (requestMethod.equalsIgnoreCase("GET")) {
                String query = exchange.getRequestURI().getRawQuery();
                //System.out.println("query: " + query);
                uri = URIUtils.createURI("http", "somehost", -1, "somepath", query, null);
            }
            
            if(uri != null) {
                List params = URLEncodedUtils.parse(uri , "UTF-8");
                
                for(NameValuePair pair : params) {
                    if(pair.getName().equalsIgnoreCase("log")) {
                        System.out.println(pair.getName() + " - " + pair.getValue());
                        try {
                            JSONObject json = new JSONObject(pair.getValue());
                            RawLog log = new RawLog();
                            log.dataString = json.toString();
                            log.timestamp = new Date();
                            log.sourceAddress = exchange.getRemoteAddress().getAddress().getHostAddress();
                            log.sourceName = exchange.getRemoteAddress().getAddress().getHostName();
                            log.sourceType = "HTTP";
                            Main.messagesToLog.add(log);
                        } catch(Exception ex) {
                            ex.printStackTrace();
                        }
                        break;
                    }
                }
            }
            exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, 1);
            OutputStream responseBody = exchange.getResponseBody();
            responseBody.write("1".getBytes());
            responseBody.close();
            
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s