Android Open Source - ExampleApp Cookie Helper






From Project

Back to project page ExampleApp.

License

The source code is released under:

Copyright (c) 2014, Altinn All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redis...

If you think the Android project ExampleApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.altinn.apps.fisher.net;
/**//w ww  . j  a  va 2  s . c  o m
 * Since we do not have any web-service which will do login for us and respond back some auth-code to get response from web-service
 * So CookieHelper class is used to retrieve cookies (Auth-Token) when we perform login operation through web-view
 * also it will update the cookies if we received (refresh-token) it while accessing web service.
 * 
 * This class stores the cookies for future references. 
 * It is a singleton class accessible throughout the application.
 * 
 */
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.message.BasicHeader;

import android.util.Log;

import com.altinn.apps.fisher.utils.PreferenceUtils;
import com.altinn.apps.fisher.utils.Utils;

public class CookieHelper {
  private static final String TAG = "com.altinn.apps.fisher.net.CookieHelper";
  private static CookieHelper mInstance;
  private  Hashtable<String, String> cookieMap;
  
  private CookieHelper(){
    cookieMap = new Hashtable<String, String>();
    initCookie();
  }
  
  public static CookieHelper getInstance(){
    if(mInstance == null)
      mInstance = new CookieHelper();
    return mInstance;
  }
  
  public void updateCookies(Header[] headers){
    
    if(headers != null){
      for(Header header:headers){
        String key = header.getName();
        String value = header.getValue();
        if(cookieMap.containsKey(key))
          cookieMap.put(key, value);
      }
    }
  }
  private void initCookie(){
    cookieMap.put("BIGipServerai2v2-tt02-intweb-80", "2652430787.20480.0000");
    cookieMap.put("ASP.NET_SessionId", "vddjtq55ca2veemsx1wzwn55");
    cookieMap.put("BIGipServerai2v2-tt02-portal-80", "2920866243.20480.0000");
    cookieMap.put(".ASPXAUTH", "D6D09DD6CC07A3B973F2CD84EACFAAF316BCFF8BAC47756E0BF787C069F5B078DD5E1B190993F4FF2E2F19C87D6E43780B37860ABA2DFF8372037939A9A3118B4A582ECBB126CACC19488B68E1BC5EB5199F43D4CB6451C5C531A818BB17221B35CBDD9DECE0BBA4EEDED8B28FC95A6110192DC42262C363CA587A5E285CBBA3219D4348586DA42B895E8D51E11696F241FDF28F91E76D8EEDD57F91C5305112D5F56C63615088DA5814EC66599D918A766AB049");
    cookieMap.put("Environment", "Identified");
    cookieMap.put("altinnContext", "lFGpvr0ZkFwGCRa+BnmNCNt7WAfYs8jMMWyj8jHhmfY=");
    cookieMap.put("altinnPersistentContext", "UL=1044&LM=10");
    updateCookie(PreferenceUtils.getInstance().getCookies(), false);
  }
  
  public void updateCookie(String cookieStr, boolean fromNetwork){
    if(Utils.isNullorEmpty(cookieStr))
      return;
    StringTokenizer tokenizer = new StringTokenizer(cookieStr, ";");
      while( tokenizer.hasMoreElements()){//Parsing of the cookies element
        String token = tokenizer.nextToken().trim();        
        int index = -1;
        index = token.indexOf('=');
        if(index>0){
          String key = token.substring(0, index);
          String value = token.substring(index+1);
          if(cookieMap.containsKey(key)){
            if(!cookieMap.get(key).startsWith(value)){
              cookieMap.put(key, value);
              //cookie associated for this (.ASPXAUTH) is primarily responsible for accessing web service
              if(key.equalsIgnoreCase(".ASPXAUTH") && fromNetwork){            
                PreferenceUtils.getInstance().setCookieUpdateTime(System.currentTimeMillis());
                Log.d(TAG, "TIME-Cokkie-Uapdate: "+(new Date(System.currentTimeMillis())).toString());
              }
            }
          }
        }
      }
      PreferenceUtils.getInstance().setCookies(cookieStr);
  }
  /**
   * A cookie header created from preferences
   * While making any call to access web-service we have to set this header
   * @return header
   */
  public Header getCookieHeader(){
    Enumeration<String> keys = cookieMap.keys();
    String cookieStr = "";
    while(keys.hasMoreElements()){
      String key = keys.nextElement();
      String value = cookieMap.get(key);
      cookieStr += key+"="+value+"; ";
    }
    
    Header header = new BasicHeader("Cookie",cookieStr.trim());
    return header;
  }
  
  /**
   * Update the cookie if we get refresh token while accessing web-service
   * if in HttpResponse there is any 'Location' header and task execution is kind of 
   * post-message in that case here we are parsing message-id from location header and returned
   * @param response
   * @return
   */
  public String updateCokkieFromHeaders(HttpResponse response){
    String msgReturn = null; //here location header get returned
    //expected like "Location: https://tt02.altinn.basefarm.net/api/910463152/messages/b1110209"
    //Here we need to return meesage-id b1110209
    if(response != null){
      System.out.println("****** HEADER START *********");
      Header[] headers = response.getAllHeaders();
      for(Header header:headers){
        System.out.println( header.getName() + ":"+header.getValue());
        if(header.getName().equalsIgnoreCase("Set-Cookie")){
          updateCookie(header.getValue(),true);
        }
        if(header.getName().equalsIgnoreCase("Location")){
          int index = header.getValue().lastIndexOf('/');
          msgReturn = header.getValue().substring(index+1);
        }
      }
      System.out.println("****** HEADER END *********");
    }
    return msgReturn;
  }
  

}




Java Source Code List

com.altinn.apps.fisher.AppContext.java
com.altinn.apps.fisher.CacheManager.java
com.altinn.apps.fisher.common.AppConstants.java
com.altinn.apps.fisher.common.IStatusMessage.java
com.altinn.apps.fisher.common.MenuItem.java
com.altinn.apps.fisher.common.StatusMessage.java
com.altinn.apps.fisher.db.DataBaseHelper.java
com.altinn.apps.fisher.db.FactoryDBHelper.java
com.altinn.apps.fisher.db.FishCategoryDBHelper.java
com.altinn.apps.fisher.db.FormDBHelper.java
com.altinn.apps.fisher.db.IDBHelper.java
com.altinn.apps.fisher.db.RegsDBHelper.java
com.altinn.apps.fisher.db.VesselDBHelper.java
com.altinn.apps.fisher.gps.CLocationProvider.java
com.altinn.apps.fisher.gps.ILocationUpdateListner.java
com.altinn.apps.fisher.models.CaughtInfoData.java
com.altinn.apps.fisher.models.InfoData.java
com.altinn.apps.fisher.models.ReportInfoData.java
com.altinn.apps.fisher.models.UserProfile.java
com.altinn.apps.fisher.net.AbstractWorkerTask.java
com.altinn.apps.fisher.net.CookieHelper.java
com.altinn.apps.fisher.net.IParser.java
com.altinn.apps.fisher.net.JSParser.java
com.altinn.apps.fisher.net.ParseManager.java
com.altinn.apps.fisher.net.TaskNotifier.java
com.altinn.apps.fisher.net.jsobj.AttachmentObj.java
com.altinn.apps.fisher.net.jsobj.FormObj.java
com.altinn.apps.fisher.net.jsobj.JSConstants.java
com.altinn.apps.fisher.net.jsobj.JsonObj.java
com.altinn.apps.fisher.net.jsobj.LinkItemObj.java
com.altinn.apps.fisher.net.jsobj.LinkObj.java
com.altinn.apps.fisher.net.jsobj.MessageObj.java
com.altinn.apps.fisher.net.jsobj.MessagesEmbedded.java
com.altinn.apps.fisher.net.jsobj.OrganisationObj.java
com.altinn.apps.fisher.net.tasks.LoginTask.java
com.altinn.apps.fisher.net.tasks.RefreshTokenTask.java
com.altinn.apps.fisher.net.tasks.SendReportTask.java
com.altinn.apps.fisher.net.tasks.UserProfileTask.java
com.altinn.apps.fisher.settings.FactoryDetails.java
com.altinn.apps.fisher.settings.FishDetails.java
com.altinn.apps.fisher.settings.SettingItem.java
com.altinn.apps.fisher.settings.VesselsDetails.java
com.altinn.apps.fisher.ui.component.DurationTimePickDialog.java
com.altinn.apps.fisher.ui.component.RAutoCompleteTextView.java
com.altinn.apps.fisher.ui.component.RButton.java
com.altinn.apps.fisher.ui.component.REditText.java
com.altinn.apps.fisher.ui.component.RTextView.java
com.altinn.apps.fisher.ui.screen.BaseActivity.java
com.altinn.apps.fisher.ui.screen.BrowserActivity.java
com.altinn.apps.fisher.ui.screen.FactoryDetailsActivity.java
com.altinn.apps.fisher.ui.screen.HomeActivity.java
com.altinn.apps.fisher.ui.screen.InformationActivity.java
com.altinn.apps.fisher.ui.screen.MenuNavigationActivity.java
com.altinn.apps.fisher.ui.screen.ReportActivity.java
com.altinn.apps.fisher.ui.screen.ReportReceivedFishActivity.java
com.altinn.apps.fisher.ui.screen.ReportSendDetailActivity.java
com.altinn.apps.fisher.ui.screen.SplashActivity.java
com.altinn.apps.fisher.ui.screen.UserProfileActivity.java
com.altinn.apps.fisher.utils.PreferenceUtils.java
com.altinn.apps.fisher.utils.Utils.java
net.simonvt.menudrawer.BuildLayerFrameLayout.java
net.simonvt.menudrawer.ColorDrawable.java
net.simonvt.menudrawer.DraggableDrawer.java
net.simonvt.menudrawer.FloatScroller.java
net.simonvt.menudrawer.MenuDrawer.java
net.simonvt.menudrawer.NoClickThroughFrameLayout.java
net.simonvt.menudrawer.OverlayDrawer.java
net.simonvt.menudrawer.PeekInterpolator.java
net.simonvt.menudrawer.Position.java
net.simonvt.menudrawer.Scroller.java
net.simonvt.menudrawer.SinusoidalInterpolator.java
net.simonvt.menudrawer.SlideDrawable.java
net.simonvt.menudrawer.SlidingDrawer.java
net.simonvt.menudrawer.SmoothInterpolator.java
net.simonvt.menudrawer.StaticDrawer.java
net.simonvt.menudrawer.ViewHelper.java
net.simonvt.menudrawer.compat.ActionBarHelperCompat.java
net.simonvt.menudrawer.compat.ActionBarHelperNative.java
net.simonvt.menudrawer.compat.ActionBarHelper.java