terça-feira, abril 15, 2008

flush String with source from external URL in to Java

Well a fetch source using servlets is nonsence. Servlets run on server side and generate responses to requests so... it's a bad idea to go that way.

If I somehow exported the connection problem outside of the browser window... I'd have an authentication problem. So the best way would be to run it somehow using javascript, there's a Dom object capable of doing HTTPRequests and it's called XMLHTTPRequest. There are several details to take care when using this code. One must set browser to allow connection to remote servers from client pages. So there's a cost in security to achieve this goal. Furthermore I couldn't find a way to configure firefox to lower it's security params so it won't run on it yet.

By now I'm still hocked up to the idea of an Applet to manage flow control, and other heavy weight operations, Though my initial idea of processing the source as a raw string has fallen in the pit of plain stupidity. XMLHTTPRequest returns a DOMDocument which can be manipulated with all the charms of the DOM model, and will certainly be better than any code I could write in 3 life times.

So in conclusion I guess I can sleep better tonight :P

If you're trying to do the same for some reason, the way you'd get a raw String with the source to be inputed in to Java would be to declare an Applet on the page containing the script with the XMLHTTPRequest Object so you can call functions who manipulate it from java using the netscape.javascript package. Furthermore XMLHTTPRequest contains a DOMString propertie which can be cast to String no questions asked, leaving us with a happy:

a) (String)window.call(“getSource”,null).

//- ------------------------------- JS
var client = new XMLHttpRequest();
var source;

function init()
{
if (window.xmlhttprequest) { request = new XMLHTTPrequest();}
else if (window.ActiveXObject) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){}
}
}
}

function handler( ) {
try{
if(this.readyState == 4 && this.status == 200) {
// so far so good
if(this.responseXML != null)
{ } // success! do nothing}
else{ }
}
else if (this.readyState == 4 && this.status != 200) { // fetched the wrong page or network error...
alert("ups check your glasses, wrong URL?");
}
}catch(e){alert("error on handler" +e );}
}

function fetchSource()
{
//wait for client to be ready =P
if(client.readyState!=4) return null;
return client.responseText;
}

function fetchURL(url){
//alert("fetchy is here!! cuxi cuxi ");
try{
client.onreadystatechange = handler;
client.open("GET", url,true);
client.send();
}
catch(e){alert("error on fetchSource:" + e);}

}

// -------------------------------- APPLET CODE
public static String getURLSource(String url)
{

if(window == null){System.out.println("window is null"); return null;}

try {
window.call("fetchURL",new Object[] {url});
Thread.sleep(2000);//give the server time to respond with source
return (String)window.call("fetchSource", null);
}
catch(Exception e){
System.out.println("GET URL CRACHED!!!");
return null;
}
}

//don't forget to start window :
public void init()
{
try {
Travian.window = netscape.javascript.JSObject.getWindow(this);
Travian.location = (JSObject)Travian.window.getMember("location");
}catch(Exception e ){System.out.println("init can't start JSObject");}
}


// -----------------------------------------------------------------------
happy coddings!

Sem comentários :