Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / jigedit / filters / PutListFrame.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigedit/filters/PutListFrame.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigedit/filters/PutListFrame.java
new file mode 100644 (file)
index 0000000..1217144
--- /dev/null
@@ -0,0 +1,425 @@
+// PutListResource.java\r
+// $Id: PutListFrame.java,v 1.1 2010/06/15 12:27:54 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1996.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.jigedit.filters;\r
+\r
+import java.io.File;\r
+import java.io.PrintStream;\r
+\r
+import java.util.Date;\r
+import java.util.Enumeration;\r
+import java.util.Vector;\r
+\r
+import java.net.URL;\r
+\r
+import org.w3c.tools.resources.FramedResource;\r
+import org.w3c.tools.resources.ProtocolException;\r
+import org.w3c.tools.resources.ReplyInterface;\r
+import org.w3c.tools.resources.RequestInterface;\r
+import org.w3c.tools.resources.ResourceException;\r
+\r
+import org.w3c.tools.resources.ProtocolException;\r
+\r
+import org.w3c.cvs2.CVS;\r
+import org.w3c.cvs2.CvsDirectory;\r
+import org.w3c.cvs2.CvsException;\r
+\r
+import org.w3c.jigsaw.auth.AuthFilter;\r
+\r
+import org.w3c.jigsaw.http.HTTPException;\r
+import org.w3c.jigsaw.http.Reply;\r
+import org.w3c.jigsaw.http.Request;\r
+\r
+import org.w3c.jigsaw.frames.HTTPFrame;\r
+import org.w3c.jigsaw.frames.PostableFrame;\r
+\r
+import org.w3c.www.http.HTTP;\r
+import org.w3c.www.http.HttpMessage;\r
+import org.w3c.www.http.HttpRequestMessage;\r
+\r
+import org.w3c.jigsaw.forms.URLDecoder;\r
+\r
+import org.w3c.jigsaw.html.HtmlGenerator;\r
+\r
+public class PutListFrame extends PostableFrame {\r
+\r
+    PutListResource putlist = null;\r
+\r
+    public void registerResource(FramedResource resource) {\r
+       super.registerResource(resource);\r
+       if (resource instanceof PutListResource) {\r
+           putlist = (PutListResource) resource;\r
+       }\r
+    }\r
+\r
+    /**\r
+     * perform the request.\r
+     * @param req the incomming request.\r
+     * @exception org.w3c.tools.resources.ProtocolException if a protocol \r
+     * error occurs\r
+     * @exception org.w3c.tools.resources.ResourceException if a server \r
+     * error occurs\r
+     */\r
+    public ReplyInterface perform(RequestInterface req) \r
+       throws ProtocolException, ResourceException\r
+    {\r
+       if (putlist == null) {\r
+           Request request = (Request) req;\r
+           Reply error = request.makeReply(HTTP.INTERNAL_SERVER_ERROR) ;\r
+           error.setContent("The PutListFrame must be associated "+\r
+                            "with a PutListResource only!!");\r
+           throw new HTTPException(error);\r
+       }\r
+       return super.perform(req);\r
+    }\r
+\r
+    protected HtmlGenerator getHtmlGenerator(String title) {\r
+       HtmlGenerator g = new HtmlGenerator(title);\r
+       addStyleSheet(g);\r
+       return g;\r
+    }\r
+\r
+    /**\r
+     * Dump the list of modified files.\r
+     * @param request The request to handle.\r
+     * @return A Reply instance.\r
+     * @exception org.w3c.tools.resources.ProtocolException if a protocol \r
+     * error occurs\r
+     * @exception org.w3c.tools.resources.ResourceException if a server \r
+     * error occurs\r
+     */\r
+\r
+    public Reply get(Request request) \r
+       throws ProtocolException, ResourceException\r
+    {\r
+       HtmlGenerator g = getHtmlGenerator("Modified files");\r
+       g.append("<h1>List of recently published files</h1>\n");\r
+       Enumeration penum = putlist.getPublishedEntries();\r
+       if (! penum.hasMoreElements())\r
+           g.append("<center><b>No recently published file</b></center>\n");\r
+       g.append("<ul>\n");\r
+       \r
+       Vector sorted = \r
+           org.w3c.tools.sorter.Sorter.sortComparableEnumeration(penum);\r
+\r
+       for (int i=0; i < sorted.size(); i++) {\r
+           PutedEntry e      = (PutedEntry) sorted.elementAt(i);\r
+           String     url    = e.getURL();\r
+           g.append("<li><a href=\"",\r
+                    url,\r
+                    "\">"+url+"</a>");\r
+           g.append("<br>Published by <em>"+e.getAuthor()+"</em> on <strong>",\r
+                    new Date(e.getTime()).toString(),\r
+                    "</strong>.</li>\n");\r
+       }\r
+\r
+       g.append("</ul>\n");\r
+\r
+       penum = putlist.getEntries();\r
+       if (penum.hasMoreElements()) {\r
+           g.append("<h1>List of modified files</h1>\n");\r
+           g.append("<form action=\""+request.getURL()+\r
+                    "\" method=\"POST\">\n");\r
+           g.append("<dl>\n");\r
+           // Dump all entries:\r
+           sorted = \r
+               org.w3c.tools.sorter.Sorter.sortComparableEnumeration(penum);\r
+\r
+           for (int i=0; i < sorted.size(); i++) {\r
+               PutedEntry e      = (PutedEntry) sorted.elementAt(i);\r
+               String     fname  = e.getFilename();\r
+               String     author = e.getAuthor();\r
+               long       time   = e.getTime();\r
+               String     url    = e.getURL();\r
+\r
+               g.append("<dt><input type=\"checkbox\" name=\""+\r
+                        e.getKey() + "\" value =\"mark\">",\r
+                        (fname != null) ? fname : url,\r
+                        "</dt><dd>");\r
+               if ( fname != null ) {\r
+                   File         file    = new File(fname);\r
+                   File         dir     = new File(file.getParent());\r
+                   // Compute the CVS directory URL for the file:\r
+                   URL          cvsurl  = null;\r
+                   try {\r
+                       cvsurl  = new URL(new URL(url), "CVS");\r
+                   } catch (Exception ex) {\r
+                       cvsurl = null;\r
+                   }\r
+                   // Display status:\r
+                   int st = -1;\r
+                   try {\r
+                       // Local status first:\r
+                       CvsDirectory cvs = \r
+                           CvsDirectory.getManager(dir,\r
+                                                   putlist.props);\r
+                       st  = cvs.status(file.getName());\r
+                       if ( cvsurl != null )\r
+                           g.append("Status: <a href=\""+cvsurl+ "\">"\r
+                                    , cvs.statusToString(st)\r
+                                    , "</a><br>");\r
+                       else\r
+                           g.append("Status: "\r
+                                    , cvs.statusToString(st)\r
+                                    , "<br>");\r
+\r
+                       if (url != null)\r
+                           g.append("URL: <a href=\"",\r
+                                    url,\r
+                                    "\">"+url+"</a><br>");\r
+         \r
+                   } catch (CvsException ex) {\r
+                       g.append("Status: <strong>CVS ERROR</strong>: "\r
+                                , ex.getMessage()\r
+                                , "<br>");\r
+                   }\r
+                   // Publish status next (when possible)\r
+                   if ( st != CVS.FILE_Q ) {\r
+                       try {\r
+                           File         sf  = putlist.getServerFile(file); \r
+                           File         sd  = new File(sf.getParent());\r
+                           CvsDirectory sc  = \r
+                               CvsDirectory.getManager(sd, putlist.props);\r
+                           int          sst = sc.status(file.getName());\r
+                           if (sst == CVS.FILE_C) {\r
+                               g.append("Publish: (needed) <B><U>",\r
+                                        sc.statusToString(sst),\r
+                                        "</U></B><br>");\r
+                           } else if ((st == CVS.FILE_M) || \r
+                                      (sst != CVS.FILE_OK)) \r
+                               {\r
+                                   g.append("Publish: (needed) <em>"\r
+                                            , sc.statusToString(sst)\r
+                                            , "</em><br>");\r
+                               } else {\r
+                                   g.append("Publish: <em>"\r
+                                            , sc.statusToString(sst)\r
+                                            , "</em><br>");\r
+                               }\r
+                       } catch (CvsException ex) {\r
+                           g.append("Publish: <strong>CVS ERROR</strong>: "\r
+                                    , ex.getMessage()\r
+                                    , "<br>");\r
+                       }\r
+                   }\r
+               }\r
+               // Display author:\r
+               if ( author != null )\r
+                   g.append("Modified by <em>"+author+"</em> on <strong>"+\r
+                            new Date(time).toString() + "</strong>.<br>\n");\r
+               else\r
+                   g.append("Modified on <strong>"+\r
+                            new Date(time).toString()+\r
+                            "</strong>.<br>\n");\r
+           }\r
+           g.append("</dl>\n");\r
+           // The command button:\r
+           g.append ("<hr noshade width=\"40%\">\n<center>\n",\r
+                     "<table border=\"0\">",\r
+                     "<tr align=\"left\"><td>\n");\r
+           g.append ("<b>Perform action on marked entries:</b><p>\n") ;\r
+           g.append ("<input type=\"radio\" name=\"action\" ",\r
+                     "value=\"publish\">Publish<br>\n");\r
+           g.append ("<input type=\"radio\" name=\"action\" value=\"remove\">"\r
+                     + "Remove \n");\r
+           g.append ("</p><center>\n") ;\r
+           g.append ("<input type=\"submit\" name=\"submit\" "+\r
+                     "value=\"Perform Action\">\n") ;\r
+           g.append ("</center>");\r
+           g.append ("</form>\n") ;\r
+           g.append ("</td></tr></table></center>\n");\r
+       }\r
+\r
+       penum = putlist.getDelEntries();\r
+       if (penum.hasMoreElements()) {\r
+           g.append("<h1>List of deleted files</h1>\n");\r
+           g.append("<form action=\""+request.getURL()+\r
+                    "\" method=\"POST\">\n");\r
+           g.append("<dl>\n");\r
+           //Dump only confirmed entries\r
+           sorted = \r
+               org.w3c.tools.sorter.Sorter.sortComparableEnumeration(penum);\r
+           for (int i=0; i < sorted.size(); i++) {\r
+               DeletedEntry e = (DeletedEntry) sorted.elementAt(i);\r
+               if (e.isConfirmed()) {\r
+                   String     fname  = e.getFilename();\r
+                   String     author = e.getAuthor();\r
+                   long       time   = e.getTime();\r
+                   String     url    = e.getURL();\r
+                   g.append("<dt><input type=\"checkbox\" name=\""+\r
+                            e.getKey() + "\" value =\"mark\">",\r
+                            (fname != null) ? fname : url,\r
+                            "</dt><dd>");\r
+                   if (url != null)\r
+                       g.append("URL: <b>"+url+"</b><br>");\r
+                   // Display author:\r
+                   if ( author != null )\r
+                       g.append("Deleted by <em>"+\r
+                                author+"</em> on <strong>"+\r
+                                new Date(time).toString() +\r
+                                "</strong>.<br>\n");\r
+                   else\r
+                       g.append("Deleted on <strong>"+\r
+                                new Date(time).toString()+\r
+                                "</strong>.<br>\n");\r
+               }\r
+           }\r
+           g.append("</dl>\n");\r
+           // The command button:\r
+           g.append ("<hr noshade width=\"40%\">\n<center>\n",\r
+                     "<table border=\"0\">",\r
+                     "<tr align=\"left\"><td>\n");\r
+           g.append ("<b>Perform action on marked entries:</b><p>\n") ;\r
+           g.append ("<input type=\"radio\" name=\"action\" ",\r
+                     "value=\"delete\">Delete<br>\n");\r
+           g.append ("<input type=\"radio\" name=\"action\" "+\r
+                     "value=\"removedel\">Remove \n");\r
+           g.append ("</p><center>\n") ;\r
+           g.append ("<input type=\"submit\" name=\"submit\" "+\r
+                     "value=\"Perform Action\">\n") ;\r
+           g.append ("</center>");\r
+           g.append ("</form>\n") ;\r
+           g.append ("</td></tr></table></center>\n");\r
+       }\r
+\r
+       g.append ("<h1>Putlist Configuration</h1>");\r
+       g.append ("<center>");\r
+       g.append ("<table border=\"0\"><tr align=\"left\"><td>");\r
+       g.append ("<form action=\""+request.getURL()+"\" method=\"POST\">\n");\r
+       g.append ("<input type=\"hidden\" name=\"action\" value=\"config\">");\r
+       g.append ("<input type=\"checkbox\"",\r
+                 (putlist.getAutoPublishFlag() ? " CHECKED " : " "),\r
+                 "name=\"autopublish\" value=\"mark\"> Auto Publish<br>");\r
+       g.append ("<input type=\"checkbox\"",\r
+                 (putlist.getAutoDeleteFlag() ? " CHECKED " : " "),\r
+                 "name=\"autodelete\" value=\"mark\"> Auto Delete<br>");\r
+       g.append ("Max published entries <input type=\"text\" "+\r
+                 "size=\"3\" maxlength=\"2\" name=\"mpe\" value=\""+\r
+                 putlist.getMaxPublishedEntryStored()+\r
+                 "\">");\r
+       g.append ("</p><center>");\r
+       g.append ("<input type=\"submit\" name=\"submit\" "+\r
+                 "value=\"Save Configuration\">") ;\r
+       g.append ("</center></form>\n");\r
+       g.append ("</td></tr></table>\n");\r
+       g.append ("</center>");\r
+       g.append ("<hr noshade width=\"80%\">");\r
+       g.close();\r
+       Reply reply = createDefaultReply(request, HTTP.OK);\r
+       reply.addPragma("no-cache");\r
+       reply.setNoCache();\r
+       reply.setStream(g);\r
+       return reply;\r
+    }\r
+\r
+    protected void performAction(Request request, String action, String key) \r
+       throws HTTPException\r
+    {\r
+\r
+       if ( action.equals("publish") ) {\r
+           PutedEntry pe = (PutedEntry) putlist.getEntry(key);\r
+           if ( pe == null ) {\r
+               // We're in troubles !\r
+               if ( putlist.debug )\r
+                   System.out.println("PutList: "+key+" not found !");\r
+               return ;\r
+           }\r
+           if (putlist.publish(pe) == PutListResource.FILE_CF) {\r
+               //send error.\r
+               Reply error = request.makeReply(HTTP.CONFLICT);\r
+               HtmlGenerator gerr = getHtmlGenerator("Warning");\r
+               gerr.append ("<H1>Warning</H1> The file on publish space has "+\r
+                            "been modified directly and attempting to merge"+\r
+                            " has failed.<p>"+\r
+                            "Ask your system administrator.");\r
+               error.setStream(gerr);\r
+               throw new HTTPException(error);\r
+           }\r
+       } else if ( action.equals("delete") ) {\r
+           DeletedEntry de = (DeletedEntry) putlist.getDelEntry(key);\r
+           if (de == null) {\r
+               // We're in troubles !\r
+               if ( putlist.debug )\r
+                   System.out.println("PutList: "+key+" not found !");\r
+               return ;\r
+           }\r
+           putlist.delete(de);\r
+       } else if ( action.equals("remove") ) {\r
+           putlist.removeEntry(key);\r
+       } else if ( action.equals("removedel") ) {\r
+           putlist.removeDelEntry(key);\r
+       } else if ( putlist.debug ) {\r
+           System.out.println("PutList: "+action+" unknown.");\r
+       }\r
+    }\r
+\r
+    /**\r
+     * handle the request.\r
+     * @param request the incomming request.\r
+     * @param data the URLDecoder.\r
+     * @exception org.w3c.tools.resources.ProtocolException if a protocol \r
+     * error occurs\r
+     */\r
+\r
+    public Reply handle(Request request, org.w3c.jigsaw.forms.URLDecoder data) \r
+       throws ProtocolException\r
+    {\r
+       // Get the action to perform:\r
+       String action = data.getValue("action");\r
+       if (action == null) {\r
+           Reply error = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);\r
+           error.setContent("You must select the action to be performed.");\r
+           return error;\r
+       }\r
+       if (action.equals("config")) {\r
+           if (data.getValue("autopublish") != null)\r
+               putlist.setAutoPublish(true);\r
+           else\r
+               putlist.setAutoPublish(false);\r
+\r
+           if (data.getValue("autodelete") != null)\r
+               putlist.setAutoDelete(true);\r
+           else\r
+               putlist.setAutoDelete(false);\r
+\r
+           String max = null;\r
+           if ((max = data.getValue("mpe")) != null) {\r
+               try {\r
+                   putlist.setMaxPublishedEntryStored(Integer.parseInt(max));\r
+               } catch (NumberFormatException ex) {\r
+                   Reply error = request.makeReply(HTTP.BAD_REQUEST);\r
+                   error.setContent("Invalid number : "+max);\r
+                   return error;\r
+               }\r
+           }\r
+       } else {\r
+           Enumeration genum = null;\r
+           if (action.equals("publish") || action.equals("remove"))\r
+               genum = putlist.getEntriesKeys();\r
+           else\r
+               genum = putlist.getDelEntriesKeys();\r
+           // Check all entries and perform action:\r
+           while ( genum.hasMoreElements() ) {\r
+               String key = (String) genum.nextElement();\r
+               if (data.getValue(key) != null) {\r
+                   // Perform action on that entry:\r
+                   if ( putlist.debug )\r
+                       System.out.println("PutList: "+action+" on "+key);\r
+                   performAction(request, action, key);\r
+               } else {\r
+                   if ( putlist.debug )\r
+                       System.out.println("PutList: "+key+" not marked !");\r
+               }\r
+           }\r
+       } \r
+       try {\r
+           return get(request);\r
+       } catch (ResourceException ex) {\r
+           Reply error = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);\r
+           error.setContent(ex.getMessage());\r
+           return error;\r
+       }\r
+    }\r
+} // PutListFrame\r