package webraveler.plugin;
import java.util.*;
import java.io.*;
import webraveler.http.HTTPRequest;
import webraveler.http.HTTPResponse;
import webraveler.ravel.PluginAPI;
import webraveler.ravel.PluginCallbackException;
import webraveler.ravel.Raveler;
import webraveler.vurl.HelpVURL;
/**
*
*/
public class NoImagesPlugin extends webraveler.ravel.RavelerPlugin {
/* variables */
private byte[] imageBytes;
private HTTPResponse response;
private boolean DEBUG = false;
/*constructors*/
public NoImagesPlugin () throws Exception{
/* let's load the image at instantiation time */
System.out.println("NoImagesPlugin: Getting dummy image byte array.");
File image = new File(Raveler.WEBRAVELER_HOME+"/data/images/noimage.png");
FileInputStream imageIn;
try {
imageIn = new FileInputStream(image);
}catch (IOException ioe) {
throw new Exception("NoImagesPlugin: couldn't get dummy image file.");
}//catch
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
int input;
while( (input = imageIn.read()) != -1 ) {
byteStream.write(input); // write the input byte to the stream...
}
imageBytes = byteStream.toByteArray();
name = "NoImagesPlugin";
mimeTypes.add("image/gif");
mimeTypes.add("image/jpeg");
mimeTypes.add("image/png");
}
/*methods*/
public void setDefaultAttributes () {
attributes = new Hashtable();
name = "NoImagesPlugin";
}
public boolean initialize () {
/* some helpful HTML to display to the user */
String helpfulHTML = new String();
helpfulHTML += "
"+Raveler.ravelerCSS+"WebRaveler2 :: NoImagesPlugin Help Page";
helpfulHTML += "NoImagesPlugin :: Help
";
helpfulHTML += "- Activating this plugin causes requests for common image files to ";
helpfulHTML += "return a one-pixel image from the WebRaveler directory.
";
helpfulHTML += "- Responses will also be checked for content-type headers indicating image files, and those will ";
helpfulHTML += "be handled in the same manner as requests.
Thank you for using NoImagesPlugin.
";
helpfulHTML += "";
HelpVURL help = new HelpVURL(helpfulHTML);
String fullURL = PluginAPI.addHelpVirtualURL(ravel,name,help);
return true;
}//initialize()
public HTTPRequest execute (HTTPRequest request, Hashtable attributes) throws PluginCallbackException {
if ( DEBUG ) System.out.println("NoImagesPlugin: executing on request for "+request.uriString());
String uri = request.uriString().toLowerCase();
if ( !uri.endsWith(".gif") &&
!uri.endsWith(".jpg") &&
!uri.endsWith(".jpeg") &&
!uri.endsWith(".png") ) {
return request;
}//if
if (DEBUG) System.out.println("NoImagesPlugin: request for image file. Returning dummy image.");
/* construct a new response to send back */
HTTPResponse response = new HTTPResponse();
response.protocolVersion = "HTTP/1.0";
response.status = 200;
response.entityBytes = imageBytes;
response.setHeader("content-type","image/gif");
/* put in the right content-length header */
response.setHeader("content-length",""+imageBytes.length);
throw new PluginCallbackException(response);
}//execute()
public HTTPResponse execute (HTTPResponse response, Hashtable attributes) throws PluginCallbackException {
if (DEBUG) System.out.println("NoImagesPlugin: executing on response...");
response.entityBytes = imageBytes;
response.deleteHeader("content-length");
response.setHeader("content-length", new String(""+response.entityBytes.length));
response.deleteHeader("content-type");
response.setHeader("content-type", "image/gif");
response.protocolVersion = "HTTP/1.0";
response.status = 200;
response.reasonPhrase = "OK";
response.htmlPage = null;
response.deleteHeader("connection");
response.setHeader("connection", new String("close"));
throw new PluginCallbackException(response);
} //execute(HTTPResponse)
public void terminate() {
} // terminate()
}//InsertAtTop