Someone “threw” the following java code to me a few days ago, thought, it might be a good way for me to get started with some light Java integration with Lucee (Railo probably would struggle for that… fyi, I’m not a Java programmer):
private struct function httpRequest(url) {
var httpClient = createObject("java","org.apache.http.impl.client.DefaultHttpClient");
var httpGet = createObject("java","org.apache.http.client.methods.HttpGet");
var jURL = createObject("java", "java.net.URL").init(arguments.url);
var host = jURL.getHost();
var path = jURL.getPath();
var httpHostTarget = createObject("java","org.apache.http.HttpHost").init(host,80,"https");
var localContext = createObject("java","org.apache.http.protocol.BasicHttpContext");
var httpContent = {};
httpContent['fileContent'] = '';
httpContent['statusCode'] = 200;
var EntityUtils = createObject("java","org.apache.http.util.EntityUtils");
httpGet.init(toString(arguments.url));
var response = httpClient.execute(httpHostTarget, httpget, localContext);
httpContent['fileContent'] = createObject("java","org.apache.http.util.EntityUtils").toString(response.getEntity());
httpContent['statusCode'] = response.getStatusLine().getStatusCode();
httpClient.getConnectionManager().shutdown();
return httpContent;
}
/*
var foo = httpRequest('http/to/url').fileContent;
*/
var result = httpRequest(url).fileContent;
The above java code does not have any Class, can we compile it into a Java class without class reference? Can Lucee call / reference a .java file directly?
I’ve compiled a tiny hello world Java code into a Java class file, and Lucee is able to find it and execute it. But this is a different case, any guidance would be much appreciated.