Search in sources :

Example 1 with Response

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;
}
Also used : Response(core.framework.web.Response) Protected(app.web.interceptor.Protected)

Example 2 with 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());
}
Also used : Response(core.framework.web.Response) Controller(core.framework.web.Controller) Test(org.junit.jupiter.api.Test)

Example 3 with Response

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);
    }
}
Also used : ErrorResponse(core.framework.impl.web.service.ErrorResponse) Response(core.framework.web.Response)

Example 4 with Response

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 ===");
    }
}
Also used : Response(core.framework.web.Response) HeaderMap(io.undertow.util.HeaderMap) HttpString(io.undertow.util.HttpString) ActionLog(core.framework.impl.log.ActionLog) RequestImpl(core.framework.impl.web.request.RequestImpl)

Example 5 with Response

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);
}
Also used : Response(core.framework.web.Response) Optional(java.util.Optional) Controller(core.framework.web.Controller) ResponseImpl(core.framework.impl.web.response.ResponseImpl) Test(org.junit.jupiter.api.Test)

Aggregations

Response (core.framework.web.Response)13 Test (org.junit.jupiter.api.Test)6 Controller (core.framework.web.Controller)4 Request (core.framework.web.Request)4 Interceptor (core.framework.web.Interceptor)2 Protected (app.web.interceptor.Protected)1 PathParam (core.framework.api.web.service.PathParam)1 ContentType (core.framework.http.ContentType)1 CodeBuilder (core.framework.impl.asm.CodeBuilder)1 ActionLog (core.framework.impl.log.ActionLog)1 RequestImpl (core.framework.impl.web.request.RequestImpl)1 ResponseImpl (core.framework.impl.web.response.ResponseImpl)1 ErrorResponse (core.framework.impl.web.service.ErrorResponse)1 NotFoundException (core.framework.web.exception.NotFoundException)1 HeaderMap (io.undertow.util.HeaderMap)1 HttpString (io.undertow.util.HttpString)1 Type (java.lang.reflect.Type)1 Path (java.nio.file.Path)1 Optional (java.util.Optional)1