⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cookie.java

📁 wap浏览器 日程安排 Rss 棋牌游戏
💻 JAVA
字号:
/* * Fire (Flexible Interface Rendering Engine) is a set of graphics widgets for creating GUIs for j2me applications.  * Copyright (C) 2006-2008 Bluevibe (www.bluevibe.net) * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. *  * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. *  * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA *  *//** *  */package gr.fire.browser.util;import gr.fire.util.Log;import gr.fire.util.StringUtil;import java.util.Calendar;import java.util.Date;import java.util.TimeZone;import java.util.Vector;/** * @author padeler * */public class Cookie{	public static final String months[] =  { "jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};	public static final String days[] =  { "Mon","Tue","Wed","Thu","Fri","Sat","Sun"};		private String name;	private String value;	private String domain=null;	private String path=null;	private Date expires=null;	private boolean secure=false;			public Cookie(String cookieStr,String host) 	{		Vector avpairs = StringUtil.split(cookieStr,";");		if(avpairs.size()<1) throw new IllegalArgumentException("Bad cookie, no name=value pair found.");				String nameValue = (String)avpairs.elementAt(0);		int idx = nameValue.indexOf('=');		if(idx==-1) throw new IllegalArgumentException("Bad cookie. malformed name=value pair");				name = nameValue.substring(0,idx).trim();		value = nameValue.substring(idx+1).trim();						for(int i=1;i<avpairs.size();++i)		{			nameValue = (String)avpairs.elementAt(i);			String attr,attrValue;						idx = nameValue.indexOf('=');			if(idx==-1){attr=nameValue.toLowerCase();attrValue=attr;}			else{				attr = nameValue.substring(0,idx).trim().toLowerCase();				attrValue = nameValue.substring(idx+1).trim();			}						if(attr.length()==0) continue;						if(attr.equals("path"))			{				path = attrValue;			}			else if(attr.equals("domain"))			{				domain = attrValue;			}			else if(attr.equals("expires"))			{				String expStr = attrValue;				Vector expV = StringUtil.split(expStr," ");				if(expV.size()<3) throw new IllegalArgumentException("Bad cookie, malformed expiration date "+expStr);				try{					// date is formated : Fri, 31-Dec-2010 23:59:59 GMT					// but the splitCookies method will remove the day part (Fri,)					// so the date is formated 31-Dec-2010 23:59:59 GMT					String date = (String)expV.elementAt(0);					String time = (String)expV.elementAt(1);										Calendar cal = Calendar.getInstance();					cal.setTimeZone(TimeZone.getTimeZone("GMT"));										// parse date					expV = StringUtil.split(date,"-");					cal.set(Calendar.YEAR,Integer.parseInt((String)expV.elementAt(2)));										String m = ((String)expV.elementAt(1)).toLowerCase();					int month=0;					for(month =0;month<months.length;++month)						if(months[month].startsWith(m)) break;										cal.set(Calendar.MONTH,month);					cal.set(Calendar.DAY_OF_MONTH,Integer.parseInt((String)expV.elementAt(0)));										//parse time					expV = StringUtil.split(time,":");					cal.set(Calendar.HOUR,Integer.parseInt((String)expV.elementAt(0)));					cal.set(Calendar.MINUTE,Integer.parseInt((String)expV.elementAt(1)));					cal.set(Calendar.SECOND,Integer.parseInt((String)expV.elementAt(2)));					expires = cal.getTime();									}catch(Exception e)				{					Log.logWarn("Failed to parse expiration date of cookie.",e);					throw new IllegalArgumentException("Bad cookie, malformed expiration date "+expStr);				}			}			else if(attr.equals("secure"))			{				secure=true;			}			else			{				Log.logWarn("Unknown cookie attribute "+nameValue);			}		}		if(path==null) path="/";		if(domain==null) domain=host;	}		public boolean equals(Object obj)	{		if(obj instanceof Cookie)		{			Cookie c = (Cookie)obj;			return c.name.equals(name) && c.domain.equals(domain) && c.path.equals(path);		}		return false;	}		public static Vector splitCookies(String cookieStr)	{		// The string can have the following form:		// COOKIE1_NAME=COOKIE1_VALUE;ATTR1=ATTR1_VAL;ATTR2=ATTR2_VAL, COOKIE2_NAME=COOKIE2_VALUE;ATTR1=ATTR1_VAL;ATTR2=ATTR2_VAL		Vector res = new Vector();				// remove all day strings from the string		StringUtil tokenizer = new StringUtil(cookieStr);		StringBuffer cookie=new StringBuffer();				String token;		while((token = tokenizer.nextToken(','))!=null)		{			boolean inDay=false;			for(int i =0 ;i<days.length;++i)			{				if(token.endsWith(days[i]))				{					token = token.substring(0,token.length()-days[i].length());					inDay=true;					break;				}			}			cookie.append(token);						if(!inDay)			{				res.addElement(cookie.toString());				cookie = new StringBuffer();			}		}		return res;	}		public boolean match(String withDomain,String withPath)	{		return (withDomain.endsWith(domain) && withPath.startsWith(path));	}		public String toString()	{		return name+"="+value;	}	public String getName()	{		return name;	}	public String getValue()	{		return value;	}	public String getDomain()	{		return domain;	}	public String getPath()	{		return path;	}	public Date getExpires()	{		return expires;	}	public boolean isSecure()	{		return secure;	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -