All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.femo.http.drivers.server.HttpHandlerStack Maven / Gradle / Ivy

Go to download

An easy to use HTTP API, that supports synchronous and asynchronous execution of HTTP request. On android only asynchronous driver is supported.

There is a newer version: 0.0.3
Show newest version
package io.femo.http.drivers.server;

import io.femo.http.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by felix on 4/25/16.
 */
public class HttpHandlerStack {

    private Logger log = LoggerFactory.getLogger("HTTP");

    private List httpHandlerHandles;

    private List after;

    public HttpHandlerStack() {
        this.httpHandlerHandles = new ArrayList<>();
        this.after = new ArrayList<>();
    }

    public void submit(HttpHandle httpHandle) {
        this.httpHandlerHandles.add(httpHandle);
    }

    public void submitAfter(HttpMiddleware httpMiddleware) {
        after.add(httpMiddleware);
    }

    public boolean handle(HttpRequest httpRequest, HttpResponse httpResponse) {
        boolean handled = false;
        try {
            for (HttpHandle httpHandle : httpHandlerHandles) {
                if (httpHandle.matches(httpRequest)) {
                    try {
                        if (httpHandle.handle(httpRequest, httpResponse)) {
                            handled = true;
                            break;
                        }
                    } catch (HttpHandleException e) {
                        log.warn("Error while handling HTTP request", e);
                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                        e.printStackTrace(new PrintStream(byteArrayOutputStream));
                        httpResponse.status(e.getStatusCode());
                        httpResponse.entity(byteArrayOutputStream.toByteArray());
                        handled = true;
                        break;
                    }
                }
            }
        } catch (Throwable t) {
            log.error("Error while handling " + httpRequest.method() + " " + httpRequest.path(), t);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            t.printStackTrace(new PrintStream(byteArrayOutputStream));
            httpResponse.entity(byteArrayOutputStream.toByteArray());
            httpResponse.status(StatusCode.INTERNAL_SERVER_ERROR);
        }
        try {
            for (HttpMiddleware middleware : after) {
                try {
                    middleware.handle(httpRequest, httpResponse);
                } catch (HttpHandleException e) {
                    log.warn("Error while performing finalizing operations on HTTP request", e);
                }
            }
        } catch (Throwable t) {
            log.error("Error while finishing " + httpRequest.method() + " " + httpRequest.path(), t);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            t.printStackTrace(new PrintStream(byteArrayOutputStream));
            httpResponse.entity(byteArrayOutputStream.toByteArray());
            httpResponse.status(StatusCode.INTERNAL_SERVER_ERROR);
        }
        return handled;
    }

    public boolean matches(HttpRequest httpRequest) {
        for (HttpHandle httpHandle : httpHandlerHandles) {
            if (httpHandle.matches(httpRequest)) {
                return true;
            }
        }
        return false;
    }

    public void parentPath(String path) {
        httpHandlerHandles.forEach(httpHandle -> httpHandle.parentPath(path));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy