Java Code Examples for io.netty.handler.codec.http.CookieDecoder#decode()

The following examples show how to use io.netty.handler.codec.http.CookieDecoder#decode() . 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 vote down vote up
@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 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 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: 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 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
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 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: 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 16
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 17
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 18
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 19
Source File: SampleHandler.java    From xio with Apache License 2.0 4 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 20
Source File: SampleHandler.java    From xio with Apache License 2.0 4 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;
}