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

📄 cookie.java

📁 FIRE (Flexible Interface Rendering Engine)是一个J2ME上的灵活的图形界面引擎
💻 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.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.util.Calendar;import java.util.Date;import java.util.TimeZone;import java.util.Vector;/** * A utility class that represents a cookie.  * @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;		/**	 * Empty constructor used only for deserialization	 * @see Cookie#deSerialize(DataInputStream)	 */	private Cookie()	{	}		/**	 * Creates a cookie instance from the given cookie string returned by an http request to the given host	 * @param cookieStr the cookie string	 * @param host the host that returned the cookie string	 */	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)		{			// get the domain name from the host.			// 			Vector v = StringUtil.split(host,".");			if(v.size()<3) domain = host;			else			{				domain="."+v.elementAt(v.size()-2)+"."+v.elementAt(v.size()-1);			}		}	}			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;	}		/**	 * From a string that contains one or more cookies this method creates a Vector with Cookie instances.<br/>	 * 	 * 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	 * 	 * @param cookieStr	 * @return	 */	public static Vector splitCookies(String cookieStr)	{		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;	}		/**	 * Writes the given cookie to the supplied data output stream.	 * @param c	 * @param out	 * @throws IOException	 */	public static void serialize(Cookie c,DataOutputStream out) throws IOException	{		out.writeUTF(c.name);		out.writeUTF(c.value);		out.writeUTF(c.domain);		out.writeUTF(c.path);		if(c.expires!=null)			out.writeLong(c.expires.getTime());		else 			out.writeLong(0L);		out.writeBoolean(c.secure);	}		/**	 * Reads a serialized Cookie from the supplied DataInputStream, and returns a new instance of that Cookie.	 * The serialized cookie must have been serialized using the {@link #serialize(Cookie, DataOutputStream)}.	 *  	 * @param in	 * @return	 * @throws IOException	 */	public static Cookie deSerialize(DataInputStream in) throws IOException	{		Cookie c = new Cookie();		c.name = in.readUTF();		c.value= in.readUTF();		c.domain=in.readUTF();		c.path = in.readUTF();		long exp = in.readLong();		if(exp==0L) c.expires=null;		else c.expires = new Date(exp);		c.secure = in.readBoolean();		return c;	}		/**	 * Checks if this Cookie mathes the given domain and path.	 * @param withDomain	 * @param withPath	 * @return	 */	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 + -