Example usage for io.netty.handler.codec.http.cookie Cookie setValue

List of usage examples for io.netty.handler.codec.http.cookie Cookie setValue

Introduction

In this page you can find the example usage for io.netty.handler.codec.http.cookie Cookie setValue.

Prototype

void setValue(String value);

Source Link

Document

Sets the value of this Cookie .

Usage

From source file:org.waarp.openr66.protocol.http.adminssl.HttpResponsiveSslHandler.java

License:Open Source License

private void handleCookies(HttpResponse response) {
    String cookieString = request.headers().get(HttpHeaderNames.COOKIE);
    boolean i18nextFound = false;
    if (cookieString != null) {
        Set<Cookie> cookies = ServerCookieDecoder.LAX.decode(cookieString);
        if (!cookies.isEmpty()) {
            // Reset the sessions if necessary.
            boolean findSession = false;
            for (Cookie cookie : cookies) {
                if (cookie.name().equalsIgnoreCase(R66SESSION + Configuration.configuration.getHOST_ID())) {
                    if (newSession) {
                        findSession = false;
                    } else {
                        findSession = true;
                        response.headers().add(HttpHeaderNames.SET_COOKIE,
                                ServerCookieEncoder.LAX.encode(cookie));
                    }/*from  w w  w.ja  v  a2  s  .  c  o  m*/
                } else if (cookie.name().equalsIgnoreCase(I18NEXT)) {
                    i18nextFound = true;
                    cookie.setValue(lang);
                    response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
                } else {
                    response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
                }
            }
            if (!i18nextFound) {
                Cookie cookie = new DefaultCookie(I18NEXT, lang);
                response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
            }
            newSession = false;
            if (!findSession) {
                if (admin != null) {
                    response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(admin));
                    logger.debug("AddSession: " + uriRequest + ":{}", admin);
                }
            }
        }
    } else {
        Cookie cookie = new DefaultCookie(I18NEXT, lang);
        response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
        if (admin != null) {
            logger.debug("AddSession: " + uriRequest + ":{}", admin);
            response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(admin));
        }
    }
}

From source file:org.waarp.openr66.protocol.http.HttpFormattedHandler.java

License:Open Source License

/**
 * Write the response//  ww w  .j a v  a 2s  . c o m
 * 
 * @param ctx
 */
private void writeResponse(ChannelHandlerContext ctx) {
    // Convert the response content to a ByteBuf.
    ByteBuf buf = Unpooled.copiedBuffer(responseContent.toString(), WaarpStringUtils.UTF8);
    responseContent.setLength(0);
    // Decide whether to close the connection or not.
    boolean keepAlive = HttpUtil.isKeepAlive(request);
    boolean close = HttpHeaderValues.CLOSE
            .contentEqualsIgnoreCase(request.headers().get(HttpHeaderNames.CONNECTION)) || (!keepAlive);

    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, buf);
    response.headers().add(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
    if (isCurrentRequestXml) {
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/xml");
    } else if (isCurrentRequestJson) {
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json");
    } else {
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html");
    }
    if (keepAlive) {
        response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }
    if (!close) {
        // There's no need to add 'Content-Length' header
        // if this is the last response.
        response.headers().set(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(buf.readableBytes()));
    }

    String cookieString = request.headers().get(HttpHeaderNames.COOKIE);
    if (cookieString != null) {
        Set<Cookie> cookies = ServerCookieDecoder.LAX.decode(cookieString);
        boolean i18nextFound = false;
        if (!cookies.isEmpty()) {
            // Reset the cookies if necessary.
            for (Cookie cookie : cookies) {
                if (cookie.name().equalsIgnoreCase(I18NEXT)) {
                    i18nextFound = true;
                    cookie.setValue(lang);
                    response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
                } else {
                    response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
                }
            }
            if (!i18nextFound) {
                Cookie cookie = new DefaultCookie(I18NEXT, lang);
                response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
            }
        }
        if (!i18nextFound) {
            Cookie cookie = new DefaultCookie(I18NEXT, lang);
            response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
        }
    }

    // Write the response.
    ChannelFuture future = ctx.writeAndFlush(response);
    // Close the connection after the write operation is done if necessary.
    if (close) {
        future.addListener(ChannelFutureListener.CLOSE);
        /*if (this.isPrivateDbSession && dbSession != null) {
           dbSession.forceDisconnect();
           DbAdmin.nbHttpSession--;
           dbSession = null;
        }*/
    }
}

From source file:org.waarp.openr66.proxy.protocol.http.HttpFormattedHandler.java

License:Open Source License

/**
 * Write the response// w w  w . j a  va 2 s .  c o m
 * 
 */
private void writeResponse(ChannelHandlerContext ctx) {
    // Convert the response content to a ByteBuf.
    ByteBuf buf = Unpooled.copiedBuffer(responseContent.toString(), WaarpStringUtils.UTF8);
    responseContent.setLength(0);
    // Decide whether to close the connection or not.
    boolean keepAlive = HttpUtil.isKeepAlive(request);
    boolean close = HttpHeaderValues.CLOSE
            .contentEqualsIgnoreCase(request.headers().get(HttpHeaderNames.CONNECTION)) || (!keepAlive);

    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, buf);
    response.headers().add(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
    if (isCurrentRequestXml) {
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/xml");
    } else if (isCurrentRequestJson) {
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json");
    } else {
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html");
    }
    if (keepAlive) {
        response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }
    if (!close) {
        // There's no need to add 'Content-Length' header
        // if this is the last response.
        response.headers().set(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(buf.readableBytes()));
    }

    String cookieString = request.headers().get(HttpHeaderNames.COOKIE);
    if (cookieString != null) {
        Set<Cookie> cookies = ServerCookieDecoder.LAX.decode(cookieString);
        boolean i18nextFound = false;
        if (!cookies.isEmpty()) {
            // Reset the cookies if necessary.
            for (Cookie cookie : cookies) {
                if (cookie.name().equalsIgnoreCase(I18NEXT)) {
                    i18nextFound = true;
                    cookie.setValue(lang);
                    response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
                } else {
                    response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
                }
            }
            if (!i18nextFound) {
                Cookie cookie = new DefaultCookie(I18NEXT, lang);
                response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
            }
        }
        if (!i18nextFound) {
            Cookie cookie = new DefaultCookie(I18NEXT, lang);
            response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
        }
    }

    // Write the response.
    ChannelFuture future = ctx.writeAndFlush(response);
    // Close the connection after the write operation is done if necessary.
    if (close) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}