Author: nextgens
Date: 2007-04-13 19:02:54 +0000 (Fri, 13 Apr 2007)
New Revision: 12648

Added:
   trunk/freenet/src/freenet/pluginmanager/AccessDeniedPluginHTTPException.java
   trunk/freenet/src/freenet/pluginmanager/DownloadPluginHTTPException.java
   trunk/freenet/src/freenet/pluginmanager/NotFoundPluginHTTPException.java
   trunk/freenet/src/freenet/pluginmanager/RedirectPluginHTTPException.java
Modified:
   trunk/freenet/src/freenet/clients/http/PproxyToadlet.java
   trunk/freenet/src/freenet/clients/http/Toadlet.java
   trunk/freenet/src/freenet/pluginmanager/PluginHTTPException.java
   trunk/freenet/src/freenet/pluginmanager/PluginManager.java
Log:
New handling of PluginExceptions... will tweak plugins soon

Modified: trunk/freenet/src/freenet/clients/http/PproxyToadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/PproxyToadlet.java   2007-04-13 
18:28:32 UTC (rev 12647)
+++ trunk/freenet/src/freenet/clients/http/PproxyToadlet.java   2007-04-13 
19:02:54 UTC (rev 12648)
@@ -9,9 +9,13 @@

 import freenet.client.HighLevelSimpleClient;
 import freenet.node.NodeClientCore;
+import freenet.pluginmanager.AccessDeniedPluginHTTPException;
+import freenet.pluginmanager.DownloadPluginHTTPException;
+import freenet.pluginmanager.NotFoundPluginHTTPException;
 import freenet.pluginmanager.PluginHTTPException;
 import freenet.pluginmanager.PluginInfoWrapper;
 import freenet.pluginmanager.PluginManager;
+import freenet.pluginmanager.RedirectPluginHTTPException;
 import freenet.support.HTMLNode;
 import freenet.support.Logger;
 import freenet.support.MultiValueTable;
@@ -71,16 +75,26 @@

                                writeReply(ctx, 200, "text/html", "OK", 
pm.handleHTTPPost(plugin, request));
                        }
-                       catch(PluginHTTPException ex)
+                       catch (RedirectPluginHTTPException e) {
+                               writeTemporaryRedirect(ctx, e.message, 
e.location);
+                       }
+                       catch (NotFoundPluginHTTPException e) {
+                               sendErrorPage(ctx, e.code, e.message, 
e.location);
+                       }
+                       catch (AccessDeniedPluginHTTPException e) {
+                               sendErrorPage(ctx, e.code, e.message, 
e.location);
+                       }
+                       catch (DownloadPluginHTTPException e) {
+                               // FIXME: maybe it ought to be defined like 
sendErrorPage : in toadlets
+                               
+                               MultiValueTable head = new MultiValueTable();
+                               head.put("Content-Disposition", "attachment; 
filename=\"" + e.filename + '"');
+                               ctx.sendReplyHeaders(e.code, "Found", head, 
e.mimeType, e.data.length);
+                               ctx.writeData(e.data);
+                       }
+                       catch(PluginHTTPException e)
                        {
-                               // TODO: make it into html
-                               if(ex.getHeaders() != null) {
-                                       String data = ex.getReply();
-                                       ctx.sendReplyHeaders(ex.getCode(), 
"Found", ex.getHeaders(), ex.getMimeType(), (data == null ? 0 : data.length()));
-                                       if(data != null)
-                                               
ctx.writeData(data.getBytes("UTF-8"));
-                               }else
-                                       writeReply(ctx, ex.getCode(), 
ex.getMimeType(), ex.getDesc(), ex.getReply());
+                               sendErrorPage(ctx, e.code, e.message, 
e.location);
                        }
                        catch(Throwable t)
                        {
@@ -207,16 +221,21 @@

                        //FetchResult result = fetch(key);
                        //writeReply(ctx, 200, result.getMimeType(), "OK", 
result.asBucket());
-                       
-               } catch (PluginHTTPException ex) {
-                       // TODO: make it into html
-                       if(ex.getHeaders() != null) {
-                               String data = ex.getReply();
-                               ctx.sendReplyHeaders(ex.getCode(), "Found", 
ex.getHeaders(), ex.getMimeType(), (data == null ? 0 : data.length()));
-                               if(data != null)
-                                       ctx.writeData(data.getBytes("UTF-8"));
-                       }else
-                               writeReply(ctx, ex.getCode(), ex.getMimeType(), 
ex.getDesc(), ex.getReply());
+               } catch (RedirectPluginHTTPException e) {
+                       writeTemporaryRedirect(ctx, e.message, e.location);
+               } catch (NotFoundPluginHTTPException e) {
+                       sendErrorPage(ctx, e.code, e.message, e.location);
+               } catch (AccessDeniedPluginHTTPException e) {
+                       sendErrorPage(ctx, e.code, e.message, e.location);
+               } catch (DownloadPluginHTTPException e) {
+                       // FIXME: maybe it ought to be defined like 
sendErrorPage : in toadlets
+
+                       MultiValueTable head = new MultiValueTable();
+                       head.put("Content-Disposition", "attachment; 
filename=\"" + e.filename + '"');
+                       ctx.sendReplyHeaders(e.code, "Found", head, e.mimeType, 
e.data.length);
+                       ctx.writeData(e.data);
+               } catch(PluginHTTPException e) {
+                       sendErrorPage(ctx, e.code, e.message, e.location);
                } catch (Throwable t) {
                        Logger.error(this, "Caught "+t, t);
                        String msg = "<html><head><title>Internal 
Error</title></head><body><h1>Internal Error: please report</h1><pre>";

Modified: trunk/freenet/src/freenet/clients/http/Toadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/Toadlet.java 2007-04-13 18:28:32 UTC 
(rev 12647)
+++ trunk/freenet/src/freenet/clients/http/Toadlet.java 2007-04-13 19:02:54 UTC 
(rev 12648)
@@ -166,21 +166,30 @@
                ctx.writeData(buf, 0, buf.length);
        }

+       protected void writeTemporaryRedirect(ToadletContext ctx, String msg, 
String location) throws ToadletContextClosedException, IOException {
+               MultiValueTable mvt = new MultiValueTable();
+               mvt.put("Location", location);
+               if(msg == null) msg = "";
+               else msg = HTMLEncoder.encode(msg);
+               String redirDoc =
+                       
"<html><head><title>"+msg+"</title></head><body><h1>Temporary redirect: "+
+                       msg+"</h1><a 
href=\""+HTMLEncoder.encode(location)+"\">Click here</a></body></html>";
+               byte[] buf;
+               try {
+                       buf = redirDoc.getBytes("UTF-8");
+               } catch (UnsupportedEncodingException e) {
+                       // No way!
+                       throw new Error(e);
+               }
+               ctx.sendReplyHeaders(302, "Found", mvt, "text/html; 
charset=UTF-8", buf.length);
+               ctx.writeData(buf, 0, buf.length);
+       }
+       
        /**
         * Send a simple error page.
         */
        protected void sendErrorPage(ToadletContext ctx, int code, String desc, 
String message) throws ToadletContextClosedException, IOException {
-               HTMLNode pageNode = ctx.getPageMaker().getPageNode(desc, ctx);
-               HTMLNode contentNode = 
ctx.getPageMaker().getContentNode(pageNode);
-               
-               HTMLNode infobox = 
contentNode.addChild(ctx.getPageMaker().getInfobox("infobox-error", desc));
-               HTMLNode infoboxContent = 
ctx.getPageMaker().getContentNode(infobox);
-               infoboxContent.addChild("#", message);
-               infoboxContent.addChild("br");
-               infoboxContent.addChild("a", "href", ".", "Return to the 
previous page.");
-               infoboxContent.addChild("a", "href", "/", "Return to the main 
page.");
-               
-               writeReply(ctx, code, "text/html; charset=UTF-8", desc, 
pageNode.generate());
+               sendErrorPage(ctx, code, desc, new HTMLNode("#", message));
        }

        /**

Added: 
trunk/freenet/src/freenet/pluginmanager/AccessDeniedPluginHTTPException.java
===================================================================
--- 
trunk/freenet/src/freenet/pluginmanager/AccessDeniedPluginHTTPException.java    
                            (rev 0)
+++ 
trunk/freenet/src/freenet/pluginmanager/AccessDeniedPluginHTTPException.java    
    2007-04-13 19:02:54 UTC (rev 12648)
@@ -0,0 +1,19 @@
+/* This code is part of Freenet. It is distributed under the GNU General
+ * Public License, version 2 (or at your option any later version). See
+ * http://www.gnu.org/ for further details of the GPL. */
+package freenet.pluginmanager;
+
+/**
+ * 403 error code.
+ * 
+ * @author Florent Daigni&egrave;re &lt;nextgens at freenetproject.org&gt;
+ */
+public class AccessDeniedPluginHTTPException extends PluginHTTPException {
+       private static final long serialVersionUID = -1;
+       
+       public final short code = 403; // Access Denied
+
+       public AccessDeniedPluginHTTPException(String errorMessage, String 
location) {
+               super(errorMessage, location);
+       }
+}

Added: trunk/freenet/src/freenet/pluginmanager/DownloadPluginHTTPException.java
===================================================================
--- trunk/freenet/src/freenet/pluginmanager/DownloadPluginHTTPException.java    
                        (rev 0)
+++ trunk/freenet/src/freenet/pluginmanager/DownloadPluginHTTPException.java    
2007-04-13 19:02:54 UTC (rev 12648)
@@ -0,0 +1,25 @@
+/* This code is part of Freenet. It is distributed under the GNU General
+ * Public License, version 2 (or at your option any later version). See
+ * http://www.gnu.org/ for further details of the GPL. */
+package freenet.pluginmanager;
+
+/**
+ * Force the download of something to disk
+ * 
+ * @author Florent Daigni&egrave;re &lt;nextgens at freenetproject.org&gt;
+ */
+public class DownloadPluginHTTPException extends PluginHTTPException {
+       private static final long serialVersionUID = -1;
+       
+       public final short code = 200; // Found
+       public final String filename;
+       public final String mimeType;
+       public final byte[] data;
+
+       public DownloadPluginHTTPException(byte[] data, String filename, String 
mimeType) {
+               super("Ok", "none");
+               this.data = data;
+               this.filename = filename;
+               this.mimeType = mimeType;
+       }
+}

Added: trunk/freenet/src/freenet/pluginmanager/NotFoundPluginHTTPException.java
===================================================================
--- trunk/freenet/src/freenet/pluginmanager/NotFoundPluginHTTPException.java    
                        (rev 0)
+++ trunk/freenet/src/freenet/pluginmanager/NotFoundPluginHTTPException.java    
2007-04-13 19:02:54 UTC (rev 12648)
@@ -0,0 +1,19 @@
+/* This code is part of Freenet. It is distributed under the GNU General
+ * Public License, version 2 (or at your option any later version). See
+ * http://www.gnu.org/ for further details of the GPL. */
+package freenet.pluginmanager;
+
+/**
+ * 404 error code.
+ * 
+ * @author Florent Daigni&egrave;re &lt;nextgens at freenetproject.org&gt;
+ */
+public class NotFoundPluginHTTPException extends PluginHTTPException {
+       private static final long serialVersionUID = -1;
+       
+       public final short code = 404; // Not Found
+
+       public NotFoundPluginHTTPException(String errorMessage, String 
location) {
+               super(errorMessage, location);
+       }
+}

Modified: trunk/freenet/src/freenet/pluginmanager/PluginHTTPException.java
===================================================================
--- trunk/freenet/src/freenet/pluginmanager/PluginHTTPException.java    
2007-04-13 18:28:32 UTC (rev 12647)
+++ trunk/freenet/src/freenet/pluginmanager/PluginHTTPException.java    
2007-04-13 19:02:54 UTC (rev 12648)
@@ -3,61 +3,20 @@
  * http://www.gnu.org/ for further details of the GPL. */
 package freenet.pluginmanager;

-import freenet.support.MultiValueTable;
-
+/**
+ * A basic, Plugin exception intended for generic error displaying.
+ * 
+ * @author Florent Daigni&egrave;re &lt;nextgens at freenetproject.org&gt;
+ */
 public class PluginHTTPException extends Exception {
        private static final long serialVersionUID = -1;

-       private int code;
-       private String mimeType;
-       private String desc;
-       private String reply;
-       private MultiValueTable headers = null;
+       public final short code = 400; // Bad Request
+       public final String message;
+       public final String location;

-       public PluginHTTPException () {
-               this(404, "text/html", "FAIL", "Page not found");
+       public PluginHTTPException(String errorMessage, String location) {
+               this.message = errorMessage;
+               this.location = location;
        }
-       public PluginHTTPException (int code, String mimeType, String desc, 
String reply) {
-               this.code = code;
-               this.mimeType = mimeType;
-               this.desc = desc;
-               this.reply = reply;
-       }
-
-       public PluginHTTPException (int code, String desc, MultiValueTable 
headers, String mimeType, String reply) {
-               this.code = code;
-               this.desc = desc;
-               this.headers = headers;
-               this.mimeType = mimeType;
-               this.reply = reply;
-       }
-       
-       public void setCode(int code) {
-               // FIXME: check!
-               this.code = code;
-       }
-       public void setDesc(String desc) {
-               this.desc = desc;
-       }
-       public void setMimeType(String mimeType) {
-               this.mimeType = mimeType;
-       }
-       public void setReply(String reply) {
-               this.reply = reply;
-       }
-       public int getCode() {
-               return code;
-       }
-       public String getDesc() {
-               return desc;
-       }
-       public String getMimeType() {
-               return mimeType;
-       }
-       public String getReply() {
-               return reply;
-       }
-       public MultiValueTable getHeaders() {
-               return headers;
-       }
 }

Modified: trunk/freenet/src/freenet/pluginmanager/PluginManager.java
===================================================================
--- trunk/freenet/src/freenet/pluginmanager/PluginManager.java  2007-04-13 
18:28:32 UTC (rev 12647)
+++ trunk/freenet/src/freenet/pluginmanager/PluginManager.java  2007-04-13 
19:02:54 UTC (rev 12648)
@@ -257,10 +257,7 @@
                if (handler instanceof FredPluginHTTP)
                        return ((FredPluginHTTP)handler).handleHTTPGet(request);

-               // no plugin found
-               PluginHTTPException t = new PluginHTTPException();
-               t.setReply("Plugin not found: " + plugin);
-               throw t;
+               throw new NotFoundPluginHTTPException("Plugin not found!", 
"/plugins");
        }

        public String handleHTTPPost(String plugin, HTTPRequest request) throws 
PluginHTTPException {
@@ -275,10 +272,7 @@
                if (handler instanceof FredPluginHTTP)
                        return 
((FredPluginHTTP)handler).handleHTTPPost(request);

-               // no plugin found
-               PluginHTTPException t = new PluginHTTPException();
-               t.setReply("Plugin not found: " + plugin);
-               throw t;
+               throw new NotFoundPluginHTTPException("Plugin not found!", 
"/plugins");
        }

        public void killPlugin(String name) {

Added: trunk/freenet/src/freenet/pluginmanager/RedirectPluginHTTPException.java
===================================================================
--- trunk/freenet/src/freenet/pluginmanager/RedirectPluginHTTPException.java    
                        (rev 0)
+++ trunk/freenet/src/freenet/pluginmanager/RedirectPluginHTTPException.java    
2007-04-13 19:02:54 UTC (rev 12648)
@@ -0,0 +1,21 @@
+/* This code is part of Freenet. It is distributed under the GNU General
+ * Public License, version 2 (or at your option any later version). See
+ * http://www.gnu.org/ for further details of the GPL. */
+package freenet.pluginmanager;
+
+/**
+ * 302 error code.
+ * 
+ * @author Florent Daigni&egrave;re &lt;nextgens at freenetproject.org&gt;
+ */
+public class RedirectPluginHTTPException extends PluginHTTPException {
+       private static final long serialVersionUID = -1;
+       
+       public final short code = 302; // Found
+       public final String newLocation;
+
+       public RedirectPluginHTTPException(String message, String location, 
String newLocation) {
+               super(message, location);
+               this.newLocation = newLocation;
+       }
+}


Reply via email to