use of act.ActResponse in project actframework by actframework.
the class ActionContext method applyContentType.
private void applyContentType(H.Format fmt) {
if (null != fmt) {
ActResponse resp = resp();
resp.initContentType(fmt.contentType());
resp.commitContentType();
}
}
use of act.ActResponse in project actframework by actframework.
the class ActBlockingExchange method close.
@Override
public void close() throws IOException {
ActionContext ctx = ctx();
if (!exchange.isComplete()) {
try {
UndertowRequest req = (UndertowRequest) ctx.req();
req.closeAndDrainRequest();
} finally {
ActResponse resp = ctx.resp();
resp.close();
}
} else {
try {
UndertowRequest req = (UndertowRequest) ctx.req();
req.freeResources();
} finally {
UndertowResponse resp = (UndertowResponse) ctx.resp();
resp.freeResources();
}
}
}
use of act.ActResponse in project actframework by actframework.
the class ActionContext method applyContentType.
public ActionContext applyContentType() {
ActResponse resp = resp();
H.Format lastContentType = resp.lastContentType();
if (null != lastContentType && $.ne(H.Format.UNKNOWN, lastContentType)) {
resp.commitContentType();
return this;
}
H.Request req = req();
H.Format fmt = req.accept();
if (H.Format.UNKNOWN == fmt) {
fmt = req.contentType();
}
applyContentType(fmt);
return this;
}
use of act.ActResponse in project actframework by actframework.
the class TemplateBase method merge.
@Override
public void merge(ActionContext context) {
Map<String, Object> renderArgs = context.renderArgs();
if (!context.isByPassImplicitTemplateVariable()) {
exposeImplicitVariables(renderArgs, context);
}
beforeRender(context);
ActResponse resp = context.resp();
merge(renderArgs, resp);
resp.commit();
}
use of act.ActResponse in project actframework by actframework.
the class ResourceGetter method handle.
protected void handle(String path, ActionContext context) {
H.Request req = context.req();
if (Act.isProd()) {
ActResponse resp = context.prepareRespForWrite();
if (preloaded) {
// this is a reloaded file resource
if (preloadFailure) {
AlwaysNotFound.INSTANCE.handle(context);
} else {
resp.contentType(preloadedContentType);
if (req.etagMatches(etag)) {
AlwaysNotModified.INSTANCE.handle(context);
} else {
H.Format contentType = cachedContentType.get(path);
if (null == contentType) {
contentType = req.contentType();
}
resp.contentType(contentType).header(CACHE_CONTROL, "public, max-age=7200").etag(this.etag).writeContent(buffer.duplicate());
}
}
return;
}
if (cachedFailures.containsKey(path)) {
AlwaysNotFound.INSTANCE.handle(context);
return;
}
if (null != req.etag() && req.etagMatches(etags.get(path))) {
H.Format contentType = cachedContentType.get(path);
if (null == contentType) {
contentType = req.contentType();
}
resp.contentType(contentType);
AlwaysNotModified.INSTANCE.handle(context);
return;
}
}
ByteBuffer buffer = cachedBuffers.get(path);
if (null != buffer) {
context.resp().contentType(cachedContentType.get(path)).header(CACHE_CONTROL, "public, max-age=7200");
context.applyContentType();
context.prepareRespForWrite().etag(etags.get(path)).writeContent(buffer.duplicate());
return;
}
try {
URL target;
String loadPath;
if (S.blank(path)) {
target = baseUrl;
loadPath = base;
if (isFolder) {
if (null == indexHandler) {
synchronized (this) {
if (null == indexHandler) {
loadPath = S.pathConcat(base, SEP, "index.html");
target = FileGetter.class.getResource(loadPath);
}
indexHandler = null == target ? AlwaysForbidden.INSTANCE : new FixedResourceGetter(loadPath);
}
}
indexHandler.handle(context);
return;
}
} else {
loadPath = S.pathConcat(base, SEP, path);
target = FileGetter.class.getResource(loadPath);
if (null == target) {
throw NotFound.get();
}
}
if (preventFolderAccess(target, loadPath, context)) {
return;
}
H.Format contentType = FileGetter.contentType(target.getPath());
ActResponse resp = context.prepareRespForWrite();
resp.contentType(contentType);
if (Act.isProd()) {
resp.header(CACHE_CONTROL, "public, max-age=7200");
}
context.applyCorsSpec().applyContentSecurityPolicy().applyContentType();
try {
int n = IO.copy(target.openStream(), resp.outputStream());
if (Act.isProd()) {
etags.put(path, String.valueOf(n));
if (n < context.config().resourcePreloadSizeLimit()) {
$.Var<String> etagBag = $.var();
buffer = doPreload(target, etagBag);
if (null == buffer) {
cachedFailures.put(path, true);
} else {
cachedBuffers.put(path, buffer);
cachedContentType.put(path, contentType);
}
}
}
} catch (NullPointerException e) {
// this is caused by accessing folder inside jar URL
folders.add(target);
AlwaysForbidden.INSTANCE.handle(context);
}
} catch (IOException e) {
logger.warn(e, "Error servicing static resource request");
throw NotFound.get();
}
}
Aggregations