use of core.framework.web.Response in project core-ng-demo-project by neowu.
the class IndexController method index.
@Protected(operation = "index")
public Response index(Request request) {
IndexPage model = new IndexPage();
model.name = message.get("key.name", languageManager.language()).orElse("world not found");
model.imageURL = "https://image.com/image123.jpg";
// Session session = request.session();
// // Optional<String> hello = session.get("hello");
// session.set("hello", "world");
Response response = Response.html("/template/index.html", model, languageManager.language());
response.cookie(Cookies.TEST, "1+2");
response.cookie(Cookies.TEST1, "hello \"\" cookies");
return response;
}
use of core.framework.web.Response in project core-ng-project by neowu.
the class WebServiceControllerBuilderTest method batch.
@Test
void batch() throws Exception {
TestWebService.TestRequest requestBean = new TestWebService.TestRequest();
requestBean.stringField = "value";
when(request.bean(Types.list(TestWebService.TestRequest.class))).thenReturn(Lists.newArrayList(requestBean));
Controller controller = new WebServiceControllerBuilder<>(TestWebService.class, serviceImpl, TestWebService.class.getDeclaredMethod("batch", List.class)).build();
Response response = controller.execute(request);
assertEquals(HTTPStatus.OK, response.status());
}
use of core.framework.web.Response in project core-ng-project by neowu.
the class HTTPServerErrorHandler method handleError.
void handleError(Throwable e, HttpServerExchange exchange, RequestImpl request, ActionLog actionLog) {
if (exchange.isResponseStarted()) {
logger.error("response was sent, discard the current http transaction");
return;
}
try {
Response errorResponse = null;
if (customErrorHandler != null) {
Optional<Response> customErrorResponse = customErrorHandler.handle(request, e);
if (customErrorResponse.isPresent())
errorResponse = customErrorResponse.get();
}
if (errorResponse == null) {
String accept = exchange.getRequestHeaders().getFirst(Headers.ACCEPT);
errorResponse = defaultErrorResponse(e, accept, actionLog);
}
responseHandler.render((ResponseImpl) errorResponse, exchange, actionLog);
} catch (Throwable error) {
logger.error(error.getMessage(), e);
if (exchange.isResponseStarted()) {
logger.error("failed to render error page, response was sent, discard the current http transaction");
return;
}
renderDefaultErrorPage(error, exchange, actionLog);
}
}
use of core.framework.web.Response in project core-ng-project by neowu.
the class HTTPServerHandler method handle.
private void handle(String path, HttpServerExchange exchange) {
ActionLog actionLog = logManager.begin("=== http transaction begin ===");
RequestImpl request = new RequestImpl(exchange, requestBeanMapper);
try {
// initialize webContext at beginning, the customerErrorHandler in errorHandler may use it if any exception
webContext.initialize(request);
requestParser.parse(request, exchange, actionLog);
request.session = sessionManager.load(request);
HeaderMap headers = exchange.getRequestHeaders();
String client = headers.getFirst(HTTPServerHandler.HEADER_CLIENT);
if (client != null)
actionLog.context("client", client);
actionLog.refId(headers.getFirst(HTTPServerHandler.HEADER_REF_ID));
ControllerHolder controller = route.get(path, request.method(), request.pathParams, actionLog);
actionLog.action(controller.action);
actionLog.context("controller", controller.controllerInfo);
logger.debug("controllerClass={}", controller.controller.getClass().getCanonicalName());
// trigger trace after action is determined due to trace log use action as part of path, is there better way?
if ("true".equals(headers.getFirst(HEADER_TRACE))) {
actionLog.trace = true;
}
Response response = new InvocationImpl(controller, interceptors, request, webContext).proceed();
sessionManager.save(request, response);
responseHandler.render((ResponseImpl) response, exchange, actionLog);
} catch (Throwable e) {
logManager.logError(e);
errorHandler.handleError(e, exchange, request, actionLog);
} finally {
webContext.cleanup();
logManager.end("=== http transaction end ===");
}
}
use of core.framework.web.Response in project core-ng-project by neowu.
the class WebServiceControllerBuilderTest method get.
@Test
void get() throws Exception {
when(request.pathParam("id", Integer.class)).thenReturn(1);
WebServiceControllerBuilder<TestWebService> builder = new WebServiceControllerBuilder<>(TestWebService.class, serviceImpl, TestWebService.class.getDeclaredMethod("get", Integer.class));
Controller controller = builder.build();
String sourceCode = builder.builder.sourceCode();
assertEquals(ClasspathResources.text("webservice-test/test-webservice-controller-get.java"), sourceCode);
Response response = controller.execute(request);
assertEquals(HTTPStatus.OK, response.status());
@SuppressWarnings("unchecked") Optional<TestWebService.TestResponse> bean = (Optional<TestWebService.TestResponse>) ((BeanBody) ((ResponseImpl) response).body).bean;
assertEquals(2, (int) bean.get().intField);
}
Aggregations