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

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

Introduction

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

Prototype

ServerCookieDecoder LAX

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

Click Source Link

Document

Lax instance that doesn't validate name and value

Usage

From source file:co.paralleluniverse.comsat.webactors.netty.HttpRequestWrapper.java

License:Open Source License

static Set<io.netty.handler.codec.http.cookie.Cookie> getNettyCookies(FullHttpRequest req) {
    final HttpHeaders heads = req.headers();
    final String head = heads != null ? heads.get(HttpHeaders.Names.COOKIE) : null;
    if (head != null)
        return ServerCookieDecoder.LAX.decode(head);
    else//from  w w w  .  jav  a2  s  . c  o m
        return EMPTY_SET;
}

From source file:com.bay1ts.bay.core.Request.java

License:Apache License

/**
 * @return request cookies (or empty Map if cookies aren't present)
 *///  w w  w .ja va  2  s  .co m
public Map<String, String> cookies() {
    Map<String, String> result = new HashMap<>();
    String cookieString = fullHttpRequest.headers().get(HttpHeaderNames.COOKIE);
    Set<Cookie> cookieSet = null;
    if (cookieString != null) {
        cookieSet = ServerCookieDecoder.LAX.decode(cookieString);
    }
    if (cookieSet != null && !cookieSet.isEmpty()) {
        for (Cookie cookie : cookieSet) {
            result.put(cookie.name(), cookie.value());
        }
    }
    //        Cookie[] cookies = fullHttpRequest.getCookies();
    //        if (cookies != null) {
    //            for (Cookie cookie : cookies) {
    //                result.put(cookie.name(), cookie.value());
    //            }
    //        }
    return result;
}

From source file:com.bay1ts.bay.core.Request.java

License:Apache License

/**
 * Gets cookie by name./*from w w w  .  ja v a 2s .com*/
 *
 * @param name name of the cookie
 * @return cookie value or null if the cookie was not found
 */
public String cookie(String name) {
    String cookieString = fullHttpRequest.headers().get(HttpHeaderNames.COOKIE);
    Set<Cookie> cookieSet = null;
    if (cookieString != null) {
        cookieSet = ServerCookieDecoder.LAX.decode(cookieString);
    }
    if (cookieSet != null && !cookieSet.isEmpty()) {
        for (Cookie cookie : cookieSet) {
            if (cookie.name().equals(name)) {
                return cookie.value();
            }
        }
    }
    return null;
}

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.
 * //from  w w  w  . jav a 2s .  c  om
 * @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.RequestImpl.java

License:Open Source License

private void readCookie() {
    if (cookies != null)
        return;/*from   w  w w  .j  a  va 2 s.  c om*/
    fullCookies = new HashMap<>();
    cookies = new HashMap<>();
    String cookieString = nettyHeaders.get(COOKIE);
    if (cookieString != null) {
        Set<Cookie> cookies = ServerCookieDecoder.LAX.decode(cookieString);
        if (!cookies.isEmpty()) {
            for (Cookie cookie : cookies) {
                fullCookies.put(cookie.name(), cookie);
                this.cookies.put(cookie.name(), cookie.value());
            }
        }

    }
}

From source file:org.atmosphere.nettosphere.BridgeRuntime.java

License:Apache License

private Set<javax.servlet.http.Cookie> getCookies(final HttpRequest request) {
    Set<javax.servlet.http.Cookie> result = new HashSet<javax.servlet.http.Cookie>();
    String cookieHeader = request.headers().get("Cookie");
    if (cookieHeader != null) {
        Set<io.netty.handler.codec.http.cookie.Cookie> cookies = ServerCookieDecoder.LAX.decode(cookieHeader);
        for (io.netty.handler.codec.http.cookie.Cookie cookie : cookies) {
            javax.servlet.http.Cookie c = new javax.servlet.http.Cookie(cookie.name(), cookie.value());

            if (cookie.domain() != null) {
                c.setDomain(cookie.domain());
            }/* ww  w. jav a  2s.  c om*/

            c.setHttpOnly(cookie.isHttpOnly());
            c.setMaxAge((int) cookie.maxAge());
            if (cookie.path() != null) {
                c.setPath(cookie.path());
            }

            c.setSecure(cookie.isSecure());
            result.add(c);

        }
    }
    return result;
}

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

License:Open Source License

/**
 * set values from Cookies/*w  w w . j  a va 2s . com*/
 * 
 * @throws HttpIncorrectRequestException
 */
protected void getCookieArgs() throws HttpIncorrectRequestException {
    Set<Cookie> cookies;
    String value = request.headers().get(HttpHeaderNames.COOKIE);
    if (value == null) {
        cookies = Collections.emptySet();
    } else {
        cookies = ServerCookieDecoder.LAX.decode(value);
    }
    if (!cookies.isEmpty()) {
        for (Cookie cookie : cookies) {
            if (isCookieValid(cookie)) {
                httpPage.setValue(businessRequest, cookie.name(), cookie.value(), FieldPosition.COOKIE);
            }
        }
    }
    cookies.clear();
    cookies = null;
}

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

License:Open Source License

/**
 * Method to set Cookies in response//from   www . j ava2 s  .c om
 * 
 * @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/*from   w  w w.j  av  a 2s  .  c om*/
 * 
 * @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.RestArgument.java

License:Open Source License

/**
 * set values from Cookies into arguments.path(ARGS_COOKIE)
 * /*  w ww. j  av a  2  s .  c o  m*/
 */
public void setCookieArgs(String cookieString) {
    Set<Cookie> cookies;
    if (cookieString == null) {
        cookies = Collections.emptySet();
    } else {
        cookies = ServerCookieDecoder.LAX.decode(cookieString);
    }
    if (!cookies.isEmpty()) {
        ObjectNode node = arguments.putObject(REST_GROUP.ARGS_COOKIE.group);
        for (Cookie cookie : cookies) {
            node.put(cookie.name(), cookie.value());
        }
    }
}