Search in sources :

Example 1 with RenderBinary

use of org.osgl.mvc.result.RenderBinary in project actframework by actframework.

the class RenderAny method apply.

// TODO: Allow plugin to support rendering pdf, xls or other binary types
public void apply(ActionContext context) {
    Boolean hasTemplate = context.hasTemplate();
    if (null != hasTemplate && hasTemplate) {
        RenderTemplate.get(context.successStatus()).apply(context);
        return;
    }
    H.Format fmt = context.accept();
    if (fmt == UNKNOWN) {
        H.Request req = context.req();
        H.Method method = req.method();
        String methodInfo = S.concat(method.name(), " method to ");
        String acceptHeader = req.header(H.Header.Names.ACCEPT);
        throw E.unsupport(S.concat("Unknown accept content type(", acceptHeader, "): ", methodInfo, req.url()));
    }
    Result result = null;
    if (JSON == fmt) {
        List<String> varNames = context.__appRenderArgNames();
        Map<String, Object> map = new HashMap<>(context.renderArgs());
        if (null != varNames && !varNames.isEmpty()) {
            for (String name : varNames) {
                map.put(name, context.renderArg(name));
            }
        }
        result = new RenderJSON(map);
    } else if (XML == fmt) {
        List<String> varNames = context.__appRenderArgNames();
        Map<String, Object> map = new HashMap<>();
        if (null != varNames && !varNames.isEmpty()) {
            for (String name : varNames) {
                map.put(name, context.renderArg(name));
            }
        }
        result = new FilteredRenderXML(map, null, context);
    } else if (HTML == fmt || TXT == fmt || CSV == fmt) {
        if (!ignoreMissingTemplate) {
            throw E.unsupport("Template[%s] not found", context.templatePath());
        }
        context.nullValueResultIgnoreRenderArgs().apply(context.req(), context.prepareRespForWrite());
        return;
    } else if (PDF == fmt || XLS == fmt || XLSX == fmt || DOC == fmt || DOCX == fmt) {
        List<String> varNames = context.__appRenderArgNames();
        if (null != varNames && !varNames.isEmpty()) {
            Object firstVar = context.renderArg(varNames.get(0));
            String action = S.str(context.actionPath()).afterLast(".").toString();
            if (firstVar instanceof File) {
                File file = (File) firstVar;
                result = new RenderBinary(file, action);
            } else if (firstVar instanceof InputStream) {
                InputStream is = (InputStream) firstVar;
                result = new RenderBinary(is, action);
            } else if (firstVar instanceof ISObject) {
                ISObject sobj = (ISObject) firstVar;
                result = new RenderBinary(sobj.asInputStream(), action);
            }
            if (null == result) {
                throw E.unsupport("Unknown render arg type [%s] for binary response", firstVar.getClass());
            }
        } else {
            throw E.unexpected("No render arg found for binary response");
        }
    }
    if (null != result) {
        ActResponse<?> resp = context.prepareRespForWrite();
        result.status(context.successStatus()).apply(context.req(), resp);
    } else {
        throw E.unexpected("Unknown accept content type: %s", fmt.contentType());
    }
}
Also used : Format(org.osgl.http.H.Format) HashMap(java.util.HashMap) InputStream(java.io.InputStream) RenderJSON(org.osgl.mvc.result.RenderJSON) H(org.osgl.http.H) ISObject(org.osgl.storage.ISObject) Result(org.osgl.mvc.result.Result) RenderBinary(org.osgl.mvc.result.RenderBinary) ISObject(org.osgl.storage.ISObject) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Aggregations

File (java.io.File)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 H (org.osgl.http.H)1 Format (org.osgl.http.H.Format)1 RenderBinary (org.osgl.mvc.result.RenderBinary)1 RenderJSON (org.osgl.mvc.result.RenderJSON)1 Result (org.osgl.mvc.result.Result)1 ISObject (org.osgl.storage.ISObject)1