com.ning.http.client.cookie.Cookie Java Examples

The following examples show how to use com.ning.http.client.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: NingCookieExtractorV1.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Override
public String getCookie(Request request) {
    final Collection<Cookie> cookies = request.getCookies();
    if (cookies.isEmpty()) {
        return null;
    }
    final StringBuilder sb = new StringBuilder();
    Iterator<Cookie> iterator = cookies.iterator();
    while (iterator.hasNext()) {
        final Cookie cookie = iterator.next();
        sb.append(cookie.getName());
        sb.append('=');
        sb.append(cookie.getValue());
        if (iterator.hasNext()) {
            sb.append(',');
        }
    }
    return sb.toString();
}
 
Example #2
Source File: ParsecHttpUtilTest.java    From parsec-libraries with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetCookie() throws Exception {
    NewCookie cookie1 = ParsecHttpUtil.getCookie(new Cookie(
        "cookie1_name",
        "cookie1_value",
        false,
        null,
        "cookie1_path",
        1,
        true,
        true
    ));

    assertEquals("cookie1_name", cookie1.getName());
    assertEquals("cookie1_value", cookie1.getValue());
    assertEquals(null, cookie1.getDomain());
    assertEquals("cookie1_path", cookie1.getPath());
    assertEquals(null, cookie1.getExpiry());
    assertEquals(1, cookie1.getMaxAge());
    assertTrue(cookie1.isSecure());
    assertTrue(cookie1.isHttpOnly());
}
 
Example #3
Source File: ParsecHttpUtil.java    From parsec-libraries with Apache License 2.0 5 votes vote down vote up
/**
 * Get {@link NewCookie} from Ning {@link Cookie}.
 *
 * @param ningCookie Ning {@link Cookie}
 * @return {@link NewCookie}
 */
public static NewCookie getCookie(final Cookie ningCookie) {
    return new NewCookie(
        ningCookie.getName(),
        ningCookie.getValue(),
        ningCookie.getPath(),
        ningCookie.getDomain(),
        "",
        (int) ningCookie.getMaxAge(),
        ningCookie.isSecure(),
        ningCookie.isHttpOnly()
    );
}
 
Example #4
Source File: CommonG.java    From bdt with Apache License 2.0 5 votes vote down vote up
public boolean cookieExists(String cookieName) {
    if (this.getSeleniumCookies() != null && this.getSeleniumCookies().size() != 0) {
        for (org.openqa.selenium.Cookie cookie : this.getSeleniumCookies()) {
            if (cookie.getName().contains(cookieName)) {
                return true;
            }
        }
    }
    return false;
}
 
Example #5
Source File: CommonG.java    From bdt with Apache License 2.0 5 votes vote down vote up
public void setResponse(String endpoint, Response response) throws IOException {

        Integer statusCode = response.getStatusCode();
        String httpResponse = response.getResponseBody();
        List<Cookie> cookies = response.getCookies();
        this.response = new HttpResponse(statusCode, httpResponse, cookies);
    }
 
Example #6
Source File: HttpResponse.java    From bdt with Apache License 2.0 4 votes vote down vote up
public List<Cookie> getCookies() {
    return cookies;
}
 
Example #7
Source File: CommonG.java    From bdt with Apache License 2.0 4 votes vote down vote up
public void setCookies(List<Cookie> cookies) {
    this.cookies = cookies;
}
 
Example #8
Source File: CommonG.java    From bdt with Apache License 2.0 4 votes vote down vote up
public List<Cookie> getCookies() {
    return cookies;
}
 
Example #9
Source File: CommonG.java    From bdt with Apache License 2.0 4 votes vote down vote up
public void setSeleniumCookies(Set<org.openqa.selenium.Cookie> cookies) {
    this.seleniumCookies = cookies;
}
 
Example #10
Source File: CommonG.java    From bdt with Apache License 2.0 4 votes vote down vote up
public Set<org.openqa.selenium.Cookie> getSeleniumCookies() {
    return seleniumCookies;
}
 
Example #11
Source File: HttpResponse.java    From bdt with Apache License 2.0 4 votes vote down vote up
public void setCookies(List<Cookie> cookies) {
    this.cookies = cookies;
}
 
Example #12
Source File: ParsecAsyncHttpRequest.java    From parsec-libraries with Apache License 2.0 4 votes vote down vote up
/**
 * Buile new {@link ParsecAsyncHttpRequest} instance.
 *
 * @return {@link ParsecAsyncHttpRequest}
 */
@SuppressWarnings("PMD.NPathComplexity")
public ParsecAsyncHttpRequest build() {
    ningRequestBuilder = new RequestBuilder(method)
        .setContentLength(contentLength)
        .setFollowRedirects(followRedirect)
        .setHeaders(headers)
        .setProxyServer(proxyServer)
        .setRangeOffset(rangeOffset)
        .setRequestTimeout(requestTimeout)
        .setVirtualHost(virtualHost)
        .setUri(new Uri(
            uri.getScheme(),
            uri.getUserInfo(),
            uri.getHost(),
            uri.getPort(),
            uri.getPath(),
            uri.getRawQuery()
    ));

    if (body != null) {
        ningRequestBuilder.setBody(body);
    } else if (byteBody != null) {
        ningRequestBuilder.setBody(byteBody);
    }

    if (bodyEncoding != null) {
        ningRequestBuilder.setBodyEncoding(bodyEncoding);
    }

    if (cookies != null && !cookies.isEmpty()) {
        for (NewCookie cookie : cookies) {
            ningRequestBuilder.addCookie(new Cookie(
                cookie.getName(),
                cookie.getValue(),
                false,
                cookie.getDomain(),
                cookie.getPath(),
                cookie.getMaxAge(),
                cookie.isSecure(),
                cookie.isHttpOnly()
            ));
        }
    }

    if (formParams != null) {
        ningRequestBuilder.setFormParams(formParams);
    }

    if (queryParams != null) {
        ningRequestBuilder.setQueryParams(queryParams);
    }

    if (acceptCompression && !headers.containsKey(ACCEPT_ENCODING_HEADER)) {
        ningRequestBuilder.addHeader(ACCEPT_ENCODING_HEADER, COMPRESSION_TYPE);
    }

    for (Part part: bodyParts) {
        ningRequestBuilder.addBodyPart(part);
    }

    ningRequestBuilder.setNameResolver(new DelegateNameResolver(nameResolver));

    return new ParsecAsyncHttpRequest(this);
}
 
Example #13
Source File: JsfRenderIT.java    From glowroot with Apache License 2.0 4 votes vote down vote up
@Override
protected void doTest(int port) throws Exception {
    AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
    Response response = asyncHttpClient
            .prepareGet("http://localhost:" + port + "/hello.xhtml").execute().get();
    int statusCode = response.getStatusCode();
    if (statusCode != 200) {
        asyncHttpClient.close();
        throw new IllegalStateException("Unexpected status code: " + statusCode);
    }
    String body = response.getResponseBody();
    Matcher matcher =
            Pattern.compile("action=\"/hello.xhtml;jsessionid=([0-9A-F]+)\"").matcher(body);
    matcher.find();
    String jsessionId = matcher.group(1);
    StringBuilder sb = new StringBuilder();
    matcher = Pattern
            .compile("<input type=\"hidden\" name=\"([^\"]+)\" value=\"([^\"]+)\" />")
            .matcher(body);
    matcher.find();
    sb.append(matcher.group(1));
    sb.append("=");
    sb.append(matcher.group(2));
    matcher = Pattern
            .compile("<input type=\"submit\" name=\"([^\"]+)\" value=\"([^\"]+)\" />")
            .matcher(body);
    matcher.find();
    sb.append("&");
    sb.append(matcher.group(1));
    sb.append("=");
    sb.append(matcher.group(2));
    matcher = Pattern.compile("name=\"([^\"]+)\" id=\"[^\"]+\" value=\"([^\"]+)\"")
            .matcher(body);
    matcher.find();
    sb.append("&");
    sb.append(matcher.group(1));
    sb.append("=");
    sb.append(matcher.group(2));
    String postBody = sb.toString().replace(":", "%3A");
    response = asyncHttpClient
            // ";xyz" is added to identify the second trace
            .preparePost(
                    "http://localhost:" + port + "/hello.xhtml;xyz")
            .setHeader("Content-Type", "application/x-www-form-urlencoded")
            .addCookie(Cookie.newValidCookie("JSESSIONID", jsessionId, "localhost",
                    jsessionId, null, -1, -1, true, true))
            .setBody(postBody)
            .execute()
            .get();
    statusCode = response.getStatusCode();
    asyncHttpClient.close();
    if (statusCode != 200) {
        throw new IllegalStateException("Unexpected status code: " + statusCode);
    }
    // sleep a bit to make sure the "last trace" is not the first http request from above
    MILLISECONDS.sleep(200);
}
 
Example #14
Source File: ParsecCompletableFutureTest.java    From parsec-libraries with Apache License 2.0 4 votes vote down vote up
@Test
public void testGet() throws Exception {
    FluentCaseInsensitiveStringsMap responseHeaders = new FluentCaseInsensitiveStringsMap();

    responseHeaders.add("header1", "header1_value1");
    responseHeaders.add("header2", "header2_value1");

    List<Cookie> ningCookies = new ArrayList<>();

    ningCookies.add(new Cookie(
        "cookie1_name",
        "cookie1_value",
        false,
        null,
        "cookie1_path",
        1,
        true,
        true
    ));

    ningCookies.add(new Cookie(
        "cookie2_name",
        "cookie2_value",
        false,
        null,
        "cookie2_path",
        2,
        false,
        false
    ));

    when(mockNingResponse.getHeaders()).thenReturn(responseHeaders);
    when(mockNingResponse.getContentType()).thenReturn(MediaType.APPLICATION_JSON);
    when(mockNingResponse.getCookies()).thenReturn(ningCookies);
    when(mockNingResponse.getStatusCode()).thenReturn(200);
    when(mockNingResponse.hasResponseHeaders()).thenReturn(true);

    when(mockNingFuture.get()).thenReturn(mockNingResponse);
    when(mockNingFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(mockNingResponse);

    ParsecCompletableFuture<Response> future = new ParsecCompletableFuture<>(mockNingFuture);
    Response response = future.get();

    assertEquals(response.getContentType(), MediaType.APPLICATION_JSON);
    assertEquals(response.getHeaders().size(), 2);

    response = future.get(3, TimeUnit.SECONDS);

    assertEquals(response.getContentType(), MediaType.APPLICATION_JSON);
    assertEquals(response.getHeaders().size(), 2);
}
 
Example #15
Source File: ParsecHttpUtilTest.java    From parsec-libraries with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetCookies() throws Exception {
    List<Cookie> ningCookies = new ArrayList<>();

    ningCookies.add(Cookie.newValidCookie(
        "cookie1_name",
        "cookie1_value",
        false,
        null,
        "cookie1_path",
        1,
        true,
        true
    ));

    ningCookies.add(Cookie.newValidCookie(
        "cookie2_name",
        "cookie2_value",
        false,
        null,
        "cookie2_path",
        2,
        false,
        false
    ));

    List<NewCookie> cookies = ParsecHttpUtil.getCookies(ningCookies);
    assertEquals(2, cookies.size());

    NewCookie cookie1 = cookies.get(0);
    assertEquals("cookie1_name", cookie1.getName());
    assertEquals("cookie1_value", cookie1.getValue());
    assertEquals(null, cookie1.getDomain());
    assertEquals("cookie1_path", cookie1.getPath());
    assertEquals(null, cookie1.getExpiry());
    assertEquals(1, cookie1.getMaxAge());
    assertTrue(cookie1.isSecure());
    assertTrue(cookie1.isHttpOnly());

    NewCookie cookie2 = cookies.get(1);
    assertEquals("cookie2_name", cookie2.getName());
    assertEquals("cookie2_value", cookie2.getValue());
    assertEquals(null, cookie2.getDomain());
    assertEquals("cookie2_path", cookie2.getPath());
    assertEquals(null, cookie2.getExpiry());
    assertEquals(2, cookie2.getMaxAge());
    assertFalse(cookie2.isSecure());
    assertFalse(cookie2.isHttpOnly());
}
 
Example #16
Source File: HttpResponse.java    From bdt with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor of an HttpResponse.
 *
 * @param statusCode
 * @param response
 */
public HttpResponse(Integer statusCode, String response, List<Cookie> cookies) {
    this.statusCode = statusCode;
    this.response = response;
    this.setCookies(cookies);
}
 
Example #17
Source File: ParsecHttpUtil.java    From parsec-libraries with Apache License 2.0 2 votes vote down vote up
/**
 * Get {@link NewCookie} {@link List} from Ning {@link Cookie} {@link Collection}.
 *
 * @param ningCookies Ning {@link Cookie} {@link Collection}
 * @return List&lt;{@link NewCookie}&gt;
 */
public static List<NewCookie> getCookies(final Collection<Cookie> ningCookies) {
    Stream<NewCookie> s = ningCookies.stream().map(ParsecHttpUtil::getCookie);
    return s.collect(Collectors.toList());
}