com.cappuccino.requestframework.CookieManager.java Source code

Java tutorial

Introduction

Here is the source code for com.cappuccino.requestframework.CookieManager.java

Source

/*
 *  Copyright 2013-2014 Jin-woo Lee
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.cappuccino.requestframework;

import java.util.List;

import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.BasicClientCookie;

import com.cappuccino.log.Log;

// final : subclassing 
// final  abstract  
// instantiation? ?   subclassing ? ?  .
public final class CookieManager {

    // private constructor : instantiation 
    private CookieManager() {
    }

    // android ? CookieManager   ?
    // CookieManager   WebView  ? ? ? 
    //  Rev:225 => Util / LoginRequest / InitActivity ? CookieManager ?
    // .
    //
    //  CookieManager    ? ?
    // ? / ?? csv  ?, parsing ? .
    // ? ? SharedPreferences  ?.

    private static CookieApplication application;
    private static boolean initialized = false;

    public static void initialize(CookieApplication application) {

        //  
        CookieManager.application = application;

        //  
        CookieManager.initialized = true;
    }

    public static void loadCookie(DefaultHttpClient client) {

        //  
        if (CookieManager.initialized == false) {
            throw new RuntimeException(" ?");
        }
        if (client == null) {
            throw new IllegalArgumentException();
        }

        //  ?  
        if (application.hasCookie() == false) {
            // ?  ?   
            return;
        } else {
            // ?  ? 
            String cookieName = application.getCookieName();
            String cookieValue = application.getCookieValue();
            String cookieDomain = application.getCookieDomain();

            if (RequestConfig.debuggable() == true) {
                Log.e();
                Log.e("  ?");
                Log.e(" ? : " + cookieName);
                Log.e("  : " + cookieValue);
                Log.e(" ?? : " + cookieDomain);
            }

            //    
            BasicClientCookie cookie = new BasicClientCookie(cookieName, cookieValue);
            cookie.setDomain(cookieDomain);

            //  
            client.getCookieStore().addCookie(cookie);
        }
    }

    public static void saveCookie(DefaultHttpClient client) {

        //  
        if (CookieManager.initialized == false) {
            throw new RuntimeException(" ?");
        }
        if (client == null) {
            throw new IllegalArgumentException();
        }

        //    ? ?
        List<Cookie> cookieList = client.getCookieStore().getCookies();
        if (!cookieList.isEmpty()) {
            //  ? 
            for (Cookie cookie : cookieList) {

                //  sp ? 
                String cookieName = cookie.getName();
                String cookieValue = cookie.getValue();
                String cookieDomain = cookie.getDomain();
                // Date cookieExpiryDate = cookie.getExpiryDate();

                application.setCookieName(cookieName);
                application.setCookieValue(cookieValue);
                application.setCookieDomain(cookieDomain);
                // application.setCookieExpiryDate(cookieExpiryDate);

                if (RequestConfig.debuggable() == true) {
                    Log.e();
                    Log.e("?  ?");
                    Log.e(" ? : " + cookieName);
                    Log.e("  : " + cookieValue);
                    Log.e(" ?? : " + cookieDomain);
                }
            }
        }
    }

}