use of com.bluenimble.platform.api.ApiResource 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.ApiResource 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.ApiResource 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);
}
}
use of com.bluenimble.platform.api.ApiResource in project serverless by bluenimble.
the class FileApiResource method children.
@Override
public List<ApiResource> children(final Selector selector) {
if (file.isFile()) {
return null;
}
File[] files = file.listFiles(new FileFilter() {
@Override
public boolean accept(File kid) {
return selector.select(kid.getName(), kid.isDirectory());
}
});
if (files == null || files.length == 0) {
return null;
}
List<ApiResource> list = new ArrayList<ApiResource>();
for (File f : files) {
list.add(new FileApiResource(owner, root, f));
}
return list;
}
use of com.bluenimble.platform.api.ApiResource in project serverless by bluenimble.
the class PlainMediaProcessor method process.
@Override
public void process(Api api, ApiService service, ApiConsumer consumer, ApiOutput output, ApiRequest request, final ApiResponse response) throws ApiMediaException {
String contentType = (String) request.get(ApiRequest.SelectedMedia);
if (Lang.isNullOrEmpty(contentType) || Lang.STAR.equals(contentType)) {
contentType = this.contentType;
}
String charset = Encodings.UTF8;
JsonObject mediaDef = MediaRoutingUtils.pickMedia(api, service, contentType);
try {
String rContentType = contentType;
if (output != null) {
String oCharset = (String) output.get(ApiOutput.Defaults.Charset);
if (!Lang.isNullOrEmpty(oCharset)) {
charset = oCharset;
}
}
rContentType += "; charset=" + charset;
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());
if (media != null) {
// if there is a template
String sTemplate = Json.getString(media, ApiService.Spec.Media.Resource);
ApiResource template = null;
if (!Lang.isNullOrEmpty(sTemplate)) {
template = api.getResourcesManager().get(Lang.split(Lang.resolve(sTemplate, vr), Lang.SLASH));
}
if (template != null) {
TemplateEngine engine = plugin.loockupEngine(api, Json.getString(media, ApiService.Spec.Media.Engine, MediaPlugin.HandlebarsEngine));
response.set(ApiHeaders.ContentType, rContentType);
response.flushHeaders();
engine.write(consumer, request, response, output, template, media);
response.close();
return;
}
}
if (WriteResponseUtils.writeError(response, api.tracer(), rContentType)) {
response.close();
return;
}
response.set(ApiHeaders.ContentType, rContentType);
DataWriter dataWriter = writers.get(contentType);
if (dataWriter == null) {
dataWriter = writers.get(this.contentType);
}
dataWriter.write(output, response);
response.close();
} catch (Exception e) {
throw new ApiMediaException(e.getMessage(), e);
}
}
Aggregations