Search in sources :

Example 1 with HTTP404V1

use of water.schemas.HTTP404V1 in project h2o-2 by h2oai.

the class RequestServer method serve.

@Override
public NanoHTTPD.Response serve(String uri, String method, Properties header, Properties parms) {
    // Jack priority for user-visible requests
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY - 1);
    // update arguments and determine control variables
    uri = maybeTransformRequest(uri);
    // determine the request type
    Request.RequestType type = Request.RequestType.requestType(uri);
    String requestName = type.requestName(uri);
    maybeLogRequest(uri, method, parms, header);
    // determine version
    int version = parseVersion(uri);
    int idx = version >> 16;
    version &= 0xFFFF;
    String uripath = uri.substring(idx);
    // Strip suffix type from middle of URI
    String path = requestName(uripath);
    Method meth = null;
    try {
        // Find handler for url
        meth = lookup(method, path);
        if (meth != null) {
            return wrap(HTTP_OK, handle(type, meth, version, parms), type);
        }
    } catch (IllegalArgumentException e) {
        return wrap(HTTP_BADREQUEST, new HTTP404V1(e.getMessage(), uri), type);
    } catch (Exception e) {
        // make sure that no Exception is ever thrown out from the request
        return wrap(e.getMessage() != "unimplemented" ? HTTP_INTERNALERROR : HTTP_NOTIMPLEMENTED, new HTTP500V1(e), type);
    }
    // Wasn't a new type of handler:
    try {
        // determine if we have known resource
        Request request = _requests.get(requestName);
        // found
        if (request == null)
            return getResource(uri);
        // Some requests create an instance per call
        request = request.create(parms);
        // call the request
        return request.serve(this, parms, type);
    } catch (Exception e) {
        if (!(e instanceof ExpectedExceptionForDebug))
            e.printStackTrace();
        // make sure that no Exception is ever thrown out from the request
        parms.setProperty(Request.ERROR, e.getClass().getSimpleName() + ": " + e.getMessage());
        return _http500.serve(this, parms, type);
    }
}
Also used : ExpectedExceptionForDebug(water.util.Utils.ExpectedExceptionForDebug) HTTP500V1(water.schemas.HTTP500V1) Method(java.lang.reflect.Method) IOException(java.io.IOException) HTTP404V1(water.schemas.HTTP404V1)

Aggregations

IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 HTTP404V1 (water.schemas.HTTP404V1)1 HTTP500V1 (water.schemas.HTTP500V1)1 ExpectedExceptionForDebug (water.util.Utils.ExpectedExceptionForDebug)1