Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / jigsaw / filters / URISizeLimiterFilter.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigsaw/filters/URISizeLimiterFilter.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigsaw/filters/URISizeLimiterFilter.java
new file mode 100644 (file)
index 0000000..3f8aef5
--- /dev/null
@@ -0,0 +1,87 @@
+// URISizeLimiterFilter.java\r
+// $Id: URISizeLimiterFilter.java,v 1.2 2010/06/15 17:52:55 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1998.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.jigsaw.filters ;\r
+\r
+import java.util.Hashtable ;\r
+\r
+import org.w3c.tools.resources.Attribute;\r
+import org.w3c.tools.resources.AttributeHolder;\r
+import org.w3c.tools.resources.AttributeRegistry;\r
+import org.w3c.tools.resources.IntegerAttribute;\r
+import org.w3c.tools.resources.ReplyInterface;\r
+import org.w3c.tools.resources.RequestInterface;\r
+import org.w3c.tools.resources.ResourceFilter;\r
+\r
+import org.w3c.www.http.HTTP;\r
+import org.w3c.www.http.HttpRequestMessage;\r
+\r
+import org.w3c.jigsaw.http.Reply;\r
+import org.w3c.jigsaw.http.Request;\r
+\r
+import org.w3c.jigsaw.html.HtmlGenerator ;\r
+\r
+/**\r
+ * This filters limit the size of URI.\r
+ */\r
+\r
+public class URISizeLimiterFilter extends ResourceFilter {\r
+    /**\r
+     * Attribute index - The maximum allowed size of the requested URL\r
+     */\r
+    protected static int ATTR_SIZE_LIMIT = -1 ;\r
+\r
+    static {\r
+       Attribute a   = null ;\r
+       Class     cls = null ;\r
+\r
+       try {\r
+           cls = Class.forName("org.w3c.jigsaw.filters.URISizeLimiterFilter");\r
+           //Added by Jeff Huang\r
+           //TODO: FIXIT\r
+       } catch (Exception ex) {\r
+           ex.printStackTrace() ;\r
+           System.exit(1) ;\r
+       }\r
+       // The limit attribute\r
+       a = new IntegerAttribute("limit"\r
+                                , new Integer(8192)\r
+                                , Attribute.EDITABLE) ;\r
+       ATTR_SIZE_LIMIT = AttributeRegistry.registerAttribute(cls, a) ;\r
+    }\r
+\r
+    /**\r
+     * Get the maximum size of the URL.\r
+     */\r
+\r
+    public int getLimit() {\r
+       return getInt(ATTR_SIZE_LIMIT, 8192) ; /* default to 8k */\r
+    }\r
+\r
+    /**\r
+     * Check the size of the URL\r
+     * if it is more than the size defined, an error is sent back\r
+     * @return a Reply or null if successful\r
+     */\r
+\r
+    public synchronized \r
+       ReplyInterface ingoingFilter (RequestInterface req) \r
+    {\r
+       Request request = (Request) req;\r
+       int limit = getLimit();\r
+\r
+       if (request.getURL().toExternalForm().length() > limit) {\r
+           Reply error = request.makeReply(HTTP.REQUEST_URI_TOO_LONG);\r
+           HtmlGenerator g = new HtmlGenerator("Request URI Too Long");\r
+           g.append ("Your request should have an URI of less than " +\r
+                     limit + " bytes");\r
+           error.setStream(g);\r
+           return error;\r
+       }\r
+       return null;\r
+    }\r
+}\r
+\r
+\r