View Javadoc

1   /*
2    * Copyright 2008 Hippo
3    *
4    * Licensed under the Apache License, Version 2.0 (the  "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" 
12   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  package nl.hippo.client.webapp.matchers;
17  
18  import java.util.Iterator;
19  import java.util.SortedSet;
20  import java.util.TreeSet;
21  
22  import nl.hippo.client.api.ClientException;
23  import nl.hippo.client.api.content.Document;
24  import nl.hippo.client.api.content.DocumentCollection;
25  import nl.hippo.client.webapp.RepositoryAdapter;
26  import nl.hippo.client.webapp.Request;
27  import nl.hippo.client.webapp.ServiceWrapper;
28  import nl.hippo.client.webapp.WebAppLogger;
29  import nl.hippo.client.webdav.WebdavResponse;
30  
31  /**
32   * Administrative console
33   */
34  public class AdminMatcher extends Matcher {
35  
36      public AdminMatcher() {
37          super("/admin.*$");    
38      }
39      
40      
41      //TODO: Quick&dirty implementation using string concatenation, consider using some webframework for this one.
42      
43      public WebdavResponse execute(final Request request) throws ClientException {
44          return wrapExec(new MatcherCallback() {
45              public WebdavResponse doExec() throws ClientException {
46                  WebAppLogger.log.debug("Executing AdminMatcher");
47                  StringBuffer result = header();
48                  
49                  SortedSet queries = new TreeSet();
50                  SortedSet transformers = new TreeSet();
51                  
52                  ServiceWrapper service = (ServiceWrapper)request.getContext().getAttribute((RepositoryAdapter.QUERIES_WEBDAV));
53                  DocumentCollection files = service.getQueries();
54                  Iterator it = files.getDocuments().iterator();
55                  while (it.hasNext()) {
56                      Document doc = (Document)it.next();
57                      String name = doc.getPath().getRelativePath();
58                      name = name.substring(name.lastIndexOf("/")+1);
59                      if (name.endsWith(".xml")) {
60                          queries.add(name);
61                      } else {
62                          transformers.add(name);
63                      }
64                  }
65                  result = result.append(rows("Queries", queries, request));
66                  result = result.append(rows("Transformers", transformers, request));
67                  result = result.append(uploadForm());
68                  result = result.append(footer()); 
69                  
70                  return new WebdavResponse(result.toString().getBytes());
71              }
72          });
73      }  
74      
75      protected StringBuffer rows(String title, SortedSet filenames, Request request) {
76          StringBuffer result = new StringBuffer()
77          .append("<tr bgcolor=\"#cccccc\">").append("\n")
78          .append("<td align=\"left\" colspan=\"2\"><font size=\"+1\"><strong>")
79          .append(title)
80          .append("</strong></font></td>").append("\n")
81          .append("</tr>").append("\n");
82  
83          Iterator it = filenames.iterator();
84          int count = 0;
85          while (it.hasNext()) {
86              String filename = (String)it.next();
87              //TODO fixme
88              result = result.append(row(count++, filename, request));
89          }
90          return result;
91      }
92      
93      protected StringBuffer row(int index, String name, Request request) {
94          String context = request.getContextPath();
95          String view = context + "/getquery/" + name;
96          String delete = context + "/deletequery/" + name;
97          return new StringBuffer()
98          .append(index % 2 == 0 ? "<tr bgcolor=\"dddddd\">" : "<tr bgcolor=\"eeeeee\">").append("\n")
99          .append("<td align=\"left\" colspan=\"1\">&nbsp;&nbsp;").append("\n")
100         .append("<a href=\"").append(view).append("\"><tt>").append(name).append("</tt></a></td>").append("\n")
101         .append("<td align=\"left\" colspan=\"1\"><a href=\"").append(delete).append("\"><tt>delete</tt></a></td>").append("\n")
102         .append("</tr>").append("\n");
103     }
104     
105     protected StringBuffer header() {
106         return new StringBuffer()
107         .append("<html>").append("\n")
108         .append("<head>").append("\n")
109         .append("<meta http-equiv=\"Content-type\" content=\"text/html; charset=UTF-8\" />")
110         .append("\n")
111         .append("<title>Hippo Repository Adapter</title>").append("\n")
112         .append("</head>").append("\n")
113         .append("<body bgcolor=\"white\">").append("\n")
114         .append("<table width=\"90%\" cellspacing=\"0\" cellpadding=\"5\" align=\"center\">").append("\n")
115         .append("<tr><td colspan=\"2\"><font size=\"+2\">").append("\n")
116         .append("<strong>Hippo Repository Adapter</strong>").append("\n")
117         .append("</font></td></tr>").append("\n")
118         .append("<tr><td colspan=\"2\" bgcolor=\"#ffffff\">&nbsp;</td></tr>").append("\n");
119     }
120     
121     protected StringBuffer footer() {
122         return new StringBuffer()
123         .append("<tr><td colspan=\"2\">&nbsp;</td></tr>").append("\n")
124         .append("<tr><td colspan=\"2\" bgcolor=\"#cccccc\"><font size=\"-1\">Hippo Repository Adapter</font></td></td></tr>").append("\n")
125         .append("</body>").append("\n")
126         .append("</html>").append("\n");
127     }
128     
129     protected StringBuffer uploadForm() {
130         return new StringBuffer()
131         .append("<tr bgcolor=\"#cccccc\">").append("\n")
132         .append("<td colspan=\"2\">").append("\n")
133         .append("<form name=\"myform\" action=\"admin\" method=\"post\" enctype=\"multipart/form-data\">").append("\n")
134         .append("<hr/>").append("\n")
135         .append("Upload new query or transformer:<br/>").append("\n")
136         .append("<input type=\"file\" name=\"uploadFileName\" size=\"20\">&nbsp;<input type=\"submit\" name=\"doUpload\" value=\"Upload\"/><br/>").append("\n")
137         .append("<hr/>").append("\n")
138         .append("</form>").append("\n")
139         .append("</td>").append("\n")
140         .append("</tr>").append("\n");
141     }
142 
143 
144 
145 }