com.lillicoder.newsblurry.net.SerializableCookie.java Source code

Java tutorial

Introduction

Here is the source code for com.lillicoder.newsblurry.net.SerializableCookie.java

Source

/**
 * Copyright 2012 Scott Weeden-Moody
 *
 * 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.lillicoder.newsblurry.net;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;

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

import android.content.SharedPreferences;

/**
 * Wrapper class for {@link Cookie} that is {@link Serializable}. This class
 * is intended for use with {@link PreferenceCookieStore} so that cookies
 * can be saved to {@link SharedPreferences}.
 * @author lillicoder
 */
public class SerializableCookie implements Serializable {

    private static final String EXCEPTION_INVALID_COOKIE_PARAM = "The given cookie must not be null.";

    /**
     * Serializable ID.
     */
    private static final long serialVersionUID = -9123076458182430144L;

    private transient Cookie _cookie;

    /**
     * Creates a new instance whose values are copied from the given {@link Cookie}.
     * @param cookie {@link Cookie} whose values will be copied.
     */
    public SerializableCookie(Cookie cookie) {
        if (cookie == null)
            throw new IllegalArgumentException(EXCEPTION_INVALID_COOKIE_PARAM);

        this._cookie = cookie;
    }

    /**
     * Gets the {@link Cookie} instance wrapped by this object.
     * @return Wrapped {@link Cookie} instance.
     */
    public Cookie getCookie() {
        return this._cookie;
    }

    /**
     * Sets the {@link Cookie} instance for this object.
     * This is kept private so that users will create instances
     * via constructor.
     */
    private void setCookie(Cookie cookie) {
        this._cookie = cookie;
    }

    /**
     *
     * Serialization methods.
     * 
     */
    private void writeObject(ObjectOutputStream out) throws IOException {
        Cookie cookie = this.getCookie();

        // Write all values that can later be restored.
        // This means excluding comment URL, persistence value,
        // and ports.
        out.writeObject(cookie.getComment());
        out.writeObject(cookie.getDomain());
        out.writeObject(cookie.getExpiryDate());
        out.writeObject(cookie.getName());
        out.writeObject(cookie.getPath());
        out.writeObject(cookie.getValue());
        out.writeInt(cookie.getVersion());
        out.writeBoolean(cookie.isSecure());
    }

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        // Read all values from write operation in order.
        String comment = (String) in.readObject();
        String domain = (String) in.readObject();
        Date expiryDate = (Date) in.readObject();
        String name = (String) in.readObject();
        String path = (String) in.readObject();
        String value = (String) in.readObject();
        int version = in.readInt();
        boolean isSecure = in.readBoolean();

        // Create a new basic cookie and set this wrapper's cookie instance.
        BasicClientCookie cookie = new BasicClientCookie(name, value);
        cookie.setComment(comment);
        cookie.setDomain(domain);
        cookie.setExpiryDate(expiryDate);
        cookie.setPath(path);
        cookie.setValue(value);
        cookie.setVersion(version);
        cookie.setSecure(isSecure);

        this.setCookie(cookie);
    }

}