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() {
         getCookieStore().removeAll();
         .edit().clear().commit();
     }
 
     public CookieManager getCookieManager() {
         return ;
     }
 
     public CookieMiddleware(Context contextString name) {
         reinit(contextname);
     }
 
     public void reinit(Context contextString name) {
          = new CookieManager(nullnull);
          = context.getSharedPreferences(name + "-cookies", Context.MODE_PRIVATE);
 
         Map<String, ?> allPrefs = .getAll();
         for (String keyallPrefs.keySet()) {
             try {
                 String value = .getString(keynull);
                 RawHeaders headers = new RawHeaders();
                 String[] lines = value.split("\n");
                 boolean first = true;
                 for (String linelines) {
                     if (first) {
                         first = false;
                         headers.setStatusLine(line);
                     }
                     else if (!TextUtils.isEmpty(line)) {
                         headers.addLine(line);
                     }
                 }
                 .put(URI.create(key), headers.toMultimap());
             }
             catch (Exception e) {
                 Log.e("Ion""unable to load cookies"e);
             }
         }
     }
 
     @Override
     public void onSocket(OnSocketData data) {
         try {
             Map<StringList<String>> cookies = .get(URI.create(data.request.getUri().toString()), data.request.getHeaders().getHeaders().toMultimap());
             data.request.getHeaders().addCookies(cookies);
         }
         catch (Exception e) {
         }
     }
 
     @Override
     public void onHeadersReceived(OnHeadersReceivedData data) {
         try {
             put(URI.create(data.request.getUri().toString()), data.headers.getHeaders());
         }
         catch (Exception e) {
         }
     }
 
     public void put(URI uriRawHeaders headers) {
         try {
             .put(uriheaders.toMultimap());
 
             // no cookies to persist.
             if (headers.get("Set-Cookie") == null)
                 return;
 
             List<HttpCookiecookies = .getCookieStore().get(uri);
 
             RawHeaders dump = new RawHeaders();
            dump.setStatusLine("HTTP/1.1 200 OK");
            for (HttpCookie cookiecookies) {
                dump.add("Set-Cookie"cookie.getName() + "=" + cookie.getValue());
            }
            String key = uri.getScheme() + "://" + uri.getAuthority();
            .edit().putString(keydump.toHeaderString()).commit();
        }
        catch (Exception e) {
        }
    }
New to GrepCode? Check out our FAQ X