Search in sources :

Example 51 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class ElasticSearchIndexer method search.

@Override
public JsonObject search(JsonObject query, String[] entities) throws IndexerException {
    String types = Lang.BLANK;
    if (entities == null || entities.length == 0) {
        types = Lang.join(entities, Lang.COMMA);
    }
    tracer.log(Tracer.Level.Info, "search documents in [{0}] with query ", types.equals(Lang.BLANK) ? "All Entities" : Lang.join(entities), query);
    JsonObject result = new JsonObject();
    Error error = new Error();
    remote.get((JsonObject) new JsonObject().set(Remote.Spec.Endpoint, url + types + (types.equals(Lang.BLANK) ? Lang.BLANK : Lang.SLASH) + Internal.Elk.Search).set(Remote.Spec.Headers, new JsonObject().set(HttpHeaders.AUTHORIZATION, authToken).set(HttpHeaders.CONTENT_TYPE, ContentTypes.Json)).set(Remote.Spec.Data, new JsonObject().set(Internal.Elk.Source, Lang.encode(query.toString()))).set(Remote.Spec.Serializer, Serializer.Name.json), new Remote.Callback() {

        @Override
        public void onSuccess(int code, Object data) {
            if (data != null) {
                result.putAll((JsonObject) data);
            }
        }

        @Override
        public void onError(int code, Object message) {
            error.set(code, message);
        }
    });
    if (error.happened()) {
        throw new IndexerException("Error occured while calling Indexer: Code=" + error.code + ", Message: " + error.message);
    }
    return result;
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) Remote(com.bluenimble.platform.remote.Remote) JsonObject(com.bluenimble.platform.json.JsonObject) IndexerException(com.bluenimble.platform.indexer.IndexerException)

Example 52 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class ElasticSearchIndexer method get.

@Override
public JsonObject get(String entity, String id) throws IndexerException {
    if (Lang.isNullOrEmpty(id)) {
        throw new IndexerException("Document Id cannot be null.");
    }
    tracer.log(Tracer.Level.Info, "Lookup document [{0}]", id);
    JsonObject result = new JsonObject();
    Error error = new Error();
    remote.get((JsonObject) new JsonObject().set(Remote.Spec.Endpoint, url + entity + Lang.SLASH + id).set(Remote.Spec.Headers, new JsonObject().set(HttpHeaders.AUTHORIZATION, authToken)).set(Remote.Spec.Serializer, Serializer.Name.json), new Remote.Callback() {

        @Override
        public void onSuccess(int code, Object data) {
            if (data != null) {
                result.putAll((JsonObject) data);
            }
        }

        @Override
        public void onError(int code, Object message) {
            error.set(code, message);
        }
    });
    if (error.happened()) {
        throw new IndexerException("Error occured while calling Indexer: Code=" + error.code + ", Message: " + error.message);
    }
    return result;
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) Remote(com.bluenimble.platform.remote.Remote) IndexerException(com.bluenimble.platform.indexer.IndexerException) JsonObject(com.bluenimble.platform.json.JsonObject)

Example 53 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class PlainMediaProcessor method process.

@Override
public void process(Api api, ApiService service, ApiConsumer consumer, ApiOutput output, ApiRequest request, final ApiResponse response) throws ApiMediaException {
    String contentType = (String) request.get(ApiRequest.SelectedMedia);
    if (Lang.isNullOrEmpty(contentType) || Lang.STAR.equals(contentType)) {
        contentType = this.contentType;
    }
    String charset = Encodings.UTF8;
    JsonObject mediaDef = MediaRoutingUtils.pickMedia(api, service, contentType);
    try {
        String rContentType = contentType;
        if (output != null) {
            String oCharset = (String) output.get(ApiOutput.Defaults.Charset);
            if (!Lang.isNullOrEmpty(oCharset)) {
                charset = oCharset;
            }
        }
        rContentType += "; charset=" + charset;
        VariableResolver vr = new DefaultVariableResolver(request, output != null ? output.data() : null, output != null ? output.meta() : null);
        JsonObject media = MediaRoutingUtils.processMedia(request, response, vr, mediaDef, api.tracer());
        if (media != null) {
            // if there is a template
            String sTemplate = Json.getString(media, ApiService.Spec.Media.Resource);
            ApiResource template = null;
            if (!Lang.isNullOrEmpty(sTemplate)) {
                template = api.getResourcesManager().get(Lang.split(Lang.resolve(sTemplate, vr), Lang.SLASH));
            }
            if (template != null) {
                TemplateEngine engine = plugin.loockupEngine(api, Json.getString(media, ApiService.Spec.Media.Engine, MediaPlugin.HandlebarsEngine));
                response.set(ApiHeaders.ContentType, rContentType);
                response.flushHeaders();
                engine.write(consumer, request, response, output, template, media);
                response.close();
                return;
            }
        }
        if (WriteResponseUtils.writeError(response, api.tracer(), rContentType)) {
            response.close();
            return;
        }
        response.set(ApiHeaders.ContentType, rContentType);
        DataWriter dataWriter = writers.get(contentType);
        if (dataWriter == null) {
            dataWriter = writers.get(this.contentType);
        }
        dataWriter.write(output, response);
        response.close();
    } catch (Exception e) {
        throw new ApiMediaException(e.getMessage(), e);
    }
}
Also used : TemplateEngine(com.bluenimble.platform.api.impls.media.engines.TemplateEngine) ApiResource(com.bluenimble.platform.api.ApiResource) DefaultVariableResolver(com.bluenimble.platform.server.plugins.media.utils.DefaultVariableResolver) JsonObject(com.bluenimble.platform.json.JsonObject) ApiMediaException(com.bluenimble.platform.api.ApiMediaException) DefaultVariableResolver(com.bluenimble.platform.server.plugins.media.utils.DefaultVariableResolver) VariableResolver(com.bluenimble.platform.Lang.VariableResolver) ApiMediaException(com.bluenimble.platform.api.ApiMediaException) DataWriter(com.bluenimble.platform.api.media.DataWriter)

Example 54 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class StreamMediaProcessor method process.

@Override
public void process(Api api, ApiService service, ApiConsumer consumer, ApiOutput output, ApiRequest request, ApiResponse response) throws ApiMediaException {
    String contentType = output != null ? output.contentType() : null;
    if (Lang.isNullOrEmpty(contentType)) {
        contentType = (String) request.get(ApiRequest.SelectedMedia);
    }
    if (Lang.isNullOrEmpty(contentType) || Lang.STAR.equals(contentType)) {
        contentType = ApiContentTypes.Stream;
    }
    // if error
    if (WriteResponseUtils.writeError(response, api.tracer(), contentType)) {
        try {
            response.close();
        } catch (Exception e) {
            throw new ApiMediaException(e.getMessage(), e);
        }
        return;
    }
    // if no output
    if (output == null) {
        response.setStatus(ApiResponse.NOT_FOUND);
        response.flushHeaders();
        try {
            response.close();
        } catch (Exception e) {
            throw new ApiMediaException(e.getMessage(), e);
        }
        return;
    }
    // process cache
    if (processCache(request, response, output)) {
        response.flushHeaders();
        try {
            response.close();
        } catch (Exception e) {
            throw new ApiMediaException(e.getMessage(), e);
        }
        return;
    }
    JsonObject mediaDef = MediaRoutingUtils.pickMedia(api, service, contentType);
    response.set(ApiHeaders.ContentType, contentType);
    VariableResolver vr = new DefaultVariableResolver(request, output != null ? output.data() : null, output != null ? output.meta() : null);
    JsonObject media = MediaRoutingUtils.processMedia(request, response, vr, mediaDef, api.tracer());
    ApiResource resource = null;
    if (media != null) {
        // if there is an associated resource
        String sResource = Json.getString(media, Resource);
        if (!Lang.isNullOrEmpty(sResource)) {
            try {
                resource = api.getResourcesManager().get(Lang.split(Lang.resolve(sResource, vr), Lang.SLASH));
            } catch (ApiResourcesManagerException ex) {
                throw new ApiMediaException(ex.getMessage(), ex);
            }
        }
    }
    OutputStream ros = null;
    try {
        ros = response.toOutput();
        // if a resource attached
        if (resource != null) {
            response.flushHeaders();
            resource.pipe(ros, 0, -1);
            return;
        }
        // write content or ranges if requested
        writeRanges(request, response, ros, output, contentType);
    } catch (Exception ex) {
        throw new ApiMediaException(ex.getMessage(), ex);
    } finally {
        try {
            response.close();
        } catch (IOException e) {
        }
    }
}
Also used : ApiResource(com.bluenimble.platform.api.ApiResource) DefaultVariableResolver(com.bluenimble.platform.server.plugins.media.utils.DefaultVariableResolver) OutputStream(java.io.OutputStream) JsonObject(com.bluenimble.platform.json.JsonObject) ApiMediaException(com.bluenimble.platform.api.ApiMediaException) IOException(java.io.IOException) DefaultVariableResolver(com.bluenimble.platform.server.plugins.media.utils.DefaultVariableResolver) VariableResolver(com.bluenimble.platform.Lang.VariableResolver) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) ApiMediaException(com.bluenimble.platform.api.ApiMediaException) ParseException(java.text.ParseException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) IOException(java.io.IOException)

Example 55 with JsonObject

use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.

the class WriteResponseUtils method writeError.

public static boolean writeError(ApiResponse response, Tracer tracer, String contentType) {
    JsonObject error = response.getError();
    if (error == null) {
        return false;
    }
    response.set(ApiHeaders.ContentType, contentType);
    try {
        response.write(Lang.isDebugMode() ? error.toString(2) : error.toString());
    } catch (Exception ex) {
        tracer.log(Tracer.Level.Error, Lang.BLANK, ex);
    }
    return true;
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject)

Aggregations

JsonObject (com.bluenimble.platform.json.JsonObject)230 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)40 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)37 JsonArray (com.bluenimble.platform.json.JsonArray)37 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)34 Database (com.bluenimble.platform.db.Database)29 ApiSpace (com.bluenimble.platform.api.ApiSpace)26 File (java.io.File)25 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)23 Map (java.util.Map)22 IOException (java.io.IOException)20 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)17 JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)16 InputStream (java.io.InputStream)14 Date (java.util.Date)14 DefaultCommandResult (com.bluenimble.platform.cli.command.impls.DefaultCommandResult)13 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)12 DefaultDatabaseObjectSerializer (com.bluenimble.platform.db.impls.DefaultDatabaseObjectSerializer)11 HashMap (java.util.HashMap)11 DatabaseException (com.bluenimble.platform.db.DatabaseException)9