Example usage for org.apache.http.cookie Cookie getValue

List of usage examples for org.apache.http.cookie Cookie getValue

Introduction

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

Prototype

String getValue();

Source Link

Document

Returns the value.

Usage

From source file:com.kolich.http.BlockingTest.java

public static void main(String[] args) {

    final HttpClient client = getNewInstanceWithProxySelector("foobar");

    final Either<Integer, String> result = new HttpClient4Closure<Integer, String>(client) {
        @Override/*from   www.j  a va2  s .  com*/
        public void before(final HttpRequestBase request) {
            request.addHeader("Authorization", "super-secret-password");
        }

        @Override
        public String success(final HttpSuccess success) throws Exception {
            return EntityUtils.toString(success.getResponse().getEntity(), UTF_8);
        }

        @Override
        public Integer failure(final HttpFailure failure) {
            return failure.getStatusCode();
        }
    }.get("http://google.com");
    if (result.success()) {
        System.out.println(result.right());
    } else {
        System.out.println(result.left());
    }

    final Either<Void, Header[]> hResult = new HttpClient4Closure<Void, Header[]>(client) {
        @Override
        public Header[] success(final HttpSuccess success) throws Exception {
            return success.getResponse().getAllHeaders();
        }
    }.head("http://example.com");
    if (hResult.success()) {
        System.out.println("Fetched " + hResult.right().length + " request headers.");
    }

    final Either<Void, String> sResult = new StringOrNullClosure(client).get("http://mark.koli.ch");
    if (sResult.success()) {
        System.out.println(sResult.right());
    } else {
        System.out.println(sResult.left());
    }

    final Either<Exception, String> eResult = new HttpClient4Closure<Exception, String>(client) {
        @Override
        public String success(final HttpSuccess success) throws Exception {
            return EntityUtils.toString(success.getResponse().getEntity(), UTF_8);
        }

        @Override
        public Exception failure(final HttpFailure failure) {
            return failure.getCause();
        }
    }.put("http://lskdjflksdfjslkf.jfjkfhddfgsdfsdf.com");
    if (!eResult.success()) {
        System.out.println(eResult.left());
    }

    // Custom check for "success".
    final Either<Exception, String> cResult = new HttpClient4Closure<Exception, String>(client) {
        @Override
        public boolean check(final HttpResponse response, final HttpContext context) {
            return (response.getStatusLine().getStatusCode() == 405);
        }

        @Override
        public String success(final HttpSuccess success) throws Exception {
            return EntityUtils.toString(success.getResponse().getEntity(), UTF_8);
        }
    }.put("http://google.com");
    if (cResult.success()) {
        System.out.println(cResult.right());
    }

    final Either<Exception, OutputStream> bResult = new HttpClient4Closure<Exception, OutputStream>(client) {
        @Override
        public OutputStream success(final HttpSuccess success) throws Exception {
            final OutputStream os = new ByteArrayOutputStream();
            IOUtils.copy(success.getResponse().getEntity().getContent(), os);
            return os;
        }

        @Override
        public Exception failure(final HttpFailure failure) {
            return failure.getCause();
        }
    }.get("http://google.com");
    if (bResult.success()) {
        System.out.println("Loaded bytes into output stream!");
    }

    final OutputStream os = new ByteArrayOutputStream();
    final Either<Exception, Integer> stResult = new HttpClient4Closure<Exception, Integer>(client) {
        @Override
        public Integer success(final HttpSuccess success) throws Exception {
            return IOUtils.copy(success.getResponse().getEntity().getContent(), os);
        }
        /*
        @Override
        public Exception failure(final HttpFailure failure) {
           return failure.getCause();
        }
        */
    }.get("http://mark.koli.ch");
    if (stResult.success()) {
        System.out.println("Loaded " + stResult.right() + " bytes.");
    }

    /*
    final HttpContext context = new BasicHttpContext();
    // Setup a basic cookie store so that the response can fetch
    // any cookies returned by the server in the response.
    context.setAttribute(COOKIE_STORE, new BasicCookieStore());
    final Either<Void,String> cookieResult =
       new HttpClientClosureExpectString(client)
    .get(new HttpGet("http://google.com"), context);
    if(cookieResult.success()) {
       // List out all cookies that came back from Google in the response.
       final CookieStore cookies = (CookieStore)context.getAttribute(COOKIE_STORE);
       for(final Cookie c : cookies.getCookies()) {
    System.out.println(c.getName() + " -> " + c.getValue());
       }
    }*/

    final Either<Integer, List<Cookie>> mmmmm = new HttpClient4Closure<Integer, List<Cookie>>(client) {
        @Override
        public void before(final HttpRequestBase request, final HttpContext context) {
            context.setAttribute(COOKIE_STORE, new BasicCookieStore());
        }

        @Override
        public List<Cookie> success(final HttpSuccess success) {
            // Extract a list of cookies from the request.
            // Might be empty.
            return ((CookieStore) success.getContext().getAttribute(COOKIE_STORE)).getCookies();
        }

        @Override
        public Integer failure(final HttpFailure failure) {
            return failure.getStatusCode();
        }
    }.get("http://google.com");
    final List<Cookie> cookies;
    if ((cookies = mmmmm.right()) != null) {
        for (final Cookie c : cookies) {
            System.out.println(c.getName() + " -> " + c.getValue());
        }
    } else {
        System.out.println("Failed miserably: " + mmmmm.left());
    }

    final Either<Void, Header[]> haResult = new HttpClient4Closure<Void, Header[]>(client) {
        @Override
        public Header[] success(final HttpSuccess success) {
            return success.getResponse().getAllHeaders();
        }
    }.head("http://java.com");
    final Header[] headers = haResult.right();
    if (headers != null) {
        for (final Header h : headers) {
            System.out.println(h.getName() + ": " + h.getValue());
        }
    }

}

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

public static CookieFormatter createFromApacheCookie(org.apache.http.cookie.Cookie cookie) {
    return new CookieFormatter(cookie.getName(), cookie.getValue(), cookie.getPath());
}

From source file:net.emphased.lastcontact.CookieFileStore.java

private static String[] cookieToStringArray(Cookie cookie) {
    return new String[] { cookie.getName(), cookie.getValue(), String.valueOf(cookie.getExpiryDate().getTime()),
            cookie.getPath(), cookie.getDomain(), };
}

From source file:org.expath.httpclient.impl.LoggerHelper.java

public static void logCookies(Log log, String prompt, Iterable<Cookie> cookies) {
    if (log.isDebugEnabled()) {
        if (cookies == null) {
            log.debug(prompt + ": null");
            return;
        }//ww w.j  a  va 2  s .  c o m
        for (Cookie c : cookies) {
            log.debug(prompt + ": " + c.getName() + ": " + c.getValue());
        }
    }
}

From source file:co.paralleluniverse.comsat.webactors.servlet.WebActorServletTest.java

private static String getByName(final CookieStore cookieStore, String name) {
    for (org.apache.http.cookie.Cookie cookie : cookieStore.getCookies())
        if (name.equals(cookie.getName()))
            return cookie.getValue();
    return null;//w w  w .  j a v a  2  s .c  o m
}

From source file:org.qi4j.library.shiro.AbstractServletTestSupport.java

protected static void soutCookies(Iterable<Cookie> cookies) {
    StringBuilder sb = new StringBuilder();
    sb.append("\nLogging cookies for the curious");
    for (Cookie eachCookie : cookies) {
        sb.append("\t").append(eachCookie.getName()).append(": ").append(eachCookie.getValue()).append(" ( ")
                .append(eachCookie.getDomain()).append(" - ").append(eachCookie.getPath()).append(" )");
    }/*from   ww  w .ja va2 s .  com*/
    System.out.println(sb.append("\n").toString());
}

From source file:com.sampullara.findmyiphone.FindMyDevice.java

private static String extractIscCode(DefaultHttpClient hc) {
    CookieStore cookies = hc.getCookieStore();
    for (Cookie cookie : cookies.getCookies()) {
        if (cookie.getName().equals("isc-secure.me.com")) {
            return cookie.getValue();
        }//from ww  w  .  j  av a 2s .c  o  m
    }
    return null;
}

From source file:Main.java

@Deprecated
// Deprecated because this uses org.apache.http, which is itself deprecated
public static HttpCookie servletCookieFromApacheCookie(org.apache.http.cookie.Cookie apacheCookie) {
    if (apacheCookie == null) {
        return null;
    }//from   w w w. ja  v  a  2s.com

    String name = apacheCookie.getName();
    String value = apacheCookie.getValue();

    HttpCookie cookie = new HttpCookie(name, value);

    value = apacheCookie.getDomain();
    if (value != null) {
        cookie.setDomain(value);
    }
    value = apacheCookie.getPath();
    if (value != null) {
        cookie.setPath(value);
    }
    cookie.setSecure(apacheCookie.isSecure());

    value = apacheCookie.getComment();
    if (value != null) {
        cookie.setComment(value);
    }

    // version
    cookie.setVersion(apacheCookie.getVersion());

    // From the Apache source code, maxAge is converted to expiry date using the following formula
    // if (maxAge >= 0) {
    //     setExpiryDate(new Date(System.currentTimeMillis() + maxAge * 1000L));
    // }
    // Reverse this to get the actual max age

    Date expiryDate = apacheCookie.getExpiryDate();
    if (expiryDate != null) {
        long maxAge = (expiryDate.getTime() - System.currentTimeMillis()) / 1000;
        // we have to lower down, no other option
        cookie.setMaxAge((int) maxAge);
    }

    // return the servlet cookie
    return cookie;
}

From source file:com.thecorpora.qbo.androidapk.MjpegInputStream.java

public static MjpegInputStream read(String url, Cookie cookie) {
    HttpResponse res;//from   w  w  w  .  j av a2  s.  c om
    DefaultHttpClient httpclient = new DefaultHttpClient();

    HttpGet get = new HttpGet(url);
    Log.d("COOKIE", cookie.getValue());
    get.setHeader("Cookie", "session_id=" + cookie.getValue());

    try {
        res = httpclient.execute(get);
        return new MjpegInputStream(res.getEntity().getContent());
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }
    return null;
}

From source file:org.sharetask.data.IntegrationTest.java

@BeforeClass
public static void login() throws Exception {
    final DefaultHttpClient client = new DefaultHttpClient();
    final HttpPost httpPost = new HttpPost(BASE_URL + "/user/login");
    httpPost.addHeader(new BasicHeader("Content-Type", "application/json"));
    final StringEntity httpEntity = new StringEntity(
            "{\"username\":\"dev1@shareta.sk\"," + "\"password\":\"password\"}");
    System.out.println(EntityUtils.toString(httpEntity));
    httpPost.setEntity(httpEntity);/*from  w  w  w .j a  va  2s  .  c o  m*/

    //when
    final HttpResponse response = client.execute(httpPost);

    //then
    Assert.assertEquals(HttpStatus.OK.value(), response.getStatusLine().getStatusCode());
    client.getCookieStore().getCookies();
    for (final Cookie cookie : client.getCookieStore().getCookies()) {
        if (cookie.getName().equals("JSESSIONID")) {
            DOMAIN = cookie.getDomain();
            SESSIONID = cookie.getValue();
        }
    }
}