1

I am using Spring for Android in a project and I need to manage the cookie store/manager. I am able to add cookies to any request by using an implementation of ClientHttpRequestInterceptor, but I would like to remove some of these when sending a request.

To be more specific, the problem I am facing is that, for Froyo, the implementation specific in Spring (with DefaultHttpClient) adds automatically to headers the cookies from CookieStore - that even if I am setting explicitly the headers. But I would like to manage these cookies myself (either remove some of them, or update their values). While for Gingerbread above (Spring implementation is done with HttpURLConnection) the cookies are added only if I am doing it myself - however I am not that sure as I don't see Spring setting any CookieHandler, but the bottom line is that I don't see them when performing a request or I can see them updated. So the issue is more specific to Froyo.

The work-around is to reset the connection factory; something like:

protected void resetCookieStoreForTemplate(RestTemplate template) {
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO) {
        template.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
    }
}

Underneath, that seems to recreate the DefaultHttpClient and will use a new CookieStore. But that seems to me a bit ugly.

To wrap up, my question would be: does Spring for Android provide some method to expose some API for Cookie management? Just the way RestTemplate exposes some abstractions for connectivity, connection factory, message converters and so on, I would be very happy to have some abstraction for cookie management.

gunar
  • 14,392
  • 7
  • 54
  • 83
  • Bounty's gone :( ... Will post this question on `Spring` community and will link that to this question. Thanks all anyway – gunar Jul 26 '13 at 06:37

2 Answers2

2

I've not used Spring myself but from what I've read about it, it follows the official advice and switches HTTP clients based on API version (which is quite clever, if seriously over-engineered for my liking). When using HTTPUrlConnection, as you mentioned, Spring probably doesn't change the CookieHandler. You should be seeing in-memory cookie handling, so everything should work for requests in the same app run but the cookies would be wiped when you close the app. Can you confirm this is what you're seeing?

If so, all you have to do is create a new CookieManager instance, passing it a custom CookieStore and null for the CookiePolicy to use the default.

It's unfortunate that a persistent store isn't built-in but it's not particularly difficult to write one either.

Edit: See here for a CookieStore that uses SharedPreferences (haven't tested it myself).

Community
  • 1
  • 1
Delyan
  • 8,745
  • 4
  • 35
  • 41
  • As mentioned, the issue I am facing is specific for Froyo (http engine is implemented with Apache `DefaultHttpClient`), as I don't see any access to `CookieStore`. The elegant solution in Gingerbread above (to set a persistent `CookieStore` through `CookieHandler`) works just fine, but I don't have such access to its Apache `CookieStore` implementation. I was hoping to have some API to abstract the access to `CookieStore` ... To be honest, I believe this question should rather be posted on Spring community as it seems to me a feature request. – gunar Jul 24 '13 at 08:52
  • Oops, sorry, from your description it sounds like Gingerbread and above are the problem. In that case, yes, it does sound like a feature request and the spring community is the best place for you. – Delyan Jul 24 '13 at 08:57
  • `from your description it sounds like Gingerbread and above are the problem` ... hmmmm. I've edited a bit the question by adding this: `But I would like to manage these cookies myself (either remove some of them, or update their values).` Hope is more clear. Thanks for flagging that observation! – gunar Jul 24 '13 at 09:16
  • I'm using [this post](http://blogs.burnsidedigital.com/2012/11/how-to-use-ssl-and-session-cookies-with-spring-for-android/) to work with cookies maybe it helps you. – Misagh Aghakhani Aug 08 '13 at 08:12
1

The ClientHttpRequestInterceptor class is a good approach when you need to pass common headers for all requests such as setting up content type, authorization etc. As far as I understood you want to pass some cookie values for specific request. You could also achieve this via HttpEntity and HttpHeaders class.

    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.add("Cookie", "name=" + value);
    HttpEntity requestEntity = new HttpEntity(null, requestHeaders);
    ResponseEntity response = restTemplate.exchange(
      "http://server/service?...",
      HttpMethod.GET,
      requestEntity,
      Response.class);

Spring rest template does not provide any off the self solution for managing cookies. The class CookieHandler is provided by Apache not part of spring. Rest template is just a basic solution for managing request response with compare to spring core.

Shamim Ahmmed
  • 7,857
  • 6
  • 22
  • 36
  • Thanks for answering, but I am afraid this is not helping as my question is: `does Spring for Android provide some method to expose some API for Cookie management?` I am already passing a custom `Cookie` as you mentioned (mentioned in 2-nd phrase), but I need to manage the whole cookie store. I can do that in API level 9 above by setting a persistent `CookieStore` in `CookieHandler.setDefault(...)`, but for Froyo I don't see such central cookie repo available to be automatically linked with `DefaultHttpClient`. – gunar Jul 24 '13 at 08:46
  • `As far as I understood you want to pass some cookie values for specific request.` I want to remove or to edit these cookies. That works for Gingerbread above, but it doesn't work for Froyo, because it seems to me that `DefaultHttpClient` uses what it has in `CookieStore` ignoring matching headers passed through `ClientHttpRequestInterceptor`. Off-topic: `CookieHandler` doesn't seem to be provided by Apache as its package name is `java.net`. So my problem is that I don't see any API to access the cookie store. I was hoping to see some abstractions there. – gunar Jul 24 '13 at 09:07
  • 1
    As said in comment to @Delyan: `To be honest, I believe this question should rather be posted on Spring community as it seems to me a feature request.` – gunar Jul 24 '13 at 09:07