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