Search in sources :

Example 1 with Status

use of com.bluenimble.platform.api.ApiResponse.Status in project serverless by bluenimble.

the class AbstractApiServer method execute.

@Override
public void execute(final ApiRequest request, final ApiResponse response, CodeExecutor.Mode mode) {
    if (keys.expiryDate() != null && keys.expiryDate().before(new Date())) {
        sendError(response, ApiResponse.FORBIDDEN, "node keys expired");
        return;
    }
    ApiSpace space = null;
    Api api = null;
    try {
        if (!(request instanceof ContainerApiRequest)) {
            requestVisitor.visit((AbstractApiRequest) request);
        }
        // is space resolved
        if (Lang.isNullOrEmpty(request.getSpace())) {
            sendError(response, ApiResponse.NOT_FOUND, "can't resolve space from request");
            request.destroy();
            return;
        }
        if (Lang.isNullOrEmpty(request.getApi())) {
            sendError(response, ApiResponse.NOT_FOUND, "can't resolve api namespace from request");
            request.destroy();
            return;
        }
        space = space(request.getSpace());
        ApiResponse.Status notFoundStatus = null;
        String notFoundMessage = null;
        if (space == null) {
            notFoundStatus = ApiResponse.NOT_FOUND;
            notFoundMessage = "space " + request.getSpace() + " not found";
        } else if (!space.isStarted()) {
            notFoundStatus = ApiResponse.SERVICE_UNAVAILABLE;
            notFoundMessage = "space " + request.getSpace() + " is not available";
        }
        if (notFoundStatus != null) {
            sendError(response, notFoundStatus, notFoundMessage);
            request.destroy();
            return;
        }
        api = space.api(request.getApi());
        if (api == null) {
            notFoundStatus = ApiResponse.NOT_FOUND;
            notFoundMessage = "api " + request.getApi() + " not found";
        } else if (api.status() != ApiStatus.Running) {
            notFoundStatus = ApiResponse.SERVICE_UNAVAILABLE;
            notFoundMessage = "api " + request.getApi() + " stopped or paused";
        }
        if (notFoundStatus != null) {
            sendError(response, notFoundStatus, notFoundMessage);
            request.destroy();
            return;
        }
    } catch (Exception ex) {
        tracer.log(Tracer.Level.Error, Lang.BLANK, ex);
        request.destroy();
        sendError(response, ApiResponse.BAD_REQUEST, ex.getMessage());
        return;
    }
    final Api fApi = api;
    try {
        space.executor().execute(new Callable<Void>() {

            @Override
            public Void call() {
                if (Thread.currentThread() instanceof SpaceThread) {
                    final SpaceThread currentThread = (SpaceThread) Thread.currentThread();
                    currentThread.setRequest(request);
                }
                try {
                    interceptor.intercept(fApi, request, response);
                } finally {
                    if (Thread.currentThread() instanceof SpaceThread) {
                        ((SpaceThread) Thread.currentThread()).setRequest(null);
                    }
                }
                return null;
            }
        }, mode);
    } catch (CodeExecutorException aaee) {
        Throwable e = aaee.getCause();
        String error = "Generic";
        ApiResponse.Status status = ApiResponse.BAD_REQUEST;
        if (e.getClass().equals(CancellationException.class)) {
            status = ApiResponse.INSUFFICIENT_SPACE_ON_RESOURCE;
            error = "Cancellation";
        } else if (e.getClass().equals(TimeoutException.class)) {
            status = ApiResponse.REQUEST_TIMEOUT;
            error = "Timeout";
        }
        tracer.log(Tracer.Level.Error, "\tCallback-OnError: ThreadGroup [" + Thread.currentThread().getThreadGroup().getName() + "], Thread [" + Thread.currentThread().getName() + "], " + error + " Error: " + e.getMessage(), e);
        sendError(response, status, error + " Error | " + e.getMessage());
        request.destroy();
    }
}
Also used : Status(com.bluenimble.platform.api.ApiResponse.Status) ApiStatus(com.bluenimble.platform.api.ApiStatus) ContainerApiRequest(com.bluenimble.platform.api.impls.ContainerApiRequest) Date(java.util.Date) ContainerApiResponse(com.bluenimble.platform.api.impls.ContainerApiResponse) ApiResponse(com.bluenimble.platform.api.ApiResponse) TimeoutException(java.util.concurrent.TimeoutException) CancellationException(java.util.concurrent.CancellationException) CodeExecutorException(com.bluenimble.platform.api.CodeExecutorException) FeatureNotFoundException(com.bluenimble.platform.server.FeatureNotFoundException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) IOException(java.io.IOException) Status(com.bluenimble.platform.api.ApiResponse.Status) ApiSpace(com.bluenimble.platform.api.ApiSpace) SpaceThread(com.bluenimble.platform.api.impls.SpaceThread) CancellationException(java.util.concurrent.CancellationException) Api(com.bluenimble.platform.api.Api) CodeExecutorException(com.bluenimble.platform.api.CodeExecutorException)

Example 2 with Status

use of com.bluenimble.platform.api.ApiResponse.Status in project serverless by bluenimble.

the class MediaRoutingUtils method processMedia.

public static JsonObject processMedia(final ApiRequest request, ApiResponse response, Lang.VariableResolver vr, JsonObject mediaDef, Tracer tracer) {
    if (mediaDef == null || (!mediaDef.containsKey(Success) && !mediaDef.containsKey(Error))) {
        return null;
    }
    String selectProcessor = (String) request.get(ApiRequest.MediaSelector);
    if (Lang.isNullOrEmpty(selectProcessor)) {
        selectProcessor = Lang.STAR;
    }
    JsonObject mediaSelections = null;
    if (response.getError() == null) {
        mediaSelections = Json.getObject(mediaDef, Success);
    } else {
        mediaSelections = Json.getObject(mediaDef, Error);
        if (mediaSelections == null) {
            mediaSelections = Json.getObject(mediaDef, Success);
        }
    }
    JsonObject media = Json.getObject(mediaSelections, selectProcessor);
    if (media != null) {
        int status = Json.getInteger(media, Status, 0);
        if (status > 0) {
            response.setStatus(new Status(status, Lang.BLANK));
        }
        JsonObject headers = Json.getObject(media, Headers);
        if (headers != null && !headers.isEmpty()) {
            Iterator<String> names = headers.keys();
            while (names.hasNext()) {
                String name = names.next();
                Object hv = headers.get(name);
                if (hv == null) {
                    hv = Lang.BLANK;
                }
                if (hv instanceof JsonArray) {
                    JsonArray arr = (JsonArray) hv;
                    if (arr.isEmpty()) {
                        continue;
                    }
                    List<String> values = new ArrayList<String>();
                    for (int i = 0; i < arr.count(); i++) {
                        values.add(Lang.resolve(String.valueOf(arr.get(i)), vr));
                    }
                    response.set(name, values);
                } else {
                    hv = Lang.resolve(hv.toString(), vr);
                    if (XLocation.toLowerCase().equals(name.toLowerCase())) {
                        if (status <= 0) {
                            response.setStatus(ApiResponse.MOVED_PERMANENTLY);
                        }
                        response.set(ApiHeaders.Location, hv);
                    }
                    if (ApiHeaders.Location.toLowerCase().equals(name.toLowerCase())) {
                        if (status <= 0) {
                            response.setStatus(ApiResponse.MOVED_PERMANENTLY);
                        }
                        response.set(ApiHeaders.Location, ApiUtils.apiEndpoint(request) + hv);
                    } else {
                        response.set(name, hv);
                    }
                }
            }
        }
    }
    return media;
}
Also used : Status(com.bluenimble.platform.api.ApiResponse.Status) JsonArray(com.bluenimble.platform.json.JsonArray) ArrayList(java.util.ArrayList) JsonObject(com.bluenimble.platform.json.JsonObject) JsonObject(com.bluenimble.platform.json.JsonObject)

Aggregations

Status (com.bluenimble.platform.api.ApiResponse.Status)2 Api (com.bluenimble.platform.api.Api)1 ApiResponse (com.bluenimble.platform.api.ApiResponse)1 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)1 ApiSpace (com.bluenimble.platform.api.ApiSpace)1 ApiStatus (com.bluenimble.platform.api.ApiStatus)1 CodeExecutorException (com.bluenimble.platform.api.CodeExecutorException)1 ContainerApiRequest (com.bluenimble.platform.api.impls.ContainerApiRequest)1 ContainerApiResponse (com.bluenimble.platform.api.impls.ContainerApiResponse)1 SpaceThread (com.bluenimble.platform.api.impls.SpaceThread)1 JsonArray (com.bluenimble.platform.json.JsonArray)1 JsonObject (com.bluenimble.platform.json.JsonObject)1 FeatureNotFoundException (com.bluenimble.platform.server.FeatureNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 CancellationException (java.util.concurrent.CancellationException)1 TimeoutException (java.util.concurrent.TimeoutException)1