8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: CookieUtils.java,v 1.3 2008/06/25 05:41:41 qcheng Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.iplanet.services.util;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.am.util.SystemProperties;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.Constants;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.debug.Debug;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.encode.URLEncDec;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Collections;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.HashSet;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Set;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.StringTokenizer;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.http.Cookie;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.http.HttpServletRequest;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Implements utility methods for handling Cookie.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @deprecated As of OpenSSO version 8.0
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * {@link com.sun.identity.shared.encode.CookieUtils}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class CookieUtils {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static boolean secureCookie = (SystemProperties
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .get(Constants.AM_COOKIE_SECURE) != null && SystemProperties.get(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Constants.AM_COOKIE_SECURE).equalsIgnoreCase("true"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static boolean cookieEncoding = (SystemProperties
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .get(Constants.AM_COOKIE_ENCODE) != null && SystemProperties.get(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Constants.AM_COOKIE_ENCODE).equalsIgnoreCase("true"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static String amCookieName = SystemProperties.get(Constants.AM_COOKIE_NAME);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static String amPCookieName = SystemProperties
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .get(Constants.AM_PCOOKIE_NAME);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static String cdssoCookiedomain = SystemProperties
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .get(Constants.SERVICES_CDSSO_COOKIE_DOMAIN);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static String fedCookieName = SystemProperties
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .get(Constants.FEDERATION_FED_COOKIE_NAME);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static Set cookieDomains = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static int defAge = -1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static Debug debug = Debug.getInstance("amCookieUtils");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Gets property value of "com.iplanet.am.cookie.name"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return the property value of "com.iplanet.am.cookie.name"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String getAmCookieName() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return amCookieName;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Gets property value of "com.iplanet.am.pcookie.name"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return the property value of "com.iplanet.am.pcookie.name"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String getAmPCookieName() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return amPCookieName;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Gets property value of "com.iplanet.services.cdsso.cookiedomain"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return the property value of "com.iplanet.services.cdsso.cookiedomain"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static Set getCdssoCookiedomain() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cookieDomains != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return cookieDomains;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set cookieDomains = new HashSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cdssoCookiedomain == null || cdssoCookiedomain.length() < 1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return Collections.EMPTY_SET;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringTokenizer st = new StringTokenizer(cdssoCookiedomain, ",");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (st.hasMoreTokens()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String token = st.nextToken().trim();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (token.length() > 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookieDomains.add(token);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cookieDomains.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return Collections.EMPTY_SET;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return cookieDomains;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Gets property value of "com.sun.identity.federation.fedCookieName"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return the property value of "com.sun.identity.federation.fedCookieName"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String getFedCookieName() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return fedCookieName;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Gets property value of "com.iplanet.am.cookie.secure"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return the property value of "com.iplanet.am.cookie.secure"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static boolean isCookieSecure() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return secureCookie;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Gets value of cookie that has mached name in servlet request
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param req
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * request
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * name in servlet request
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return value of that name of cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String getCookieValueFromReq(HttpServletRequest req,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String name) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String cookieValue = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Cookie cookie = getCookieFromReq(req, name);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cookie != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return getCookieValue(cookie);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.message("No Cookie is in the request");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (Exception e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.error("Error getting cookie : ", e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return cookieValue;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Gets cookie object that has mached name in servlet request
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param req
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * request
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * name in servlet request
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return value of that name of cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static Cookie getCookieFromReq(HttpServletRequest req, String name) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Cookie cookies[] = req.getCookies();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cookies != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int nCookie = 0; nCookie < cookies.length; nCookie++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cookies[nCookie].getName().equalsIgnoreCase(name)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return cookies[nCookie];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Gets normalized value of cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * cookie object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return value
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String getCookieValue(Cookie cookie) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String cookieValue = checkDoubleQuote(cookie.getValue());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check property value and it decode value
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Bea, IBM
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cookieValue != null && cookieEncoding) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return URLEncDec.decode(cookieValue);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return cookieValue;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Gets Array of cookie in servlet request
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param req
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * request
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static Cookie[] getCookieArrayFromReq(HttpServletRequest req) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Cookie cookies[] = req.getCookies();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!cookieEncoding) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return cookies;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cookies != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int nCookie = 0; nCookie < cookies.length; nCookie++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String cookieValue = checkDoubleQuote(cookies[nCookie]
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getValue());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cookieValue != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookies[nCookie].setValue(URLEncDec.decode(cookieValue));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return cookies;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Constructs a cookie with a specified name and value.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a String specifying the name of the cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param value
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a String specifying the value of the cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return constructed cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static Cookie newCookie(String name, String value) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return newCookie(name, value, defAge, null, null);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Constructs a cookie with a specified name and value and sets the maximum
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * age of the cookie in seconds.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a String specifying the name of the cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param value
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a String specifying the value of the cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param maxAge
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * an integer specifying the maximum age of the cookie in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * seconds; if negative, means the cookie is not stored; if zero,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * deletes the cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return constructed cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static Cookie newCookie(String name, String value, int maxAge) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return newCookie(name, value, maxAge, null, null);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Constructs a cookie with a specified name and value and sets a path for
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the cookie to which the client should return the cookie.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a String specifying the name of the cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param value
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a String specifying the value of the cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param path
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a String specifying a path
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return constructed cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static Cookie newCookie(String name, String value, String path) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return newCookie(name, value, defAge, path, null);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Constructs a cookie with a specified name and value and sets a path for
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the cookie to which the client should return the cookie and sets the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * domain within which this cookie should be presented.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a String specifying the name of the cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param value
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a String specifying the value of the cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param path
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a String specifying a path
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param domain
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a String containing the domain name within which this cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * is visible; form is according to <code>RFC 2109</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return constructed cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static Cookie newCookie(String name, String value, String path,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String domain) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return newCookie(name, value, defAge, path, domain);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Constructs a cookie with a specified name and value and sets the maximum
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * age of the cookie in seconds and sets a path for the cookie to which the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * client should return the cookie and sets the domain within which this
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * cookie should be presented.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a String specifying the name of the cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param value
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a String specifying the value of the cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param maxAge
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * an integer specifying the maximum age of the cookie in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * seconds; if negative, means the cookie is not stored; if zero,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * deletes the cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param path
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a String specifying a path
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param domain
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a String containing the domain name within which this cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * is visible; form is according to RFC 2109
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return constructed cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static Cookie newCookie(String name, String value, int maxAge,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String path, String domain) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Cookie cookie = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Based on property value it does url encoding.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // BEA, IBM
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (cookieEncoding) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookie = new Cookie(name, URLEncDec.encode(value));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookie = new Cookie(name, value);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookie.setMaxAge(maxAge);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((path != null) && (path.length() > 0)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookie.setPath(path);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookie.setPath("/");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((domain != null) && (domain.length() > 0)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookie.setDomain(domain);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookie.setSecure(isCookieSecure());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return cookie;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Check cookie value whether it has double quote or not. Remove start /
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * ending double quote from cookie and returns cookie value only.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a String value of cookie
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return cookie value without double quote
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String checkDoubleQuote(String cookie) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String double_quote = "\"";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((cookie != null) && cookie.startsWith(double_quote)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster && cookie.endsWith(double_quote)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int last = cookie.length() - 1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cookie = cookie.substring(1, last);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return cookie;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}