Java Code Examples for org.takes.Response#head()

The following examples show how to use org.takes.Response#head() . 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: TkJoinedCookies.java    From takes with MIT License 6 votes vote down vote up
/**
 * Join them.
 * @param response The response
 * @return New response
 * @throws Exception If fails
 */
private static Response join(final Response response) throws Exception {
    final StringBuilder cookies = new StringBuilder();
    for (final String header : response.head()) {
        final Matcher matcher =
            TkJoinedCookies.PTN.matcher(header);
        if (!matcher.matches()) {
            continue;
        }
        cookies.append(matcher.group(1)).append(", ");
    }
    final Response out;
    if (cookies.length() > 0) {
        out = new RsWithHeader(
            new RsWithoutHeader(response, "Set-cookie"),
            "Set-Cookie", cookies.toString()
        );
    } else {
        out = response;
    }
    return out;
}
 
Example 2
Source File: RsWithStatus.java    From takes with MIT License 6 votes vote down vote up
/**
 * Make head.
 * @param origin Original response
 * @param status Status
 * @param reason Reason
 * @return Head
 * @throws IOException If fails
 */
private static Iterable<String> head(final Response origin,
    final int status, final CharSequence reason) throws IOException {
    // @checkstyle MagicNumber (1 line)
    if (status < 100 || status > 999) {
        throw new IllegalArgumentException(
            String.format(
                // @checkstyle LineLength (1 line)
                "according to RFC 7230 HTTP status code must have three digits: %d",
                status
            )
        );
    }
    return new Joined<>(
        new FormattedText(
            "HTTP/1.1 %d %s", status, reason
        ).asString(),
        new Filtered<>(
            item -> new Not(
                new StartsWith(item, "HTTP/")
            ).value(),
            origin.head()
        )
    );
}
 
Example 3
Source File: RsWithoutHeader.java    From takes with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param res Original response
 * @param name Header name
 */
public RsWithoutHeader(final Response res, final CharSequence name) {
    super(
        new ResponseOf(
            () -> new Filtered<>(
                header -> new Not(
                    new StartsWith(
                        new Lowered(header),
                        new FormattedText(
                            "%s:",
                            new Lowered(name.toString())
                        )
                    )
                ).value(),
                res.head()
            ),
            res::body
        )
    );
}
 
Example 4
Source File: RsWithHeaders.java    From takes with MIT License 5 votes vote down vote up
/**
 * Add to head additional headers.
 * @param res Original response
 * @param headers Values witch will be added to head
 * @return Head with additional headers
 * @throws IOException If fails
 */
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private static Iterable<String> extend(final Response res,
    final Iterable<? extends CharSequence> headers) throws IOException {
    Response resp = res;
    for (final CharSequence hdr : headers) {
        resp = new RsWithHeader(resp, hdr);
    }
    return resp.head();
}