Search in sources :

Example 1 with HTTP500V1

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

the class Handler method handle.

// Invoke the handler with parameters.  Can throw any exception the called handler can throw.
protected final Schema handle(int version, Method meth, Properties parms) throws Exception {
    if (// Version check!
    !(min_ver() <= version && version <= max_ver()))
        return new HTTP500V1(new IllegalArgumentException("Version " + version + " is not in range V" + min_ver() + "-V" + max_ver()));
    // Make a version-specific Schema; primitive-parse the URL into the Schema,
    // fill the Handler from the versioned Schema.
    // Version-specific Schema
    S s = schema(version).fillFrom(parms).fillInto((H) this);
    // Run the Handler in the Nano Thread (nano does not grok CPS!)
    _t_start = System.currentTimeMillis();
    try {
        meth.invoke(this);
    }// rather uselessly.  Peel out the original exception & throw it.
     catch (InvocationTargetException ite) {
        Throwable t = ite.getCause();
        throw (t instanceof Exception) ? (Exception) t : new RuntimeException(t);
    }
    _t_stop = System.currentTimeMillis();
    // Version-specific unwind from the Handler back into the Schema
    return s.fillFrom((H) this);
}
Also used : HTTP500V1(water.schemas.HTTP500V1) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with HTTP500V1

use of water.schemas.HTTP500V1 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

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