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@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.InputStream;
49  import java.io.OutputStream;
50  import java.io.IOException;
51  
52  import java.util.Hashtable;
53  
54  import javax.servlet.ServletException;
55  
56  import org.melati.servlet.ConfigServlet;
57  import org.melati.servlet.MemoryFormDataAdaptorFactory;
58  import org.melati.servlet.MultipartFormField;
59  import org.melati.servlet.MultipartFormDataDecoder;
60  import org.melati.Melati;
61  import org.melati.MelatiConfig;
62  import org.melati.util.MelatiBugMelatiException;
63  import org.melati.util.MelatiWriter;
64  
65  /**
66   * Test a Melati configuration without using a Template Engine.
67   */
68  public class ConfigServletTest extends ConfigServlet {
69  
70    private static final long serialVersionUID = 5538437218064525327L;
71  
72    protected void doConfiguredRequest(Melati melati)
73        throws ServletException, IOException {
74  
75      String method = melati.getMethod();
76      if (method != null && method.equals("Upload")) {
77        Hashtable<String, MultipartFormField> fields = null;
78        InputStream in = melati.getRequest().getInputStream();
79        MultipartFormDataDecoder decoder=
80          new MultipartFormDataDecoder(melati,
81              in,
82              melati.getRequest().getContentType(),
83              melati.getConfig().getFormDataAdaptorFactory());
84        fields = decoder.parseData();
85        MultipartFormField field = (MultipartFormField)fields.get("file");
86        byte[] data = field.getData();
87        if (data.length == 0) {
88          melati.getWriter().write("No file was uploaded");
89          return;
90        }
91        melati.getResponse().setContentType(field.getContentType());
92        OutputStream output = melati.getResponse().getOutputStream();
93        output.write(data);
94        output.close();
95        return;
96      }
97  
98      MelatiConfig config = melati.getConfig();
99      melati.setResponseContentType("text/html");
100     MelatiWriter output = melati.getWriter();
101 
102     output.write(
103     "<html><head><title>ConfigServlet Test</title></head>\n");
104     output.write(
105     "<body><h2>ConfigServlet Test</h2>\n");
106     output.write(
107     "<p>This servlet tests your basic melati " +
108     "configuration. <br>\n" +
109     "If you can read this message, it means that you have " +
110     "successfully created a Melati using the configuration " +
111     "given in org.melati.MelatiConfig.properties.<br>\n"+
112     "Please note that this " +
113     "servlet does not construct a POEM session or initialise a template " +
114     "engine.</p>\n");
115     output.write(
116     "<h4>Your Melati is configured with the following properties: " +
117     "</h4>\n<table>");
118     output.write(
119     "<tr><td>AccessHandler</td><td>" +
120     config.getAccessHandler().getClass().getName() +
121     "</td></tr>\n");
122     output.write(
123     "<tr><td>ServletTemplateEngine</td><td>" +
124     config.getTemplateEngine().getClass().getName() +
125     "</td></tr>\n");
126     output.write(
127     "<tr><td>StaticURL</td><td>" +
128     config.getStaticURL() + "</td></tr>\n");
129     output.write(
130     "<tr><td>JavascriptLibraryURL</td><td>" +
131     config.getJavascriptLibraryURL() +
132     "</td></tr>\n");
133     output.write(
134     "<tr><td>FormDataAdaptorFactory</td><td>" +
135     config.getFormDataAdaptorFactory().getClass().getName() +
136     "</td></tr>\n");
137     output.write(
138     "<tr><td>Locale</td><td>" +
139     MelatiConfig.getPoemLocale().getClass().getName() +
140     "</td></tr>\n");
141     output.write(
142     "<tr><td>TempletLoader</td><td>" +
143     config.getTempletLoader().getClass().getName() +
144     "</td></tr>\n");
145     output.write(
146     "</table>\n" +
147 
148     "<h4>This servlet was called with the following Method (taken from " +
149     "melati.getMethod()): " + 
150     melati.getMethod() + 
151     "</h4>\n");
152     output.write(
153     "<h4>Further Testing:</h4>\n" + 
154     "You can test melati Exception handling by " +
155     "clicking <a href=" + 
156     melati.getSameURL() + 
157     "/Exception>Exception</a><br>\n");
158     output.write(
159     "You can test melati Redirect " +
160     "handling by clicking <a href=" +
161     melati.getSameURL() + 
162     "/Redirect>Redirect</a><br>\n");
163     output.write(
164     "You can test your " +
165     "POEM setup (connecting to logical database <tt>melatitest</tt>) by " +
166     "clicking <a href=" + 
167     melati.getZoneURL() + 
168     "/org.melati.test.PoemServletTest/melatitest/>" +
169     "org.melati.test.PoemServletTest/melatitest/</a><br>\n");
170     output.write(
171     "<form method=\"post\" action=\"" + 
172     melati.getSameURL() + 
173     "/Upload\" enctype=\"multipart/form-data\" target='Upload'>" +
174     "You can upload a file here:<br>\n" +
175     "<input type=hidden name='upload' value='yes'>" +
176     "<input type=\"file\" name=\"file\" enctype=\"multipart/form-data\">" +
177     "<input type=\"submit\" name=\"Submit\" value=\"Upload file\"><br>" +
178     getUploadMessage(melati) +
179     "</form>\n");
180     if (method != null) {
181       if (method.equals("Exception")) 
182         throw new MelatiBugMelatiException("It got caught!");
183       if (method.equals("Redirect")) {
184         melati.getResponse().sendRedirect("http://www.melati.org");
185         return;
186       }
187     }
188   }
189 
190  /**
191   * Demonstrates how to use a different melati configuration.
192   */
193   protected MelatiConfig melatiConfig() {
194     MelatiConfig config = super.melatiConfig();
195     config.setFormDataAdaptorFactory(new MemoryFormDataAdaptorFactory());
196     return config;
197   }
198 
199   protected String getUploadMessage(Melati melati) {
200     return "This will save your file in memory. Try saving a file in your " +
201            "/tmp directory <a href='" + melati.getZoneURL() +
202            "/org.melati.test.ConfigServletTestOverride/'>here</a>.";
203   }
204 
205 }
206 
207 
208 
209 
210 
211