SimpleCookie.java

/***************************************************************************
   Copyright 2008 Emily Estes

   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 net.metanotion.web.concrete;


import net.metanotion.web.Cookie;

/** This class is just a simple value class to contain cookie information. */
public final class SimpleCookie implements Cookie {
	private final String comment;
	private final String domain;
	private final int maxAge;
	private final String name;
	private final String path;
	private final Cookie.Security isSecure;
	private final Cookie.Access isHttpOnly;
	private final String value;
	private final Version version;

	/** Create a cookie with default values for most fields.
		@param name The name of the cookie (see {@link #getName}).
		@param value The value of the cookie (see {@link #getValue}).
		@param forever True if the cooke should last {@link net.metanotion.web.Cookie#ONE_YEAR_IN_SECONDS} or -1 which
			means it will just live for the sssion only.
	*/
	public SimpleCookie(final String name, final String value, final boolean forever) {
		this(name,
			value,
			null,
			null,
			(forever ? Cookie.ONE_YEAR_IN_SECONDS : -1),
			null,
			Cookie.Security.SSL,
			Cookie.Access.HTTP,
			Cookie.Version.NETSCAPE);
	}

	/** Create a session only cookie with default values for most fields.
		@param name The name of the cookie (see {@link #getName}).
		@param value The value of the cookie (see {@link #getValue}).
	*/
	public SimpleCookie(final String name, final String value) {
		this(name, value, null, null, -1, null, Cookie.Security.SSL, Cookie.Access.HTTP, Cookie.Version.NETSCAPE);
	}

	/** Create a session only cookie with default values for most fields.
		@param name The name of the cookie (see {@link #getName}).
		@param value The value of the cookie (see {@link #getValue}).
		@param domain The domain name the cookie is attached to (see {@link #getDomain()}).
		@param path The resource path on the server the cookie is attached to (see {@link #getPath}).
	*/
	public SimpleCookie(final String name, final String value, final String domain, final String path) {
		this(name, value, null, domain, -1, path, Cookie.Security.SSL, Cookie.Access.HTTP, Cookie.Version.NETSCAPE);
	}

	/** Create a session only cookie.
		@param name The name of the cookie (see {@link #getName}).
		@param value The value of the cookie (see {@link #getValue}).
		@param comment The comment attached to the cookie (see {@link #getComment}).
		@param domain The domain name the cookie is attached to (see {@link #getDomain}).
		@param path The resource path on the server the cookie is attached to (see {@link #getPath}).
	*/
	public SimpleCookie(final String name, final String value, final String comment, final String domain, final String path) {
		this(name, value, comment, domain, -1, path, Cookie.Security.SSL, Cookie.Access.HTTP, Cookie.Version.NETSCAPE);
	}

	/** Create a cookie.
		@param name The name of the cookie (see {@link #getName}).
		@param value The value of the cookie (see {@link #getValue}).
		@param comment The comment attached to the cookie (see {@link #getComment}).
		@param domain The domain name the cookie is attached to (see {@link #getDomain}).
		@param maxAge -1 means the cookie only persists for the browsing session, 0 means delete, and a positive number
			is the length of time in seconds to keep the cookie (see {@link #getMaxAge}).
		@param path The resource path on the server the cookie is attached to (see {@link #getPath}).
		@param isSecure Whether the cookie can be transmitted in the clear or not (see {@link #getSecure}).
		@param isHttpOnly Whether the cookie can be accessed via JavaScript or not (see {@link #getHttpOnly}).
		@param version The cookie specification this cookie complies with (see {@link #getVersion}).
	*/
	public SimpleCookie(final String name,
			final String value,
			final String comment,
			final String domain,
			final int maxAge,
			final String path,
			final Cookie.Security isSecure,
			final Cookie.Access isHttpOnly,
			final Cookie.Version version) {
		this.name = name;
		this.value = value;
		this.comment = comment;
		this.domain = domain;
		this.maxAge = maxAge;
		this.path = path;
		this.isSecure = isSecure;
		this.isHttpOnly = isHttpOnly;
		this.version = version;
	}

	@Override public String getComment() { return comment; }
	@Override public String getDomain() { return domain; }
	@Override public int getMaxAge() { return maxAge; }
	@Override public String getName() { return name; }
	@Override public String getPath() { return path; }
	@Override public Cookie.Security getSecure() { return isSecure; }
	@Override public Cookie.Access getHttpOnly() { return isHttpOnly; }
	@Override public String getValue() { return value; }
	@Override public Cookie.Version getVersion() { return version; }
}