Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / jigsaw / filters / PutSizeFilter.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigsaw/filters/PutSizeFilter.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigsaw/filters/PutSizeFilter.java
new file mode 100644 (file)
index 0000000..50b9f7d
--- /dev/null
@@ -0,0 +1,85 @@
+// PutSizeFilter.java\r
+// $Id: PutSizeFilter.java,v 1.2 2010/06/15 17:52:55 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1997.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.jigsaw.filters;\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.BooleanAttribute;\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.HttpEntityMessage;\r
+import org.w3c.www.http.HttpRequestMessage;\r
+\r
+import org.w3c.jigsaw.http.Reply;\r
+import org.w3c.jigsaw.http.Request;\r
+\r
+public class PutSizeFilter extends ResourceFilter {\r
+    /**\r
+     * Attribute index - The maximum size of the put document\r
+     */\r
+\r
+    protected static int ATTR_PUTSIZE = -1;\r
+    protected static int ATTR_STRICT  = -1;\r
+\r
+    static {\r
+       Class     c = null;\r
+       Attribute a = null;\r
+\r
+       try {\r
+           c = Class.forName("org.w3c.jigsaw.filters.PutSizeFilter");\r
+           //Added by Jeff Huang\r
+           //TODO: FIXIT\r
+       } catch (Exception ex) {\r
+           ex.printStackTrace();\r
+           System.exit(1);\r
+       }\r
+       // Register the PutList URL attribute:\r
+       a = new IntegerAttribute("put-size"\r
+                                , new Integer(65536)\r
+                                , Attribute.EDITABLE|Attribute.MANDATORY);\r
+       ATTR_PUTSIZE = AttributeRegistry.registerAttribute(c, a);\r
+       a = new BooleanAttribute("strict"\r
+                                , new Boolean(true)\r
+                                , Attribute.EDITABLE|Attribute.MANDATORY);\r
+       ATTR_STRICT = AttributeRegistry.registerAttribute(c, a);\r
+    }\r
+\r
+    private Reply notifyFailure(Request request, boolean no_size) {\r
+       Reply er = null;\r
+       if (request.getExpect() != null)\r
+           er = request.makeReply(HTTP.EXPECTATION_FAILED);\r
+       else {\r
+           if (no_size)\r
+               er = request.makeReply(HTTP.LENGTH_REQUIRED);\r
+           else\r
+               er = request.makeReply(HTTP.REQUEST_ENTITY_TOO_LARGE);\r
+       }\r
+       er.setContent("<P>You are not allowed to PUT documents more than " +\r
+                     getInt(ATTR_PUTSIZE, -1) + " bytes long</P>");\r
+       return er;\r
+    }\r
+\r
+    public ReplyInterface ingoingFilter(RequestInterface req) {\r
+       Request request = (Request) req;\r
+       if(request.getMethod().equals("PUT")) {\r
+           if(getBoolean(ATTR_STRICT, true) && !request.hasContentLength()) \r
+               return notifyFailure(request, true);\r
+           if(request.getContentLength() > getInt(ATTR_PUTSIZE, -1))\r
+               return notifyFailure(request, false);\r
+       }\r
+       return null;\r
+    }\r
+\r
+    public ReplyInterface outgoingFilter(RequestInterface req, \r
+                                        ReplyInterface rep) {\r
+       return null;\r
+    }\r
+}\r