com.cooksys.httpserver.IncomingHttpHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.cooksys.httpserver.IncomingHttpHandler.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cooksys.httpserver;

import com.cooksys.postmaster.PostmasterModelSingleton;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javafx.application.Platform;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;

/**
 *
 * @author Tim Davidson Implementation of HttpRequestHandler. handle method gets
 * called for all incoming http requests. It takes an HttpResonse parameter that
 * is sent back to the client.
 */
public class IncomingHttpHandler implements HttpRequestHandler {

    private PostmasterModelSingleton serverModel = PostmasterModelSingleton.getInstance();
    private String rawMessage = "";

    @Override
    public void handle(final HttpRequest request, final HttpResponse response, HttpContext context)
            throws HttpException, IOException {
        //put the entity stream into a string - sending this object to JavaFX runlater
        //queue causes the stream to close, so we will have to do it here
        final String messageBody;
        if (request instanceof BasicHttpEntityEnclosingRequest) {
            HttpEntity entity = ((BasicHttpEntityEnclosingRequest) request).getEntity();
            if (entity.getContentLength() < Integer.MAX_VALUE / 2) {
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                entity.writeTo(stream);
                messageBody = stream.toString();
            } else {
                messageBody = "Data too large";
            }
        } else {
            messageBody = "";
        }

        //Print the raw request message to the message console
        rawMessage = "\n\n";
        rawMessage += request.getRequestLine().toString() + "\n";

        Header[] headers = request.getAllHeaders();
        for (Header header : headers) {
            rawMessage += header.getName() + ": " + header.getValue() + "\n";
        }
        rawMessage += "\n";
        rawMessage += messageBody;

        //get the default response from the model, and copy it to the already provided HttpResponse parameter
        HttpResponse defaultResponse = serverModel.getResponseList().get(serverModel.getDefaultResponseIndex())
                .encodeResponse();

        response.setStatusLine(defaultResponse.getStatusLine());
        response.setEntity(defaultResponse.getEntity());
        response.setHeaders(defaultResponse.getAllHeaders());

        System.out.println("sending response -> " + response.toString());
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                //update the model with the console message
                String originalConsole = serverModel.getMessageConsole().getValue();
                serverModel.getMessageConsole()
                        .set(originalConsole == null ? rawMessage : originalConsole + rawMessage);
            }
        });

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                //update the model with the new message
                serverModel.incomingRequest(request, messageBody);
            }
        });
        System.out.println("handle() end");
    }

}