Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.koushikdutta.ion.cookie;
  
  import  android.content.Context;
  import  android.content.SharedPreferences;
  import  android.text.TextUtils;
  import  android.util.Log;
  
 
 import java.net.URI;
 import java.util.List;
 import java.util.Map;

Created by koush on 5/29/13.
 
 public class CookieMiddleware extends SimpleMiddleware {
     SharedPreferences preferences;
 
     public CookieStore getCookieStore() {
         return .getCookieStore();
     }
 
     public void clear() {
         maybeInit();
         getCookieStore().removeAll();
         .edit().clear().apply();
     }
 
     public CookieManager getCookieManager() {
         maybeInit();
         return ;
     }
 
     Ion ion;
     public CookieMiddleware(Ion ion) {
         this. = ion;
     }
 
     public void reinit() {
          = new CookieManager(nullnull);
          = .getContext().getSharedPreferences(.getName() + "-cookies", Context.MODE_PRIVATE);
 
         Map<String, ?> allPrefs = .getAll();
         for (String keyallPrefs.keySet()) {
             try {
                 String value = .getString(keynull);
                 Headers headers = new Headers();
                 String[] lines = value.split("\n");
                 boolean first = true;
                 for (String linelines) {
                     if (first) {
                         first = false;
                     }
                     else if (!TextUtils.isEmpty(line)) {
                         headers.addLine(line);
                     }
                 }
                 .put(URI.create(key), headers.getMultiMap());
             }
             catch (Exception e) {
                 Log.e("Ion""unable to load cookies"e);
             }
         }
     }
 
     public static void addCookies(Map<StringList<String>> allCookieHeadersHeaders headers) {
         for (Map.Entry<StringList<String>> entry : allCookieHeaders.entrySet()) {
             String key = entry.getKey();
             if ("Cookie".equalsIgnoreCase(key) || "Cookie2".equalsIgnoreCase(key)) {
                 headers.addAll(keyentry.getValue());
             }
         }
     }
 
     private void maybeInit() {
         if ( == null)
             reinit();
     }
 
     @Override
     public void onRequest(OnRequestData data) {
         maybeInit();
         try {
             Map<StringList<String>> cookies = .get(
                 URI.create(
                     data.request.getUri().toString()),
                     data.request.getHeaders().getMultiMap());
             addCookies(cookiesdata.request.getHeaders());
         }
         catch (Exception e) {
         }
     }
    @Override
        maybeInit();
        try {
            put(URI.create(data.request.getUri().toString()), data.response.headers());
        }
        catch (Exception e) {
        }
    }
    public void put(URI uriHeaders headers) {
        maybeInit();
        try {
            .put(uriheaders.getMultiMap());
            // no cookies to persist.
            if (headers.get("Set-Cookie") == null)
                return;
            List<HttpCookiecookies = .getCookieStore().get(uri);
            Headers dump = new Headers();
            for (HttpCookie cookiecookies) {
                dump.add("Set-Cookie"cookie.getName() + "=" + cookie.getValue());
            }
            String key = uri.getScheme() + "://" + uri.getAuthority();
            .edit().putString(keydump.toPrefixString("HTTP/1.1 200 OK")).commit();
        }
        catch (Exception e) {
        }
    }
New to GrepCode? Check out our FAQ X