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) {
}
}
}
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);
}
}
};
}
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);
}
}
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);
}
}
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);
}
}
Aggregations