Search in sources :

Example 21 with JsonArray

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

the class SplitCommand method execute.

@Override
public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
    String str = (String) tool.currentContext().get(ToolContext.CommandLine);
    if (Lang.isNullOrEmpty(str)) {
        return null;
    }
    JsonArray arr = new JsonArray();
    Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(str);
    while (m.find()) {
        String s = m.group(1);
        if (s.startsWith(Lang.QUOT)) {
            s = s.substring(1);
        }
        if (s.endsWith(Lang.QUOT)) {
            s = s.substring(0, s.length() - 1);
        }
        arr.add(s);
    }
    return new DefaultCommandResult(CommandResult.OK, arr);
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) Matcher(java.util.regex.Matcher)

Example 22 with JsonArray

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

the class ApiImpl method describe.

@Override
public JsonObject describe(final DescribeOption... options) {
    if (options == null || options.length == 0) {
        return JsonObject.Blank;
    }
    final Map<DescribeOption.Option, DescribeOption> opts = DescribeUtils.toMap(options);
    JsonObject describe = new JsonObject();
    if (opts.containsKey(DescribeOption.Option.info)) {
        describe.set(Api.Spec.Namespace, getNamespace());
        describe.set(Api.Spec.Name, getName());
        describe.set(Api.Spec.Description, getDescription());
        describe.set(Api.Spec.Status, status().name());
        if (failure != null) {
            describe.set(Describe.Failure, failure);
        }
        describe.set(Api.Spec.Release, getRelease());
    }
    if (getSecurity() != null && opts.containsKey(DescribeOption.Option.security)) {
        describe.set(Api.Spec.Security.class.getSimpleName().toLowerCase(), getSecurity().duplicate());
    }
    if (getTracking() != null && opts.containsKey(DescribeOption.Option.tracking)) {
        describe.set(Api.Spec.Tracking.class.getSimpleName().toLowerCase(), getTracking().duplicate());
    }
    if (getFeatures() != null && opts.containsKey(DescribeOption.Option.features)) {
        describe.set(Api.Spec.Features, getFeatures().duplicate());
    }
    if (getCustom() != null && opts.containsKey(DescribeOption.Option.custom)) {
        describe.set(Api.Spec.Custom, getCustom().duplicate());
    }
    if (servicesManager.isEmpty(null)) {
        return describe;
    }
    final JsonObject failedServices = new JsonObject();
    describe.set(Describe.FailedServices, failedServices);
    JsonArray aServices = new JsonArray();
    describe.set(Describe.Services, aServices);
    final JsonArray fServices = aServices;
    final ValueHolder<Integer> apiMarkers = new ValueHolder<Integer>(0);
    servicesManager.list(new Selector() {

        @Override
        public boolean select(ApiService service) {
            if (opts.containsKey(DescribeOption.Option.services)) {
                DescribeOption opt = opts.get(DescribeOption.Option.services);
                JsonObject jService = service.toJson();
                JsonObject sDesc = jService;
                if (!opt.isVerbose()) {
                    sDesc = new JsonObject();
                    sDesc.set(ApiService.Spec.Name, jService.get(ApiService.Spec.Name));
                    sDesc.set(ApiService.Spec.Endpoint, jService.get(ApiService.Spec.Endpoint));
                    sDesc.set(ApiService.Spec.Verb, jService.get(ApiService.Spec.Verb));
                    sDesc.set(ApiService.Spec.Status, jService.get(ApiService.Spec.Status));
                    sDesc.set(ApiService.Spec.Security.class.getSimpleName().toLowerCase(), service.getSecurity());
                    JsonArray aMarkers = Json.getArray(jService, Api.Spec.Markers);
                    int markers = aMarkers != null ? aMarkers.size() : 0;
                    apiMarkers.set(apiMarkers.get() + markers);
                    if (markers > 0) {
                        sDesc.set(Api.Spec.Markers, markers);
                    }
                }
                fServices.add(sDesc);
            }
            if (ApiStatus.Failed.equals(service.status())) {
                failedServices.put(service.getVerb().name() + Lang.SPACE + Json.getString(service.toJson(), ApiService.Spec.Endpoint), service.getFailure());
            }
            return false;
        }
    });
    describe.set(Api.Spec.Markers, apiMarkers.get());
    if (fServices.isEmpty()) {
        describe.remove(Describe.Services);
    }
    if (failedServices.isEmpty()) {
        describe.remove(Describe.FailedServices);
    }
    return describe;
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) ValueHolder(com.bluenimble.platform.ValueHolder) JsonArray(com.bluenimble.platform.json.JsonArray) ApiService(com.bluenimble.platform.api.ApiService) DescribeOption(com.bluenimble.platform.api.DescribeOption) DescribeOption(com.bluenimble.platform.api.DescribeOption) Selector(com.bluenimble.platform.api.ApiServicesManager.Selector)

Example 23 with JsonArray

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

the class ApiSpaceImpl method describe.

@Override
public JsonObject describe(DescribeOption... options) {
    if (options == null || options.length == 0) {
        return JsonObject.Blank;
    }
    Map<DescribeOption.Option, DescribeOption> opts = DescribeUtils.toMap(options);
    JsonObject describe = new JsonObject();
    if (opts.containsKey(DescribeOption.Option.info)) {
        describe.set(ApiSpace.Spec.Namespace, getNamespace());
        describe.set(ApiSpace.Spec.Name, getName());
        describe.set(ApiSpace.Spec.Description, getDescription());
        describe.set(Describe.Status, isStarted() ? ApiStatus.Running.name() : ApiStatus.Stopped.name());
        describe.set(ApiSpace.Spec.Blocked, isBlocked());
        if (opts.size() == 1) {
            return describe;
        }
    }
    descriptor = descriptor.duplicate();
    if (opts.containsKey(DescribeOption.Option.keys) && keystore != null) {
        List<KeyPair> keys = null;
        try {
            keys = keystore.list(0, 100);
        } catch (SpaceKeyStoreException e) {
            tracer.log(Tracer.Level.Error, Lang.BLANK, e);
        }
        JsonArray aKeys = new JsonArray();
        if (keys != null) {
            for (KeyPair kp : keys) {
                JsonObject okp = kp.toJson().duplicate();
                okp.remove(KeyPair.Fields.SecretKey);
                aKeys.add(okp);
            }
        }
        describe.set(DescribeOption.Option.keys.name(), aKeys);
    }
    if (opts.containsKey(DescribeOption.Option.secrets)) {
        describe.set(DescribeOption.Option.secrets.name(), descriptor.get(Spec.secrets.class.getSimpleName()));
    }
    if (opts.containsKey(DescribeOption.Option.features)) {
        describe.set(DescribeOption.Option.features.name(), descriptor.get(Spec.Features));
    }
    if (opts.containsKey(DescribeOption.Option.runtime)) {
        describe.set(DescribeOption.Option.runtime.name(), descriptor.get(RuntimeKey));
    }
    if (opts.containsKey(DescribeOption.Option.apis)) {
        final JsonArray aApis = new JsonArray();
        describe.set(DescribeOption.Option.apis.name(), aApis);
        list(new Selector() {

            @Override
            public boolean select(Api api) {
                aApis.add(api.describe(options));
                return false;
            }
        });
    }
    if (opts.containsKey(DescribeOption.Option.workers) && executor != null) {
        describe.set(DescribeOption.Option.workers.name(), executor.describe());
    }
    return describe;
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) KeyPair(com.bluenimble.platform.security.KeyPair) SpaceKeyStoreException(com.bluenimble.platform.security.SpaceKeyStoreException) DescribeOption(com.bluenimble.platform.api.DescribeOption) JsonObject(com.bluenimble.platform.json.JsonObject) DescribeOption(com.bluenimble.platform.api.DescribeOption) Api(com.bluenimble.platform.api.Api)

Example 24 with JsonArray

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

the class QueryEntitySpi method execute.

@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String provider = (String) request.get(CommonSpec.Provider);
    String sEntity = (String) request.get(CommonSpec.Entity);
    String serializer = (String) request.get(CommonSpec.Serializer);
    String[] levels = Lang.split(serializer, Lang.COMMA);
    int allStopLevel = 2;
    if (levels != null && levels.length > 0) {
        try {
            allStopLevel = Integer.valueOf(levels[0]);
        } catch (Exception ex) {
        }
    }
    int minStopLevel = 3;
    if (levels != null && levels.length > 0) {
        try {
            minStopLevel = Integer.valueOf(levels[1]);
        } catch (Exception ex) {
        }
    }
    JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
    payload.set(Database.Fields.Entity, sEntity);
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    List<DatabaseObject> records = null;
    try {
        Database db = space.feature(Database.class, provider, request);
        records = db.find(null, new JsonQuery(payload), null);
    } catch (DatabaseException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    JsonObject result = new JsonObject();
    JsonArray aRecords = new JsonArray();
    result.set(Output.Records, aRecords);
    if (records == null || records.isEmpty()) {
        return new JsonApiOutput(result);
    }
    for (int i = 0; i < records.size(); i++) {
        aRecords.add(records.get(i).toJson(new DefaultDatabaseObjectSerializer(allStopLevel, minStopLevel)));
    }
    return new JsonApiOutput(result);
}
Also used : JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) JsonObject(com.bluenimble.platform.json.JsonObject) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) DatabaseException(com.bluenimble.platform.db.DatabaseException) JsonArray(com.bluenimble.platform.json.JsonArray) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) DefaultDatabaseObjectSerializer(com.bluenimble.platform.db.impls.DefaultDatabaseObjectSerializer) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Database(com.bluenimble.platform.db.Database) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) DatabaseException(com.bluenimble.platform.db.DatabaseException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 25 with JsonArray

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

the class ListKeysSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    Role cRole = Role.valueOf((String) consumer.get(CommonSpec.Role));
    int offset = (Integer) request.get(Spec.Offset);
    int length = (Integer) request.get(Spec.Length);
    String sFilters = (String) request.get(Spec.Filters);
    SpaceKeyStore.ListFilter[] filters = null;
    if (!Lang.isNullOrEmpty(sFilters)) {
        String[] aFilters = Lang.split(sFilters, Lang.COMMA, true);
        filters = new SpaceKeyStore.ListFilter[aFilters.length + 1];
        for (int i = 0; i < aFilters.length; i++) {
            String f = aFilters[i];
            int idexOfStartUnderscore = f.indexOf(Token);
            if (idexOfStartUnderscore < -1) {
                continue;
            }
            int idexOfEndUnderscore = f.indexOf(Token, idexOfStartUnderscore + 2);
            if (idexOfEndUnderscore < -1) {
                continue;
            }
            filters[i] = new SpaceKeyStore.ListFilter() {

                @Override
                public String name() {
                    return f.substring(0, idexOfStartUnderscore);
                }

                @Override
                public Object value() {
                    String value = f.substring(idexOfEndUnderscore + 2);
                    if (Lang.isNullOrEmpty(value)) {
                        return null;
                    }
                    return value;
                }

                @Override
                public Operator operator() {
                    try {
                        return Operator.valueOf(f.substring(idexOfStartUnderscore + 2, idexOfEndUnderscore));
                    } catch (Exception ex) {
                        return Operator.eq;
                    }
                }
            };
        }
    } else {
        filters = new SpaceKeyStore.ListFilter[1];
    }
    JsonObject result = new JsonObject();
    JsonArray aKeys = new JsonArray();
    result.set(Output.Keys, aKeys);
    if (Role.SUPER.equals(cRole)) {
        filters[filters.length - 1] = new SpaceKeyStore.ListFilter() {

            @Override
            public String name() {
                return CommonSpec.Role;
            }

            @Override
            public Object value() {
                return Role.ADMIN.name();
            }

            @Override
            public Operator operator() {
                return Operator.eq;
            }
        };
        try {
            Collection<ApiSpace> spaces = api.space().spaces();
            for (ApiSpace space : spaces) {
                addSpaceKeys(space, offset, length, filters, aKeys);
            }
        } catch (ApiAccessDeniedException e) {
            throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
        }
    } else {
        filters[filters.length - 1] = new SpaceKeyStore.ListFilter() {

            @Override
            public String name() {
                return CommonSpec.Role;
            }

            @Override
            public Object value() {
                return Role.DEVELOPER.name();
            }

            @Override
            public Operator operator() {
                return Operator.eq;
            }
        };
        ApiSpace consumerSpace;
        try {
            consumerSpace = MgmUtils.space(consumer, api);
        } catch (ApiAccessDeniedException e) {
            throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
        }
        addSpaceKeys(consumerSpace, offset, length, filters, aKeys);
    }
    return new JsonApiOutput(result);
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) Role(com.bluenimble.platform.apis.mgm.Role) JsonArray(com.bluenimble.platform.json.JsonArray) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) SpaceKeyStore(com.bluenimble.platform.security.SpaceKeyStore) JsonObject(com.bluenimble.platform.json.JsonObject) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Aggregations

JsonArray (com.bluenimble.platform.json.JsonArray)42 JsonObject (com.bluenimble.platform.json.JsonObject)37 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)9 Map (java.util.Map)8 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)6 ApiSpace (com.bluenimble.platform.api.ApiSpace)5 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)5 Database (com.bluenimble.platform.db.Database)5 IOException (java.io.IOException)5 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)4 ApiAuthenticationException (com.bluenimble.platform.api.security.ApiAuthenticationException)4 CommandExecutionException (com.bluenimble.platform.cli.command.CommandExecutionException)4 DefaultCommandResult (com.bluenimble.platform.cli.command.impls.DefaultCommandResult)4 ApiService (com.bluenimble.platform.api.ApiService)3 DescribeOption (com.bluenimble.platform.api.DescribeOption)3 DatabaseException (com.bluenimble.platform.db.DatabaseException)3 JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)3 KeyPair (com.bluenimble.platform.security.KeyPair)3 ArrayList (java.util.ArrayList)3 ApiOutput (com.bluenimble.platform.api.ApiOutput)2