Added support for ETag and If-Modified-Since conditional GET

This commit is contained in:
hns 2002-10-27 18:44:20 +00:00
parent 138eb38903
commit 2922208ccc

View file

@ -15,6 +15,10 @@ import FESI.Data.*;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
import java.text.*; import java.text.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.HttpURLConnection;
import java.text.SimpleDateFormat;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
/** /**
@ -571,18 +575,42 @@ public final class HopExtension {
if (arguments.length < 1) if (arguments.length < 1)
return ESNull.theNull; return ESNull.theNull;
try { try {
ByteArrayOutputStream body = new ByteArrayOutputStream (); URL url = new URL (arguments[0].toString ());
java.net.URL url = new java.net.URL (arguments[0].toString ()); URLConnection con = url.openConnection ();
// do we have if-modified-since or etag headers to set?
if (arguments.length > 1) {
if (arguments[1] instanceof DatePrototype) {
Date date = (Date) arguments[1].toJavaObject();
con.setIfModifiedSince(date.getTime());
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.UK);
format.setTimeZone(TimeZone.getTimeZone("GMT"));
con.setRequestProperty("If-Modified-Since", format.format(date));
} else if (arguments[1] != null) {
con.setRequestProperty ("If-None-Match", arguments[1].toString());
}
}
con.setAllowUserInteraction(false);
String filename = url.getFile (); String filename = url.getFile ();
java.net.URLConnection con = url.openConnection ();
String contentType = con.getContentType (); String contentType = con.getContentType ();
InputStream in = new BufferedInputStream (con.getInputStream ()); long lastmod = con.getLastModified ();
byte[] b = new byte[1024]; String etag = con.getHeaderField ("ETag");
int read; int length = con.getContentLength();
while ((read = in.read (b)) > -1) int resCode = 0;
body.write (b, 0, read); if (con instanceof HttpURLConnection)
resCode = ((HttpURLConnection) con).getResponseCode();
ByteArrayOutputStream body = new ByteArrayOutputStream ();
InputStream in = con.getInputStream ();
if (length != 0 && resCode != 304) {
byte[] b = new byte[1024];
int read;
while ((read = in.read (b)) > -1)
body.write (b, 0, read);
}
in.close (); in.close ();
MimePart mime = new MimePart (filename, body.toByteArray(), contentType); MimePart mime = new MimePart (filename, body.toByteArray(), contentType);
if (lastmod > 0)
mime.lastModified = new Date(lastmod);
mime.eTag = etag;
return ESLoader.normalizeObject (mime, evaluator); return ESLoader.normalizeObject (mime, evaluator);
} catch (Exception ignore) {} } catch (Exception ignore) {}
return ESNull.theNull; return ESNull.theNull;