Example usage for io.netty.handler.codec.http HttpHeaders setInt

List of usage examples for io.netty.handler.codec.http HttpHeaders setInt

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpHeaders setInt.

Prototype

public abstract HttpHeaders setInt(CharSequence name, int value);

Source Link

Document

Set the name to value .

Usage

From source file:com.chiorichan.http.HttpResponseWrapper.java

License:Mozilla Public License

/**
 * Sends the data to the client. Internal Use.
 *
 * @throws IOException/*from   w w w. ja v a  2s.co  m*/
 *              if there was a problem sending the data, like the connection was unexpectedly closed.
 */
public void sendResponse() throws IOException {
    if (stage == HttpResponseStage.CLOSED || stage == HttpResponseStage.WRITTEN)
        return;

    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, httpStatus, output);
    HttpHeaders h = response.headers();

    if (request.hasSession()) {
        Session session = request.getSession();

        /**
         * Initiate the Session Persistence Method.
         * This is usually done with a cookie but we should make a param optional
         */
        session.processSessionCookie(request.getDomain());

        for (HttpCookie c : session.getCookies().values())
            if (c.needsUpdating())
                h.add("Set-Cookie", c.toHeaderValue());

        if (session.getSessionCookie().needsUpdating())
            h.add("Set-Cookie", session.getSessionCookie().toHeaderValue());
    }

    if (h.get("Server") == null)
        h.add("Server", Versioning.getProduct() + " Version " + Versioning.getVersion());

    // This might be a temporary measure - TODO Properly set the charset for each request.
    h.set("Content-Type", httpContentType + "; charset=" + encoding.name());

    h.add("Access-Control-Allow-Origin",
            request.getLocation().getConfig().getString("site.web-allowed-origin", "*"));

    for (Entry<String, String> header : headers.entrySet())
        h.add(header.getKey(), header.getValue());

    // Expires: Wed, 08 Apr 2015 02:32:24 GMT
    // DateTimeFormatter formatter = DateTimeFormat.forPattern( "EE, dd-MMM-yyyy HH:mm:ss zz" );

    // h.set( HttpHeaders.Names.EXPIRES, formatter.print( DateTime.now( DateTimeZone.UTC ).plusDays( 1 ) ) );
    // h.set( HttpHeaders.Names.CACHE_CONTROL, "public, max-age=86400" );

    h.setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
    h.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);

    stage = HttpResponseStage.WRITTEN;

    request.getChannel().writeAndFlush(response);
}

From source file:com.linecorp.armeria.server.http.Http1RequestDecoder.java

License:Apache License

private void fail(ChannelHandlerContext ctx, HttpResponseStatus status) {
    discarding = true;//w  w  w.ja  va2  s. c  o m
    req = null;

    final ChannelFuture future;
    if (receivedRequests <= sentResponses) {
        // Just close the connection if sending an error response will make the number of the sent
        // responses exceed the number of the received requests, which doesn't make sense.
        future = ctx.writeAndFlush(Unpooled.EMPTY_BUFFER);
    } else {
        final ByteBuf content = Unpooled.copiedBuffer(status.toString(), StandardCharsets.UTF_8);
        final FullHttpResponse res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content);

        final HttpHeaders headers = res.headers();
        headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
        headers.set(HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8);
        headers.setInt(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());

        future = ctx.writeAndFlush(res);
    }

    future.addListener(ChannelFutureListener.CLOSE);
}