Search in sources :

Example 6 with ApiResourcesManagerException

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

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

the class ResourceTemplateLoader method sourceAt.

@Override
public TemplateSource sourceAt(String name) throws IOException {
    JsonObject runtime = api.getRuntime();
    String templatesPath = Json.getString(runtime, Templates, DefaultTemplatesPath);
    if (templatesPath.startsWith(Lang.SLASH)) {
        templatesPath = templatesPath.substring(1);
    }
    if (templatesPath.endsWith(Lang.SLASH)) {
        templatesPath = templatesPath.substring(0, templatesPath.length() - 1);
    }
    String templateExt = Json.getString(runtime, TemplateExtension, DotHtml);
    final String fileName = templatesPath + Lang.SLASH + name + templateExt;
    ApiResource res;
    try {
        res = api.getResourcesManager().get(Lang.split(fileName, Lang.SLASH));
    } catch (ApiResourcesManagerException e) {
        throw new IOException(e.getMessage(), e);
    }
    final ApiResource resource = res;
    return new TemplateSource() {

        @Override
        public long lastModified() {
            return resource.timestamp().getTime();
        }

        @Override
        public String filename() {
            return fileName;
        }

        @Override
        public String content() throws IOException {
            InputStream is = null;
            try {
                is = resource.toInput();
                return IOUtils.toString(is);
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    };
}
Also used : ApiResource(com.bluenimble.platform.api.ApiResource) TemplateSource(com.github.jknack.handlebars.io.TemplateSource) InputStream(java.io.InputStream) JsonObject(com.bluenimble.platform.json.JsonObject) IOException(java.io.IOException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException)

Example 8 with ApiResourcesManagerException

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

the class CreateSpaceSpi method onStart.

@Override
public void onStart(Api api, ApiService service, ApiContext context) throws ApiManagementException {
    super.onStart(api, service, context);
    // load space model
    ApiResource resource;
    try {
        resource = api.getResourcesManager().get(SpaceModel);
    } catch (ApiResourcesManagerException ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
    if (resource == null) {
        return;
    }
    InputStream stream = null;
    try {
        stream = resource.toInput();
        spaceModel = Json.load(stream);
    } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(stream);
    }
}
Also used : ApiResource(com.bluenimble.platform.api.ApiResource) InputStream(java.io.InputStream) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException)

Example 9 with ApiResourcesManagerException

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

the class ScriptableApiServiceSpi method onStart.

@Override
public void onStart(Api api, ApiService service, ApiContext context) throws ApiManagementException {
    SpecAndSpiPair helper = (SpecAndSpiPair) api.getHelper();
    if (helper == null) {
        throw new ApiManagementException("api '" + api.getNamespace() + "' doesn't support scripting or couldn't start correctly.");
    }
    Object jsApi = helper.spec();
    if (jsApi == null) {
        throw new ApiManagementException("api '" + api.getNamespace() + "' doesn't support scripting");
    }
    JsonObject runtime = service.getRuntime();
    String script = Json.getString(runtime, Api.Spec.Runtime.Function);
    if (Lang.isNullOrEmpty(script)) {
        throw new ApiManagementException("script not found in " + ApiUtils.RuntimeKey);
    }
    String[] path = Lang.split(script, Lang.SLASH);
    ApiResource rScript = null;
    try {
        rScript = api.getResourcesManager().get(path);
    } catch (ApiResourcesManagerException ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    if (rScript == null) {
        throw new ApiManagementException("function '" + Lang.join(path, Lang.SLASH) + "' not found");
    }
    ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, context);
    // create the spec
    Object jsService = null;
    try {
        jsService = engine.invoke(null, ApiService.class.getSimpleName(), service);
    } catch (Exception ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    if (jsService == null) {
        throw new ApiManagementException("can't create 'service spec' js object");
    }
    // create the spi
    Object spi = null;
    try {
        spi = engine.eval(Supported.Javascript, api, rScript, ScriptContext.Empty);
    } catch (Exception ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    if (spi == null) {
        throw new ApiManagementException("script returned an undefined object");
    }
    service.setHelper(new SpecAndSpiPair(jsService, spi));
    if (!engine.has(spi, Functions.OnStart)) {
        return;
    }
    // invoke onStart
    try {
        engine.invoke(spi, Functions.OnStart, jsApi, jsService, context);
    } catch (ScriptingEngineException ex) {
        ex.setScript(script);
        throw new ApiManagementException(ex.getMessage(), ex);
    }
}
Also used : ApiResource(com.bluenimble.platform.api.ApiResource) ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) JsonObject(com.bluenimble.platform.json.JsonObject) JsonObject(com.bluenimble.platform.json.JsonObject) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException)

Example 10 with ApiResourcesManagerException

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

the class DefaultApiResourcesManager method delete.

@Override
public void delete(String[] path) throws ApiResourcesManagerException {
    if (path == null || path.length == 0) {
        throw new ApiResourcesManagerException("invalid or null resource path");
    }
    if (path.length == 1 && Reserved.contains(path[0])) {
        throw new ApiResourcesManagerException("can't delete protected resource " + path[0]);
    }
    String sPath = checkAndGetPath(path);
    if (Lang.isNullOrEmpty(sPath)) {
        throw new ApiResourcesManagerException("invalid resource path " + Lang.join(path, Lang.SLASH));
    }
    File r = new File(resources, sPath);
    if (!r.exists()) {
        throw new ApiResourcesManagerException("resource " + sPath + " not found");
    }
    try {
        FileUtils.delete(r);
    } catch (IOException e) {
        throw new ApiResourcesManagerException(e.getMessage(), e);
    }
}
Also used : IOException(java.io.IOException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) File(java.io.File)

Aggregations

ApiResourcesManagerException (com.bluenimble.platform.api.ApiResourcesManagerException)10 ApiResource (com.bluenimble.platform.api.ApiResource)7 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)5 JsonObject (com.bluenimble.platform.json.JsonObject)4 IOException (java.io.IOException)4 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)3 InputStream (java.io.InputStream)3 ApiAuthenticationException (com.bluenimble.platform.api.security.ApiAuthenticationException)2 ScriptingEngine (com.bluenimble.platform.scripting.ScriptingEngine)2 ScriptingEngineException (com.bluenimble.platform.scripting.ScriptingEngineException)2 File (java.io.File)2 VariableResolver (com.bluenimble.platform.Lang.VariableResolver)1 ApiMediaException (com.bluenimble.platform.api.ApiMediaException)1 ApiResourcesManager (com.bluenimble.platform.api.ApiResourcesManager)1 ApiServicesManagerException (com.bluenimble.platform.api.ApiServicesManagerException)1 ApiResourceOutput (com.bluenimble.platform.api.impls.ApiResourceOutput)1 FileApiResource (com.bluenimble.platform.server.impls.fs.FileApiResource)1 DefaultVariableResolver (com.bluenimble.platform.server.plugins.media.utils.DefaultVariableResolver)1 TemplateSource (com.github.jknack.handlebars.io.TemplateSource)1 OutputStream (java.io.OutputStream)1