use of com.blade.mvc.RouteContext in project blade by biezhi.
the class HttpServerHandler method executeLogic.
private WebContext executeLogic(WebContext webContext) {
try {
WebContext.set(webContext);
Request request = webContext.getRequest();
String method = request.method();
String uri = request.uri();
Instant start = null;
if (ALLOW_COST && !PERFORMANCE) {
start = Instant.now();
}
if (isStaticFile(method, uri)) {
staticFileHandler.handle(webContext);
} else {
if (HttpMethod.OPTIONS.name().equals(method) && null != WebContext.blade().corsMiddleware()) {
WebContext.blade().corsMiddleware().handle(new RouteContext(webContext.getRequest(), webContext.getResponse()));
} else {
Route route = routeMatcher.lookupRoute(method, uri);
if (null != route) {
webContext.setRoute(route);
} else {
throw new NotFoundException(uri);
}
routeHandler.handle(webContext);
}
if (PERFORMANCE) {
return webContext;
}
if (ALLOW_COST) {
long cost = log200AndCost(log, start, BladeCache.getPaddingMethod(method), uri);
request.attribute(REQUEST_COST_TIME, cost);
} else {
log200(log, BladeCache.getPaddingMethod(method), uri);
}
}
return webContext;
} catch (Exception e) {
throw BladeException.wrapper(e);
}
}
use of com.blade.mvc.RouteContext in project blade by biezhi.
the class RouteMethodHandler method handle.
@Override
public void handle(WebContext webContext) throws Exception {
RouteContext context = new RouteContext(webContext.getRequest(), webContext.getResponse());
// if execution returns false then execution is interrupted
String uri = context.uri();
Route route = webContext.getRoute();
if (null == route) {
throw new NotFoundException(context.uri());
}
// init route, request parameters, route action method and parameter.
context.initRoute(route);
// execution middleware
if (hasMiddleware && !invokeMiddleware(routeMatcher.getMiddleware(), context)) {
return;
}
context.injectParameters();
// web hook before
if (hasBeforeHook && !invokeHook(routeMatcher.getBefore(uri), context)) {
return;
}
// execute
this.routeHandle(context);
// webHook
if (hasAfterHook) {
this.invokeHook(routeMatcher.getAfter(uri), context);
}
}
use of com.blade.mvc.RouteContext in project blade by biezhi.
the class BasicAuthMiddlewareTest method testAuthFail.
@Test
public void testAuthFail() throws Exception {
Request mockRequest = mockHttpRequest("GET");
WebContext.init(Blade.of(), "/");
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Basic YmxhZGU6YmxhZGUyMg==");
when(mockRequest.parameters()).thenReturn(new HashMap<>());
when(mockRequest.headers()).thenReturn(headers);
Request request = new HttpRequest(mockRequest);
Response response = mockHttpResponse(200);
RouteContext context = new RouteContext(request, response);
context.initRoute(Route.builder().action(AuthHandler.class.getMethod("handle", RouteContext.class)).targetType(AuthHandler.class).target(new AuthHandler()).build());
WebContext.set(new WebContext(request, response, null));
AuthOption authOption = AuthOption.builder().build();
authOption.addUser("admin", "123456");
BasicAuthMiddleware basicAuthMiddleware = new BasicAuthMiddleware(authOption);
boolean flag = basicAuthMiddleware.before(context);
assertFalse(flag);
}
use of com.blade.mvc.RouteContext in project blade by biezhi.
the class BasicAuthMiddlewareTest method testAuthSuccess.
@Test
public void testAuthSuccess() throws Exception {
Request mockRequest = mockHttpRequest("GET");
WebContext.init(Blade.of(), "/");
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Basic YWRtaW46MTIzNDU2");
when(mockRequest.parameters()).thenReturn(new HashMap<>());
when(mockRequest.headers()).thenReturn(headers);
Request request = new HttpRequest(mockRequest);
Response response = mockHttpResponse(200);
RouteContext context = new RouteContext(request, response);
context.initRoute(Route.builder().action(AuthHandler.class.getMethod("handle", RouteContext.class)).targetType(AuthHandler.class).target(new AuthHandler()).build());
WebContext.set(new WebContext(request, response, null));
AuthOption authOption = AuthOption.builder().build();
authOption.addUser("admin", "123456");
BasicAuthMiddleware basicAuthMiddleware = new BasicAuthMiddleware(authOption);
boolean flag = basicAuthMiddleware.before(context);
assertTrue(flag);
}
Aggregations