| 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 | 12 | 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 | 44 | String method = melati.getMethod(); |
| 76 | 44 | if (method != null && method.equals("Upload")) { |
| 77 | 12 | Hashtable fields = null; |
| 78 | 12 | InputStream in = melati.getRequest().getInputStream(); |
| 79 | 12 | MultipartFormDataDecoder decoder= |
| 80 | |
new MultipartFormDataDecoder(melati, |
| 81 | |
in, |
| 82 | |
melati.getRequest().getContentType(), |
| 83 | |
melati.getConfig().getFormDataAdaptorFactory()); |
| 84 | 12 | fields = decoder.parseData(); |
| 85 | 12 | MultipartFormField field = (MultipartFormField)fields.get("file"); |
| 86 | 12 | byte[] data = field.getData(); |
| 87 | 12 | if (data.length == 0) { |
| 88 | 4 | melati.getWriter().write("No file was uploaded"); |
| 89 | 4 | return; |
| 90 | |
} |
| 91 | 8 | melati.getResponse().setContentType(field.getContentType()); |
| 92 | 8 | OutputStream output = melati.getResponse().getOutputStream(); |
| 93 | 8 | output.write(data); |
| 94 | 8 | output.close(); |
| 95 | 8 | return; |
| 96 | |
} |
| 97 | |
|
| 98 | 32 | MelatiConfig config = melati.getConfig(); |
| 99 | 32 | melati.setResponseContentType("text/html"); |
| 100 | 32 | MelatiWriter output = melati.getWriter(); |
| 101 | |
|
| 102 | 32 | output.write( |
| 103 | |
"<html><head><title>ConfigServlet Test</title></head>\n"); |
| 104 | 32 | output.write( |
| 105 | |
"<body><h2>ConfigServlet Test</h2>\n"); |
| 106 | 32 | 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 | 32 | 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 | |
config.getAccessHandler().getClass().getName() + |
| 121 | |
"</td></tr>\n"); |
| 122 | 32 | output.write( |
| 123 | |
"<tr><td>ServletTemplateEngine</td><td>" + |
| 124 | |
config.getTemplateEngine().getClass().getName() + |
| 125 | |
"</td></tr>\n"); |
| 126 | 32 | output.write( |
| 127 | |
"<tr><td>StaticURL</td><td>" + |
| 128 | |
config.getStaticURL() + "</td></tr>\n"); |
| 129 | 32 | output.write( |
| 130 | |
"<tr><td>JavascriptLibraryURL</td><td>" + |
| 131 | |
config.getJavascriptLibraryURL() + |
| 132 | |
"</td></tr>\n"); |
| 133 | 32 | output.write( |
| 134 | |
"<tr><td>FormDataAdaptorFactory</td><td>" + |
| 135 | |
config.getFormDataAdaptorFactory().getClass().getName() + |
| 136 | |
"</td></tr>\n"); |
| 137 | 32 | output.write( |
| 138 | |
"<tr><td>Locale</td><td>" + |
| 139 | |
MelatiConfig.getPoemLocale().getClass().getName() + |
| 140 | |
"</td></tr>\n"); |
| 141 | 32 | output.write( |
| 142 | |
"<tr><td>TempletLoader</td><td>" + |
| 143 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | 32 | if (method != null) { |
| 181 | 16 | if (method.equals("Exception")) |
| 182 | 4 | throw new MelatiBugMelatiException("It got caught!"); |
| 183 | 12 | if (method.equals("Redirect")) { |
| 184 | 4 | melati.getResponse().sendRedirect("http://www.melati.org"); |
| 185 | 4 | return; |
| 186 | |
} |
| 187 | |
} |
| 188 | 24 | } |
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
protected MelatiConfig melatiConfig() { |
| 194 | 12 | MelatiConfig config = super.melatiConfig(); |
| 195 | 12 | config.setFormDataAdaptorFactory(new MemoryFormDataAdaptorFactory()); |
| 196 | 12 | return config; |
| 197 | |
} |
| 198 | |
|
| 199 | |
protected String getUploadMessage(Melati melati) { |
| 200 | 30 | 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 | |
|