Search in sources :

Example 16 with ApiSpace

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

the class RenameObjectSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    String provider = (String) request.get(CommonSpec.Provider);
    String path = (String) request.get(Spec.Object);
    String name = (String) request.get(StorageObject.Fields.Name);
    Storage storage = space.feature(Storage.class, provider, request);
    StorageObject so = null;
    try {
        so = storage.root().get(path);
    } catch (StorageException e) {
        throw new ApiServiceExecutionException("storage object '" + path + "' not found", e).status(ApiResponse.NOT_FOUND);
    }
    try {
        so.rename(name);
    } catch (StorageException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Renamed, true));
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) Storage(com.bluenimble.platform.storage.Storage) StorageObject(com.bluenimble.platform.storage.StorageObject) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) StorageException(com.bluenimble.platform.storage.StorageException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 17 with ApiSpace

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

the class UpdateObjectSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    ApiSpace space;
    try {
        space = MgmUtils.space(consumer, api);
    } catch (ApiAccessDeniedException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
    }
    String provider = (String) request.get(CommonSpec.Provider);
    String path = (String) request.get(Spec.Object);
    Boolean append = (Boolean) request.get(Spec.Append);
    if (append == null) {
        append = false;
    }
    ApiStreamSource ss = (ApiStreamSource) request.get(ApiRequest.Payload, Scope.Stream);
    Storage storage = space.feature(Storage.class, provider, request);
    StorageObject so = null;
    try {
        so = storage.root().get(path);
    } catch (StorageException e) {
        throw new ApiServiceExecutionException("storage object '" + path + "' not found", e).status(ApiResponse.NOT_FOUND);
    }
    if (so.isFolder() && ss != null) {
        throw new ApiServiceExecutionException("object '" + path + "' isn't a valid content aware storage object").status(ApiResponse.BAD_REQUEST);
    }
    InputStream stream = ss.stream();
    try {
        so.update(stream, append);
    } catch (StorageException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(stream);
    }
    return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Updated, true));
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) Storage(com.bluenimble.platform.storage.Storage) StorageObject(com.bluenimble.platform.storage.StorageObject) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) InputStream(java.io.InputStream) JsonObject(com.bluenimble.platform.json.JsonObject) ApiStreamSource(com.bluenimble.platform.api.ApiStreamSource) StorageException(com.bluenimble.platform.storage.StorageException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 18 with ApiSpace

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

the class CreateSpaceSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String namespace = (String) request.get(Spec.Space);
    JsonObject oSpace = (JsonObject) spaceModel.duplicate().set(ApiSpace.Spec.Namespace, namespace);
    // set default secrets
    JsonObject defaultSecrets = Json.getObject(Json.getObject(oSpace, ApiSpace.Spec.secrets.class.getSimpleName()), ApiSpace.Secrets.Default);
    if (defaultSecrets != null) {
        defaultSecrets.set(ApiSpace.Spec.secrets.Key, Lang.UUID(16));
    }
    // create space
    ApiSpace newSpace = null;
    try {
        newSpace = api.space().create(oSpace);
    } catch (ApiManagementException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    // create root keys
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(CommonSpec.Role, Role.ADMIN.name());
    List<KeyPair> keys = null;
    try {
        keys = newSpace.keystore().create(1, null, properties);
    } catch (Exception e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    JsonObject result = newSpace.describe(DescribeOption.Info);
    if (keys != null) {
        result.set(CommonOutput.Keys, keys.get(0).toJson());
    }
    return new JsonApiOutput(result);
}
Also used : KeyPair(com.bluenimble.platform.security.KeyPair) HashMap(java.util.HashMap) JsonObject(com.bluenimble.platform.json.JsonObject) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) CommonSpec(com.bluenimble.platform.apis.mgm.CommonSpec) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 19 with ApiSpace

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

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

the class FileSystemApiServer method create.

private ApiSpace create(JsonObject oSpace, boolean save) throws ApiManagementException {
    String spaceNs = Json.getString(oSpace, Spec.Namespace);
    if (Lang.isNullOrEmpty(spaceNs)) {
        throw new ApiManagementException("space namespace not found");
    }
    if (!InstallUtils.isValidSpaceNs(spaceNs)) {
        throw new ApiManagementException("invalid space namespace '" + spaceNs + "'");
    }
    File spacesHome = new File(runtimeHome, ConfigKeys.Folders.Spaces);
    File spaceHome = new File(spacesHome, spaceNs);
    if (!spaceHome.exists()) {
        spaceHome.mkdir();
    }
    ApiSpace space = null;
    try {
        space = new ApiSpaceImpl(this, oSpace, spaceHome);
    } catch (Exception ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    addSpace(space);
    if (save) {
        try {
            Json.store(oSpace, new File(spaceHome, ConfigKeys.Descriptor.Space));
        } catch (Exception ex) {
            throw new ApiManagementException(ex.getMessage(), ex);
        }
    }
    // notify space creation
    try {
        if (space.isStarted()) {
            getPluginsRegistry().onEvent(Event.Create, space);
        }
    } catch (Exception ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    return space;
}
Also used : ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiSpaceImpl(com.bluenimble.platform.api.impls.ApiSpaceImpl) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) File(java.io.File) ServerStartupException(com.bluenimble.platform.server.ServerStartupException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) IOException(java.io.IOException)

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