use of act.handler.builtin.FileGetter in project actframework by actframework.
the class RouteTableRouterBuilderTest method withBuiltInHandler.
@Test
public void withBuiltInHandler() {
addRouteMap("GET /public file:/public");
RequestHandler h = router.getInvoker(GET, "/public/file1.txt", ctx);
yes(h instanceof FileGetter);
eq(new File("target/test-classes/public"), ActTestBase.fieldVal(h, "base"));
}
use of act.handler.builtin.FileGetter in project actframework by actframework.
the class RouterTest method doNotOverrideExistingRouting.
@Test
public void doNotOverrideExistingRouting() {
routeWithStaticDir();
router.addMapping(GET, "/public", "file:/private", ACTION_ANNOTATION);
RequestHandler handler = router.getInvoker(GET, "/public/foo/bar.txt", ctx);
yes(handler instanceof FileGetter);
yes(handler.supportPartialPath());
eq(new File(BASE, "/public"), fieldVal(handler, "base"));
}
use of act.handler.builtin.FileGetter in project actframework by actframework.
the class RouterTest method overrideExistingRouting.
@Test
public void overrideExistingRouting() {
routeWithStaticDir();
router.addMapping(GET, "/public", "file:/private");
RequestHandler handler = router.getInvoker(GET, "/public/foo/bar.txt", ctx);
yes(handler instanceof FileGetter);
yes(handler.supportPartialPath());
eq(new File(BASE, "/private"), fieldVal(handler, "base"));
}
use of act.handler.builtin.FileGetter in project actframework by actframework.
the class RouterTest method routeWithStaticDir.
@Test
public void routeWithStaticDir() {
router.addMapping(GET, "/public", "file:/public");
RequestHandler handler = router.getInvoker(GET, "/public/foo/bar.txt", ctx);
yes(handler instanceof FileGetter);
yes(handler.supportPartialPath());
eq(new File(BASE, "/public"), fieldVal(handler, "base"));
}
use of act.handler.builtin.FileGetter in project actframework by actframework.
the class NetworkHandler method handle.
public void handle(final ActionContext ctx, final NetworkDispatcher dispatcher) {
if (isDestroyed()) {
return;
}
Exception refreshError = null;
if (Act.isDev()) {
try {
boolean updated = app.checkUpdates(false);
if (updated && !app.hasBlockIssue()) {
app.jobManager().on(SysEventId.POST_START, new Runnable() {
@Override
public void run() {
handle(ctx, dispatcher);
}
}, true);
dispatcher.keep();
return;
}
} catch (Exception e) {
refreshError = e;
}
}
final H.Request req = ctx.req();
String url = req.url();
H.Method method = req.method();
url = contentSuffixProcessor.apply(req, url);
try {
url = urlContextProcessor.apply(req, url);
} catch (NotFound notFound) {
ctx.handler(AlwaysNotFound.INSTANCE);
ctx.saveLocal();
AlwaysNotFound.INSTANCE.apply(ctx);
return;
}
Timer timer = metric.startTimer(MetricInfo.ROUTING);
final RequestHandler requestHandler = router().getInvoker(method, url, ctx);
ctx.handler(requestHandler);
timer.stop();
boolean resourceGetter = requestHandler instanceof ResourceGetter || requestHandler instanceof FileGetter;
if (null != refreshError && !resourceGetter) {
ctx.saveLocal();
handleException(refreshError, ctx, "Error refreshing app");
ActionContext.clearCurrent();
return;
}
NetworkJob job = new NetworkJob() {
@Override
public void run() {
Timer timer = Metric.NULL_METRIC.startTimer("null");
if (metric != Metric.NULL_METRIC) {
String key = S.concat(MetricInfo.HTTP_HANDLER, ":", requestHandler.toString());
timer = metric.startTimer(key);
}
ctx.saveLocal();
EventBus eventBus = app.eventBus();
try {
eventBus.emit(new PreHandle(ctx));
requestHandler.handle(ctx);
} catch (Result r) {
if (isError(r)) {
ctx.handler(FastRequestHandler.DUMB);
}
try {
r = RequestHandlerProxy.GLOBAL_AFTER_INTERCEPTOR.apply(r, ctx);
} catch (Exception e) {
logger.error(e, "Error calling global after interceptor");
r = ActErrorResult.of(e);
}
if (null == ctx.handler() || isError(r)) {
ctx.handler(FastRequestHandler.DUMB);
}
H.Format fmt = req.accept();
if (H.Format.UNKNOWN == fmt) {
fmt = req.contentType();
}
ctx.prepareRespForWrite().addHeaderIfNotAdded(H.Header.Names.CONTENT_TYPE, fmt.contentType());
r.apply(req, ctx.prepareRespForWrite());
} catch (Exception e) {
handleException(e, ctx, "Error handling network request");
} finally {
// we don't destroy ctx here in case it's been passed to
// another thread
eventBus.emit(new PostHandle(ctx));
ActionContext.clearCurrent();
timer.stop();
}
}
};
if (method.unsafe() || !requestHandler.express(ctx)) {
dispatcher.dispatch(job);
} else {
job.run();
}
}
Aggregations