Search in sources :

Example 11 with ActionContext

use of act.app.ActionContext in project actframework by actframework.

the class TemplatePathResolver method amendSuffix.

protected String amendSuffix(String path, ActContext context) {
    if (path.contains(".")) {
        return path;
    }
    H.Format fmt = context.accept();
    if (UNKNOWN == fmt) {
        fmt = HTML;
    }
    if (isAcceptFormatSupported(fmt)) {
        return S.concat(path, ".", fmt.name());
    }
    if (context instanceof ActionContext) {
        ActionContext actionContext = $.cast(context);
        H.Request req = actionContext.req();
        throw E.unsupport("Error handling %s request to %s - Request accept not supported: %s", req.method(), req.url(), fmt);
    }
    throw E.unsupport("Request accept not supported: %s", fmt);
}
Also used : Format(org.osgl.http.H.Format) H(org.osgl.http.H) ActionContext(act.app.ActionContext)

Example 12 with ActionContext

use of act.app.ActionContext in project actframework by actframework.

the class RequestScope method put.

public <T> void put(String key, T t) {
    if (null == t) {
        return;
    }
    ActionContext actionContext = ActionContext.current();
    if (null != actionContext) {
        actionContext.attribute(key, t);
    }
    CliContext cliContext = CliContext.current();
    if (null != cliContext) {
        cliContext.attribute(key, t);
    }
}
Also used : ActionContext(act.app.ActionContext) CliContext(act.cli.CliContext)

Example 13 with ActionContext

use of act.app.ActionContext in project actframework by actframework.

the class ActErrorPageRender method renderTemplate.

@Override
protected String renderTemplate(ErrorResult error, H.Format format) {
    ActionContext context = ActionContext.current();
    if (null == context) {
        return null;
    }
    Integer errorCode = error.errorCode();
    int statusCode = error.statusCode();
    fixRequestAcceptFormat(context);
    Template t = getTemplate(statusCode, context);
    if (null == t) {
        String errorMsg = error.getMessage();
        if (null == errorMsg) {
            errorMsg = MvcConfig.errorMessage(error.status());
        }
        if (i18n()) {
            String translated = context.i18n(true, errorMsg);
            if (translated == errorMsg) {
                translated = context.i18n(true, MvcConfig.class, errorMsg);
            }
            errorMsg = translated;
        }
        H.Format accept = context.accept();
        if (H.Format.JSON == accept) {
            return jsonContent(error, errorCode, errorMsg);
        } else if (HTML == accept) {
            String header = S.concat("HTTP/1.1 ", Integer.toString(statusCode), " ", errorMsg);
            return S.concat("<!DOCTYPE html><html><head><meta charset='utf-8'><title>", header, "</title></head><body><h1>", header, "</h1></body></html>");
        } else if (H.Format.XML == accept) {
            S.Buffer sb = context.strBuf();
            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><error>");
            if (null != errorCode) {
                sb.append("<code>").append(errorCode).append("</code");
            }
            sb.append("<message>").append(errorMsg).append("</message></error>");
            return sb.toString();
        } else if (CSV == accept) {
            if (null == errorCode) {
                return S.concat("message\n", errorMsg);
            } else {
                return S.concat("code,message\n", Integer.toString(errorCode), ",", errorMsg);
            }
        } else if (H.Format.TXT == accept) {
            return null == errorCode ? errorMsg : S.concat(Integer.toString(errorCode), " ", errorMsg);
        } else {
            // Unknown accept format
            return "";
        }
    }
    if (HTML == context.accept()) {
        String header = S.concat("HTTP/1.1 ", Integer.toString(statusCode), " ", MvcConfig.errorMessage(error.status()));
        context.renderArg("header", header);
    }
    context.renderArg(ARG_ERROR, error);
    return t.render(context);
}
Also used : MvcConfig(org.osgl.mvc.MvcConfig) S(org.osgl.util.S) H(org.osgl.http.H) ActionContext(act.app.ActionContext) Template(act.view.Template)

Example 14 with ActionContext

use of act.app.ActionContext in project actframework by actframework.

the class FindBy method resolve.

private static String resolve(String bindName, ActContext ctx) {
    String value = ctx.paramVal(bindName);
    if (S.notEmpty(value)) {
        return value;
    }
    if (ctx instanceof ActionContext) {
        ActionContext actionContext = (ActionContext) ctx;
        JsonDTO dto = actionContext.attribute(JsonDTO.CTX_ATTR_KEY);
        if (null != dto) {
            value = S.string(dto.get(bindName));
        }
    }
    if (S.notEmpty(value)) {
        return value;
    }
    Keyword keyword = Keyword.of(bindName);
    if (keyword.tokens().size() > 1) {
        return resolve(keyword, ctx);
    } else {
        keyword = Keyword.of(bindName + " id");
        return resolve(keyword, ctx);
    }
}
Also used : ActionContext(act.app.ActionContext) JsonDTO(act.inject.param.JsonDTO)

Example 15 with ActionContext

use of act.app.ActionContext in project actframework by actframework.

the class HelloWorldApp method greeting.

@GetAction("/greeting")
public Result greeting(String who, int age) {
    ActionContext ctx = ActionContext.current();
    ctx.renderArg("who", who);
    return render(who, age);
}
Also used : ActionContext(act.app.ActionContext) GetAction(org.osgl.mvc.annotation.GetAction)

Aggregations

ActionContext (act.app.ActionContext)15 CliContext (act.cli.CliContext)4 H (org.osgl.http.H)3 CliSession (act.cli.CliSession)2 ActResponse (act.ActResponse)1 JsonDTO (act.inject.param.JsonDTO)1 UrlPath (act.route.UrlPath)1 Template (act.view.Template)1 Format (org.osgl.http.H.Format)1 MvcConfig (org.osgl.mvc.MvcConfig)1 GetAction (org.osgl.mvc.annotation.GetAction)1 ISObject (org.osgl.storage.ISObject)1 S (org.osgl.util.S)1