Java Code Examples for com.blade.mvc.http.Response#text()

The following examples show how to use com.blade.mvc.http.Response#text() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TemplateController.java    From tale with MIT License 5 votes vote down vote up
@GetRoute("content")
public void getContent(@Param String fileName, Response response) {
    try {
        String themePath = Const.CLASSPATH + File.separatorChar + "templates" + File.separatorChar + "themes" + File.separatorChar + Commons.site_theme();
        String filePath  = themePath + File.separatorChar + fileName;
        String content   = Files.readAllLines(Paths.get(filePath)).stream().collect(Collectors.joining("\n"));
        response.text(content);
    } catch (IOException e) {
        log.error("获取模板文件失败", e);
    }
}
 
Example 2
Source File: BaseWebHook.java    From tale with MIT License 5 votes vote down vote up
@Override
public boolean before(Signature signature) {
    Request  request  = signature.request();
    Response response = signature.response();

    String uri = request.uri();
    String ip  = request.address();

    // 禁止该ip访问
    if (TaleConst.BLOCK_IPS.contains(ip)) {
        response.text("You have been banned, brother");
        return false;
    }

    log.info("UserAgent: {}", request.userAgent());
    log.info("用户访问地址: {}, 来路地址: {}", uri, ip);

    if (uri.startsWith(TaleConst.STATIC_URI)) {
        return true;
    }

    if (!TaleConst.INSTALLED && !uri.startsWith(TaleConst.INSTALL_URI)) {
        response.redirect(TaleConst.INSTALL_URI);
        return false;
    }

    if (TaleConst.INSTALLED) {
        return isRedirect(request, response);
    }
    return true;
}
 
Example 3
Source File: AttributesExampleController.java    From tutorials with MIT License 5 votes vote down vote up
@GetRoute("/session-attribute-example")
public void getSessionAttribute(Request request, Response response) {
    Session session = request.session();
    session.attribute("session-val", SESSION_VALUE);
    String sessionVal = session.attribute("session-val");
    response.text(sessionVal);
}
 
Example 4
Source File: LogExampleController.java    From tutorials with MIT License 5 votes vote down vote up
@Route(value = "/test-logs")
public void testLogs(Response response) {
    log.trace("This is a TRACE Message");
    log.debug("This is a DEBUG Message");
    log.info("This is an INFO Message");
    log.warn("This is a WARN Message");
    log.error("This is an ERROR Message");
    response.text("Check in ./logs");
}
 
Example 5
Source File: DefaultExceptionHandler.java    From blade with Apache License 2.0 4 votes vote down vote up
protected void handleBladeException(BladeException e, Request request, Response response) {
    var blade = WebContext.blade();
    response.status(e.getStatus());

    var modelAndView = new ModelAndView();
    modelAndView.add("title", e.getStatus() + " " + e.getName());
    modelAndView.add("message", e.getMessage());

    if (null != e.getCause()) {
        request.attribute(VARIABLE_STACKTRACE, getStackTrace(e));
    }

    if (e.getStatus() == InternalErrorException.STATUS) {
        log.error("", e);
        this.render500(request, response);
    }

    String paddingMethod = BladeCache.getPaddingMethod(request.method());

    if (e.getStatus() == NotFoundException.STATUS) {
        log404(log, paddingMethod, request.uri());

        if (request.isJsonRequest()) {
            response.json(RestResponse.fail(NotFoundException.STATUS, "Not Found [" + request.uri() + "]"));
        } else {
            var page404 = Optional.ofNullable(blade.environment().get(ENV_KEY_PAGE_404, null));
            if (page404.isPresent()) {
                modelAndView.setView(page404.get());
                renderPage(response, modelAndView);
                response.render(page404.get());
            } else {
                HtmlCreator htmlCreator = new HtmlCreator();
                htmlCreator.center("<h1>404 Not Found - " + request.uri() + "</h1>");
                htmlCreator.hr();
                response.html(htmlCreator.html());
            }
        }
    }

    if (e.getStatus() == MethodNotAllowedException.STATUS) {

        log405(log, paddingMethod, request.uri());

        if (request.isJsonRequest()) {
            response.json(RestResponse.fail(MethodNotAllowedException.STATUS, e.getMessage()));
        } else {
            response.text(e.getMessage());
        }
    }
}
 
Example 6
Source File: DemoController.java    From blade with Apache License 2.0 4 votes vote down vote up
@GetRoute("csrf")
public void getCsrfToken(Request request, Response response) {
    response.text("token: " + request.attribute("_csrf_token"));
}
 
Example 7
Source File: RouteBuilderTest.java    From blade with Apache License 2.0 4 votes vote down vote up
@GetRoute
public void hello(Response response) {
    response.text("Ok.");
}
 
Example 8
Source File: RouteExampleController.java    From tutorials with MIT License 4 votes vote down vote up
@Route(value = "/user/foo")
public void urlCoveredByNarrowedWebhook(Response response) {
    response.text("Check out for the WebHook covering '/user/*' in the logs");
}
 
Example 9
Source File: ParameterInjectionExampleController.java    From tutorials with MIT License 4 votes vote down vote up
@GetRoute("/params/form")
public void formParam(@Param String name, Response response) {
    log.info("name: " + name);
    response.text(name);
}
 
Example 10
Source File: ParameterInjectionExampleController.java    From tutorials with MIT License 4 votes vote down vote up
@GetRoute("/params/path/:uid")
public void restfulParam(@PathParam Integer uid, Response response) {
    log.info("uid: " + uid);
    response.text(String.valueOf(uid));
}
 
Example 11
Source File: ParameterInjectionExampleController.java    From tutorials with MIT License 4 votes vote down vote up
@GetRoute("/params/header")
public void headerParam(@HeaderParam String customheader, Response response) {
    log.info("Custom header: " + customheader);
    response.text(customheader);
}
 
Example 12
Source File: ParameterInjectionExampleController.java    From tutorials with MIT License 4 votes vote down vote up
@GetRoute("/params/cookie")
public void cookieParam(@CookieParam(defaultValue = "default value") String myCookie, Response response) {
    log.info("myCookie: " + myCookie);
    response.text(myCookie);
}
 
Example 13
Source File: AttributesExampleController.java    From tutorials with MIT License 4 votes vote down vote up
@GetRoute("/request-attribute-example")
public void getRequestAttribute(Request request, Response response) {
    request.attribute("request-val", REQUEST_VALUE);
    String requestVal = request.attribute("request-val");
    response.text(requestVal);
}