Search in sources :

Example 1 with DataWriter

use of com.bluenimble.platform.api.media.DataWriter 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);
    }
}
Also used : TemplateEngine(com.bluenimble.platform.api.impls.media.engines.TemplateEngine) ApiResource(com.bluenimble.platform.api.ApiResource) DefaultVariableResolver(com.bluenimble.platform.server.plugins.media.utils.DefaultVariableResolver) JsonObject(com.bluenimble.platform.json.JsonObject) ApiMediaException(com.bluenimble.platform.api.ApiMediaException) DefaultVariableResolver(com.bluenimble.platform.server.plugins.media.utils.DefaultVariableResolver) VariableResolver(com.bluenimble.platform.Lang.VariableResolver) ApiMediaException(com.bluenimble.platform.api.ApiMediaException) DataWriter(com.bluenimble.platform.api.media.DataWriter)

Aggregations

VariableResolver (com.bluenimble.platform.Lang.VariableResolver)1 ApiMediaException (com.bluenimble.platform.api.ApiMediaException)1 ApiResource (com.bluenimble.platform.api.ApiResource)1 TemplateEngine (com.bluenimble.platform.api.impls.media.engines.TemplateEngine)1 DataWriter (com.bluenimble.platform.api.media.DataWriter)1 JsonObject (com.bluenimble.platform.json.JsonObject)1 DefaultVariableResolver (com.bluenimble.platform.server.plugins.media.utils.DefaultVariableResolver)1