All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.ginere.base.web.util.CookieUtils Maven / Gradle / Ivy

/**
 * Copyright: Angel-Ventura Mendo Gomez
 *	      [email protected]
 *
 * $Id: CookieUtils.java,v 1.2 2006/11/25 07:19:55 ventura Exp $
 */
package eu.ginere.base.web.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import eu.ginere.base.util.properties.GlobalFileProperties;
import eu.ginere.base.web.connectors.users.UsersConnector;
import eu.ginere.base.web.session.SessionAccesor;

/**
 * Utilities for generating and reading cookies
 *
 * @author Angel Mendo
 * @version $Revision: 1.2 $
 */
public class CookieUtils {
	public static final Logger log = Logger.getLogger(UserAgentManager.class);
	
	private static final String COOKIE_UUID_NAME=GlobalFileProperties.getStringValue(CookieUtils.class, "UUIDCookieName","UUID");
	private static final String COOKIE_LOGIN_NAME=GlobalFileProperties.getStringValue(CookieUtils.class, "LoginCookieName","Login");
	private static final int COOKIE_MAX_AGE=Integer.MAX_VALUE; // 24*60*60*1000; // time in seconds

	public static void storeUserLoginCookie(HttpServletRequest request,
 											HttpServletResponse response,
 											boolean storeCookie,
											String userId){
		
		// First erase the old cookie if any
		removeCoockieAuth(request,response);
		
		// Then creatting a new one
//		String uuid=getUUIDCookie(request);
		String uuid=SessionAccesor.getUUID(request);
		String stringValue=UsersConnector.generateLoginCookie(userId, uuid);

//		if (stringValue == null){
//			// si hay un genrador salimo
//			return ;
//		}
		
		Cookie cookie=new Cookie(COOKIE_LOGIN_NAME,stringValue+" "+userId);
		cookie.setComment(userId);
		
		if (storeCookie){
			cookie.setMaxAge(COOKIE_MAX_AGE);
		} else {
			cookie.setMaxAge(-1);
		}
// Montado en apache a si que para todo el pat
//		cookie.setPath(request.getContextPath());
		cookie.setPath("/");
	
// No hay gestion de DNS.
//		
//		// for domaines like ventura.bloghispano.org , set .bloghispano.org
//		// domain name begins with a dot (.foo.com) and means that the cookie is
//		// visible to servers in a specified Domain Name System (DNS) zone (for 
//		// example, www.foo.com, but not a.b.foo.com). By default, cookies are 
//		// only returned to the server that sent them. 
//		String domain=request.getServerName();
//		int firstIndex=domain.indexOf('.');
//
//		if (firstIndex>=0){
//			int sencondIndex=domain.indexOf('.',firstIndex+1);
//			if (sencondIndex>=0){
//				int thirdIndex=domain.indexOf('.',sencondIndex+1);
//				if (thirdIndex<0){
//					domain=domain.substring(firstIndex);
//				}
//			}			
//		}

//		cookie.setDomain(domain);
		
		response.addCookie(cookie);

//
//		log.warn("STORE ++++++++++++++++++++++++++++++++++++++++++");
//		log.warn("++++++++++++++++++++++++++++++++++++++++++");
//		log.warn("++++++++++++++++++++++++++++++++++++++++++");
//		log.warn("StringValue:"+stringValue);
//		log.warn("COOKIE_MAX_AGE:"+COOKIE_MAX_AGE);
//		log.warn("userId:"+userId);
//		
//
//		log.warn("getName:"+cookie.getName());
//		log.warn("getDomain:"+cookie.getDomain());
//		log.warn("getPath:"+cookie.getPath());
//		log.warn("getSecure:"+cookie.getSecure());
//		log.warn("getValue:"+cookie.getValue());
//		log.warn("getComment:"+cookie.getComment());
//			
//		log.warn("++++++++++++++++++++++++++++++++++++++++++");
//		log.warn("++++++++++++++++++++++++++++++++++++++++++");
//		log.warn("++++++++++++++++++++++++++++++++++++++++++");

	}

	/**
	 * Returns the userId from cookie auth si la cookie es valida
	 */
 	public static String getUserIdFromCoockieAuth(HttpServletRequest request,
												  HttpServletResponse response,
												  String uuid){
		Cookie cookies[]=request.getCookies();
		if (cookies==null){
			return null;
		}
		for (int i=0;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy