当前位置: 首页>>代码示例>>Java>>正文


Java Cookie类代码示例

本文整理汇总了Java中io.netty.handler.codec.http.cookie.Cookie的典型用法代码示例。如果您正苦于以下问题:Java Cookie类的具体用法?Java Cookie怎么用?Java Cookie使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Cookie类属于io.netty.handler.codec.http.cookie包,在下文中一共展示了Cookie类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getUser

import io.netty.handler.codec.http.cookie.Cookie; //导入依赖的package包/类
/**
 * 从Http Cookie中获取用户Id
 * 
 * @param data
 * @return
 */
public static final PushUser getUser(HandshakeData data) {
    String _cookie = data.getSingleHeader(HttpHeaders.Names.COOKIE);
    if (_cookie != null) {
        Set<Cookie> cookies = ServerCookieDecoder.LAX.decode(_cookie);
        for (Cookie cookie : cookies) {
            if (TokenManager.LOGIN_COOKIE_NAME.equals(cookie.name())) {
                String value = cookie.value();
                if (value != null) {
                    return getUserIdFromCookie(value);
                }
            }
        }
    }
    return null;
}
 
开发者ID:bridgeli,项目名称:netty-socketio-demo,代码行数:22,代码来源:CookieUserAuthorizationListener.java

示例2: onRequestReceived

import io.netty.handler.codec.http.cookie.Cookie; //导入依赖的package包/类
@Override
public void onRequestReceived(ChannelHandlerContext ctx, HttpRequest request) {

    HttpSessionThreadLocal.unset();

    Collection<Cookie> cookies = Utils.getCookies(HttpSessionImpl.SESSION_ID_KEY, request);
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            String jsessionId = cookie.value();
            HttpSession s = HttpSessionThreadLocal.getSessionStore().findSession(jsessionId);
            if (s != null) {
                HttpSessionThreadLocal.set(s);
                this.sessionRequestedByCookie = true;
                break;
            }
        }
    }
}
 
开发者ID:geeker-lait,项目名称:tasfe-framework,代码行数:19,代码来源:HttpSessionInterceptor.java

示例3: getUrlConnection

import io.netty.handler.codec.http.cookie.Cookie; //导入依赖的package包/类
protected HttpsURLConnection getUrlConnection(String username, String password, URL url) throws Exception {
    HttpsURLConnection.setDefaultSSLSocketFactory(getSSLSocketFactory());
    URL loginURL = new URL(url.getProtocol() + "://" + url.getHost() + ":" + url.getPort() + "/login");
    HttpsURLConnection con = (HttpsURLConnection) loginURL.openConnection();
    con.setHostnameVerifier((host, session) -> true);
    con.setRequestMethod("GET");
    con.setDoOutput(true);
    con.setRequestProperty("Content-Type", "application/json");
    con.connect();
    int responseCode = con.getResponseCode();
    if (401 == responseCode) {
        throw new UnauthorizedUserException();
    }
    Assert.assertEquals(200, responseCode);
    List<String> cookies = con.getHeaderFields().get(Names.SET_COOKIE);
    Assert.assertEquals(1, cookies.size());
    Cookie sessionCookie = ClientCookieDecoder.STRICT.decode(cookies.get(0));
    Assert.assertEquals(Constants.COOKIE_NAME, sessionCookie.name());
    con = (HttpsURLConnection) url.openConnection();
    con.setRequestProperty(Names.COOKIE, sessionCookie.name() + "=" + sessionCookie.value());
    con.setHostnameVerifier((host, session) -> true);
    return con;
}
 
开发者ID:NationalSecurityAgency,项目名称:timely,代码行数:24,代码来源:TwoWaySSLIT.java

示例4: createNewBaseResponseInfoForTesting

import io.netty.handler.codec.http.cookie.Cookie; //导入依赖的package包/类
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 T getContentForFullResponse() {
            throw new UnsupportedOperationException("not implemented, don't call me during the test");
        }

        @Override
        public void setContentForFullResponse(T contentForFullResponse) {
            throw new UnsupportedOperationException("not implemented, don't call me during the test");
        }
    };

}
 
开发者ID:Nike-Inc,项目名称:riposte,代码行数:26,代码来源:BaseResponseInfoTest.java

示例5: uber_constructor_for_full_response_sets_fields_as_expected

import io.netty.handler.codec.http.cookie.Cookie; //导入依赖的package包/类
@Test
public void uber_constructor_for_full_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
    BaseResponseInfo<?> responseInfo = createNewBaseResponseInfoForTesting(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.isPreventCompressedOutput(), is(preventCompressedResponse));
    assertThat(responseInfo.isResponseSendingStarted(), is(false));
    assertThat(responseInfo.isResponseSendingLastChunkSent(), is(false));
}
 
开发者ID:Nike-Inc,项目名称:riposte,代码行数:25,代码来源:BaseResponseInfoTest.java

示例6: extractCookies_works_if_cookies_defined_in_headers

import io.netty.handler.codec.http.cookie.Cookie; //导入依赖的package包/类
@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));
}
 
开发者ID:Nike-Inc,项目名称:riposte,代码行数:18,代码来源:HttpUtilsTest.java

示例7: extractCookies_works_if_cookies_defined_in_trailing_headers

import io.netty.handler.codec.http.cookie.Cookie; //导入依赖的package包/类
@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));
}
 
开发者ID:Nike-Inc,项目名称:riposte,代码行数:19,代码来源:HttpUtilsTest.java

示例8: extractCookies_handles_cookie_values_leniently

import io.netty.handler.codec.http.cookie.Cookie; //导入依赖的package包/类
@Test
public void extractCookies_handles_cookie_values_leniently() {
    // given
    //these are cookie values seen in the wild...
    Cookie cookie1 = new DefaultCookie(UUID.randomUUID().toString(), "2094%3Az%7C2021%3Ab");
    Cookie cookie2 = new DefaultCookie(UUID.randomUUID().toString(), "geoloc=cc=US,rc=OR,tp=vhigh,tz=PST,la=45.4978,lo=-122.6937,bw=5000");
    Cookie cookie3 = new DefaultCookie(UUID.randomUUID().toString(), "\"dm=n.com&si=27431295-a282-4745-8cd5-542e7fce" +
            "429e&ss=1477551008358&sl=76&tt=437632&obo=12&sh=1477552753923%3D76%3A12%3A437632%2C1477552698670%3D75%3" +
            "A12%3A429879%2C1477552677137%3D74%3A12%3A426596%2C1477552672564%3D73%3A12%3A425585%2C1477552669893%3D72" +
            "%3A12%3A423456&bcn=%2F%2F3408178b.mpstat.us%2F&ld=1477552753923&r=http%3A%2F%2Fwww.nike.com%2Fbe%2Fde_de%" +
            "2F&ul=1477552756811\"");
    HttpHeaders headers = new DefaultHttpHeaders().add(HttpHeaders.Names.COOKIE, ClientCookieEncoder.LAX.encode(cookie1, cookie2, cookie3));

    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));
    assertThat(extractedCookies.contains(cookie3), is(true));
}
 
开发者ID:Nike-Inc,项目名称:riposte,代码行数:25,代码来源:HttpUtilsTest.java

示例9: createServerDefault

import io.netty.handler.codec.http.cookie.Cookie; //导入依赖的package包/类
public static HttpResponse createServerDefault(String requestCookie) {
	HttpResponse ret = new HttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.buffer());

	ret.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json; charset=UTF-8");

	if (requestCookie == null) { return ret; }

	Set<Cookie> cookies = ServerCookieDecoder.STRICT.decode(requestCookie);
	if (cookies.isEmpty()) { return ret; }

	// Reset the cookies if necessary.
	for (Cookie cookie : cookies) {
		ret.headers().add(HttpHeaderNames.SET_COOKIE, ClientCookieEncoder.STRICT.encode(cookie));
	}

	return ret;
}
 
开发者ID:anyflow,项目名称:lannister,代码行数:18,代码来源:HttpResponse.java

示例10: captureRequestCookies

import io.netty.handler.codec.http.cookie.Cookie; //导入依赖的package包/类
protected void captureRequestCookies(HttpRequest httpRequest) {
    Log.e("InnerHandle", "captureRequestCookies " + harEntry.getId());
    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());

        harRequest.getRequest().getCookies().add(harCookie);
        harRequest.addHeader(cookie.name(), cookie.value());
    }
}
 
开发者ID:misakuo,项目名称:Dream-Catcher,代码行数:20,代码来源:HarCaptureFilter.java

示例11: makeEppHttpRequest

import io.netty.handler.codec.http.cookie.Cookie; //导入依赖的package包/类
public static FullHttpRequest makeEppHttpRequest(
    String content,
    String host,
    String path,
    String accessToken,
    String sslClientCertificateHash,
    String serverHostname,
    String clientAddress,
    Cookie... cookies) {
  FullHttpRequest request = makeHttpPostRequest(content, host, path);
  request
      .headers()
      .set(HttpHeaderNames.AUTHORIZATION, "Bearer " + accessToken)
      .set(HttpHeaderNames.CONTENT_TYPE, EPP_CONTENT_TYPE)
      .set(HttpHeaderNames.ACCEPT, EPP_CONTENT_TYPE)
      .set(SSL_CLIENT_CERTIFICATE_HASH_FIELD, sslClientCertificateHash)
      .set(REQUESTED_SERVERNAME_VIA_SNI_FIELD, serverHostname)
      .set(FORWARDED_FOR_FIELD, clientAddress);
  if (cookies.length != 0) {
    request.headers().set(HttpHeaderNames.COOKIE, ClientCookieEncoder.STRICT.encode(cookies));
  }
  return request;
}
 
开发者ID:google,项目名称:nomulus,代码行数:24,代码来源:TestUtils.java

示例12: testSuccess_setCookies

import io.netty.handler.codec.http.cookie.Cookie; //导入依赖的package包/类
@Test
public void testSuccess_setCookies() throws Exception {
  setHandshakeSuccess();
  // First inbound message is hello.
  channel.readInbound();
  String responseContent = "<epp>response</epp>";
  Cookie cookie1 = new DefaultCookie("name1", "value1");
  Cookie cookie2 = new DefaultCookie("name2", "value2");
  channel.writeOutbound(
      makeEppHttpResponse(responseContent, HttpResponseStatus.OK, cookie1, cookie2));
  ByteBuf response = channel.readOutbound();
  assertThat(response).isEqualTo(Unpooled.wrappedBuffer(responseContent.getBytes(UTF_8)));
  String requestContent = "<epp>request</epp>";
  channel.writeInbound(Unpooled.wrappedBuffer(requestContent.getBytes(UTF_8)));
  FullHttpRequest request = channel.readInbound();
  assertHttpRequestEquivalent(request, makeEppHttpRequest(requestContent, cookie1, cookie2));
  // Nothing further to pass to the next handler.
  assertThat((Object) channel.readInbound()).isNull();
  assertThat((Object) channel.readOutbound()).isNull();
  assertThat(channel.isActive()).isTrue();
}
 
开发者ID:google,项目名称:nomulus,代码行数:22,代码来源:EppServiceHandlerTest.java

示例13: extract

import io.netty.handler.codec.http.cookie.Cookie; //导入依赖的package包/类
@Override
public String extract(ObjectNode node, RakamHttpRequest request)
{
    for (Cookie cookie : request.cookies()) {
        if (name.equals(cookie.name())) {
            // TODO fixme: the value of cookie parameter always must be String.
            return cookie.value();
        }
    }

    if (required) {
        throw new HttpRequestException("'" + name + "' cookie is required.", BAD_REQUEST);
    }

    return null;
}
 
开发者ID:buremba,项目名称:netty-rest,代码行数:17,代码来源:IRequestParameter.java

示例14: getUserFromCookie

import io.netty.handler.codec.http.cookie.Cookie; //导入依赖的package包/类
public User getUserFromCookie(FullHttpRequest request) {
    String cookieString = request.headers().get(HttpHeaderNames.COOKIE);

    if (cookieString != null) {
        Set<Cookie> cookies = ServerCookieDecoder.STRICT.decode(cookieString);
        if (!cookies.isEmpty()) {
            for (Cookie cookie : cookies) {
                if (isValid(cookie)) {
                    String token = cookie.value();
                    return httpSession.get(token);
                }
            }
        }
    }

    return null;
}
 
开发者ID:blynkkk,项目名称:blynk-server,代码行数:18,代码来源:SessionDao.java

示例15: channelRead

import io.netty.handler.codec.http.cookie.Cookie; //导入依赖的package包/类
@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());
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:19,代码来源:NettyConnector.java


注:本文中的io.netty.handler.codec.http.cookie.Cookie类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。