use of com.bluenimble.platform.server.plugins.media.utils.DefaultVariableResolver 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);
}
}
use of com.bluenimble.platform.server.plugins.media.utils.DefaultVariableResolver 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) {
}
}
}
Aggregations