Search in sources :

Example 21 with ApiSpace

use of com.bluenimble.platform.api.ApiSpace in project serverless by bluenimble.

the class FileSystemApiServer method stop.

@Override
public void stop() {
    tracer.log(Tracer.Level.Info, "Shutting down BlueNimble Node");
    Collection<ApiSpace> spaces = spaces();
    if (spaces != null) {
        for (ApiSpace space : spaces) {
            ((ApiSpaceImpl) space).shutdown();
        }
    }
    if (pluginsRegistry != null) {
        pluginsRegistry.shutdown();
    }
    if (keyStoreManager != null) {
        keyStoreManager.stop();
    }
    tracer.onShutdown(this);
}
Also used : ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiSpaceImpl(com.bluenimble.platform.api.impls.ApiSpaceImpl)

Example 22 with ApiSpace

use of com.bluenimble.platform.api.ApiSpace in project serverless by bluenimble.

the class ElasticSearchPlugin method init.

@Override
public void init(final ApiServer server) throws Exception {
    Feature aFeature = Indexer.class.getAnnotation(Feature.class);
    if (aFeature == null || Lang.isNullOrEmpty(aFeature.name())) {
        return;
    }
    feature = aFeature.name();
    server.addFeature(new ServerFeature() {

        private static final long serialVersionUID = -9012279234275100528L;

        @Override
        public Class<?> type() {
            return Indexer.class;
        }

        @Override
        public Object get(ApiSpace space, String name) {
            String remotePluginName = Json.getString(remote, Spec.RemotePlugin);
            String remoteObjectId = Json.getString(remote, Spec.RemoteObject);
            if (Lang.isNullOrEmpty(remotePluginName) || Lang.isNullOrEmpty(remoteObjectId)) {
                return null;
            }
            Plugin pRemote = server.getPluginsRegistry().lockup(Json.getString(remote, Spec.RemotePlugin));
            if (pRemote == null) {
                return null;
            }
            PackageClassLoader pcl = (PackageClassLoader) pRemote.getClass().getClassLoader();
            Remote iRemote = (Remote) pcl.lookupObject(Json.getString(remote, Spec.RemoteObject));
            if (iRemote == null) {
                return null;
            }
            // build basic auth and url
            String token = buildAuthToken(space, name);
            if (Lang.isNullOrEmpty(token)) {
                return null;
            }
            String url = buildUrl(space, name);
            if (Lang.isNullOrEmpty(url)) {
                return null;
            }
            return new ElasticSearchIndexer(iRemote, url, token, tracer());
        }

        @Override
        public String provider() {
            return ElasticSearchPlugin.this.getName();
        }

        @Override
        public Plugin implementor() {
            return ElasticSearchPlugin.this;
        }
    });
}
Also used : ServerFeature(com.bluenimble.platform.server.ServerFeature) ApiSpace(com.bluenimble.platform.api.ApiSpace) Remote(com.bluenimble.platform.remote.Remote) JsonObject(com.bluenimble.platform.json.JsonObject) Feature(com.bluenimble.platform.Feature) ServerFeature(com.bluenimble.platform.server.ServerFeature) PackageClassLoader(com.bluenimble.platform.PackageClassLoader) Plugin(com.bluenimble.platform.plugins.Plugin) AbstractPlugin(com.bluenimble.platform.plugins.impls.AbstractPlugin) ElasticSearchIndexer(com.bluenimble.platform.indexer.impls.ElasticSearchIndexer)

Example 23 with ApiSpace

use of com.bluenimble.platform.api.ApiSpace in project serverless by bluenimble.

the class DataSourcePlugin method onEvent.

@Override
public void onEvent(Event event, Object target) throws PluginRegistryException {
    if (!ApiSpace.class.isAssignableFrom(target.getClass())) {
        return;
    }
    tracer().log(Tracer.Level.Info, "onEvent {0}, target {1}", event, target.getClass().getSimpleName());
    ApiSpace space = (ApiSpace) target;
    switch(event) {
        case Create:
            createDataSources(space);
            break;
        case AddFeature:
            createDataSources(space);
            break;
        case DeleteFeature:
            dropDataSources(space);
            break;
        default:
            break;
    }
}
Also used : ApiSpace(com.bluenimble.platform.api.ApiSpace)

Example 24 with ApiSpace

use of com.bluenimble.platform.api.ApiSpace in project serverless by bluenimble.

the class ImportDatabaseSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String provider = (String) request.get(CommonSpec.Provider);
    String[] aEntities = Lang.split((String) request.get(Spec.Entities), Lang.COMMA, true);
    Set<String> entities = null;
    if (aEntities != null && aEntities.length > 0) {
        entities = new HashSet<String>();
        for (String entity : aEntities) {
            entities.add(entity.toUpperCase());
        }
    }
    String[] aOptions = Lang.split((String) request.get(Spec.Options), Lang.COMMA, true);
    Map<ExchangeOption, Boolean> options = null;
    if (aOptions != null && aOptions.length > 0) {
        for (String o : aOptions) {
            try {
                options.put(ExchangeOption.valueOf(o.toLowerCase()), true);
            } catch (Exception ex) {
            // ignore malformed options
            }
        }
    }
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    String file = (String) request.get(Spec.File);
    JsonObject result = new JsonObject();
    result.set(Spec.File, file);
    final StringBuilder sb = new StringBuilder();
    try {
        space.feature(Database.class, provider, request).imp(entities, space.feature(Storage.class, provider, request).root().get(file).reader(request), options, new Database.ExchangeListener() {

            @Override
            public void onMessage(String message) {
                sb.append(message).append(Lang.ENDLN);
            }
        });
    } catch (Exception e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    result.set(Output.Feedback, sb.toString());
    sb.setLength(0);
    return new JsonApiOutput(result);
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Database(com.bluenimble.platform.db.Database) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput) ExchangeOption(com.bluenimble.platform.db.Database.ExchangeOption)

Example 25 with ApiSpace

use of com.bluenimble.platform.api.ApiSpace 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)

Aggregations

ApiSpace (com.bluenimble.platform.api.ApiSpace)48 JsonObject (com.bluenimble.platform.json.JsonObject)33 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)31 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)27 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)27 Feature (com.bluenimble.platform.Feature)10 Plugin (com.bluenimble.platform.plugins.Plugin)10 ServerFeature (com.bluenimble.platform.server.ServerFeature)10 AbstractPlugin (com.bluenimble.platform.plugins.impls.AbstractPlugin)9 Database (com.bluenimble.platform.db.Database)8 DatabaseException (com.bluenimble.platform.db.DatabaseException)8 Storage (com.bluenimble.platform.storage.Storage)6 StorageException (com.bluenimble.platform.storage.StorageException)6 StorageObject (com.bluenimble.platform.storage.StorageObject)6 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)5 KeyPair (com.bluenimble.platform.security.KeyPair)5 Api (com.bluenimble.platform.api.Api)4 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)4 JsonArray (com.bluenimble.platform.json.JsonArray)4 PackageClassLoader (com.bluenimble.platform.PackageClassLoader)3