/* this belongs to the WebRaveler package */
package webraveler.vurl;
import java.io.*;
import webraveler.http.HTTPRequest;
import webraveler.http.HTTPResponse;
import webraveler.proxy.ProxyClient;
import webraveler.ravel.PluginCallbackException;
import webraveler.ravel.Raveler;
import webraveler.ravel.RavelerException;
import webraveler.ravel.RavelerUser;
/**
* This VURL allows documents to be fetched from the Web without having
* the banner added.
*
* @author The Web Raveler Development Squad
* @version 1.0
*/
public class GetUnframedDocumentVURL implements VURL {
private boolean DEBUG = false;
/**
*
*/
public GetUnframedDocumentVURL() {
} // GetUnframedDocumentVURL()
/**
* Use ProxyClient to get a fresh copy of the resource identified by the query
* string. Return null if ProxyClient reports an error getting the requested
* resource.
*
* @param username the String containing the current RavelerUser's name
* @param request the HTTPRequest we're messing with here
* @param query the String containing the query added to the VURL (should be null)
* @param ravel the current Raveler instance
*/
public HTTPResponse resolveVURL(String username, String query, HTTPRequest request, Raveler ravel) {
if ( DEBUG ) System.out.println("GetUnframedDocumentVURL: query string = \""+query+"\"");
if ( DEBUG ) System.out.println("GUDVURL: username = "+username);
HTTPResponse response = null;
RavelerUser currentUser = new RavelerUser();
ProxyClient proxy;
/* get the user object from the running instance of Raveler */
try {
currentUser = ravel.getUser(username);
} catch (RavelerException re) {
if ( DEBUG ) System.out.println("GetUnframedDocumentVURL: "+re.getMessage());
//ravel.printError("GetUnframedDocumentVURL",new RavelerError(re.getMessage()),request.uriString());
response = ravel.getOriginResponse(request);
//ravel.handleErrors(response,request.uriString());
return addTitleFixer(response);
} //catch
/* Set the url of the request to the query - the url to retrieve */
request.setUriString(query);
/* Change the remote host and port to that of the query url */
request.setRemoteHost(new String());
request.setRemotePort(0);
try{ request.parseURI(query); }catch(IOException ioe) {request.setUriString("");}
/* Change the host header to that of the query url */
request.deleteHeader("Host");
request.setHeader("Host",request.remoteHost());
request = fixStupidRedirects(request);
/* send the request through the plugins */
try {
request = ravel.passThroughPlugins(currentUser, request);
} catch( PluginCallbackException pce ) {
if ( DEBUG ) System.out.println("GUDVURL: Plugin generated callback response.");
return pce.getResponse();
}//catch
response = ravel.getOriginResponse(request);
/* check for a redirect, and return the new document if applicable */
if ( response.isRedirect() ) {
if ( DEBUG ) System.out.println("GetUnframedDocumentVURL: HANDLING REDIRECT.");
//response = handleRedirect(response,ravel);
/* make the "location" header a virtual URL */
String redirectLocation = (String) response.getHeader("Location");
if ( redirectLocation != null ) {
if ( DEBUG ) System.out.println("redirect location = "+redirectLocation);
/* [partial] fix for STUPID relative locations */
if ( !redirectLocation.startsWith("http://") )
if ( redirectLocation.startsWith("/") )
redirectLocation = "http://" + request.remoteHost() + redirectLocation;
else
redirectLocation = request.uriString().substring(0,request.uriString().lastIndexOf("/")+1) + redirectLocation;
redirectLocation = "http://temp.webraveler2.getunframeddocument/getunframeddocument!"+redirectLocation;
response.setHeader("Location",redirectLocation);
}//if
return addTitleFixer(response); /* this is the original redirect, with the location header modified */
}//if
/* try making an HTMLPage object to represent the content, if applicable.
* also, we have to make URLs absolute, using VURLs. */
String contentType;
contentType = (String) response.getHeader("content-type");
if ( DEBUG ) System.out.println("GetUnframedDocumentVURL: "+query+" content-type = '"+contentType+"'");
if ( contentType != null && contentType.startsWith("text/html")) {
if ( DEBUG ) System.out.println("GUDVURL: making urls absolute.");
response.getHtmlPage().makeURLsAbsolute(request,response);
response.setHeader("Pragma","nocache");
response.setHeader("Expires","-1");
if ( DEBUG ) System.out.println("GUDVURL: done making urls absolute.");
}//if
/* pass the response through the plugins */
response = ravel.passThroughPlugins(currentUser,response,request.uriString());
if (response == null) {
response = Raveler.generateErrorResponse();
if ( DEBUG ) System.out.println("GetUnframedDocumentVURL: getResponse() returned null. Generating error response.");
}//if
else {
/* Insert a link to the errors/warnings */
response = ravel.handleErrors(response,request.uriString());
}
//System.out.println("GetUnframedDocumentVURL: '"+response.print()+"'");
//if ( response.getHtmlPage() != null) {
// response.entityText = response.htmlPage.toString();
// response.entityBytes = response.entityText.getBytes();
// response.setHeader("Content-Length",""+response.entityBytes.length);
// }
// response.getHtmlPage().insertInHead("\n");
response.correctContentLength();
return addTitleFixer(response);
} // resolveVURL
private HTTPResponse addTitleFixer(HTTPResponse resp) {
if ( resp.getHeader("Content-Type").startsWith("text/html") ) {
// System.out.println("\n\nFIXING TITLE\n\n");
//resp.htmlPage.insertAtTop("");
//resp.htmlPage.insertAtTop("");
//resp.entityText = resp.htmlPage.toString();
//resp.entityBytes = resp.entityText.getBytes();
//resp.setHeader("Content-Length",resp.entityBytes.length+"");
}
return resp;
}
/**
* Given an HTTPResponse with a redirect (status 302), forward the original request
* to the new location (as per the location header), and return the response received.
*
* @param response an HTTPResponse containing a redirect
* @param ravel the Raveler instance we're currently dealing with
* @return the resource the redirect refers us to
*/
public HTTPResponse handleRedirect(HTTPResponse response,Raveler ravel) {
String location = (String) response.getHeader("Location");
HTTPResponse redirectResponse = new HTTPResponse();
if ( location == null ) {
return Raveler.generateErrorResponse();
}//if
else {
HTTPRequest request = HTTPRequest.generateGetRequest(location);
ProxyClient proxy = new ProxyClient(request);
try {
redirectResponse = proxy.getResponse();
}catch(IOException ioe) {
return Raveler.generateErrorResponse();
} //catch
/* if there's another redirect, handle it, too */
if ( redirectResponse.isRedirect() ) {
redirectResponse = handleRedirect(redirectResponse, ravel);
}//if
}//else
return redirectResponse;
}//handleRedirect
private HTTPRequest fixStupidRedirects(HTTPRequest req){
if ( req.uriString().indexOf("http//",1) != -1 ) {
StringBuffer newUri = new StringBuffer();
newUri.append(req.uriString().substring(req.uriString().indexOf("http//")));
newUri.insert(4,":");
req.setUriString(newUri.toString());
try { req.parseURI(newUri.toString()); } catch(IOException ioe) {}
}
return req;
}
} // class GetUnframedDocumentVURL