View Javadoc
1   /*
2    * $Source$
3    * $Revision$
4    *
5    * Copyright (C) 2000 Tim Joyce
6    *
7    * Part of Melati (http://melati.org), a framework for the rapid
8    * development of clean, maintainable web applications.
9    *
10   * Melati is free software; Permission is granted to copy, distribute
11   * and/or modify this software under the terms either:
12   *
13   * a) the GNU General Public License as published by the Free Software
14   *    Foundation; either version 2 of the License, or (at your option)
15   *    any later version,
16   *
17   *    or
18   *
19   * b) any version of the Melati Software License, as published
20   *    at http://melati.org
21   *
22   * You should have received a copy of the GNU General Public License and
23   * the Melati Software License along with this program;
24   * if not, write to the Free Software Foundation, Inc.,
25   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26   * GNU General Public License and visit http://melati.org to obtain the
27   * Melati Software License.
28   *
29   * Feel free to contact the Developers of Melati (http://melati.org),
30   * if you would like to work out a different arrangement than the options
31   * outlined here.  It is our intention to allow Melati to be used by as
32   * wide an audience as possible.
33   *
34   * This program is distributed in the hope that it will be useful,
35   * but WITHOUT ANY WARRANTY; without even the implied warranty of
36   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   * GNU General Public License for more details.
38   *
39   * Contact details for copyright holder:
40   *
41   *     Tim Joyce <timj At paneris.org>
42   *     http://paneris.org/
43   *     68 Sandbanks Rd, Poole, Dorset. BH14 8BY. UK
44   */
45  
46  package org.melati.test;
47  
48  import java.io.IOException;
49  import java.io.InputStream;
50  import java.io.OutputStream;
51  import java.util.Hashtable;
52  
53  import javax.servlet.ServletException;
54  
55  import org.melati.Melati;
56  import org.melati.MelatiConfig;
57  import org.melati.poem.Database;
58  import org.melati.poem.PoemTask;
59  import org.melati.poem.Table;
60  import org.melati.poem.Capability;
61  import org.melati.poem.AccessToken;
62  import org.melati.poem.AccessPoemException;
63  import org.melati.poem.PoemThread;
64  import org.melati.servlet.MultipartFormDataDecoder;
65  import org.melati.servlet.MultipartFormField;
66  import org.melati.servlet.PoemServlet;
67  import org.melati.util.MelatiBugMelatiException;
68  import org.melati.util.MelatiWriter;
69  
70  /**
71   * Test a Melati configuration which accesses a POEM database 
72   * without using a Template Engine.
73   */
74  public class PoemServletTest extends PoemServlet {
75  
76    private static final long serialVersionUID = -2216872878288661630L;
77  
78    /**
79     * Constructor.
80     */
81     public PoemServletTest() {
82       super();
83     }
84     
85  
86    /**
87     * Normally one would ensure that these settings are present in 
88     * the database, but they are ensured here so that everything 
89     * is in one place.
90     * {@inheritDoc}
91     * @see org.melati.servlet.PoemServlet#prePoemSession(org.melati.Melati)
92     */
93    protected void prePoemSession(Melati melati) throws Exception {
94      final Database db = melati.getDatabase();
95      final MelatiConfig mc = melati.getConfig();
96      db.inSession(AccessToken.root, new PoemTask() {
97        public void run() {
98          db.getSettingTable().
99          ensure("UploadDir", 
100                mc.getStaticURL(), 
101                "Upload Directory",
102                "Directory to upload to");
103         db.getSettingTable().
104         ensure("UploadURL",
105                mc.getStaticURL(), 
106                "Uploaded URL",
107                "URL of uploaded files, defaults to Melati Static ");
108       }
109     });
110     
111   }
112 
113 
114   protected void doPoemRequest(Melati melati)
115      throws ServletException, IOException {
116      String method = melati.getMethod();
117      if (method != null && method.equals("Upload")) {
118        doUpload(melati);
119       return;
120      }
121 
122      melati.getResponse().setContentType("text/html");
123      MelatiWriter output = melati.getWriter();
124 
125      output.write(
126      "<html><head><title>PoemServlet Test</title></head>\n");
127      output.write("<body>\n");
128      output.write("<h2>PoemServlet " +
129      "Test</h2>\n");
130      output.write("<p>This servlet tests your melati/poem configuration. " +
131      "</p>\n");
132      output.write("<p>If you can read this message, it means that you have " +
133                   "successfully created a  POEM session, \n");
134      output.write("using the configurations given in " +
135                   "org.melati.LogicalDatabase.properties. </p>\n");
136      output.write("<p><b>Note</b> that this " +
137                   "servlet does not initialise a template engine.</p>\n");
138      output.write("<h4>The PoemContext</h4>\n");
139      output.write("<h4>The PoemContext enables access to a database, a table, a record and a method.</h4>\n");
140      output.write("<p>Your <b>PoemContext</b> is set up as: " +
141                   melati.getPoemContext() +
142                   ".</p> \n");
143      output.write("<p>Method:" + method + "</p>\n");
144      
145      output.write("<p>\nThe PoemContext can be setup using the servlet's PathInfo.</p>\n");
146      output.write("<ul>\n");
147      output.write("<li>\n");
148      output.write("<a href=" +
149          melati.getZoneURL() +
150          "/org.melati.test.PoemServletTest/" +
151          melati.getPoemContext().getLogicalDatabase() +
152          "/tableinfo/0/View>" + 
153          "tableinfo/0/View" +
154          "</a>)\n");
155      output.write("</li>\n");
156      output.write("<li>\n");
157      output.write("<a href=" +
158          melati.getZoneURL() +
159          "/org.melati.test.PoemServletTest/" +
160          melati.getPoemContext().getLogicalDatabase() +
161          "/columninfo/0/View>" + 
162          "columninfo/0/View" +
163          "</a>)\n");
164      output.write("</li>\n");
165      output.write("<li>\n");
166      output.write("<a href=" +
167          melati.getZoneURL() +
168          "/org.melati.test.PoemServletTest/" +
169          melati.getPoemContext().getLogicalDatabase() +
170          "/user/1/View>user/1/View" +
171          "</a>)\n");
172      output.write("</li>\n");
173      output.write("</ul>\n");
174      output.write("");
175      output.write("<table>");
176      output.write("<tr><th colspan=2>Tables in the Database " + melati.getDatabaseName() + "</th></tr>\n");
177 
178      for (Table<?> t : melati.getDatabase().getDisplayTables()) { 
179        output.write("<tr>\n <td>");
180        output.write(t.getDisplayName());
181        output.write("</td>\n <td>");
182        output.write(t.getDescription());
183        output.write("</td>\n</tr>\n");
184      }
185      output.write("</table>\n");
186 
187 
188      output.write("<h4>File upload</h4>\n");
189      output.write("<p>\n");
190      output.write("A <b>PoemFileDataAdaptor</b> ");
191      output.write("obtains the name of the file upload directory from ");
192      output.write("a setting in the settings table.\n");
193      output.write("</p>\n");
194      
195      output.write(
196          "<form method=\"post\" action=\"Upload" + 
197          "\" enctype=\"multipart/form-data\" target='Upload'>" +
198          p("You can upload a file here:") +
199          "<input type=hidden name='upload' value='yes'>\n" +
200          "<input type=\"file\" name=\"file\" enctype=\"multipart/form-data\">\n" +
201          "<input type=\"submit\" name=\"Submit\" value=\"Upload file\">\n" +
202          getUploadMessage(melati) +
203          "</form>\n");
204      
205      output.write("<h4>Melati Exception handling</h4\n>");
206      output.write("<p>An exception is rendered to the output.</p>\n");     
207      output.write("<p>An access violation provokes a login challenge.</p>\n");     
208      output.write("<p>You curently have the access token " + melati.getUser() + ".</p>\n<");     
209      output.write("<ul>\n");
210      output.write("<li>\n");
211      output.write("<a href='" + 
212              melati.getSameURL() +
213              "/Exception'>Exception</a>\n");
214      output.write("</li>\n");
215      output.write("<li>\n");
216      output.write("<a href='" +
217              melati.getSameURL() +
218              "/AccessPoemException'>Access Poem " +
219      "Exception</a> (requiring you to log-in as an administrator)\n");
220      output.write("</li>\n");
221      output.write("</ul>\n");
222      
223     if (method != null) {
224       if (method.equals("Exception")) 
225         throw new MelatiBugMelatiException("It got caught!");
226       if (method.equals("AccessPoemException")) {
227         Capability admin = PoemThread.database().administerCapability();
228         AccessToken token = PoemThread.accessToken();
229         output.write("<p>You are logged in as "+token+" and have " + admin + " capability.</p>\n");
230         if (!token.givesCapability(admin))
231           throw new AccessPoemException(token, admin);
232       }
233     }
234 
235     output.write("<h3>Further Testing</h3>\n");
236     output.write("<h4>Template Engine Testing</h4>\n");
237     output.write(p("You are currently using: <b>" + 
238     melati.getConfig().getTemplateEngine().getClass().getName() + 
239     "</b>."));
240     output.write(p("You can test your WebMacro installation by clicking <a href=" + 
241     melati.getZoneURL() + 
242     "/org.melati.test.WebmacroStandalone/>WebmacroStandalone</a>"));
243     output.write(p("You can test your Template Engine working with " +
244     "Melati by clicking <a href=" + 
245     melati.getZoneURL() + 
246     "/org.melati.test.TemplateServletTest/" + 
247     melati.getPoemContext().getLogicalDatabase() + 
248     ">" + 
249     "org.melati.test.TemplateServletTest/" + 
250     melati.getPoemContext().getLogicalDatabase() + "</a>"));
251 
252     output.write(p("Make sure the <a href='"+ 
253       melati.getZoneURL() + 
254       "/org.melati.admin.Admin/" + 
255       melati.getPoemContext().getLogicalDatabase() + 
256       "/Main'>Admin System</a> is working." + 
257       "\n"));
258     
259     output.write("</body></html>");
260 
261    
262    }
263 
264   private void doUpload(Melati melati) throws IOException {
265 
266     Hashtable<String, MultipartFormField> fields = null;
267     InputStream in = melati.getRequest().getInputStream();
268     MultipartFormDataDecoder decoder =
269         new MultipartFormDataDecoder(melati,
270             in,
271             melati.getRequest().getContentType(),
272             melati.getConfig().getFormDataAdaptorFactory());
273     fields = decoder.parseData();
274     MultipartFormField field = (MultipartFormField)fields.get("file");
275     byte[] data = field.getData();
276     if (data.length == 0) {
277       melati.getWriter().write(p("No file was uploaded"));
278       return;
279     }
280     melati.getResponse().setContentType(field.getContentType());
281     OutputStream output = melati.getResponse().getOutputStream();
282     output.write(data);
283     output.close();
284     return;
285   }
286   
287 
288   protected String getUploadMessage(Melati melati) {
289     return p("This will save your file in a file at a path specified in the database's Settings table.") + 
290     p("  Try saving a file in your " +
291            "/tmp directory <a href='" + melati.getZoneURL() +
292            "/org.melati.test.ConfigServletTestOverride/'>here</a>.");
293   }
294   
295   private String p(String sentence) { 
296     return "<p>" + sentence + "</p>\n";
297   }
298 
299 }
300 
301 
302 
303 
304 
305