use of com.bluenimble.platform.api.ApiResourcesManagerException in project serverless by bluenimble.
the class DefaultApiResourcesManager method put.
@Override
public ApiResource put(String[] path, InputStream payload, boolean overwrite) 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("a protected resource called " + path[0] + " already exists");
}
String sPath = checkAndGetPath(path);
if (Lang.isNullOrEmpty(sPath)) {
throw new ApiResourcesManagerException("invalid resource path " + Lang.join(path, Lang.SLASH));
}
File parent = null;
String[] aParent = Lang.moveRight(path, 1);
if (aParent == null) {
parent = resources;
} else {
parent = new File(resources, Lang.join(aParent));
}
File file = new File(parent, path[path.length - 1]);
if (file.exists()) {
if (file.isFile()) {
if (!overwrite) {
throw new ApiResourcesManagerException("resource " + sPath + " already exists");
}
} else {
throw new ApiResourcesManagerException("resource " + sPath + " already exists");
}
}
if (parent.exists()) {
if (parent.isFile()) {
throw new ApiResourcesManagerException("parent resource " + Lang.join(aParent, Lang.SLASH) + " is a file. It should be a valid folder");
}
} else {
parent.mkdirs();
}
FileApiResource resource = new FileApiResource(owner, resources, file);
if (payload == null) {
file.mkdir();
} else {
try {
resource.pipe(payload, 0, -1);
} catch (IOException e) {
throw new ApiResourcesManagerException(e.getMessage(), e);
}
}
return new FileApiResource(owner, resources, file);
}
use of com.bluenimble.platform.api.ApiResourcesManagerException in project serverless by bluenimble.
the class DefaultApiServicesManager method load.
@Override
public void load(Api api) throws ApiServicesManagerException {
this.api = (ApiImpl) api;
ApiResourcesManager rmgr = api.getResourcesManager();
try {
load(rmgr, rmgr.get(new String[] { ConfigKeys.Folders.Services }));
} catch (ApiResourcesManagerException e) {
throw new ApiServicesManagerException(e.getMessage(), e);
}
}
use of com.bluenimble.platform.api.ApiResourcesManagerException in project serverless by bluenimble.
the class ScriptableApiSpi method onStart.
@Override
public void onStart(Api api, ApiContext context) throws ApiManagementException {
String script = Json.getString(api.getRuntime(), Api.Spec.Runtime.Function);
if (Lang.isNullOrEmpty(script)) {
throw new ApiManagementException("function not defined 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 spi
Object jsSpi = null;
try {
jsSpi = engine.eval(Supported.Javascript, api, rScript, ScriptContext.Empty);
} catch (Exception ex) {
throw new ApiManagementException(ex.getMessage(), ex);
}
if (jsSpi == null) {
throw new ApiManagementException("function returned an undefined object");
}
// create the api object
Object jsApi = null;
try {
jsApi = engine.invoke(null, Api.class.getSimpleName(), api);
} catch (Exception ex) {
throw new ApiManagementException(ex.getMessage(), ex);
}
if (jsApi == null) {
throw new ApiManagementException("can't create 'api spec' js object");
}
api.setHelper(new SpecAndSpiPair(jsApi, jsSpi));
if (!engine.has(jsSpi, Functions.OnStart)) {
return;
}
// invoke onStart
try {
engine.invoke(jsSpi, Functions.OnStart, jsApi, context);
} catch (ScriptingEngineException ex) {
ex.setScript(script);
throw new ApiManagementException(ex.getMessage(), ex);
}
}
use of com.bluenimble.platform.api.ApiResourcesManagerException in project serverless by bluenimble.
the class GetResourceApiServiceSpi method execute.
@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
String path = (String) request.get(Spec.Path);
if (Lang.isNullOrEmpty(path)) {
throw new ApiServiceExecutionException("Resource / not found").status(ApiResponse.BAD_REQUEST);
}
String location = (String) Json.find(request.getService().getCustom(), Custom.Resources, Custom.Root);
if (!Lang.isNullOrEmpty(location)) {
path = location + Lang.SLASH + path;
}
ApiResource r;
try {
r = api.getResourcesManager().get(Lang.split(path, Lang.SLASH));
} catch (ApiResourcesManagerException e) {
throw new ApiServiceExecutionException(e.getMessage()).status(ApiResponse.BAD_REQUEST);
}
if (r == null) {
throw new ApiServiceExecutionException("Resource " + path + " not found").status(ApiResponse.NOT_FOUND);
}
return new ApiResourceOutput(r);
}
use of com.bluenimble.platform.api.ApiResourcesManagerException in project serverless by bluenimble.
the class MgmApiSpi method onStart.
@Override
public void onStart(Api api, ApiContext context) {
ApiResource resource;
try {
resource = api.getResourcesManager().get(Consumers);
} catch (ApiResourcesManagerException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
if (resource == null) {
return;
}
InputStream stream = null;
try {
stream = resource.toInput();
consumers = Json.load(stream);
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex);
} finally {
IOUtils.closeQuietly(stream);
}
}
Aggregations