Example usage for io.netty.handler.codec.http.cookie ServerCookieEncoder LAX

List of usage examples for io.netty.handler.codec.http.cookie ServerCookieEncoder LAX

Introduction

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

Prototype

ServerCookieEncoder LAX

To view the source code for io.netty.handler.codec.http.cookie ServerCookieEncoder LAX.

Click Source Link

Document

Lax instance that doesn't validate name and value, and that allows multiple cookies with the same name.

Usage

From source file:com.topsec.bdc.platform.api.server.http.HttpSnoopServerHandler.java

License:Apache License

/**
 * send back response and close the connection in server side.
 * //  w  w w  . j a v  a2 s  .co  m
 * @param ctx
 * @param responseStatus
 * @param responseContent
 */
private void writeResponseAndClose(ChannelHandlerContext ctx, String message) {

    //response?????
    ByteBuf responseContentBuf = null;
    if (Assert.isEmptyString(message) == true) {
        responseContentBuf = Unpooled.copiedBuffer(_responseStatus.reasonPhrase(), CharsetUtil.UTF_8);
    } else {
        responseContentBuf = Unpooled.copiedBuffer(message, CharsetUtil.UTF_8);
    }
    // Decide whether to close the connection or not.
    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, _responseStatus, responseContentBuf);
    //???
    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
    //disallow keepAlive
    //boolean keepAlive = HttpHeaders.isKeepAlive(_request);
    //response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    // Add 'Content-Length' header only for a keep-alive connection.
    response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
    // Add keep alive header as per:
    // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
    //???
    response.headers().set(CONNECTION, HttpHeaders.Values.CLOSE);
    // Encode the cookie.
    String cookieString = _request.headers().get(COOKIE);
    if (cookieString != null) {
        Set<Cookie> cookies = ServerCookieDecoder.LAX.decode(cookieString);
        if (!cookies.isEmpty()) {
            // Reset the cookies if necessary.
            for (Cookie cookie : cookies) {
                response.headers().add(SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
            }
        }
    } else {
        // Browser sent no cookie.  Add some.
        //response.headers().add(SET_COOKIE, ServerCookieEncoder.LAX.encode("key1", "value1"));
        //response.headers().add(SET_COOKIE, ServerCookieEncoder.LAX.encode("key2", "value2"));
    }
    try {
        // Write the response.
        ctx.write(response);
        // close connection
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    } catch (Throwable t) {
        t.printStackTrace();
        ctx.close();
    }
}

From source file:nikoladasm.aspark.ResponseImpl.java

License:Open Source License

private void sendChunked() {
    HttpResponse response = new DefaultHttpResponse(version, HttpResponseStatus.valueOf(status));
    setHeades(response);//ww w .  j av a2s  .  c  om
    response.headers().set(TRANSFER_ENCODING, CHUNKED);
    cookies.forEach(
            (name, cookie) -> response.headers().add(SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie)));
    ctx.channel().write(response);
    Object body;
    if (!httpMethod.equals(HEAD)) {
        body = new HttpChunkedInput(new ChunkedStream(stream, DEFAULT_CHUNK_SIZE));
    } else {
        body = LastHttpContent.EMPTY_LAST_CONTENT;
    }
    writeObjectToChannel(body).addListener(channelFuture -> stream.close());
}

From source file:nikoladasm.aspark.ResponseImpl.java

License:Open Source License

private void sendUnChunked() throws Exception {
    FullHttpResponse response = new DefaultFullHttpResponse(version, HttpResponseStatus.valueOf(status));
    setHeades(response);/*  w w w  .j  a va  2  s.  com*/
    if (stream != null)
        sendStream(response);
    else
        sendByteArray(response);
    response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
    cookies.forEach(
            (name, cookie) -> response.headers().add(SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie)));
    if (httpMethod.equals(HEAD))
        response.content().clear();
    writeObjectToChannel(response);
}

From source file:org.waarp.gateway.kernel.http.HttpRequestHandler.java

License:Open Source License

/**
 * Method to add specific Cookies from business definition
 * // w  w w  .j a v a 2  s.c o  m
 * Override if needed
 * 
 * @param response
 * @param cookieNames
 */
protected void addBusinessCookie(FullHttpResponse response, Set<String> cookieNames) {
    for (AbstractHttpField field : httpPage.getFieldsForRequest(businessRequest).values()) {
        if (field.isFieldcookieset() && !cookieNames.contains(field.getFieldname())) {
            response.headers().add(HttpHeaderNames.SET_COOKIE,
                    ServerCookieEncoder.LAX.encode(field.getFieldname(), field.fieldvalue));
        }
    }
}

From source file:org.waarp.gateway.kernel.http.HttpRequestHandler.java

License:Open Source License

/**
 * Method to set Cookies in response/*from  w  w  w.j  a  va  2s.co m*/
 * 
 * @param response
 */
protected void setCookieEncoder(FullHttpResponse response) {
    Set<Cookie> cookies;
    String value = request.headers().get(HttpHeaderNames.COOKIE);
    if (value == null) {
        cookies = Collections.emptySet();
    } else {
        cookies = ServerCookieDecoder.LAX.decode(value);
    }
    boolean foundCookieSession = false;
    Set<String> cookiesName = new HashSet<String>();
    if (!cookies.isEmpty()) {
        // Reset the cookies if necessary.
        for (Cookie cookie : cookies) {
            if (isCookieValid(cookie)) {
                response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
                if (cookie.name().equals(cookieSession)) {
                    foundCookieSession = true;
                }
                cookiesName.add(cookie.name());
            }
        }
    }
    if (!foundCookieSession) {
        response.headers().add(HttpHeaderNames.SET_COOKIE,
                ServerCookieEncoder.LAX.encode(cookieSession, session.getCookieSession()));
        cookiesName.add(cookieSession);
    }
    addBusinessCookie(response, cookiesName);
    cookiesName.clear();
    cookiesName = null;
}

From source file:org.waarp.gateway.kernel.http.HttpWriteCacheEnable.java

License:Open Source License

/**
 * Remove the given named cookie/*w  w w  .  j a va2 s . co  m*/
 * 
 * @param request
 * @param response
 * @param cookieNameToRemove
 */
public static void handleCookies(HttpRequest request, HttpResponse response, String cookieNameToRemove) {
    String cookieString = request.headers().get(HttpHeaderNames.COOKIE);
    if (cookieString != null) {
        Set<Cookie> cookies = ServerCookieDecoder.LAX.decode(cookieString);
        if (!cookies.isEmpty()) {
            // Reset the sessions if necessary.
            // Remove all Session for images
            for (Cookie cookie : cookies) {
                if (cookie.name().equalsIgnoreCase(cookieNameToRemove)) {
                } else {
                    response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
                }
            }
        }
    }
}

From source file:org.waarp.gateway.kernel.rest.HttpRestHandler.java

License:Open Source License

/**
 * Method to set Cookies in httpResponse from response ObjectNode
 * /*from   w  w w. ja v a 2s . c o  m*/
 * @param httpResponse
 */
protected void setCookies(FullHttpResponse httpResponse) {
    if (response == null) {
        return;
    }
    ObjectNode cookieON = response.getCookieArgs();
    if (!cookieON.isMissingNode()) {
        Iterator<Entry<String, JsonNode>> iter = cookieON.fields();
        while (iter.hasNext()) {
            Entry<String, JsonNode> entry = iter.next();
            httpResponse.headers().add(HttpHeaderNames.SET_COOKIE,
                    ServerCookieEncoder.LAX.encode(entry.getKey(), entry.getValue().asText()));
        }
    }
}

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.java2s  .c om
                } 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/*  w  w  w  .j  ava  2 s .co  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/*from  ww w .j  a v a  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);
    }
}