Implemented global getURL() function
This commit is contained in:
parent
02f8ad1e3e
commit
4d419c3a46
1 changed files with 89 additions and 1 deletions
|
@ -21,9 +21,18 @@ import helma.framework.core.*;
|
||||||
import helma.objectmodel.*;
|
import helma.objectmodel.*;
|
||||||
import helma.objectmodel.db.*;
|
import helma.objectmodel.db.*;
|
||||||
import helma.util.HtmlEncoder;
|
import helma.util.HtmlEncoder;
|
||||||
|
import helma.util.MimePart;
|
||||||
import org.mozilla.javascript.*;
|
import org.mozilla.javascript.*;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
import java.text.*;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -49,7 +58,7 @@ public class GlobalObject extends ScriptableObject {
|
||||||
"renderSkin", "renderSkinAsString", "getProperty",
|
"renderSkin", "renderSkinAsString", "getProperty",
|
||||||
"authenticate", "createSkin", "format", "encode",
|
"authenticate", "createSkin", "format", "encode",
|
||||||
"encodeXml", "encodeForm", "stripTags",
|
"encodeXml", "encodeForm", "stripTags",
|
||||||
"getDBConnection"
|
"getDBConnection", "getURL"
|
||||||
};
|
};
|
||||||
|
|
||||||
defineFunctionProperties(globalFuncs, GlobalObject.class, 0);
|
defineFunctionProperties(globalFuncs, GlobalObject.class, 0);
|
||||||
|
@ -207,6 +216,85 @@ public class GlobalObject extends ScriptableObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Object getURL(String location, Object opt) {
|
||||||
|
if (location == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
URL url = new URL(location);
|
||||||
|
URLConnection con = url.openConnection();
|
||||||
|
|
||||||
|
// do we have if-modified-since or etag headers to set?
|
||||||
|
if (opt != null && opt != Undefined.instance) {
|
||||||
|
if (opt instanceof Scriptable) {
|
||||||
|
Scriptable scr = (Scriptable) opt;
|
||||||
|
if ("Date".equals(scr.getClassName())) {
|
||||||
|
Date date = new Date((long) ScriptRuntime.toNumber(scr));
|
||||||
|
|
||||||
|
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 {
|
||||||
|
con.setRequestProperty("If-None-Match", scr.toString());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
con.setRequestProperty("If-None-Match", opt.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String httpUserAgent = app.getProperty("httpUserAgent");
|
||||||
|
|
||||||
|
if (httpUserAgent != null) {
|
||||||
|
con.setRequestProperty("User-Agent", httpUserAgent);
|
||||||
|
}
|
||||||
|
|
||||||
|
con.setAllowUserInteraction(false);
|
||||||
|
|
||||||
|
String filename = url.getFile();
|
||||||
|
String contentType = con.getContentType();
|
||||||
|
long lastmod = con.getLastModified();
|
||||||
|
String etag = con.getHeaderField("ETag");
|
||||||
|
int length = con.getContentLength();
|
||||||
|
int resCode = 0;
|
||||||
|
|
||||||
|
if (con instanceof HttpURLConnection) {
|
||||||
|
resCode = ((HttpURLConnection) con).getResponseCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
ByteArrayOutputStream body = new ByteArrayOutputStream();
|
||||||
|
|
||||||
|
if ((length != 0) && (resCode != 304)) {
|
||||||
|
InputStream in = new BufferedInputStream(con.getInputStream());
|
||||||
|
byte[] b = new byte[1024];
|
||||||
|
int read;
|
||||||
|
|
||||||
|
while ((read = in.read(b)) > -1)
|
||||||
|
body.write(b, 0, read);
|
||||||
|
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
MimePart mime = new MimePart(filename, body.toByteArray(), contentType);
|
||||||
|
|
||||||
|
if (lastmod > 0) {
|
||||||
|
mime.lastModified = new Date(lastmod);
|
||||||
|
}
|
||||||
|
|
||||||
|
mime.eTag = etag;
|
||||||
|
|
||||||
|
return mime;
|
||||||
|
} catch (Exception ignore) {
|
||||||
|
System.err.println ("EXCEPT: "+ignore);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue