--- /dev/null
+// SimpleGrepFIlter.java\r
+// $Id: SimpleGrepFilter.java,v 1.2 2010/06/15 17:53:03 smhuang Exp $\r
+// (c) COPYRIGHT MIT, INRIA and Keio, 1999.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.jigedit.filters;\r
+\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+\r
+import org.w3c.tools.resources.Attribute;\r
+import org.w3c.tools.resources.AttributeRegistry;\r
+import org.w3c.tools.resources.FramedResource;\r
+import org.w3c.tools.resources.InvalidResourceException;\r
+import org.w3c.tools.resources.ReplyInterface;\r
+import org.w3c.tools.resources.RequestInterface;\r
+import org.w3c.tools.resources.Resource;\r
+import org.w3c.tools.resources.ResourceFilter;\r
+import org.w3c.tools.resources.ResourceFrame;\r
+import org.w3c.tools.resources.ResourceReference;\r
+import org.w3c.tools.resources.StringArrayAttribute;\r
+import org.w3c.tools.resources.StringAttribute;\r
+\r
+import org.w3c.www.http.HTTP;\r
+import org.w3c.www.http.HttpEntityMessage;\r
+import org.w3c.www.http.HttpReplyMessage;\r
+import org.w3c.www.http.HttpRequestMessage;\r
+\r
+import org.w3c.www.mime.MimeType;\r
+\r
+import org.w3c.jigsaw.http.Client;\r
+import org.w3c.jigsaw.http.Reply;\r
+import org.w3c.jigsaw.http.Request;\r
+\r
+import org.w3c.jigsaw.frames.HTTPFrame;\r
+\r
+public class SimpleGrepFilter extends ResourceFilter {\r
+\r
+ class ByteArrayComp {\r
+ byte[] tab = null;\r
+ String string = null;\r
+ int idx = -1;\r
+ int length = -1;\r
+\r
+ boolean matchNow(byte c) {\r
+ if (tab[idx++] == c) {\r
+ return (idx == length);\r
+ } else {\r
+ idx = 0;\r
+ return false;\r
+ }\r
+ }\r
+\r
+ void reset() {\r
+ idx = 0;\r
+ }\r
+\r
+ String getString() {\r
+ return string;\r
+ }\r
+\r
+ ByteArrayComp(String string) {\r
+ tab = string.getBytes();\r
+ idx = 0;\r
+ length = tab.length;\r
+ this.string = string;\r
+ }\r
+ }\r
+\r
+ protected ByteArrayComp[] forbiddenBytes = null;\r
+\r
+ /**\r
+ * Attribute index - The strings to grep.\r
+ */\r
+ protected static int ATTR_FORBIDSTRING_ARRAY = -1;\r
+\r
+ /**\r
+ * Attribute index - The url to redirect.\r
+ */\r
+ protected static int ATTR_REDIRECT = -1;\r
+\r
+ static {\r
+ Class c = null;\r
+ Attribute a = null;\r
+\r
+ try {\r
+ c = Class.forName("org.w3c.jigedit.filters.SimpleGrepFilter");\r
+ //Added by Jeff Huang\r
+ //TODO: FIXIT\r
+ } catch (Exception ex) {\r
+ ex.printStackTrace();\r
+ System.exit(1);\r
+ }\r
+ a = new StringArrayAttribute("forbidden-strings"\r
+ , null\r
+ , Attribute.EDITABLE|Attribute.MANDATORY);\r
+ ATTR_FORBIDSTRING_ARRAY = AttributeRegistry.registerAttribute(c, a);\r
+\r
+ a = new StringAttribute("redirect-url"\r
+ , null\r
+ , Attribute.EDITABLE|Attribute.MANDATORY);\r
+ ATTR_REDIRECT = AttributeRegistry.registerAttribute(c, a);\r
+ }\r
+\r
+ protected String[] getForbiddenStrings() {\r
+ return (String[]) getValue(ATTR_FORBIDSTRING_ARRAY, null);\r
+ }\r
+\r
+ protected String getRedirectURL() {\r
+ return (String) getValue(ATTR_REDIRECT, null);\r
+ }\r
+\r
+ protected ByteArrayComp[] getForbiddenBytes() {\r
+ if (forbiddenBytes == null ) {\r
+ String[] fstrings = getForbiddenStrings();\r
+ forbiddenBytes = new ByteArrayComp[fstrings.length];\r
+ for (int i = 0 ; i < fstrings.length; i++)\r
+ forbiddenBytes[i] = new ByteArrayComp(fstrings[i]);\r
+ }\r
+ return forbiddenBytes;\r
+ }\r
+\r
+ /**\r
+ * Catch assignements to the forbidden strings attribute.\r
+ * <p>When a change to that attribute is detected, the cached value\r
+ * are updated.\r
+ */\r
+ public void setValue(int idx, Object value) {\r
+ super.setValue(idx, value);\r
+ if (idx == ATTR_FORBIDSTRING_ARRAY) {\r
+ forbiddenBytes = null;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Searh for a forbidden string in given stream.\r
+ * @param in the InputStream\r
+ * @return The String found or <strong>null</strong> if none\r
+ * was found.\r
+ */\r
+ protected String searchForbiddenStrings(InputStream in) {\r
+ if (getForbiddenStrings() == null)\r
+ return null;\r
+ try {\r
+ ByteArrayComp comp[] = getForbiddenBytes();\r
+ int len = in.available();\r
+ int c;\r
+ in.mark(len);\r
+ int baclen = comp.length;\r
+\r
+ for (int j = 0; j < baclen; j++)\r
+ comp[j].reset();\r
+\r
+ while ((c = in.read()) != -1) {\r
+ for (int i = 0; i < baclen; i++) {\r
+ if (comp[i].matchNow((byte)c)) {\r
+ in.reset();\r
+ return comp[i].getString();\r
+ }\r
+ }\r
+ } \r
+ in.reset();\r
+ return null;\r
+ } catch (IOException ex) {\r
+ return null;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Search the forbidden string in the body, if found return\r
+ * an ACCES FORBIDDEN Reply.\r
+ * @param request The request that is about to be processsed.\r
+ */\r
+\r
+ public ReplyInterface ingoingFilter(RequestInterface req) {\r
+ Request request = (Request) req;\r
+ if(request.getMethod().equals("PUT")) {\r
+ try {\r
+ MimeType req_mt = request.getContentType();\r
+ if (req_mt.match(MimeType.TEXT) == MimeType.NO_MATCH)\r
+ return null;\r
+ } catch (NullPointerException ex) {\r
+ // no Content-Type sent! check anyway\r
+ }\r
+\r
+ InputStream in = null;\r
+ try {\r
+ in = request.getInputStream();\r
+ if ( in == null ) {\r
+ return null;\r
+ }\r
+ } catch (IOException ex) {\r
+ return null;\r
+ }\r
+ // verify that the target resource is putable\r
+ ResourceReference rr = request.getTargetResource();\r
+ if (rr != null) {\r
+ try {\r
+ FramedResource target = (FramedResource) rr.lock();\r
+ HTTPFrame frame = null;\r
+ try {\r
+ frame = (HTTPFrame) target.getFrame( \r
+ Class.forName("org.w3c.jigsaw.frames.HTTPFrame"));\r
+ //Added by Jeff Huang\r
+ //TODO: FIXIT\r
+ } catch (ClassNotFoundException cex) {\r
+ cex.printStackTrace();\r
+ //big big problem ...\r
+ }\r
+ if (frame == null) // can't be putable\r
+ return null;\r
+ // now we can verify if the target resource is putable\r
+ if (! frame.getPutableFlag()) {\r
+ return null;\r
+ }\r
+ // and that the PUT can happen (taken from putFileResource\r
+ int cim = frame.checkIfMatch(request);\r
+ if ((cim == HTTPFrame.COND_FAILED)\r
+ || (cim == HTTPFrame.COND_WEAK)\r
+ || (frame.checkIfNoneMatch(request) == \r
+ HTTPFrame.COND_FAILED)\r
+ || (frame.checkIfModifiedSince(request) == \r
+ HTTPFrame.COND_FAILED)\r
+ || (frame.checkIfUnmodifiedSince(request) == \r
+ HTTPFrame.COND_FAILED)) {\r
+ Reply r = request.makeReply(HTTP.PRECONDITION_FAILED);\r
+ r.setContent("Pre-condition failed.");\r
+ return r;\r
+ }\r
+ } catch (InvalidResourceException ex) {\r
+ ex.printStackTrace();\r
+ // problem ...\r
+ } finally {\r
+ rr.unlock();\r
+ }\r
+ }\r
+ String expect = request.getExpect();\r
+ if (expect != null) {\r
+ if (expect.startsWith("100")) { // expect 100?\r
+ Client client = request.getClient();\r
+ if (client != null) {\r
+ try {\r
+ client.sendContinue();\r
+ } catch (java.io.IOException ex) {\r
+ return null;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ String found = searchForbiddenStrings(in);\r
+ if (found != null) {\r
+ Reply error = request.makeReply(HTTP.FORBIDDEN);\r
+ error.setReason("the string \""+found+"\" is forbidden.");\r
+ error.setContent ("<p>the string \""+found+\r
+ "\" is forbidden.</p><br> click "+\r
+ "<A HREF=\""+getRedirectURL()+"\">here</A>"+\r
+ " for explaination.");\r
+ return error;\r
+ }\r
+ return null;\r
+ } else \r
+ return null;\r
+ }\r
+\r
+}\r
+\r
+\r