Search in sources :

Example 1 with CookieEncoder

use of org.jboss.netty.handler.codec.http.CookieEncoder in project databus by linkedin.

the class DatabusRequestExecutionHandler method generateEmptyResponse.

private HttpResponse generateEmptyResponse() {
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    //response.setContent(ChannelBuffers.wrappedBuffer(responseBody));
    response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8");
    response.setHeader("Access-Control-Allow-Origin", "*");
    setTrackingInfo(response);
    // Encode the cookie.
    String cookieString = _httpRequest.getHeader(COOKIE);
    if (cookieString != null) {
        CookieDecoder cookieDecoder = new CookieDecoder();
        Set<Cookie> cookies = cookieDecoder.decode(cookieString);
        if (!cookies.isEmpty()) {
            // Reset the cookies if necessary.
            CookieEncoder cookieEncoder = new CookieEncoder(true);
            for (Cookie cookie : cookies) {
                cookieEncoder.addCookie(cookie);
            }
            response.addHeader(SET_COOKIE, cookieEncoder.encode());
        }
    }
    return response;
}
Also used : Cookie(org.jboss.netty.handler.codec.http.Cookie) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) CookieEncoder(org.jboss.netty.handler.codec.http.CookieEncoder) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) CookieDecoder(org.jboss.netty.handler.codec.http.CookieDecoder)

Example 2 with CookieEncoder

use of org.jboss.netty.handler.codec.http.CookieEncoder in project load-balancer by RestComm.

the class HttpRequestHandler method writeResponse.

private void writeResponse(MessageEvent e, HttpResponseStatus status, String responseString) {
    // Convert the response content to a ChannelBuffer.
    ChannelBuffer buf = ChannelBuffers.copiedBuffer(responseString, Charset.forName("UTF-8"));
    // Decide whether to close the connection or not.
    boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request.getHeader(HttpHeaders.Names.CONNECTION)) || request.getProtocolVersion().equals(HttpVersion.HTTP_1_0) && !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request.getHeader(HttpHeaders.Names.CONNECTION));
    // Build the response object.
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
    response.setContent(buf);
    response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8");
    if (!close) {
        // There's no need to add 'Content-Length' header
        // if this is the last response.
        response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buf.readableBytes()));
    }
    String cookieString = request.getHeader(HttpHeaders.Names.COOKIE);
    if (cookieString != null) {
        CookieDecoder cookieDecoder = new CookieDecoder();
        Set<Cookie> cookies = cookieDecoder.decode(cookieString);
        if (!cookies.isEmpty()) {
            // Reset the cookies if necessary.
            CookieEncoder cookieEncoder = new CookieEncoder(true);
            for (Cookie cookie : cookies) {
                cookieEncoder.addCookie(cookie);
            }
            response.addHeader(HttpHeaders.Names.SET_COOKIE, cookieEncoder.encode());
        }
    }
    // Write the response.
    ChannelFuture future = null;
    if (status.equals(HttpResponseStatus.SERVICE_UNAVAILABLE) || status.equals(HttpResponseStatus.LOCKED))
        future = e.getChannel().write(buf);
    else
        future = e.getChannel().write(response);
    // Close the connection after the write operation is done if necessary.
    if (close) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}
Also used : Cookie(org.jboss.netty.handler.codec.http.Cookie) ChannelFuture(org.jboss.netty.channel.ChannelFuture) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) CookieEncoder(org.jboss.netty.handler.codec.http.CookieEncoder) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) CookieDecoder(org.jboss.netty.handler.codec.http.CookieDecoder) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 3 with CookieEncoder

use of org.jboss.netty.handler.codec.http.CookieEncoder in project load-balancer by RestComm.

the class HttpServerRequestHandler method writeResponse.

@SuppressWarnings("deprecation")
private void writeResponse(MessageEvent e, HttpResponseStatus status, String responseString) {
    // Convert the response content to a ChannelBuffer.
    if (chunk)
        for (int i = 0; i < 1000; i++) responseString += "HOW MUCH IS THE FISH";
    ChannelBuffer buf = ChannelBuffers.copiedBuffer(responseString, Charset.forName("UTF-8"));
    // Decide whether to close the connection or not.
    boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request.getHeader(HttpHeaders.Names.CONNECTION)) || request.getProtocolVersion().equals(HttpVersion.HTTP_1_0) && !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request.getHeader(HttpHeaders.Names.CONNECTION));
    // Build the response object.
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
    if (!chunk)
        response.setContent(buf);
    response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8");
    if (chunk)
        response.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, "chunked");
    if (!chunk)
        if (!close) {
            // There's no need to add 'Content-Length' header
            // if this is the last response.
            response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buf.readableBytes()));
        }
    String cookieString = request.getHeader(HttpHeaders.Names.COOKIE);
    if (cookieString != null) {
        CookieDecoder cookieDecoder = new CookieDecoder();
        Set<Cookie> cookies = cookieDecoder.decode(cookieString);
        if (!cookies.isEmpty()) {
            // Reset the cookies if necessary.
            CookieEncoder cookieEncoder = new CookieEncoder(true);
            for (Cookie cookie : cookies) {
                cookieEncoder.addCookie(cookie);
            }
            response.addHeader(HttpHeaders.Names.SET_COOKIE, cookieEncoder.encode());
        }
    }
    // Write the response.
    ChannelFuture future = e.getChannel().write(response);
    if (chunk) {
        while (buf.readableBytes() > 0) {
            int maxBytes = 1000;
            if (buf.readableBytes() < 1000)
                maxBytes = buf.readableBytes();
            HttpChunk currChunk = new DefaultHttpChunk(buf.readBytes(maxBytes));
            future = e.getChannel().write(currChunk);
        }
        HttpChunk currChunk = new DefaultHttpChunk(buf);
        future = e.getChannel().write(currChunk);
    }
    // Close the connection after the write operation is done if necessary.
    if (close) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}
Also used : Cookie(org.jboss.netty.handler.codec.http.Cookie) ChannelFuture(org.jboss.netty.channel.ChannelFuture) DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) CookieEncoder(org.jboss.netty.handler.codec.http.CookieEncoder) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) CookieDecoder(org.jboss.netty.handler.codec.http.CookieDecoder) DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk)

Aggregations

Cookie (org.jboss.netty.handler.codec.http.Cookie)3 CookieDecoder (org.jboss.netty.handler.codec.http.CookieDecoder)3 CookieEncoder (org.jboss.netty.handler.codec.http.CookieEncoder)3 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)3 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)3 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)2 ChannelFuture (org.jboss.netty.channel.ChannelFuture)2 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)1 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)1