1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
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 | |
|
67 | |
|
68 | 6 | 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 | 22 | String method = melati.getMethod(); |
76 | 22 | if (method != null && method.equals("Upload")) { |
77 | 6 | Hashtable<String, MultipartFormField> fields = null; |
78 | 6 | InputStream in = melati.getRequest().getInputStream(); |
79 | 6 | MultipartFormDataDecoder decoder= |
80 | |
new MultipartFormDataDecoder(melati, |
81 | |
in, |
82 | 6 | melati.getRequest().getContentType(), |
83 | 6 | melati.getConfig().getFormDataAdaptorFactory()); |
84 | 6 | fields = decoder.parseData(); |
85 | 6 | MultipartFormField field = (MultipartFormField)fields.get("file"); |
86 | 6 | byte[] data = field.getData(); |
87 | 6 | if (data.length == 0) { |
88 | 2 | melati.getWriter().write("No file was uploaded"); |
89 | 2 | return; |
90 | |
} |
91 | 4 | melati.getResponse().setContentType(field.getContentType()); |
92 | 4 | OutputStream output = melati.getResponse().getOutputStream(); |
93 | 4 | output.write(data); |
94 | 4 | output.close(); |
95 | 4 | return; |
96 | |
} |
97 | |
|
98 | 16 | MelatiConfig config = melati.getConfig(); |
99 | 16 | melati.setResponseContentType("text/html"); |
100 | 16 | MelatiWriter output = melati.getWriter(); |
101 | |
|
102 | 16 | output.write( |
103 | |
"<html><head><title>ConfigServlet Test</title></head>\n"); |
104 | 16 | output.write( |
105 | |
"<body><h2>ConfigServlet Test</h2>\n"); |
106 | 16 | 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 | 16 | output.write( |
116 | |
"<h4>Your Melati is configured with the following properties: " + |
117 | |
"</h4>\n<table>"); |
118 | 32 | output.write( |
119 | |
"<tr><td>AccessHandler</td><td>" + |
120 | 16 | config.getAccessHandler().getClass().getName() + |
121 | |
"</td></tr>\n"); |
122 | 32 | output.write( |
123 | |
"<tr><td>ServletTemplateEngine</td><td>" + |
124 | 16 | config.getTemplateEngine().getClass().getName() + |
125 | |
"</td></tr>\n"); |
126 | 32 | output.write( |
127 | |
"<tr><td>StaticURL</td><td>" + |
128 | 16 | config.getStaticURL() + "</td></tr>\n"); |
129 | 32 | output.write( |
130 | |
"<tr><td>JavascriptLibraryURL</td><td>" + |
131 | 16 | config.getJavascriptLibraryURL() + |
132 | |
"</td></tr>\n"); |
133 | 32 | output.write( |
134 | |
"<tr><td>FormDataAdaptorFactory</td><td>" + |
135 | 16 | config.getFormDataAdaptorFactory().getClass().getName() + |
136 | |
"</td></tr>\n"); |
137 | 32 | output.write( |
138 | |
"<tr><td>Locale</td><td>" + |
139 | 16 | MelatiConfig.getPoemLocale().getClass().getName() + |
140 | |
"</td></tr>\n"); |
141 | 32 | output.write( |
142 | |
"<tr><td>TempletLoader</td><td>" + |
143 | 16 | config.getTempletLoader().getClass().getName() + |
144 | |
"</td></tr>\n"); |
145 | 32 | output.write( |
146 | |
"</table>\n" + |
147 | |
|
148 | |
"<h4>This servlet was called with the following Method (taken from " + |
149 | |
"melati.getMethod()): " + |
150 | 16 | melati.getMethod() + |
151 | |
"</h4>\n"); |
152 | 32 | output.write( |
153 | |
"<h4>Further Testing:</h4>\n" + |
154 | |
"You can test melati Exception handling by " + |
155 | |
"clicking <a href=" + |
156 | 16 | melati.getSameURL() + |
157 | |
"/Exception>Exception</a><br>\n"); |
158 | 32 | output.write( |
159 | |
"You can test melati Redirect " + |
160 | |
"handling by clicking <a href=" + |
161 | 16 | melati.getSameURL() + |
162 | |
"/Redirect>Redirect</a><br>\n"); |
163 | 32 | output.write( |
164 | |
"You can test your " + |
165 | |
"POEM setup (connecting to logical database <tt>melatitest</tt>) by " + |
166 | |
"clicking <a href=" + |
167 | 16 | melati.getZoneURL() + |
168 | |
"/org.melati.test.PoemServletTest/melatitest/>" + |
169 | |
"org.melati.test.PoemServletTest/melatitest/</a><br>\n"); |
170 | 32 | output.write( |
171 | |
"<form method=\"post\" action=\"" + |
172 | 16 | 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 | 16 | getUploadMessage(melati) + |
179 | |
"</form>\n"); |
180 | 16 | if (method != null) { |
181 | 8 | if (method.equals("Exception")) |
182 | 2 | throw new MelatiBugMelatiException("It got caught!"); |
183 | 6 | if (method.equals("Redirect")) { |
184 | 2 | melati.getResponse().sendRedirect("http://www.melati.org"); |
185 | 2 | return; |
186 | |
} |
187 | |
} |
188 | 12 | } |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
protected MelatiConfig melatiConfig() { |
194 | 6 | MelatiConfig config = super.melatiConfig(); |
195 | 6 | config.setFormDataAdaptorFactory(new MemoryFormDataAdaptorFactory()); |
196 | 6 | return config; |
197 | |
} |
198 | |
|
199 | |
protected String getUploadMessage(Melati melati) { |
200 | 15 | return "This will save your file in memory. Try saving a file in your " + |
201 | 15 | "/tmp directory <a href='" + melati.getZoneURL() + |
202 | |
"/org.melati.test.ConfigServletTestOverride/'>here</a>."; |
203 | |
} |
204 | |
|
205 | |
} |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|