/* this belongs to the WebRaveler package */
package webraveler.ravel;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import webraveler.html.HTMLPage2;
import webraveler.http.HTTPRequest;
import webraveler.http.HTTPResponse;
import webraveler.proxy.ProxyClient;
import webraveler.vurl.UnravelVURL;
import webraveler.vurl.VURL;
/**
* Implementation of Web Raveler
*
* @author The Web Raveler Development Squad
* @version 1.0
*/
public class Raveler {
/* variables */
public static String WEBRAVELER_HOME = "/home/schmitzc/Summer_2004/WR_v3/WebRaveler2/";
private UserStack userStack;
private PluginStack pluginStack;
private VURLStack vurlStack;
public static final String ravelerCSS = "";
String bannerHTML;
private String bannerCSS;
String bannerScript;
Hashtable bannerImages;
boolean error;
private int requestCnt;
//private Vector loggedIn;
//private static int port;
//private int realmCount;
/* constructors */
public Raveler() {
/* Initialize the plugin vector*/
System.out.println("Raveler: Loading the plugins.");
pluginStack = new PluginStack(this);
/* Initialize the virtual URL Hashtable */
System.out.println("Raveler: Loading Virtual URLs");
vurlStack = new VURLStack(this);
/* Initialize the plugins */
System.out.println("Raveler: Initializing the plugins");
pluginStack.initializePlugins();
/* Initialize the users vector */
userStack = new UserStack(this);
error = false;
requestCnt = 0;
} // Raveler
/* methods */
/**
* Gets an HTTPResponse directly from the server
*
* @param request an HTTPRequest
* @param response an HTTPResponse containing the unraveled response from the server
*/
public HTTPResponse getOriginResponse(HTTPRequest request) {
HTTPResponse response;
ProxyClient proxy = new ProxyClient(request);
/* try to get the response from a ProxyClient */
try {
response = proxy.getResponse();
}catch(IOException ioe) {
System.out.println("GetUnframedDocumentVURL: error getting response from ProxyClient.");
response = Raveler.generateErrorResponse();
} //catch
return response;
}
/**
* Process the given HTTP Request, and return the
* HTTP Response that results
*
* @return the resulting HTTPResponse
*/
public HTTPResponse handleRequest(RavelerUser user,HTTPRequest request) {
if ( getVurlStack().isVirtualURL(request.uriString() ) ) {
return processVirtualURL(user,request);
} else {
return framedResponse(user,request);
}
}//HTTPResponse handleRequest(HTTPRequest)
public HTTPResponse framedResponse( RavelerUser currentUser, HTTPRequest request ) {
HTTPResponse response = new HTTPResponse();
System.out.println("RavelerManager: Generating Raveler Banner");
String frameHTML = new String("");
String bannerHTML = new String("
WR 1.0 Banner");
frameHTML += "Replace Me\n\n";
frameHTML += "Uh, oh! We require that you use frames. You suck. Get a better browser.";
/* construct the response containing the frameset to be returned */
Hashtable frameHash = new Hashtable();
frameHash.put("content-type","text/html");
response = new HTTPResponse("HTTP/1.0",
200,
"OK",
new HTMLPage2(frameHTML),
frameHash);
response.correctContentLength();
/* send the framed html back to the client and exit */
System.out.println("RavelerManager: Writing response back to ProxyServer.");
return response;
}
public HTTPResponse getBannerResponse(RavelerUser currentUser, HTTPRequest request) {
/* insert the raveler banner into the vurl for the top frame */
String bannerHTML = new String("WR 1.0 Banner");
bannerHTML = insertRavelerBanner(currentUser,bannerHTML,request);
Hashtable bannerHash = new Hashtable();
bannerHash.put("content-type","text/html");
bannerHash.put("content-length",""+bannerHTML.getBytes().length);
HTTPResponse bannerResponse = new HTTPResponse("HTTP/1.0",
200,
"OK",
new HTMLPage2(bannerHTML),
bannerHash);
bannerResponse.correctContentLength();
return bannerResponse;
}
/**
* Handle processing of virtual URLs.
*/
private HTTPResponse processVirtualURL(RavelerUser user, HTTPRequest request) {
//System.out.println("ProxyServer: Request for virtual url. Bypassing RavelerManager.");
boolean DEBUG = false;
HTTPResponse response;
String[] vurlStrings = processVurlString(request.uriString());
/* resolve the vurl, sending the result back to the client */
response = getVURL(user.username,vurlStrings[0],request,vurlStrings[1]);
if ( response == null ) {
response = Raveler.generateErrorResponse();
}//if
if ( request.uriString().equals("http://perm.webraveler2.banner.bannerhtml"))
return getBannerResponse(user,request);
return response;
}//handleVirtualURL
/**
* Return the vurl base string, followed by the
* query string, as a String[].
*
* @param url
* @return
*/
private String[] processVurlString(String url){
boolean DEBUG = false;
String queryString=null;
String vurlBaseString="";
/* get the query string, if any */
try{
queryString = url.substring(url.indexOf("!")); // if there's no "!" the exception gets thrown
queryString = queryString.substring(1); // take off the "!"
if (queryString.startsWith("?")) queryString = queryString.substring(1); /* for actual form queries that added "?" */
}catch (StringIndexOutOfBoundsException s) {queryString = null;}
if ( DEBUG ) System.out.println("QUERY STRING = "+queryString);
/* separate the base URL from the query string */
try {
vurlBaseString = url.substring(0,url.indexOf("!"));
} catch (StringIndexOutOfBoundsException s) {vurlBaseString = url;} // vurl base string should be vurlString if there's no "!"
if ( DEBUG ) {
System.out.println("VURL BASE STRING = "+vurlBaseString);
System.out.println("VURL STRING = "+url);
}
return new String[] {vurlBaseString,queryString};
}
/**
* Return a generic 400 error response for when ProxyClient fails
* to get the document in question.
*
* @return an HTTPResponse containing a generic error response for the client.
*/
public static HTTPResponse generateErrorResponse(){
HTTPResponse response = new HTTPResponse();
// construct an HTML error page to send back
String errorPage;
String reasonPhrase = "Error Connecting to Remote Host";
String httpVersion = "HTTP/1.0";
int statusCode = 400;
Hashtable headers = new Hashtable();
headers.put("content-type", "text/html");
headers.put("connection", "close");
//generate some HTML
errorPage = "" + Raveler.ravelerCSS;
errorPage += "Error Getting Server Response";
errorPage += "
Web Raveler";
errorPage += " encountered an error processing your request.
";
errorPage += "
Please check the URL and try again.";
errorPage += "
";
errorPage += "(Web Raveler v1.0. The Web Raveler Development Squad, 2004.)";
errorPage += "
";
//put the response together
response = new HTTPResponse( httpVersion,
statusCode,
reasonPhrase,
new HTMLPage2(errorPage),
headers );
//make the content-length header accurate
response.correctContentLength();
return response;
}//generateErrorResponse
/**
*
*/
public HTTPResponse handleErrors(HTTPResponse response, String url) {
if (error) {
System.out.println("Raveler: Handling errors...");
response.getHtmlPage().insertAtTop("Error(s)/Warning(s) were reported while raveling this page.
");
response.correctContentLength();
error=false;
//old stuff
// HTMLPage2 htmlpage = new HTMLPage2(pageHTML);
// htmlpage.insertAtTop();
// response.entityText = htmlpage.toString();
//
//
// /* Change the entityBytes of the passed HTTPResponse to the HTML page w/inserted text
// * at the top */
// response.entityBytes = response.entityText.getBytes();
// response.setHeader("content-length", new String(""+response.entityBytes.length));
} //error
return response;
} //handleErrors(String)
/**
* Return true if the proposed username and password match
* those we read from the users.txt file.
*
*/
public boolean checkCredentials(String username, String password) {
boolean DEBUG = false;
String realPass = userStack.getUserPassword(username);
if ( realPass != null ) {
if ( DEBUG ) {
System.out.println("username = "+username);
System.out.println("password = "+password);
System.out.println("realpass = "+realPass);
}
return realPass.equals(password);
}//if
return false;
}//boolean checkCredentials(String,String)
public HTTPRequest passThroughPlugins(RavelerUser user, HTTPRequest request) throws PluginCallbackException {
return pluginStack.passThroughPlugins(user,request);
}
public HTTPResponse passThroughPlugins(RavelerUser user, HTTPResponse response,String url) {
return pluginStack.passThroughPlugins(user,response,url);
}
public HTTPResponse getVURL(String username,String vurlString, HTTPRequest request, String queryString) {
return getVurlStack().getVURLResponse(username,vurlString,request,queryString);
}
/**
* Inserts the Raveler Banner at the top of the HTML page
*
* @param currentUser the RavelerUser requesting the page
* @param pageHTML a string containing the HTML page
* @param pageURL a string containing the location of the page
* @param request an HTTPRequest containing the unmodified request
* @return a string containing the modified HTML
*/
public String insertRavelerBanner(RavelerUser currentUser, String pageHTML, HTTPRequest request) {
/* Set a vurl to get the unraveled page */
UnravelVURL unravel = new UnravelVURL(request);
String unravelVURL = getVurlStack().addTemporaryVirtualURL(".unravel/"+request.uriString()+requestCnt,unravel);
requestCnt++;
System.out.println("Raveler: insertRavelerBanner: unravel URL = '"+request.uriString()+"'");
String additions = new String("");
additions += "[on] | \n";
additions += "  | \n";
additions += "Unravel | \n";
additions += "  | ";
additions += "Config | \n";
additions += "  | ";
additions += "Help | \n";
additions += "  | ";
additions += "Logout | \n";
additions += "\n";
additions += "  | \n";
additions += "\n";
/* Set up the form for the plugins menu */
additions += " | \n";
HTMLPage2 htmlpage = new HTMLPage2(pageHTML);
htmlpage.insertInHead("");
htmlpage.insertInHead(bannerScript+dynScript+"");
htmlpage.insertAtTop(bannerHTML+additions);
return htmlpage.toString();
} //insertRavelerBanner
// +------------------+---------------------------------------------
// | VURL Methods |
// +------------------+
public String addTemporaryVirtuaLURL(String relativeURL, VURL proc) {
return getVurlStack().addTemporaryVirtualURL(relativeURL,proc);
}
// +------------------------+--------------------------------------
// | Raveler User Methods |
// +------------------------+
/**
* Saves a user's settings to disk
*
* @param username A string containing the username
*/
public void saveUserSettings(String username) {
try {
userStack.saveUserSettings(username);
} catch (RavelerException re) {
System.out.println(re.getMessage());
}
} //saveUserSettings(String)
/**
* Gets the RavelerUser associated with the given username
*
* @param username a string containing the username
* @return the RavelerUser object associated with the given username
*/
public RavelerUser getUser(String username) throws RavelerException {
try {
return userStack.getUser(username);
} catch (RavelerException re) {
throw re;
}
} //getUser(String)
/**
* Gets the requested banner image
*
*/
public byte[] getImage(String name) {
return (byte[]) this.bannerImages.get(name);
} //getImage(name)
/**
* Return an Enumeration of the Plugins
*
* @return
*/
public Enumeration getPlugins() {
return pluginStack.getPlugins();
}//Enumeration plugins()
/**
* @return Returns the vurlStack.
*/
public VURLStack getVurlStack() {
return vurlStack;
}
/**
* @return Returns the bannerCSS.
*/
public String getBannerCSS() {
return bannerCSS;
}
/**
* @return Sets the bannerCSS.
*/
public void setBannerCSS(String bannerCSS) {
this.bannerCSS = bannerCSS;
}
/**
* @param error The error to set.
*/
public void setError(boolean error) {
this.error = error;
}
}// class Raveler