io.netty.handler.codec.http.cookie.Cookie Java Examples
The following examples show how to use
io.netty.handler.codec.http.cookie.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: HarCaptureFilter.java From AndroidHttpCapture with MIT License | 6 votes |
protected void captureRequestCookies(HttpRequest httpRequest) { String cookieHeader = httpRequest.headers().get(HttpHeaders.Names.COOKIE); if (cookieHeader == null) { return; } Set<Cookie> cookies = ServerCookieDecoder.LAX.decode(cookieHeader); for (Cookie cookie : cookies) { HarCookie harCookie = new HarCookie(); harCookie.setName(cookie.name()); harCookie.setValue(cookie.value()); harEntry.getRequest().getCookies().add(harCookie); } }
Example #2
Source File: BaseResponseInfoTest.java From riposte with Apache License 2.0 | 6 votes |
public static <T> BaseResponseInfo<T> createNewBaseResponseInfoForTesting(Integer httpStatusCode, HttpHeaders headers, String desiredContentWriterMimeType, Charset desiredContentWriterEncoding, Set<Cookie> cookies, boolean preventCompressedOutput) { return new BaseResponseInfo<T>(httpStatusCode, headers, desiredContentWriterMimeType, desiredContentWriterEncoding, cookies, preventCompressedOutput) { @Override public boolean isChunkedResponse() { throw new UnsupportedOperationException("not implemented, don't call me during the test"); } @Override public @Nullable T getContentForFullResponse() { throw new UnsupportedOperationException("not implemented, don't call me during the test"); } @Override public void setContentForFullResponse(@Nullable T contentForFullResponse) { throw new UnsupportedOperationException("not implemented, don't call me during the test"); } }; }
Example #3
Source File: SessionAwareInterceptor.java From vertx-web with Apache License 2.0 | 6 votes |
private void prepareRedirectRequest(HttpContext<?> context) { // Now the context contains the redirect request in clientRequest() and the original request in request() HttpClientRequest redirectRequest = context.clientRequest(); HttpRequestImpl<?> originalRequest = (HttpRequestImpl<?>) context.request(); WebClientSessionAware webclient = (WebClientSessionAware) originalRequest.client; MultiMap headers = context.get(HEADERS_CONTEXT_KEY); if (headers == null) { headers = HttpHeaders.headers().addAll(redirectRequest.headers()); context.set(SessionAwareInterceptor.HEADERS_CONTEXT_KEY, headers); } String redirectHost = URI.create(redirectRequest.absoluteURI()).getHost(); String domain; if (redirectHost.equals(originalRequest.host()) && originalRequest.virtualHost != null) { domain = originalRequest.virtualHost; } else { domain = redirectHost; } Iterable<Cookie> cookies = webclient.cookieStore().get(originalRequest.ssl, domain, redirectRequest.path()); for (Cookie c : cookies) { redirectRequest.headers().add("cookie", ClientCookieEncoder.STRICT.encode(c)); } }
Example #4
Source File: Utils.java From cxf with Apache License 2.0 | 6 votes |
public static Collection<Cookie> getCookies(String name, HttpRequest request) { String cookieString = request.headers().get(COOKIE); if (cookieString != null) { List<Cookie> foundCookie = new ArrayList<>(); Set<Cookie> cookies = ServerCookieDecoder.STRICT.decode(cookieString); for (Cookie cookie : cookies) { if (cookie.name().equals(name)) { foundCookie.add(cookie); } } return foundCookie; } return null; }
Example #5
Source File: HttpUtilsTest.java From riposte with Apache License 2.0 | 6 votes |
@Test public void extractCookies_works_if_cookies_defined_in_headers() { // given Cookie cookie1 = new DefaultCookie(UUID.randomUUID().toString(), UUID.randomUUID().toString()); Cookie cookie2 = new DefaultCookie(UUID.randomUUID().toString(), UUID.randomUUID().toString()); HttpHeaders headers = new DefaultHttpHeaders().add(HttpHeaders.Names.COOKIE, ClientCookieEncoder.LAX.encode(cookie1, cookie2)); HttpRequest nettyRequestMock = mock(HttpRequest.class); doReturn(headers).when(nettyRequestMock).headers(); // when Set<Cookie> extractedCookies = HttpUtils.extractCookies(nettyRequestMock); // then assertThat(extractedCookies.contains(cookie1), is(true)); assertThat(extractedCookies.contains(cookie2), is(true)); }
Example #6
Source File: HttpUtilsTest.java From riposte with Apache License 2.0 | 6 votes |
@Test public void extractCookies_works_if_cookies_defined_in_trailing_headers() { // given Cookie cookie1 = new DefaultCookie(UUID.randomUUID().toString(), UUID.randomUUID().toString()); Cookie cookie2 = new DefaultCookie(UUID.randomUUID().toString(), UUID.randomUUID().toString()); HttpHeaders trailingHeaders = new DefaultHttpHeaders().add(HttpHeaders.Names.COOKIE, ClientCookieEncoder.LAX.encode(cookie1, cookie2)); FullHttpRequest nettyRequestMock = mock(FullHttpRequest.class); doReturn(new DefaultHttpHeaders()).when(nettyRequestMock).headers(); doReturn(trailingHeaders).when(nettyRequestMock).trailingHeaders(); // when Set<Cookie> extractedCookies = HttpUtils.extractCookies(nettyRequestMock); // then assertThat(extractedCookies.contains(cookie1), is(true)); assertThat(extractedCookies.contains(cookie2), is(true)); }
Example #7
Source File: ReactorServerHttpResponse.java From spring-analysis-note with MIT License | 6 votes |
@Override protected void applyCookies() { for (String name : getCookies().keySet()) { for (ResponseCookie httpCookie : getCookies().get(name)) { Cookie cookie = new DefaultCookie(name, httpCookie.getValue()); if (!httpCookie.getMaxAge().isNegative()) { cookie.setMaxAge(httpCookie.getMaxAge().getSeconds()); } if (httpCookie.getDomain() != null) { cookie.setDomain(httpCookie.getDomain()); } if (httpCookie.getPath() != null) { cookie.setPath(httpCookie.getPath()); } cookie.setSecure(httpCookie.isSecure()); cookie.setHttpOnly(httpCookie.isHttpOnly()); this.response.addCookie(cookie); } } }
Example #8
Source File: HttpController.java From litchi with Apache License 2.0 | 6 votes |
public void writeFile(String fileName, String text) { ByteBuf content = Unpooled.copiedBuffer(text, CharsetUtil.UTF_8); HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content); response.headers().set("Pragma", "Pragma"); response.headers().set("Expires", "0"); response.headers().set("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.headers().set("Content-Type", "application/download"); response.headers().set("Content-Disposition", "attachment;filename=" + fileName); response.headers().set("Content-Transfer-Encoding", "binary"); if (enableCookies) { for (Map.Entry<String, Cookie> entry : cookieMaps.entrySet()) { response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode(entry.getValue())); } } // 跨域支持 response.headers().add("Access-Control-Allow-Origin", "*"); response.headers().add("Access-Control-Allow-Methods", "POST"); HttpUtil.setContentLength(response, content.readableBytes()); channel.writeAndFlush(response); //.addListener(ChannelFutureListener.CLOSE); }
Example #9
Source File: AuthController.java From leo-im-server with Apache License 2.0 | 6 votes |
/** * 从cookie中得到Session Id * @param request * @return */ private String getJSessionId(FullHttpRequest request) { try { String cookieStr = request.headers().get("Cookie"); if(cookieStr == null || cookieStr.trim().isEmpty()) { return null; } Set<Cookie> cookies = ServerCookieDecoder.STRICT.decode(cookieStr); Iterator<Cookie> it = cookies.iterator(); while (it.hasNext()) { Cookie cookie = it.next(); if (cookie.name().equals(CacheKeys.JSESSIONID)) { if (CacheManagerFactory.getCacheManager().get(cookie.value()) != null) { return cookie.value(); } } } } catch (Exception e1) { return null; } return null; }
Example #10
Source File: UserController.java From leo-im-server with Apache License 2.0 | 6 votes |
/** * 从cookie中得到Session Id * @return */ private String getJSessionId(FullHttpRequest request) { try { String cookieStr = request.headers().get("Cookie"); if(cookieStr == null || cookieStr.trim().isEmpty()) { return null; } Set<Cookie> cookies = ServerCookieDecoder.STRICT.decode(cookieStr); Iterator<Cookie> it = cookies.iterator(); while (it.hasNext()) { Cookie cookie = it.next(); if (cookie.name().equals(CacheKeys.JSESSIONID)) { if (CacheManagerFactory.getCacheManager().get(cookie.value()) != null) { return cookie.value(); } } } } catch (Exception e1) { return null; } return null; }
Example #11
Source File: NettyConnector.java From activemq-artemis with Apache License 2.0 | 6 votes |
@Override public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception { FullHttpResponse response = (FullHttpResponse) msg; if (httpRequiresSessionId && !active) { final List<String> setCookieHeaderValues = response.headers().getAll(HttpHeaderNames.SET_COOKIE); for (String setCookieHeaderValue : setCookieHeaderValues) { final Cookie cookie = ClientCookieDecoder.LAX.decode(setCookieHeaderValue); if ("JSESSIONID".equals(cookie.name())) { this.cookie = setCookieHeaderValue; break; } } active = true; handShakeFuture.run(); } waitingGet = false; ctx.fireChannelRead(response.content()); }
Example #12
Source File: DefaultWebRes.java From krpc with Apache License 2.0 | 5 votes |
public String getCookie(String name) { if (cookies == null) return null; for (Cookie c : cookies) { if (c.name().equals(name)) return c.value(); } return null; }
Example #13
Source File: HttpServerOperations.java From reactor-netty with Apache License 2.0 | 5 votes |
@Override public Map<CharSequence, Set<Cookie>> cookies() { if (cookieHolder != null) { return cookieHolder.getCachedCookies(); } throw new IllegalStateException("request not parsed"); }
Example #14
Source File: HttpSnoopServerHandler.java From julongchain with Apache License 2.0 | 5 votes |
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) { // 用以判断是否要关闭链接. boolean keepAlive = HttpUtil.isKeepAlive(request); // 构建Respons对象. FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, currentObj.decoderResult().isSuccess()? OK : BAD_REQUEST, Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8)); response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8"); if (keepAlive) { // 只为keep-alive 链接增加 'Content-Length' 头. response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes()); // 根据以下规范增加 keep alive header a头: // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); } // 编码 cookie. String cookieString = request.headers().get(HttpHeaderNames.COOKIE); if (cookieString != null) { Set<Cookie> cookies = ServerCookieDecoder.STRICT.decode(cookieString); if (!cookies.isEmpty()) { // 重置cookie. for (Cookie cookie: cookies) { response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode(cookie)); } } } else { // 为浏览器添加一些cookie. response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode("key1", "value1")); response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode("key2", "value2")); } // 发送响应内容. ctx.write(response); return keepAlive; }
Example #15
Source File: HttpUtilsTest.java From riposte with Apache License 2.0 | 5 votes |
@Test public void extractCookies_does_not_use_trailing_headers_if_trailing_headers_is_null() { // given HttpRequest nettyRequestMock = mock(HttpRequest.class); doReturn(new DefaultHttpHeaders()).when(nettyRequestMock).headers(); // when Set<Cookie> extractedCookies = HttpUtils.extractCookies(nettyRequestMock); // then assertThat(extractedCookies, notNullValue()); assertThat(extractedCookies.isEmpty(), is(true)); }
Example #16
Source File: ChunkedResponseInfoTest.java From riposte with Apache License 2.0 | 5 votes |
@Test public void uber_constructor_for_chunked_response_sets_fields_as_expected() { // given int httpStatusCode = 200; HttpHeaders headers = new DefaultHttpHeaders(); String mimeType = "text/text"; Charset contentCharset = CharsetUtil.UTF_8; Set<Cookie> cookies = Sets.newHashSet(new DefaultCookie("key1", "val1"), new DefaultCookie("key2", "val2")); boolean preventCompressedResponse = true; // when ChunkedResponseInfo responseInfo = new ChunkedResponseInfo(httpStatusCode, headers, mimeType, contentCharset, cookies, preventCompressedResponse); // then assertThat(responseInfo.getHttpStatusCode(), is(httpStatusCode)); assertThat(responseInfo.getHeaders(), is(headers)); assertThat(responseInfo.getDesiredContentWriterMimeType(), is(mimeType)); assertThat(responseInfo.getDesiredContentWriterEncoding(), is(contentCharset)); assertThat(responseInfo.getCookies(), is(cookies)); assertThat(responseInfo.getUncompressedRawContentLength(), nullValue()); assertThat(responseInfo.getFinalContentLength(), nullValue()); assertThat(responseInfo.isPreventCompressedOutput(), is(preventCompressedResponse)); assertThat(responseInfo.isChunkedResponse(), is(true)); assertThat(responseInfo.isResponseSendingStarted(), is(false)); assertThat(responseInfo.isResponseSendingLastChunkSent(), is(false)); }
Example #17
Source File: HttpClientOperations.java From reactor-netty with Apache License 2.0 | 5 votes |
@Override public HttpClientRequest addCookie(Cookie cookie) { if (!hasSentHeaders()) { this.requestHeaders.add(HttpHeaderNames.COOKIE, cookieEncoder.encode(cookie)); } else { throw new IllegalStateException("Status and headers already sent"); } return this; }
Example #18
Source File: ChunkedResponseInfoTest.java From riposte with Apache License 2.0 | 5 votes |
@Test public void builder_works_as_expected_for_all_fields() { // given ChunkedResponseInfo.ChunkedResponseInfoBuilder builder = ResponseInfo.newChunkedResponseBuilder(); int statusCode = 42; HttpHeaders headers = mock(HttpHeaders.class); String mimeType = UUID.randomUUID().toString(); Charset encoding = CharsetUtil.US_ASCII; Set<Cookie> cookies = mock(Set.class); boolean preventCompressedOutput = Math.random() > 0.5; // when ChunkedResponseInfo responseInfo = builder .withHttpStatusCode(statusCode) .withHeaders(headers) .withDesiredContentWriterMimeType(mimeType) .withDesiredContentWriterEncoding(encoding) .withCookies(cookies) .withPreventCompressedOutput(preventCompressedOutput) .build(); // then assertThat(responseInfo.getHttpStatusCode(), is(statusCode)); assertThat(responseInfo.getHeaders(), is(headers)); assertThat(responseInfo.getDesiredContentWriterMimeType(), is(mimeType)); assertThat(responseInfo.getDesiredContentWriterEncoding(), is(encoding)); assertThat(responseInfo.getCookies(), is(cookies)); assertThat(responseInfo.isPreventCompressedOutput(), is(preventCompressedOutput)); }
Example #19
Source File: BaseTransport.java From vertx-web with Apache License 2.0 | 5 votes |
static MultiMap removeCookieHeaders(MultiMap headers) { // We don't want to remove the JSESSION cookie. String cookieHeader = headers.get(COOKIE); if (cookieHeader != null) { headers.remove(COOKIE); Set<Cookie> nettyCookies = ServerCookieDecoder.STRICT.decode(cookieHeader); for (Cookie cookie: nettyCookies) { if (cookie.name().equals("JSESSIONID")) { headers.add(COOKIE, ServerCookieEncoder.STRICT.encode(cookie)); break; } } } return headers; }
Example #20
Source File: SessionAwareWebClientTest.java From vertx-web with Apache License 2.0 | 5 votes |
@Test public void testCookieStoreIsFluent(TestContext context) { CookieStore store = CookieStore.build(); Cookie cookie = new DefaultCookie("a", "a"); context.assertTrue(store == store.put(cookie)); context.assertTrue(store == store.remove(cookie)); }
Example #21
Source File: DefaultWebRes.java From krpc with Apache License 2.0 | 5 votes |
public DefaultWebRes addCookie(String name, String value) { String paramsStr = null; int p = value.indexOf("^"); if (p >= 0) { paramsStr = value.substring(p + 1); value = value.substring(0, p); } if (cookies == null) cookies = new ArrayList<>(); Cookie c = new DefaultCookie(name, value); if (paramsStr != null) { Map<String, String> params = Plugin.defaultSplitParams(paramsStr); if (params.containsKey("domain")) c.setDomain(params.get("domain")); if (params.containsKey("path")) c.setPath(params.get("path")); if (params.containsKey("maxAge")) c.setMaxAge(Long.parseLong(params.get("maxAge"))); if (params.containsKey("httpOnly")) c.setHttpOnly(Boolean.parseBoolean(params.get("httpOnly"))); else c.setHttpOnly(true); if (params.containsKey("secure")) c.setSecure(Boolean.parseBoolean(params.get("secure"))); if (params.containsKey("wrap")) c.setWrap(Boolean.parseBoolean(params.get("wrap"))); } cookies.add(c); return this; }
Example #22
Source File: RakamHttpRequest.java From netty-rest with Apache License 2.0 | 5 votes |
public Set<Cookie> cookies() { if (cookies == null) { String header = request.headers().get(COOKIE); cookies = header != null ? STRICT.decode(header) : ImmutableSet.of(); } return cookies; }
Example #23
Source File: EppServiceHandlerTest.java From nomulus with Apache License 2.0 | 5 votes |
private FullHttpRequest makeEppHttpRequest(String content, Cookie... cookies) { return TestUtils.makeEppHttpRequest( content, RELAY_HOST, RELAY_PATH, ACCESS_TOKEN, getCertificateHash(clientCertificate), CLIENT_ADDRESS, cookies); }
Example #24
Source File: BaseResponseInfoBuilderTest.java From riposte with Apache License 2.0 | 5 votes |
@Test public void builder_stores_values_as_expected() { // given String content = UUID.randomUUID().toString(); int httpStatusCode = 200; HttpHeaders headers = new DefaultHttpHeaders(); String mimeType = "text/text"; Charset contentCharset = CharsetUtil.ISO_8859_1; Set<Cookie> cookies = Sets.newHashSet(new DefaultCookie("key1", "val1"), new DefaultCookie("key2", "val2")); boolean preventCompressedOutput = true; // when BaseResponseInfoBuilder<String> responseInfoBuilder = new BaseResponseInfoBuilder<String>(){} .withHttpStatusCode(httpStatusCode) .withHeaders(headers) .withDesiredContentWriterMimeType(mimeType) .withDesiredContentWriterEncoding(contentCharset).withCookies(cookies) .withPreventCompressedOutput(preventCompressedOutput); // then assertThat(responseInfoBuilder.getHttpStatusCode(), is(httpStatusCode)); assertThat(responseInfoBuilder.getHeaders(), is(headers)); assertThat(responseInfoBuilder.getDesiredContentWriterMimeType(), is(mimeType)); assertThat(responseInfoBuilder.getDesiredContentWriterEncoding(), is(contentCharset)); assertThat(responseInfoBuilder.getCookies(), is(cookies)); assertThat(responseInfoBuilder.isPreventCompressedOutput(), is(preventCompressedOutput)); }
Example #25
Source File: RedantContext.java From redant with Apache License 2.0 | 5 votes |
public RedantContext addCookies(Set<Cookie> cookieSet){ if(CollectionUtil.isNotEmpty(cookieSet)){ if(CollectionUtil.isEmpty(cookies)){ cookies = new HashSet<>(); } cookies.addAll(cookieSet); } return this; }
Example #26
Source File: HttpResponse.java From blade with Apache License 2.0 | 5 votes |
@Override public Response cookie(@NonNull String name, @NonNull String value, int maxAge, boolean secured) { Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(name, value); nettyCookie.setPath("/"); nettyCookie.setMaxAge(maxAge); nettyCookie.setSecure(secured); this.cookies.add(nettyCookie); return this; }
Example #27
Source File: DefaultCookieManager.java From redant with Apache License 2.0 | 5 votes |
@Override public boolean deleteCookie(String name) { Cookie cookie = getCookie(name); if (cookie != null) { cookie.setMaxAge(0); cookie.setPath("/"); setCookie(cookie); return true; } return false; }
Example #28
Source File: DefaultCookieManager.java From redant with Apache License 2.0 | 5 votes |
@Override public Set<Cookie> getCookies() { HttpRequest request = RedantContext.currentContext().getRequest(); Set<Cookie> cookies = new HashSet<>(); if (request != null) { String value = request.headers().get(HttpHeaderNames.COOKIE); if (value != null) { cookies = ServerCookieDecoder.STRICT.decode(value); } } return cookies; }
Example #29
Source File: DefaultCookieManager.java From redant with Apache License 2.0 | 5 votes |
@Override public Map<String, Cookie> getCookieMap() { Map<String, Cookie> cookieMap = new HashMap<>(); Set<Cookie> cookies = getCookies(); if (null != cookies && !cookies.isEmpty()) { for (Cookie cookie : cookies) { cookieMap.put(cookie.name(), cookie); } } return cookieMap; }
Example #30
Source File: DefaultCookieManager.java From redant with Apache License 2.0 | 5 votes |
@Override public void setCookies() { Set<Cookie> cookies = getCookies(); if (!cookies.isEmpty()) { for (Cookie cookie : cookies) { setCookie(cookie); } } }