io.netty.handler.codec.http.CookieDecoder Java Examples
The following examples show how to use
io.netty.handler.codec.http.CookieDecoder.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NettyHttpServletRequest.java From Jinx with Apache License 2.0 | 6 votes |
@Override public Cookie[] getCookies() { String cookieString = this.request.headers().get(COOKIE); if (cookieString != null) { Set<io.netty.handler.codec.http.Cookie> cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { Cookie[] cookiesArray = new Cookie[cookies.size()]; int indx = 0; for (io.netty.handler.codec.http.Cookie c : cookies) { Cookie cookie = new Cookie(c.getName(), c.getValue()); cookie.setComment(c.getComment()); cookie.setDomain(c.getDomain()); cookie.setMaxAge((int) c.getMaxAge()); cookie.setPath(c.getPath()); cookie.setSecure(c.isSecure()); cookie.setVersion(c.getVersion()); cookiesArray[indx] = cookie; indx++; } return cookiesArray; } } return new Cookie[0]; }
Example #2
Source File: HttpRequestMessageImpl.java From zuul with Apache License 2.0 | 6 votes |
@Override public Cookies reParseCookies() { Cookies cookies = new Cookies(); for (String aCookieHeader : getHeaders().getAll(HttpHeaderNames.COOKIE)) { try { if (CLEAN_COOKIES.get()) { aCookieHeader = cleanCookieHeader(aCookieHeader); } Set<Cookie> decoded = CookieDecoder.decode(aCookieHeader, false); for (Cookie cookie : decoded) { cookies.add(cookie); } } catch (Exception e) { LOG.error(String.format("Error parsing request Cookie header. cookie=%s, request-info=%s", aCookieHeader, getInfoForLogging())); } } parsedCookies = cookies; return cookies; }
Example #3
Source File: TrackingPixelRouteHandler.java From Sparkngin with GNU Affero General Public License v3.0 | 6 votes |
void setCookie(HttpRequest request, FullHttpResponse response) { // Encode the cookie. String cookieString = request.headers().get(COOKIE); if (cookieString != null) { Set<Cookie> cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie: cookies) { if("visit-count".equals(cookie.getName())) { int count = Integer.parseInt(cookie.getValue()) ; cookie.setValue(Integer.toString(count + 1)); //System.out.println("Visit: " + count); } response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } } else { // Browser sent no cookie. Add some. response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("id", UUID.randomUUID().toString())); response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("visit-count", "1")); response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("first-visit-time", new Date().toString())); } }
Example #4
Source File: ServletNettyChannelHandler.java From netty-cookbook with Apache License 2.0 | 6 votes |
void copyToServletCookie(FullHttpRequest fullHttpReq, MockHttpServletRequest servletRequest){ String cookieString = fullHttpReq.headers().get(HttpHeaders.Names.COOKIE); if (cookieString != null) { Set<Cookie> cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { // Reset the cookies if necessary. javax.servlet.http.Cookie[] sCookies = new javax.servlet.http.Cookie[cookies.size()]; int i = 0; for (Cookie cookie: cookies) { javax.servlet.http.Cookie sCookie = new javax.servlet.http.Cookie(cookie.getName(), cookie.getValue()); sCookie.setPath(cookie.getPath()); sCookie.setMaxAge((int) cookie.getMaxAge()); sCookies[i++] = sCookie; } servletRequest.setCookies(sCookies); } } else { servletRequest.setCookies( new javax.servlet.http.Cookie[0]); } }
Example #5
Source File: FunctionsChannelHandler.java From netty-cookbook with Apache License 2.0 | 6 votes |
@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception { if (!request.getDecoderResult().isSuccess()) { sendError(ctx, BAD_REQUEST); return; } String uri = request.getUri(); QueryStringDecoder decoder = new QueryStringDecoder(uri); SimpleHttpRequest req = new SimpleHttpRequest(decoder.path(), decoder.parameters()); String cookieString = request.headers().get(HttpHeaders.Names.COOKIE); if (cookieString != null) { Set<Cookie> cookies = CookieDecoder.decode(cookieString); req.setCookies(cookies); } else { req.setCookies(Collections.emptySet()); } req.setHeaders(request.headers()); copyHttpBodyData(request, req); SimpleHttpResponse resp = eventHandler.apply(req); ctx.write( HttpEventHandler.buildHttpResponse(resp.toString(), resp.getStatus(), resp.getContentType()) ); ctx.flush().close(); }
Example #6
Source File: HttpRequestData.java From netty-cookbook with Apache License 2.0 | 5 votes |
public HttpRequestData(HttpRequestEvent event){ HttpHeaders headers = event.getRequest().headers(); String refererUrl = NettyHttpUtil.getRefererUrl(headers); String userAgent = headers.get(USER_AGENT); String cookieString = headers.get(COOKIE); Map<String, String> cookiesMap = null; if (cookieString != null) { try { Set<Cookie> cookies = CookieDecoder.decode(cookieString); int z = cookies.size(); if (z > 0) { cookiesMap = new HashMap<>(z); for (Cookie cookie : cookies) { cookiesMap.put(cookie.getName(), cookie.getValue()); } } else { cookiesMap = new HashMap<>(0); } } catch (Exception e) { e.printStackTrace(); System.err.println("--cookie: "+cookieString); } } set(userAgent, refererUrl, event.getParams(), cookiesMap); //System.out.println(cookieString + " request COOKIE " + cookies ); }
Example #7
Source File: HttpResponseMessageImpl.java From zuul with Apache License 2.0 | 5 votes |
@Override public boolean hasSetCookieWithName(String cookieName) { boolean has = false; for (String setCookieValue : getHeaders().getAll(HttpHeaderNames.SET_COOKIE)) { for (Cookie cookie : CookieDecoder.decode(setCookieValue)) { if (cookie.getName().equalsIgnoreCase(cookieName)) { has = true; break; } } } return has; }
Example #8
Source File: HttpResponseMessageImpl.java From zuul with Apache License 2.0 | 5 votes |
@Override public Cookies parseSetCookieHeader(String setCookieValue) { Cookies cookies = new Cookies(); for (Cookie cookie : CookieDecoder.decode(setCookieValue)) { cookies.add(cookie); } return cookies; }
Example #9
Source File: NettyRequest.java From ob1k with Apache License 2.0 | 5 votes |
private void populateCookies() { cookies = Maps.newHashMap(); final String cookieHeaderValue = inner.headers().get("Cookie"); if (cookieHeaderValue == null) return; final Set<Cookie> cookiesSet = CookieDecoder.decode(cookieHeaderValue, false); for (final Cookie cookie : cookiesSet) { cookies.put(cookie.getName(), cookie); } }
Example #10
Source File: HttpRequestData.java From netty-cookbook with Apache License 2.0 | 5 votes |
public HttpRequestData(HttpRequestEvent event){ HttpHeaders headers = event.getRequest().headers(); String refererUrl = NettyHttpUtil.getRefererUrl(headers); String userAgent = headers.get(USER_AGENT); String cookieString = headers.get(COOKIE); Map<String, String> cookiesMap = null; if (cookieString != null) { try { Set<Cookie> cookies = CookieDecoder.decode(cookieString); int z = cookies.size(); if (z > 0) { cookiesMap = new HashMap<>(z); for (Cookie cookie : cookies) { cookiesMap.put(cookie.getName(), cookie.getValue()); } } else { cookiesMap = new HashMap<>(0); } } catch (Exception e) { e.printStackTrace(); System.err.println("--cookie: "+cookieString); } } set(userAgent, refererUrl, event.getParams(), cookiesMap); //System.out.println(cookieString + " request COOKIE " + cookies ); }
Example #11
Source File: HttpRequestData.java From netty-cookbook with Apache License 2.0 | 5 votes |
public HttpRequestData(HttpRequestEvent event){ HttpHeaders headers = event.getRequest().headers(); String refererUrl = NettyHttpUtil.getRefererUrl(headers); String userAgent = headers.get(USER_AGENT); String cookieString = headers.get(COOKIE); Map<String, String> cookiesMap = null; if (cookieString != null) { try { Set<Cookie> cookies = CookieDecoder.decode(cookieString); int z = cookies.size(); if (z > 0) { cookiesMap = new HashMap<>(z); for (Cookie cookie : cookies) { cookiesMap.put(cookie.getName(), cookie.getValue()); } } else { cookiesMap = new HashMap<>(0); } } catch (Exception e) { e.printStackTrace(); System.err.println("--cookie: "+cookieString); } } set(userAgent, refererUrl, event.getParams(), cookiesMap); //System.out.println(cookieString + " request COOKIE " + cookies ); }
Example #12
Source File: HttpRequestData.java From netty-cookbook with Apache License 2.0 | 5 votes |
public HttpRequestData(HttpRequestEvent event){ HttpHeaders headers = event.getRequest().headers(); String refererUrl = NettyHttpUtil.getRefererUrl(headers); String userAgent = headers.get(USER_AGENT); String cookieString = headers.get(COOKIE); Map<String, String> cookiesMap = null; if (cookieString != null) { try { Set<Cookie> cookies = CookieDecoder.decode(cookieString); int z = cookies.size(); if (z > 0) { cookiesMap = new HashMap<>(z); for (Cookie cookie : cookies) { cookiesMap.put(cookie.getName(), cookie.getValue()); } } else { cookiesMap = new HashMap<>(0); } } catch (Exception e) { e.printStackTrace(); System.err.println("--cookie: "+cookieString); } } set(userAgent, refererUrl, event.getParams(), cookiesMap); //System.out.println(cookieString + " request COOKIE " + cookies ); }
Example #13
Source File: HttpRequestData.java From netty-cookbook with Apache License 2.0 | 5 votes |
public HttpRequestData(HttpRequestEvent event){ HttpHeaders headers = event.getRequest().headers(); String refererUrl = NettyHttpUtil.getRefererUrl(headers); String userAgent = headers.get(USER_AGENT); String cookieString = headers.get(COOKIE); Map<String, String> cookiesMap = null; if (cookieString != null) { try { Set<Cookie> cookies = CookieDecoder.decode(cookieString); int z = cookies.size(); if (z > 0) { cookiesMap = new HashMap<>(z); for (Cookie cookie : cookies) { cookiesMap.put(cookie.getName(), cookie.getValue()); } } else { cookiesMap = new HashMap<>(0); } } catch (Exception e) { e.printStackTrace(); System.err.println("--cookie: "+cookieString); } } set(userAgent, refererUrl, event.getParams(), cookiesMap); //System.out.println(cookieString + " request COOKIE " + cookies ); }
Example #14
Source File: NettyHttpResponse.java From arcusplatform with Apache License 2.0 | 5 votes |
private void decodeCookies() { List<String> values = response.headers().getAll(HttpHeaders.Names.SET_COOKIE); cookies = new HashMap<>(values.size()); for (String value : values) { Set<Cookie> setOfCookies = CookieDecoder.decode(value); for(Cookie cookie: setOfCookies) { cookies.put(cookie.getName(), cookie.getValue()); } } }
Example #15
Source File: HttpRequestData.java From netty-cookbook with Apache License 2.0 | 5 votes |
public HttpRequestData(HttpRequestEvent event){ HttpHeaders headers = event.getRequest().headers(); String refererUrl = NettyHttpUtil.getRefererUrl(headers); String userAgent = headers.get(USER_AGENT); String cookieString = headers.get(COOKIE); Map<String, String> cookiesMap = null; if (cookieString != null) { try { Set<Cookie> cookies = CookieDecoder.decode(cookieString); int z = cookies.size(); if (z > 0) { cookiesMap = new HashMap<>(z); for (Cookie cookie : cookies) { cookiesMap.put(cookie.getName(), cookie.getValue()); } } else { cookiesMap = new HashMap<>(0); } } catch (Exception e) { e.printStackTrace(); System.err.println("--cookie: "+cookieString); } } set(userAgent, refererUrl, event.getParams(), cookiesMap); //System.out.println(cookieString + " request COOKIE " + cookies ); }
Example #16
Source File: HttpRequestData.java From netty-cookbook with Apache License 2.0 | 5 votes |
public HttpRequestData(HttpRequestEvent event){ HttpHeaders headers = event.getRequest().headers(); String refererUrl = NettyHttpUtil.getRefererUrl(headers); String userAgent = headers.get(USER_AGENT); String cookieString = headers.get(COOKIE); Map<String, String> cookiesMap = null; if (cookieString != null) { try { Set<Cookie> cookies = CookieDecoder.decode(cookieString); int z = cookies.size(); if (z > 0) { cookiesMap = new HashMap<>(z); for (Cookie cookie : cookies) { cookiesMap.put(cookie.getName(), cookie.getValue()); } } else { cookiesMap = new HashMap<>(0); } } catch (Exception e) { e.printStackTrace(); System.err.println("--cookie: "+cookieString); } } set(userAgent, refererUrl, event.getParams(), cookiesMap); //System.out.println(cookieString + " request COOKIE " + cookies ); }
Example #17
Source File: HttpSnoopServerHandler.java From netty.book.kor with MIT License | 5 votes |
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) { // Decide whether to close the connection or not. boolean keepAlive = HttpHeaders.isKeepAlive(request); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, currentObj.decoderResult().isSuccess()? OK : BAD_REQUEST, Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8)); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); if (keepAlive) { // 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.KEEP_ALIVE); } // Encode the cookie. String cookieString = request.headers().get(COOKIE); if (cookieString != null) { Set<Cookie> cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie: cookies) { response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } } else { // Browser sent no cookie. Add some. response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1")); response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2")); } // Write the response. ctx.write(response); return keepAlive; }
Example #18
Source File: HttpSnoopServerHandler.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) { // Decide whether to close the connection or not. boolean keepAlive = HttpHeaders.isKeepAlive(request); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, currentObj.getDecoderResult().isSuccess()? OK : BAD_REQUEST, Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8)); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); if (keepAlive) { // 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.KEEP_ALIVE); } // Encode the cookie. String cookieString = request.headers().get(COOKIE); if (cookieString != null) { Set<Cookie> cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie: cookies) { response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } } else { // Browser sent no cookie. Add some. response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1")); response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2")); } // Write the response. ctx.write(response); return keepAlive; }
Example #19
Source File: Utils.java From Jinx with Apache License 2.0 | 5 votes |
public static final Collection<Cookie> getCookies(String name, HttpResponse response) { String cookieString = response.headers().get(COOKIE); if (cookieString != null) { List<Cookie> foundCookie = new ArrayList<Cookie>(); Set<Cookie> cookies = CookieDecoder.decode(cookieString); for (Cookie cookie : cookies) { if (cookie.getName().equals(name)) { foundCookie.add(cookie); } } return foundCookie; } return null; }
Example #20
Source File: Utils.java From Jinx with Apache License 2.0 | 5 votes |
public static Collection<Cookie> getCookies(String name, HttpRequest request) { String cookieString = request.headers().get(COOKIE); if (cookieString != null) { List<Cookie> foundCookie = new ArrayList<Cookie>(); Set<Cookie> cookies = CookieDecoder.decode(cookieString); for (Cookie cookie : cookies) { if (cookie.getName().equals(name)) foundCookie.add(cookie); } return foundCookie; } return null; }
Example #21
Source File: SampleHandler.java From xio with Apache License 2.0 | 4 votes |
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) { // Decide whether to close the connection or not. boolean keepAlive = HttpHeaders.isKeepAlive(request); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST, Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8)); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); if (keepAlive) { // 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.KEEP_ALIVE); } // Encode the cookie. String cookieString = request.headers().get(COOKIE); if (cookieString != null) { Set<Cookie> cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie : cookies) { response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } } else { // Browser sent no cookie. Add some. response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1")); response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2")); } // Write the response. ctx.write(response); return keepAlive; }
Example #22
Source File: SampleHandler.java From xio with Apache License 2.0 | 4 votes |
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) { // Decide whether to close the connection or not. boolean keepAlive = HttpHeaders.isKeepAlive(request); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST, Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8)); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); if (keepAlive) { // 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.KEEP_ALIVE); } // Encode the cookie. String cookieString = request.headers().get(COOKIE); if (cookieString != null) { Set<Cookie> cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie : cookies) { response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } } else { // Browser sent no cookie. Add some. response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1")); response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2")); } // Write the response. ctx.write(response); return keepAlive; }
Example #23
Source File: HttpUploadServerHandler.java From netty4.0.27Learn with Apache License 2.0 | 4 votes |
private void writeResponse(Channel channel) { // Convert the response content to a ChannelBuffer. ByteBuf buf = copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8); responseContent.setLength(0); // Decide whether to close the connection or not. boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request.headers().get(CONNECTION)) || request.getProtocolVersion().equals(HttpVersion.HTTP_1_0) && !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request.headers().get(CONNECTION)); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf); response.headers().set(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.headers().set(CONTENT_LENGTH, buf.readableBytes()); } Set<Cookie> cookies; String value = request.headers().get(COOKIE); if (value == null) { cookies = Collections.emptySet(); } else { cookies = CookieDecoder.decode(value); } if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie : cookies) { response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } // Write the response. ChannelFuture future = channel.writeAndFlush(response); // Close the connection after the write operation is done if necessary. if (close) { future.addListener(ChannelFutureListener.CLOSE); } }