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

The following examples show how to use com.blade.mvc.http.Response#header() . 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: CorsMiddleware.java    From blade with Apache License 2.0 6 votes vote down vote up
private CorsMiddleware allowHeads(Response response) {
    boolean isDefaultAllowHeads = corsConfig == null || corsConfig.getAllowedHeaders() == null
            || corsConfig.getAllowedHeaders().size() == 0;

    if (isDefaultAllowHeads) {
        response.header("Access-Control-Allow-Headers", CorsConfiger.ALL);
        return this;
    }

    String heads = corsConfig.getAllowedHeaders().stream()
            .collect(Collector.of(
                    () -> new StringJoiner(","),
                    StringJoiner::add,
                    StringJoiner::merge,
                    StringJoiner::toString
            ));

    response.header("Access-Control-Allow-Headers", heads);
    return this;
}
 
Example 2
Source File: CorsMiddleware.java    From blade with Apache License 2.0 6 votes vote down vote up
private CorsMiddleware allowMethods(Response response) {
    boolean isDefaultAllowMethods = corsConfig == null || corsConfig.getAllowedMethods() == null
            || corsConfig.getAllowedMethods().size() == 0;

    if (isDefaultAllowMethods) {
        response.header("Access-Control-Allow-Methods",
                CorsConfiger.DEFAULT_ALLOWED_METHODS);
        return this;
    }

    String methods = corsConfig.getAllowedMethods().stream().collect(Collector.of(
            () -> new StringJoiner(", "),
            (j, method) -> j.add(method.toUpperCase()),
            StringJoiner::merge,
            StringJoiner::toString
    ));

    response.header("Access-Control-Allow-Methods", methods);
    return this;
}
 
Example 3
Source File: CorsMiddleware.java    From blade with Apache License 2.0 5 votes vote down vote up
private CorsMiddleware allowCredentials(Response response) {
    boolean isDefaultAllowCredentials = corsConfig == null || corsConfig.getAllowCredentials() == null;

    if (isDefaultAllowCredentials) {
        response.header("Access-Control-Allow-Credentials",
                CorsConfiger.DEFAULT_ALLOW_CREDENTIALS);
        return this;
    }
    response.header("Access-Control-Allow-Credentials",
            corsConfig.getAllowCredentials().toString());
    return this;
}
 
Example 4
Source File: CorsMiddleware.java    From blade with Apache License 2.0 5 votes vote down vote up
private CorsMiddleware setMaxAge(Response response) {
    boolean isDefaultMaxAge = corsConfig == null || corsConfig.getMaxAge() == null;
    if (isDefaultMaxAge) {
        response.header("Access-Control-Max-Age",
                CorsConfiger.DEFAULT_MAX_AGE.toString());
        return this;
    }
    response.header("Access-Control-Max-Age", corsConfig.getMaxAge().toString());
    return this;
}
 
Example 5
Source File: HttpResponseTest.java    From blade with Apache License 2.0 5 votes vote down vote up
@Test
public void testHeaders() {
    Response mockResponse = mockHttpResponse(200);

    when(mockResponse.headers()).thenReturn(new HashMap<>());

    Response response = new HttpResponse(mockResponse);
    assertEquals(0, response.headers().size());

    response.header("a", "123");
    assertEquals(1, response.headers().size());
}
 
Example 6
Source File: AttributesExampleController.java    From tutorials with MIT License 4 votes vote down vote up
@GetRoute("/header-example")
public void getHeader(Request request, Response response) {
    String headerVal = request.header("a-header", HEADER);
    response.header("a-header", headerVal);
}