Example usage for org.apache.http.cookie CookieOrigin CookieOrigin

List of usage examples for org.apache.http.cookie CookieOrigin CookieOrigin

Introduction

In this page you can find the example usage for org.apache.http.cookie CookieOrigin CookieOrigin.

Prototype

public CookieOrigin(final String host, final int port, final String path, final boolean secure) 

Source Link

Usage

From source file:com.woonoz.proxy.servlet.CookieSpecTest.java

@Test
public void testParseCookie() throws MalformedCookieException {
    BestMatchSpec parser = new BestMatchSpec();
    List<Cookie> cookies = parser.parse(
            new BasicHeader("Set-Cookie", "JSESSIONID=F39EC36E999C90604EAFF7A87F88DA58; Path=/"),
            new CookieOrigin("localhost", 80, "/toto", false));
    Assert.assertEquals(cookies.size(), 1);
    Assert.assertEquals(cookies.get(0).getPath(), "/");
    Assert.assertEquals(cookies.get(0).getName(), "JSESSIONID");
    Assert.assertEquals(cookies.get(0).getValue(), "F39EC36E999C90604EAFF7A87F88DA58");
}

From source file:org.apache.solr.client.solrj.impl.SolrPortAwareCookieSpecTest.java

@Test
public void testDomainHostPortValidate() throws Exception {
    final BasicClientCookie cookie = new BasicClientCookie("name", "value");
    final CookieOrigin origin = new CookieOrigin("somehost", 80, "/", false);
    final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();

    cookie.setDomain("somehost:80");
    h.validate(cookie, origin);//w  ww. ja v  a2 s  . c  o  m

    cookie.setDomain("somehost:1234");
    try {
        h.validate(cookie, origin);
        Assert.fail("MalformedCookieException should have been thrown");
    } catch (final MalformedCookieException ex) {
        // expected
    }
}

From source file:org.sigmond.net.AsyncHttpTask.java

protected HttpRequestInfo doInBackground(HttpRequestInfo... params) {
    HttpRequestInfo rinfo = params[0];/* www.  j  a va2 s .  co  m*/
    try {
        client.setCookieStore(rinfo.getCookieStore()); //cookie handling
        HttpResponse resp = client.execute(rinfo.getRequest()); //execute request

        //store any new cookies recieved
        CookieStore store = rinfo.getCookieStore();
        Header[] allHeaders = resp.getAllHeaders();
        CookieSpecBase base = new BrowserCompatSpec();
        URI uri = rinfo.getRequest().getURI();
        int port = uri.getPort();
        if (port <= 0)
            port = 80;
        CookieOrigin origin = new CookieOrigin(uri.getHost(), port, uri.getPath(), false);
        for (Header header : allHeaders) {
            List<Cookie> parse = base.parse(header, origin);
            for (Cookie cookie : parse) {
                if (cookie.getValue() != null && cookie.getValue() != "")
                    store.addCookie(cookie);
            }
        }
        rinfo.setCookieStore(store);
        //store the response
        rinfo.setResponse(resp);
        //process the response string. This is required in newer Android versions.
        //newer versions will not allow reading from a network response input stream in the main thread.
        rinfo.setResponseString(HttpUtils.responseToString(resp));
    } catch (Exception e) {
        rinfo.setException(e);
    }
    return rinfo;
}

From source file:org.apache.solr.client.solrj.impl.SolrPortAwareCookieSpecTest.java

@Test
public void testDomainHostPortMatch() throws Exception {
    final BasicClientCookie cookie = new BasicClientCookie("name", "value");
    final CookieOrigin origin = new CookieOrigin("myhost", 80, "/", false);
    final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();

    cookie.setDomain("myhost");
    try {/*from w  w  w.j  a  va  2 s. com*/
        h.match(cookie, null);
        Assert.fail("IllegalArgumentException should have been thrown, since origin is null.");
    } catch (final IllegalArgumentException ex) {
        // expected
    }

    cookie.setDomain(null);
    Assert.assertFalse(h.match(cookie, origin));

    cookie.setDomain("otherhost");
    Assert.assertFalse(h.match(cookie, origin));

    cookie.setDomain("myhost");
    Assert.assertTrue(h.match(cookie, origin));

    cookie.setDomain("myhost:80");
    Assert.assertTrue(h.match(cookie, origin));

    cookie.setDomain("myhost:8080");
    Assert.assertFalse(h.match(cookie, origin));
}

From source file:com.gargoylesoftware.htmlunit.httpclient.HtmlUnitPathHandler.java

@Override
public boolean match(final Cookie cookie, final CookieOrigin origin) {
    CookieOrigin newOrigin = origin;//from ww  w.  ja  va2  s .  c o m
    String targetpath = origin.getPath();
    if (browserVersion_.hasFeature(HTTP_COOKIE_EXTRACT_PATH_FROM_LOCATION) && !targetpath.isEmpty()) {
        final int lastSlashPos = targetpath.lastIndexOf('/');
        if (lastSlashPos > 1 && lastSlashPos < targetpath.length()) {
            targetpath = targetpath.substring(0, lastSlashPos);
            newOrigin = new CookieOrigin(origin.getHost(), origin.getPort(), targetpath, origin.isSecure());
        }
    }

    return super.match(cookie, newOrigin);
}

From source file:com.woonoz.proxy.servlet.UrlRewriterImpl.java

public String rewriteCookie(String headerValue) throws URISyntaxException, InvalidCookieException {
    BestMatchSpec parser = new BestMatchSpec();
    List<Cookie> cookies;/*from w w  w .  ja  v  a  2s . co m*/
    try {
        cookies = parser.parse(new BasicHeader("Set-Cookie", headerValue), new CookieOrigin(
                targetServer.getHost(), getPortOrDefault(targetServer), targetServer.getPath(), false));
    } catch (MalformedCookieException e) {
        throw new InvalidCookieException(e);
    }
    if (cookies.size() != 1) {
        throw new InvalidCookieException();
    }
    Cookie cookie = rewriteCookiePathIfNeeded(cookies.get(0));
    CookieFormatter cookieFormatter = CookieFormatter.createFromApacheCookie(cookie);
    return cookieFormatter.asString();
}

From source file:org.esigate.http.HttpResponseUtils.java

/**
 * Removes ";jsessionid=&lt;id&gt;" from the url, if the session id is also set in "httpResponse".
 * <p>/*from  w w w .ja  v  a2  s  . c o m*/
 * This methods first looks for the following header :
 * 
 * <pre>
 * Set-Cookie: JSESSIONID=
 * </pre>
 * 
 * If found and perfectly matches the jsessionid value in url, the complete jsessionid definition is removed from
 * the url.
 * 
 * @param uri
 *            original uri, may contains a jsessionid.
 * @param httpResponse
 *            the response which set the jsessionId
 * @return uri, without jsession
 */
public static String removeSessionId(String uri, HttpResponse httpResponse) {
    CookieSpec cookieSpec = new BrowserCompatSpec();
    // Dummy origin, used only by CookieSpec for setting the domain for the
    // cookie but we don't need it
    CookieOrigin cookieOrigin = new CookieOrigin("dummy", Http.DEFAULT_HTTP_PORT, "/", false);
    Header[] responseHeaders = httpResponse.getHeaders("Set-cookie");
    String jsessionid = null;
    for (int i = 0; i < responseHeaders.length; i++) {
        Header header = responseHeaders[i];
        try {
            List<Cookie> cookies = cookieSpec.parse(header, cookieOrigin);
            for (Cookie cookie : cookies) {
                if ("JSESSIONID".equalsIgnoreCase(cookie.getName())) {
                    jsessionid = cookie.getValue();
                }
                break;
            }
        } catch (MalformedCookieException ex) {
            LOG.warn("Malformed header: " + header.getName() + ": " + header.getValue());
        }
        if (jsessionid != null) {
            break;
        }
    }
    if (jsessionid == null) {
        return uri;
    }

    return UriUtils.removeSessionId(jsessionid, uri);

}

From source file:org.apache.solr.client.solrj.impl.SolrPortAwareCookieSpecTest.java

@Test
public void testDomainValidate1() throws Exception {
    final BasicClientCookie cookie = new BasicClientCookie("name", "value");
    final CookieOrigin origin = new CookieOrigin("somehost", 80, "/", false);
    final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();

    cookie.setDomain("somehost");
    h.validate(cookie, origin);//from   ww w. ja v a  2 s  . co m

    cookie.setDomain("otherhost");
    try {
        h.validate(cookie, origin);
        Assert.fail("MalformedCookieException should have been thrown");
    } catch (final MalformedCookieException ex) {
        // expected
    }
}

From source file:com.gargoylesoftware.htmlunit.CookieManager.java

/**
 * Helper that builds a CookieOrigin./*  w ww . j a v a2  s  . co m*/
 * @param url the url to be used
 * @return the new CookieOrigin
 */
public CookieOrigin buildCookieOrigin(final URL url) {
    final URL normalizedUrl = replaceForCookieIfNecessary(url);

    return new CookieOrigin(normalizedUrl.getHost(), getPort(normalizedUrl), normalizedUrl.getPath(),
            "https".equals(normalizedUrl.getProtocol()));
}

From source file:com.machinepublishers.jbrowserdriver.CookieStore.java

/**
 * {@inheritDoc}//  w  w  w. ja  va 2 s .com
 */
@Override
public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException {
    if (isJavascript(uri.getScheme())) {
        for (Map.Entry<String, List<String>> entry : responseHeaders.entrySet()) {
            for (String value : entry.getValue()) {
                try {
                    List<Cookie> cookies = spec.parse(new BasicHeader(entry.getKey(), value),
                            new CookieOrigin(uri.getHost(), canonicalPort(uri.getScheme(), uri.getPort()),
                                    canonicalPath(uri.getPath()), isSecure(uri.getScheme())));
                    for (Cookie cookie : cookies) {
                        synchronized (store) {
                            store.addCookie(cookie);
                        }
                    }
                } catch (MalformedCookieException e) {
                    LogsServer.instance().warn(
                            "Malformed cookie for cookie named " + entry.getKey() + ". " + e.getMessage());
                }
            }
        }
    }
}