Search in sources :

Example 11 with CookieAttributeHandler

use of org.apache.http.cookie.CookieAttributeHandler in project lucene-solr by apache.

the class SolrPortAwareCookieSpecTest method testDomainMatch2.

@Test
public void testDomainMatch2() throws Exception {
    final BasicClientCookie cookie = new BasicClientCookie("name", "value");
    final CookieOrigin origin = new CookieOrigin("www.whatever.somedomain.com", 80, "/", false);
    final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();
    cookie.setDomain(".somedomain.com");
    Assert.assertTrue(h.match(cookie, origin));
}
Also used : CookieAttributeHandler(org.apache.http.cookie.CookieAttributeHandler) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) CookieOrigin(org.apache.http.cookie.CookieOrigin) Test(org.junit.Test)

Example 12 with CookieAttributeHandler

use of org.apache.http.cookie.CookieAttributeHandler in project lucene-solr by apache.

the class SolrPortAwareCookieSpecTest method testDomainInvalidInput.

@Test
public void testDomainInvalidInput() throws Exception {
    final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();
    try {
        h.match(null, null);
        Assert.fail("IllegalArgumentException must have been thrown");
    } catch (final IllegalArgumentException ex) {
    // expected
    }
    try {
        h.match(new BasicClientCookie("name", "value"), null);
        Assert.fail("IllegalArgumentException must have been thrown");
    } catch (final IllegalArgumentException ex) {
    // expected
    }
}
Also used : CookieAttributeHandler(org.apache.http.cookie.CookieAttributeHandler) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Test(org.junit.Test)

Example 13 with CookieAttributeHandler

use of org.apache.http.cookie.CookieAttributeHandler in project lucene-solr by apache.

the class SolrPortAwareCookieSpecTest method testDomainValidate4.

@Test
public void testDomainValidate4() throws Exception {
    final BasicClientCookie cookie = new BasicClientCookie("name", "value");
    final CookieOrigin origin = new CookieOrigin("www.a.b.c", 80, "/", false);
    final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();
    cookie.setDomain(".a.b.c");
    h.validate(cookie, origin);
    cookie.setDomain(".b.c");
    try {
        h.validate(cookie, origin);
        Assert.fail("MalformedCookieException should have been thrown");
    } catch (final MalformedCookieException ex) {
    // expected
    }
}
Also used : CookieAttributeHandler(org.apache.http.cookie.CookieAttributeHandler) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) CookieOrigin(org.apache.http.cookie.CookieOrigin) Test(org.junit.Test)

Example 14 with CookieAttributeHandler

use of org.apache.http.cookie.CookieAttributeHandler in project platform_external_apache-http by android.

the class RFC2965Spec method parse.

@Override
public List<Cookie> parse(final Header header, CookieOrigin origin) throws MalformedCookieException {
    if (header == null) {
        throw new IllegalArgumentException("Header may not be null");
    }
    if (origin == null) {
        throw new IllegalArgumentException("Cookie origin may not be null");
    }
    origin = adjustEffectiveHost(origin);
    HeaderElement[] elems = header.getElements();
    List<Cookie> cookies = new ArrayList<Cookie>(elems.length);
    for (HeaderElement headerelement : elems) {
        String name = headerelement.getName();
        String value = headerelement.getValue();
        if (name == null || name.length() == 0) {
            throw new MalformedCookieException("Cookie name may not be empty");
        }
        BasicClientCookie cookie;
        if (header.getName().equals(SM.SET_COOKIE2)) {
            cookie = createCookie2(name, value, origin);
        } else {
            cookie = createCookie(name, value, origin);
        }
        // cycle through the parameters
        NameValuePair[] attribs = headerelement.getParameters();
        // Eliminate duplicate attributes. The first occurrence takes precedence
        // See RFC2965: 3.2  Origin Server Role
        Map<String, NameValuePair> attribmap = new HashMap<String, NameValuePair>(attribs.length);
        for (int j = attribs.length - 1; j >= 0; j--) {
            NameValuePair param = attribs[j];
            attribmap.put(param.getName().toLowerCase(Locale.ENGLISH), param);
        }
        for (Map.Entry<String, NameValuePair> entry : attribmap.entrySet()) {
            NameValuePair attrib = entry.getValue();
            String s = attrib.getName().toLowerCase(Locale.ENGLISH);
            cookie.setAttribute(s, attrib.getValue());
            CookieAttributeHandler handler = findAttribHandler(s);
            if (handler != null) {
                handler.parse(cookie, attrib.getValue());
            }
        }
        cookies.add(cookie);
    }
    return cookies;
}
Also used : ClientCookie(org.apache.http.cookie.ClientCookie) Cookie(org.apache.http.cookie.Cookie) NameValuePair(org.apache.http.NameValuePair) HashMap(java.util.HashMap) HeaderElement(org.apache.http.HeaderElement) CookieAttributeHandler(org.apache.http.cookie.CookieAttributeHandler) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 15 with CookieAttributeHandler

use of org.apache.http.cookie.CookieAttributeHandler in project platform_external_apache-http by android.

the class CookieSpecBase method parse.

protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin) throws MalformedCookieException {
    List<Cookie> cookies = new ArrayList<Cookie>(elems.length);
    for (HeaderElement headerelement : elems) {
        String name = headerelement.getName();
        String value = headerelement.getValue();
        if (name == null || name.length() == 0) {
            throw new MalformedCookieException("Cookie name may not be empty");
        }
        BasicClientCookie cookie = new BasicClientCookie(name, value);
        cookie.setPath(getDefaultPath(origin));
        cookie.setDomain(getDefaultDomain(origin));
        // cycle through the parameters
        NameValuePair[] attribs = headerelement.getParameters();
        for (int j = attribs.length - 1; j >= 0; j--) {
            NameValuePair attrib = attribs[j];
            String s = attrib.getName().toLowerCase(Locale.ENGLISH);
            cookie.setAttribute(s, attrib.getValue());
            CookieAttributeHandler handler = findAttribHandler(s);
            if (handler != null) {
                handler.parse(cookie, attrib.getValue());
            }
        }
        cookies.add(cookie);
    }
    return cookies;
}
Also used : Cookie(org.apache.http.cookie.Cookie) NameValuePair(org.apache.http.NameValuePair) HeaderElement(org.apache.http.HeaderElement) CookieAttributeHandler(org.apache.http.cookie.CookieAttributeHandler) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) ArrayList(java.util.ArrayList)

Aggregations

CookieAttributeHandler (org.apache.http.cookie.CookieAttributeHandler)15 MalformedCookieException (org.apache.http.cookie.MalformedCookieException)11 BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)9 Test (org.junit.Test)9 CookieOrigin (org.apache.http.cookie.CookieOrigin)8 ArrayList (java.util.ArrayList)6 HeaderElement (org.apache.http.HeaderElement)6 NameValuePair (org.apache.http.NameValuePair)6 Cookie (org.apache.http.cookie.Cookie)6 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ClientCookie (org.apache.http.cookie.ClientCookie)3