Search in sources :

Example 56 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie 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 57 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project ma-modules-public by infiniteautomation.

the class MangoStoreClient method login.

/**
 * Login to the store
 * @param email
 * @param password
 * @param retries
 * @throws ClientProtocolException
 * @throws IOException
 * @throws HttpException
 */
public void login(String email, String password, int retries) throws ClientProtocolException, IOException, HttpException {
    BasicCookieStore cookieStore = new BasicCookieStore();
    httpClient = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
    HttpPost httppost = new HttpPost(storeUrl + "/login");
    // Request parameters and other properties.
    List<NameValuePair> params = new ArrayList<NameValuePair>(2);
    params.add(new BasicNameValuePair("email", email));
    params.add(new BasicNameValuePair("password", password));
    params.add(new BasicNameValuePair("submit", "Login"));
    httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    // Execute and get the response.
    HttpResponse response = executeRequest(httppost, 302, retries);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        try {
            for (Header header : response.getAllHeaders()) {
                if (header.getName().equals("Set-Cookie")) {
                    // Set our cookie here
                    String[] cookiePath = header.getValue().split(";");
                    // Now we have JSESSIONID=stuff , Path=/
                    String[] cookie = cookiePath[0].split("=");
                    String[] path = cookiePath[1].split("=");
                    this.sessionId = cookie[1];
                    BasicClientCookie c = new BasicClientCookie(cookie[0], cookie[1]);
                    c.setDomain(storeUrl);
                    c.setPath(path[1]);
                    c.setVersion(0);
                    cookieStore.addCookie(c);
                }
            }
        } finally {
            instream.close();
        }
    }
    return;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Header(org.apache.http.Header) BasicNameValuePair(org.apache.http.message.BasicNameValuePair)

Example 58 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project knox by apache.

the class HadoopAuthCookieStoreTest method testOozieCookieWorkaroundKnox1171.

/**
 * Test for the issue reported as KNOX-1171
 * Tests the required to workaround Oozie 4.3/Hadoop 2.4 not properly formatting the hadoop.auth cookie.
 * See the following jiras for additional context:
 *   https://issues.apache.org/jira/browse/HADOOP-10710
 *   https://issues.apache.org/jira/browse/HADOOP-10379
 */
@Test
public void testOozieCookieWorkaroundKnox1171() {
    String rawValue = "u=knox&p=knox/host.example.com.com@EXAMPLE.COM&t=kerberos&e=1517900515610&s=HpSXUOhoXR/2wXrsgPz5lSbNuf8=";
    String quotedValue = "\"" + rawValue + "\"";
    HadoopAuthCookieStore store;
    List<Cookie> cookies;
    Cookie cookie;
    store = new HadoopAuthCookieStore();
    store.addCookie(new BasicClientCookie("hadoop.auth", rawValue));
    cookies = store.getCookies();
    cookie = cookies.get(0);
    assertThat(cookie.getValue(), is(quotedValue));
    store = new HadoopAuthCookieStore();
    store.addCookie(new BasicClientCookie("hadoop.auth", quotedValue));
    cookies = store.getCookies();
    cookie = cookies.get(0);
    assertThat(cookie.getValue(), is(quotedValue));
    store = new HadoopAuthCookieStore();
    store.addCookie(new BasicClientCookie("hadoop.auth", null));
    cookies = store.getCookies();
    cookie = cookies.get(0);
    assertThat(cookie.getValue(), is(nullValue()));
    store = new HadoopAuthCookieStore();
    store.addCookie(new BasicClientCookie("hadoop.auth", ""));
    cookies = store.getCookies();
    cookie = cookies.get(0);
    assertThat(cookie.getValue(), is(""));
}
Also used : BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Cookie(org.apache.http.cookie.Cookie) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Test(org.junit.Test)

Example 59 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project opacclient by opacapp.

the class Heidi method getResultById.

@Override
public DetailedItem getResultById(String id, final String homebranch) throws IOException {
    if (sessid == null) {
        start();
    }
    // Homebranch
    if (homebranch != null && !"".equals(homebranch)) {
        cookieStore.addCookie(new BasicClientCookie("zweig", homebranch));
    }
    String html = httpGet(opac_url + "/titel.cgi?katkey=" + id + "&sess=" + sessid, ENCODING, false, cookieStore);
    Document doc = Jsoup.parse(html);
    DetailedItem item = new DetailedItem();
    item.setId(id);
    Elements table = doc.select(".titelsatz tr");
    for (Element tr : table) {
        if (tr.select("th").size() == 0 || tr.select("td").size() == 0) {
            continue;
        }
        String d = tr.select("th").first().text();
        String c = tr.select("td").first().text();
        if (d.equals("Titel:")) {
            item.setTitle(c);
        } else if ((d.contains("URL") || d.contains("Link")) && tr.select("td a").size() > 0) {
            item.addDetail(new Detail(d, tr.select("td a").first().attr("href")));
        } else {
            item.addDetail(new Detail(d, c));
        }
    }
    if (doc.select(".ex table tr").size() > 0) {
        table = doc.select(".ex table tr");
        DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);
        for (Element tr : table) {
            if (tr.hasClass("exueber") || tr.select(".exsig").size() == 0 || tr.select(".exso").size() == 0 || tr.select(".exstatus").size() == 0) {
                continue;
            }
            Copy copy = new Copy();
            copy.setShelfmark(tr.select(".exsig").first().text());
            copy.setBranch(tr.select(".exso").first().text());
            String status = tr.select(".exstatus").first().text();
            if (status.contains("entliehen bis")) {
                copy.setReturnDate(fmt.parseLocalDate(status.replaceAll("entliehen bis ([0-9.]+) .*", "$1")));
                copy.setReservations(status.replaceAll(".*\\(.*Vormerkungen: ([0-9]+)\\)", "$1"));
                copy.setStatus("entliehen");
            } else {
                copy.setStatus(status);
            }
            item.addCopy(copy);
        }
    }
    for (Element a : doc.select(".status1 a")) {
        if (a.attr("href").contains("bestellung.cgi")) {
            item.setReservable(true);
            item.setReservation_info(id);
            break;
        }
    }
    for (Element a : doc.select(".titelsatz a")) {
        if (a.text().trim().matches("B.+nde")) {
            Map<String, String> volumesearch = new HashMap<>();
            volumesearch.put("query", getQueryParamsFirst(a.attr("href")).get("query"));
            item.setVolumesearch(volumesearch);
        }
    }
    return item;
}
Also used : Copy(de.geeksfactory.opacclient.objects.Copy) HashMap(java.util.HashMap) Element(org.jsoup.nodes.Element) DetailedItem(de.geeksfactory.opacclient.objects.DetailedItem) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Detail(de.geeksfactory.opacclient.objects.Detail)

Example 60 with BasicClientCookie

use of org.apache.http.impl.cookie.BasicClientCookie in project tutorials by eugenp.

the class HttpClientCookieLiveTest method givenUsingDeprecatedApi_whenSettingCookiesOnTheHttpClient_thenCorrect.

@Test
public final void givenUsingDeprecatedApi_whenSettingCookiesOnTheHttpClient_thenCorrect() throws IOException {
    final BasicCookieStore cookieStore = new BasicCookieStore();
    final BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
    cookie.setDomain(".github.com");
    cookie.setPath("/");
    cookieStore.addCookie(cookie);
    final HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
    final HttpGet request = new HttpGet("http://www.github.com");
    response = (CloseableHttpResponse) client.execute(request);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
Also used : BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Test(org.junit.Test)

Aggregations

BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)88 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)36 Test (org.junit.Test)24 HttpResponse (org.apache.http.HttpResponse)19 Cookie (org.apache.http.cookie.Cookie)17 HttpClient (org.apache.http.client.HttpClient)15 HttpGet (org.apache.http.client.methods.HttpGet)15 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)15 Map (java.util.Map)14 RequestConfig (org.apache.http.client.config.RequestConfig)13 HttpPost (org.apache.http.client.methods.HttpPost)9 CookieAttributeHandler (org.apache.http.cookie.CookieAttributeHandler)9 CookieOrigin (org.apache.http.cookie.CookieOrigin)9 URL (java.net.URL)8 HttpEntity (org.apache.http.HttpEntity)8 CookieStore (org.apache.http.client.CookieStore)8 IOException (java.io.IOException)7 Date (java.util.Date)7 ClientCookie (org.apache.http.cookie.ClientCookie)7 HashMap (java.util.HashMap)6