📄 connectionmanager.java
字号:
buffer.append (resource);
url = new URL (fixSpaces (buffer.toString ()));
ret = openConnection (url);
}
catch (MalformedURLException murle2)
{
String msg = "Error in opening a connection to " + string;
ParserException ex = new ParserException (msg, murle2);
throw ex;
}
catch (IOException ioe)
{
String msg = "Error in opening a connection to " + string;
ParserException ex = new ParserException (msg, ioe);
throw ex;
}
}
return (ret);
}
/**
* Generate a HTTP cookie header value string from the cookie jar.
* <pre>
* The syntax for the header is:
*
* cookie = "Cookie:" cookie-version
* 1*((";" | ",") cookie-value)
* cookie-value = NAME "=" VALUE [";" path] [";" domain]
* cookie-version = "$Version" "=" value
* NAME = attr
* VALUE = value
* path = "$Path" "=" value
* domain = "$Domain" "=" value
*
* </pre>
* @param connection The connection being accessed.
* @see <a href="http://www.ietf.org/rfc/rfc2109.txt">RFC 2109</a>
* @see <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>
*/
public void addCookies (URLConnection connection)
{
Vector list;
URL url;
String host;
String path;
String domain;
if (null != mCookieJar)
{
list = null;
// get the site from the URL
url = connection.getURL ();
host = url.getHost ();
path = url.getPath ();
if (0 == path.length ())
path = "/";
if (null != host)
{ // http://www.objectsdevelopment.com/portal/modules/freecontent/content/javawebserver.html
list = addCookies ((Vector)mCookieJar.get (host), path, list);
domain = getDomain (host);
if (null != domain)
list = addCookies ((Vector)mCookieJar.get (domain),
path, list);
else
// maybe it is the domain we're accessing
list = addCookies ((Vector)mCookieJar.get ("." + host),
path, list);
}
if (null != list)
connection.setRequestProperty ("Cookie",
generateCookieProperty (list));
}
}
/**
* Add qualified cookies from cookies into list.
* @param cookies The list of cookies to check (may be null).
* @param path The path being accessed.
* @param list The list of qualified cookies.
* @return The list of qualified cookies.
*/
protected Vector addCookies (Vector cookies, String path, Vector list)
{
Cookie cookie;
Date expires;
Date now;
if (null != cookies)
{
now = new Date ();
for (int i = 0; i < cookies.size (); i++)
{
cookie = (Cookie)cookies.elementAt (i);
expires = cookie.getExpiryDate ();
if ((null != expires) && expires.before (now))
{
cookies.remove (i);
i--; // dick with the loop variable
}
else
if (path.startsWith (cookie.getPath ()))
{
if (null == list)
list = new Vector ();
list.addElement (cookie);
}
}
}
return (list);
}
/**
* Get the domain from a host.
* @param host The supposed host name.
* @return The domain (with the leading dot),
* or null if the domain cannot be determined.
*/
protected String getDomain (String host)
{
StringTokenizer tokenizer;
int count;
String server;
int length;
boolean ok;
char c;
String ret;
ret = null;
tokenizer = new StringTokenizer (host, ".");
count = tokenizer.countTokens ();
if (3 <= count)
{
// have at least two dots,
// check if we were handed an IP address by mistake
length = host.length ();
ok = false;
for (int i = 0; i < length && !ok; i++)
{
c = host.charAt (i);
if (!(Character.isDigit (c) || (c == '.')))
ok = true;
}
if (ok)
{
// so take everything after the first token
server = tokenizer.nextToken ();
length = server.length ();
ret = host.substring (length);
}
}
return (ret);
}
/**
* Creates the cookie request property value from the list of
* valid cookies for the domain.
* @param cookies The list of valid cookies to be encoded in the request.
* @return A string suitable for inclusion as the value of
* the "Cookie:" request property.
*/
protected String generateCookieProperty (Vector cookies)
{
int version;
Cookie cookie;
StringBuffer buffer;
String ret;
ret = null;
buffer = new StringBuffer ();
version = 0;
for (int i = 0; i < cookies.size (); i++)
version = Math.max (version,
((Cookie)cookies.elementAt (i)).getVersion ());
if (0 != version)
{
buffer.append ("$Version=\"");
buffer.append (version);
buffer.append ("\"");
}
for (int i = 0; i < cookies.size (); i++)
{
cookie = (Cookie)cookies.elementAt (i);
if (0 != buffer.length ())
buffer.append ("; ");
buffer.append (cookie.getName ());
buffer.append ("=");
if (0 != version)
buffer.append ("\"");
buffer.append (cookie.getValue ());
if (0 != version)
buffer.append ("\"");
if (0 != version)
{
if ((null != cookie.getPath ())
&& (0 != cookie.getPath ().length ()))
{
buffer.append ("; $Path=\"");
buffer.append (cookie.getPath ());
buffer.append ("\"");
}
if ((null != cookie.getDomain ())
&& (0 != cookie.getDomain ().length ()))
{
buffer.append ("; $Domain=\"");
buffer.append (cookie.getDomain ());
buffer.append ("\"");
}
}
}
if (0 != buffer.length ())
ret = buffer.toString ();
return (ret);
}
/**
* Check for cookie and parse into cookie jar.
* @param connection The connection to extract cookie information from.
*/
public void parseCookies (URLConnection connection)
{
String string;
Vector cookies;
StringTokenizer tokenizer;
String token;
int index;
String name;
String key;
String value;
Cookie cookie;
string = connection.getHeaderField ("Set-Cookie");
if (null != string)
{
// set-cookie = "Set-Cookie:" cookies
// cookies = 1#cookie
// cookie = NAME "=" VALUE *(";" cookie-av)
// NAME = attr
// VALUE = value
// cookie-av = "Comment" "=" value
// | "Domain" "=" value
// | "Max-Age" "=" value
// | "Path" "=" value
// | "Secure"
// | "Version" "=" 1*DIGIT
cookies = new Vector ();
tokenizer = new StringTokenizer (string, ";,", true);
cookie = null;
while (tokenizer.hasMoreTokens ())
{
token = tokenizer.nextToken ().trim ();
if (token.equals (";"))
continue;
else if (token.equals (","))
{
cookie = null;
continue;
}
index = token.indexOf ('=');
if (-1 == index)
{
name = token;
value = null;
if (null == cookie)
throw new IllegalStateException ("no cookie value");
key = name.toLowerCase ();
}
else
{
name = token.substring (0, index);
value = token.substring (index + 1);
key = name.toLowerCase ();
}
if (null == cookie)
{
cookie = new Cookie (name, value);
cookies.addElement (cookie);
}
else
{
if (key.equals ("expires")) // Wdy, DD-Mon-YY HH:MM:SS GMT
{
String comma = tokenizer.nextToken ();
String rest = tokenizer.nextToken ();
try
{
Date date = mFormat.parse (value + comma + rest);
cookie.setExpiryDate (date);
}
catch (ParseException pe)
{
// ok now what
cookie.setExpiryDate (null);
}
}
else
if (key.equals ("domain"))
cookie.setDomain (value);
else
if (key.equals ("path"))
cookie.setPath (value);
else
if (key.equals ("secure"))
cookie.setSecure (true);
else
if (key.equals ("comment"))
cookie.setComment (value);
else
if (key.equals ("version"))
cookie.setVersion (
Integer.parseInt (value));
else
if (key.equals ("max-age"))
{
Date date = new Date ();
long then = date.getTime ()
+ Integer.parseInt (value)
* 1000;
date.setTime (then);
cookie.setExpiryDate (date);
}
else
{ // error,? unknown attribute,
// maybe just another cookie
// not separated by a comma
cookie = new Cookie (name,
value);
cookies.addElement (cookie);
}
}
}
if (0 != cookies.size ())
saveCookies (cookies, connection);
}
}
/**
* Save the cookies received in the response header.
* @param list The list of cookies extracted from the response header.
* @param connection The connection (used when a cookie has no domain).
*/
protected void saveCookies (Vector list, URLConnection connection)
{
Cookie cookie;
String domain;
for (int i = 0; i < list.size (); i++)
{
cookie = (Cookie)list.elementAt (i);
domain = cookie.getDomain ();
if (null == domain)
domain = connection.getURL ().getHost ();
setCookie (cookie, domain);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -