URLConnection.java [plain text]
package java.net;
import java.io.*;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Hashtable;
import java.util.StringTokenizer;
import gnu.gcj.io.MimeTypes;
public abstract class URLConnection
{
protected URL url;
protected boolean doInput = true;
protected boolean doOutput = false;
protected boolean allowUserInteraction;
protected boolean useCaches;
protected long ifModifiedSince = 0L;
protected boolean connected = false;
private static boolean defaultAllowUserInteraction = false;
private static boolean defaultUseCaches = true;
private static FileNameMap fileNameMap; private static ContentHandlerFactory factory;
private static ContentHandler contentHandler;
private static Hashtable handlers = new Hashtable();
private static Locale locale;
private static SimpleDateFormat dateFormat1, dateFormat2, dateFormat3;
private static boolean dateformats_initialized = false;
protected URLConnection(URL url)
{
this.url = url;
allowUserInteraction = defaultAllowUserInteraction;
useCaches = defaultUseCaches;
}
public abstract void connect() throws IOException;
public URL getURL()
{
return url;
}
public int getContentLength()
{
return getHeaderFieldInt("content-length", -1);
}
public String getContentType()
{
return getHeaderField("content-type");
}
public String getContentEncoding()
{
return getHeaderField("content-encoding");
}
public long getExpiration()
{
return getHeaderFieldDate("expiration", 0L);
}
public long getDate()
{
return getHeaderFieldDate("date", 0L);
}
public long getLastModified()
{
return getHeaderFieldDate("last-modified", 0L);
}
public String getHeaderField(int n)
{
return null;
}
public String getHeaderField(String name)
{
return null;
}
public int getHeaderFieldInt(String name, int val)
{
String str = getHeaderField(name);
try
{
if (str != null)
val = Integer.parseInt(str);
}
catch (NumberFormatException e)
{
; }
return val;
}
public long getHeaderFieldDate(String name, long val)
{
if (! dateformats_initialized)
initializeDateFormats();
String str = getHeaderField(name);
if (str != null)
{
Date date;
if ((date = dateFormat1.parse(str, new ParsePosition(0))) != null)
val = date.getTime();
else if ((date = dateFormat2.parse(str, new ParsePosition(0))) != null)
val = date.getTime();
else if ((date = dateFormat3.parse(str, new ParsePosition(0))) != null)
val = date.getTime();
}
return val;
}
public String getHeaderFieldKey(int n)
{
return null;
}
public Object getContent() throws IOException
{
String cType = getContentType();
contentHandler = setContentHandler(cType);
if (contentHandler == null)
return getInputStream();
return contentHandler.getContent(this);
}
public InputStream getInputStream() throws IOException
{
throw new UnknownServiceException("Protocol " + url.getProtocol() +
" does not support input.");
}
public OutputStream getOutputStream() throws IOException
{
throw new UnknownServiceException("Protocol " + url.getProtocol() +
" does not support output.");
}
public String toString()
{
return this.getClass().getName() + ":" + url.toString();
}
public void setDoInput(boolean doinput)
{
if (connected)
throw new IllegalAccessError("Already connected");
doInput = doinput;
}
public boolean getDoInput()
{
return doInput;
}
public void setDoOutput(boolean dooutput)
{
if (connected)
throw new IllegalAccessError("Already connected");
doOutput = dooutput;
}
public boolean getDoOutput()
{
return doOutput;
}
public void setAllowUserInteraction(boolean allowuserinteraction)
{
if (connected)
throw new IllegalAccessError("Already connected");
allowUserInteraction = allowuserinteraction;
}
public boolean getAllowUserInteraction()
{
return allowUserInteraction;
}
public static void
setDefaultAllowUserInteraction(boolean defaultallowuserinteraction)
{
defaultAllowUserInteraction = defaultallowuserinteraction;
}
public static boolean getDefaultAllowUserInteraction()
{
return defaultAllowUserInteraction;
}
public void setUseCaches(boolean usecaches)
{
if (connected)
throw new IllegalAccessError("Already connected");
useCaches = usecaches;
}
public boolean getUseCaches()
{
return useCaches;
}
public void setIfModifiedSince(long ifmodifiedsince)
{
if (connected)
throw new IllegalAccessError("Already connected");
ifModifiedSince = ifmodifiedsince;
}
public long getIfModifiedSince()
{
return ifModifiedSince;
}
public boolean getDefaultUseCaches()
{
return defaultUseCaches;
}
public void setDefaultUseCaches(boolean defaultusecaches)
{
defaultUseCaches = defaultusecaches;
}
public void setRequestProperty(String key, String value)
{
}
public String getRequestProperty(String key)
{
return null;
}
public static void setDefaultRequestProperty(String key, String value)
{
}
public static String getDefaultRequestProperty(String key)
{
return null;
}
public static void setContentHandlerFactory(ContentHandlerFactory fac)
{
if (factory != null)
throw new Error("ContentHandlerFactory already set");
SecurityManager s = System.getSecurityManager();
if (s != null)
s.checkSetFactory();
factory = fac;
}
protected static String guessContentTypeFromName(String fname)
{
int dot = fname.lastIndexOf (".");
if (dot != -1)
{
if (dot == fname.length())
return ("application/octet-stream");
else
fname = fname.substring (dot + 1);
}
String type = MimeTypes.getMimeTypeFromExtension (fname);
if (type == null)
return("application/octet-stream");
return(type);
}
public static FileNameMap getFileNameMap()
{
return fileNameMap;
}
public static void setFileNameMap(FileNameMap map)
{
SecurityManager s = System.getSecurityManager();
if (s != null)
s.checkSetFactory();
fileNameMap = map;
}
private ContentHandler setContentHandler(String contentType)
{
ContentHandler handler;
if (contentType == null || contentType == "")
return null;
if ((handler = (ContentHandler) handlers.get(contentType)) != null)
if (handler instanceof ContentHandler)
return handler;
else
return null;
if (factory != null)
handler = factory.createContentHandler(contentType);
if (handler == null)
{
String propVal = System.getProperty("java.content.handler.pkgs");
propVal = (propVal == null) ? "" : (propVal + "|");
propVal = propVal + "gnu.gcj.content|sun.net.www.content";
StringTokenizer pkgPrefix = new StringTokenizer(propVal, "|");
char[] cArray = contentType.toCharArray();
for (int i = 0; i < cArray.length; i++)
{
if (cArray[i] == '/')
cArray[i] = '.';
else if (! ((cArray[i] >= 'A' && cArray[i] <= 'Z') ||
(cArray[i] >= 'a' && cArray[i] <= 'z') ||
(cArray[i] >= '0' && cArray[i] <= '9')))
cArray[i] = '_';
}
String contentClass = new String(cArray);
do
{
String facName = pkgPrefix.nextToken() + "." + contentClass;
try
{
handler =
(ContentHandler) Class.forName(facName).newInstance();
}
catch (Exception e)
{
}
} while ((handler == null ||
! (handler instanceof ContentHandler)) &&
pkgPrefix.hasMoreTokens());
}
if (handler != null && handler instanceof ContentHandler)
{
handlers.put(contentType, handler);
return handler;
}
handlers.put(contentType, contentType);
return null;
}
private synchronized void initializeDateFormats()
{
if (dateformats_initialized)
return;
locale = new Locale("En", "Us", "Unix");
dateFormat1 = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss 'GMT'",
locale);
dateFormat2 = new SimpleDateFormat("EEEE, dd-MMM-yy hh:mm:ss 'GMT'",
locale);
dateFormat3 = new SimpleDateFormat("EEE MMM d hh:mm:ss yyyy", locale);
dateformats_initialized = true;
}
}