io.netty.handler.codec.http.Cookie Java Examples

The following examples show how to use io.netty.handler.codec.http.Cookie. 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: HttpRequestMessageImpl.java    From zuul with Apache License 2.0 6 votes vote down vote up
@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 #2
Source File: TrackingPixelRouteHandler.java    From Sparkngin with GNU Affero General Public License v3.0 6 votes vote down vote up
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 #3
Source File: ServletNettyChannelHandler.java    From netty-cookbook with Apache License 2.0 6 votes vote down vote up
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 #4
Source File: FunctionsChannelHandler.java    From netty-cookbook with Apache License 2.0 6 votes vote down vote up
@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 #5
Source File: NettyHttpRequest.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private void addCookies(Map<String, String> cookies) {
   if (cookies != null && cookies.size() > 0) {
      List<Cookie> cookieList = new ArrayList<>();
      for (String name : cookies.keySet()) {
         cookieList.add(new DefaultCookie(name, cookies.get(name)));
      }
      request.headers().set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(cookieList));
   }
}
 
Example #6
Source File: Cookies.java    From zuul with Apache License 2.0 5 votes vote down vote up
public String getFirstValue(String name)
{
    Cookie c = getFirst(name);
    String value;
    if (c != null) {
        value = c.getValue();
    } else {
        value = null;
    }
    return value;
}
 
Example #7
Source File: Cookies.java    From zuul with Apache License 2.0 5 votes vote down vote up
public Cookie getFirst(String name)
{
    List<Cookie> found = map.get(name);
    if (found == null || found.size() == 0) {
        return null;
    }
    return found.get(0);
}
 
Example #8
Source File: Cookies.java    From zuul with Apache License 2.0 5 votes vote down vote up
public void add(Cookie cookie)
{
    List<Cookie> existing = map.get(cookie.getName());
    if (existing == null) {
        existing = new ArrayList<>();
        map.put(cookie.getName(), existing);
    }
    existing.add(cookie);
    all.add(cookie);
}
 
Example #9
Source File: HttpResponseMessageImpl.java    From zuul with Apache License 2.0 5 votes vote down vote up
@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 #10
Source File: HttpResponseMessageImpl.java    From zuul with Apache License 2.0 5 votes vote down vote up
@Override
public Cookies parseSetCookieHeader(String setCookieValue)
{
    Cookies cookies = new Cookies();
    for (Cookie cookie : CookieDecoder.decode(setCookieValue)) {
        cookies.add(cookie);
    }
    return cookies;
}
 
Example #11
Source File: SamplePushAuthHandler.java    From zuul with Apache License 2.0 5 votes vote down vote up
@Override
protected PushUserAuth doAuth(FullHttpRequest req) {
    final Cookies cookies = parseCookies(req);
    for (final Cookie c : cookies.getAll()) {
        if(c.getName().equals("userAuthCookie")) {
            final String customerId = c.getValue();
            if (!Strings.isNullOrEmpty(customerId)) {
                return new SamplePushUserAuth(customerId);
            }
        }
    }
    return new SamplePushUserAuth(HttpResponseStatus.UNAUTHORIZED.code());
}
 
Example #12
Source File: NettyRequest.java    From ob1k with Apache License 2.0 5 votes vote down vote up
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 #13
Source File: NettyRequest.java    From ob1k with Apache License 2.0 5 votes vote down vote up
@Override
public String getCookie(final String cookieName) {
  if (cookies == null) {
    populateCookies();
  }

  final Cookie cookie = cookies.get(cookieName);
  return cookie != null ? cookie.getValue() : null;
}
 
Example #14
Source File: HttpRequestData.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
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 #15
Source File: HttpRequestData.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
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 vote down vote up
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: NettyHttpResponse.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
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 #18
Source File: HttpRequestData.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
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 #19
Source File: HttpRequestData.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
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 #20
Source File: HttpRequestData.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
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 #21
Source File: HttpRequestData.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
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 #22
Source File: Utils.java    From Jinx with Apache License 2.0 5 votes vote down vote up
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 #23
Source File: HttpSnoopServerHandler.java    From netty.book.kor with MIT License 5 votes vote down vote up
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 #24
Source File: HttpSnoopServerHandler.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
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 #25
Source File: Utils.java    From Jinx with Apache License 2.0 5 votes vote down vote up
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 #26
Source File: SimpleHttpRequest.java    From netty-cookbook with Apache License 2.0 4 votes vote down vote up
public Set<Cookie> getCookies() {
	return cookies;
}
 
Example #27
Source File: Cookies.java    From zuul with Apache License 2.0 4 votes vote down vote up
public List<Cookie> get(String name)
{
    return map.get(name);
}
 
Example #28
Source File: Cookies.java    From zuul with Apache License 2.0 4 votes vote down vote up
public List<Cookie> getAll()
{
    return all;
}
 
Example #29
Source File: HttpResponseMessageImpl.java    From zuul with Apache License 2.0 4 votes vote down vote up
@Override
public void setSetCookie(Cookie cookie)
{
    getHeaders().set(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.encode(cookie));
}
 
Example #30
Source File: HttpResponseMessageImpl.java    From zuul with Apache License 2.0 4 votes vote down vote up
@Override
public void addSetCookie(Cookie cookie)
{
    getHeaders().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.encode(cookie));
}