Author: toad
Date: 2008-02-13 23:08:30 +0000 (Wed, 13 Feb 2008)
New Revision: 17886

Added:
   trunk/freenet/src/freenet/clients/http/PermanentRedirectException.java
Modified:
   trunk/freenet/src/freenet/clients/http/SimpleToadletServer.java
   trunk/freenet/src/freenet/clients/http/Toadlet.java
   trunk/freenet/src/freenet/clients/http/ToadletContainer.java
   trunk/freenet/src/freenet/clients/http/ToadletContextImpl.java
Log:
Better to do a permanent-redirect.

Added: trunk/freenet/src/freenet/clients/http/PermanentRedirectException.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/PermanentRedirectException.java      
                        (rev 0)
+++ trunk/freenet/src/freenet/clients/http/PermanentRedirectException.java      
2008-02-13 23:08:30 UTC (rev 17886)
@@ -0,0 +1,17 @@
+package freenet.clients.http;
+
+import java.net.URI;
+
+public class PermanentRedirectException extends Exception {
+
+       URI newuri;
+       
+       public PermanentRedirectException() {
+               super();
+       }
+       
+       public PermanentRedirectException(URI newURI) {
+               this.newuri = newURI;
+       }
+       
+}

Modified: trunk/freenet/src/freenet/clients/http/SimpleToadletServer.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/SimpleToadletServer.java     
2008-02-13 23:04:20 UTC (rev 17885)
+++ trunk/freenet/src/freenet/clients/http/SimpleToadletServer.java     
2008-02-13 23:08:30 UTC (rev 17886)
@@ -11,6 +11,7 @@
 import java.net.SocketException;
 import java.net.SocketTimeoutException;
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.net.URLDecoder;
@@ -470,7 +471,7 @@
                }
        }

-       public Toadlet findToadlet(URI uri) {
+       public Toadlet findToadlet(URI uri) throws PermanentRedirectException {
                Iterator i = toadlets.iterator();
                String path = uri.getPath();
                while(i.hasNext()) {
@@ -479,8 +480,15 @@
                        if(path.startsWith(te.prefix))
                                return te.t;
                        if(te.prefix.length() > 0 && 
te.prefix.charAt(te.prefix.length()-1) == '/') {
-                               if(path.equals(te.prefix.substring(0, 
te.prefix.length()-1)))
-                                       return te.t;
+                               if(path.equals(te.prefix.substring(0, 
te.prefix.length()-1))) {
+                                       URI newURI;
+                                       try {
+                                               newURI = new URI(te.prefix);
+                                       } catch (URISyntaxException e) {
+                                               throw new Error(e);
+                                       }
+                                       throw new 
PermanentRedirectException(newURI);
+                               }
                        }
                }
                return null;

Modified: trunk/freenet/src/freenet/clients/http/Toadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/Toadlet.java 2008-02-13 23:04:20 UTC 
(rev 17885)
+++ trunk/freenet/src/freenet/clients/http/Toadlet.java 2008-02-13 23:08:30 UTC 
(rev 17886)
@@ -88,7 +88,7 @@
                
toadletContext.writeData(pageBuffer.toString().getBytes("UTF-8"));
        }

-       private String l10n(String key, String pattern, String value) {
+       private static String l10n(String key, String pattern, String value) {
                return L10n.getString("Toadlet."+key, new String[] { pattern }, 
new String[] { value });
        }

@@ -173,7 +173,7 @@
                context.writeData(buffer, startIndex, length);
        }

-       protected void writePermanentRedirect(ToadletContext ctx, String msg, 
String location) throws ToadletContextClosedException, IOException {
+       static void writePermanentRedirect(ToadletContext ctx, String msg, 
String location) throws ToadletContextClosedException, IOException {
                MultiValueTable mvt = new MultiValueTable();
                mvt.put("Location", location);
                if(msg == null) msg = "";

Modified: trunk/freenet/src/freenet/clients/http/ToadletContainer.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/ToadletContainer.java        
2008-02-13 23:04:20 UTC (rev 17885)
+++ trunk/freenet/src/freenet/clients/http/ToadletContainer.java        
2008-02-13 23:08:30 UTC (rev 17886)
@@ -5,6 +5,7 @@

 import java.net.InetAddress;
 import java.net.URI;
+import java.net.URISyntaxException;

 import freenet.support.HTMLNode;

@@ -20,8 +21,11 @@

        /**
         * Find a Toadlet by URI.
+        * @throws URISyntaxException 
+        * @throws RedirectException 
+        * @throws PermanentRedirectException 
         */
-       public Toadlet findToadlet(URI uri);
+       public Toadlet findToadlet(URI uri) throws PermanentRedirectException;

        /**
         * Get the name of the theme to be used by all the Toadlets

Modified: trunk/freenet/src/freenet/clients/http/ToadletContextImpl.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/ToadletContextImpl.java      
2008-02-13 23:04:20 UTC (rev 17885)
+++ trunk/freenet/src/freenet/clients/http/ToadletContextImpl.java      
2008-02-13 23:08:30 UTC (rev 17886)
@@ -321,7 +321,13 @@
                                        // don't go around the loop unless set 
explicitly
                                        redirect = false;

-                                       Toadlet t = container.findToadlet(uri);
+                                       Toadlet t;
+                                       try {
+                                               t = container.findToadlet(uri);
+                                       } catch (PermanentRedirectException e) {
+                                               
Toadlet.writePermanentRedirect(ctx, "Found elsewhere", 
e.newuri.toASCIIString());
+                                               break;
+                                       }

                                        if(t == null) {
                                                
ctx.sendNoToadletError(ctx.shouldDisconnect);


Reply via email to