| 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.IOException; |
| 49 | |
import java.io.InputStream; |
| 50 | |
import java.io.OutputStream; |
| 51 | |
import java.util.Enumeration; |
| 52 | |
import java.util.Hashtable; |
| 53 | |
|
| 54 | |
import javax.servlet.ServletException; |
| 55 | |
|
| 56 | |
import org.melati.Melati; |
| 57 | |
import org.melati.MelatiConfig; |
| 58 | |
import org.melati.poem.Database; |
| 59 | |
import org.melati.poem.PoemTask; |
| 60 | |
import org.melati.poem.Table; |
| 61 | |
import org.melati.poem.Capability; |
| 62 | |
import org.melati.poem.AccessToken; |
| 63 | |
import org.melati.poem.AccessPoemException; |
| 64 | |
import org.melati.poem.PoemThread; |
| 65 | |
import org.melati.servlet.MultipartFormDataDecoder; |
| 66 | |
import org.melati.servlet.MultipartFormField; |
| 67 | |
import org.melati.servlet.PoemServlet; |
| 68 | |
import org.melati.util.MelatiBugMelatiException; |
| 69 | |
import org.melati.util.MelatiWriter; |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
public class PoemServletTest extends PoemServlet { |
| 76 | |
|
| 77 | |
private static final long serialVersionUID = -2216872878288661630L; |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
public PoemServletTest() { |
| 83 | 20 | super(); |
| 84 | 20 | } |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
protected void prePoemSession(Melati melati) throws Exception { |
| 95 | 58 | final Database db = melati.getDatabase(); |
| 96 | 58 | final MelatiConfig mc = melati.getConfig(); |
| 97 | 58 | db.inSession(AccessToken.root, new PoemTask() { |
| 98 | |
public void run() { |
| 99 | 58 | db.getSettingTable(). |
| 100 | |
ensure("UploadDir", |
| 101 | |
mc.getStaticURL(), |
| 102 | |
"Upload Directory", |
| 103 | |
"Directory to upload to"); |
| 104 | 58 | db.getSettingTable(). |
| 105 | |
ensure("UploadURL", |
| 106 | |
mc.getStaticURL(), |
| 107 | |
"Uploaded URL", |
| 108 | |
"URL of uploaded files, defaults to Melati Static "); |
| 109 | 58 | } |
| 110 | |
}); |
| 111 | |
|
| 112 | 58 | } |
| 113 | |
|
| 114 | |
|
| 115 | |
protected void doPoemRequest(Melati melati) |
| 116 | |
throws ServletException, IOException { |
| 117 | 58 | String method = melati.getMethod(); |
| 118 | 58 | if (method != null && method.equals("Upload")) { |
| 119 | 12 | doUpload(melati); |
| 120 | 12 | return; |
| 121 | |
} |
| 122 | |
|
| 123 | 46 | melati.getResponse().setContentType("text/html"); |
| 124 | 46 | MelatiWriter output = melati.getWriter(); |
| 125 | |
|
| 126 | 46 | output.write( |
| 127 | |
"<html><head><title>PoemServlet Test</title></head>\n"); |
| 128 | 46 | output.write("<body>\n"); |
| 129 | 46 | output.write("<h2>PoemServlet " + |
| 130 | |
"Test</h2>\n"); |
| 131 | 46 | output.write("<p>This servlet tests your melati/poem configuration. " + |
| 132 | |
"</p>\n"); |
| 133 | 46 | output.write("<p>If you can read this message, it means that you have " + |
| 134 | |
"successfully created a POEM session, \n"); |
| 135 | 46 | output.write("using the configurations given in " + |
| 136 | |
"org.melati.LogicalDatabase.properties. </p>\n"); |
| 137 | 46 | output.write("<p><b>Note</b> that this " + |
| 138 | |
"servlet does not initialise a template engine.</p>\n"); |
| 139 | 46 | output.write("<h4>The PoemContext</h4>\n"); |
| 140 | 46 | output.write("<h4>The PoemContext enables access to a database, a table, a record and a method.</h4>\n"); |
| 141 | 46 | output.write("<p>Your <b>PoemContext</b> is set up as: " + |
| 142 | |
melati.getPoemContext() + |
| 143 | |
".</p> \n"); |
| 144 | 46 | output.write("<p>Method:" + method + "</p>\n"); |
| 145 | |
|
| 146 | 46 | output.write("<p>\nThe PoemContext can be setup using the servlet's PathInfo.</p>\n"); |
| 147 | 46 | output.write("<ul>\n"); |
| 148 | 46 | output.write("<li>\n"); |
| 149 | 46 | output.write("<a href=" + |
| 150 | |
melati.getZoneURL() + |
| 151 | |
"/org.melati.test.PoemServletTest/" + |
| 152 | |
melati.getPoemContext().getLogicalDatabase() + |
| 153 | |
"/tableinfo/0/View>" + |
| 154 | |
"tableinfo/0/View" + |
| 155 | |
"</a>)\n"); |
| 156 | 46 | output.write("</li>\n"); |
| 157 | 46 | output.write("<li>\n"); |
| 158 | 46 | output.write("<a href=" + |
| 159 | |
melati.getZoneURL() + |
| 160 | |
"/org.melati.test.PoemServletTest/" + |
| 161 | |
melati.getPoemContext().getLogicalDatabase() + |
| 162 | |
"/columninfo/0/View>" + |
| 163 | |
"columninfo/0/View" + |
| 164 | |
"</a>)\n"); |
| 165 | 46 | output.write("</li>\n"); |
| 166 | 46 | output.write("<li>\n"); |
| 167 | 46 | output.write("<a href=" + |
| 168 | |
melati.getZoneURL() + |
| 169 | |
"/org.melati.test.PoemServletTest/" + |
| 170 | |
melati.getPoemContext().getLogicalDatabase() + |
| 171 | |
"/user/1/View>user/1/View" + |
| 172 | |
"</a>)\n"); |
| 173 | 46 | output.write("</li>\n"); |
| 174 | 46 | output.write("</ul>\n"); |
| 175 | 46 | output.write(""); |
| 176 | 46 | output.write("<table>"); |
| 177 | 46 | output.write("<tr><th colspan=2>Tables in the Database " + melati.getDatabaseName() + "</th></tr>\n"); |
| 178 | |
|
| 179 | 46 | for (Enumeration e = melati.getDatabase().getDisplayTables(); |
| 180 | 532 | e.hasMoreElements();) { |
| 181 | 486 | Table t = (Table)e.nextElement(); |
| 182 | 486 | output.write("<tr>\n <td>"); |
| 183 | 486 | output.write(t.getDisplayName()); |
| 184 | 486 | output.write("</td>\n <td>"); |
| 185 | 486 | output.write(t.getDescription()); |
| 186 | 486 | output.write("</td>\n</tr>\n"); |
| 187 | 486 | } |
| 188 | 46 | output.write("</table>\n"); |
| 189 | |
|
| 190 | |
|
| 191 | 46 | output.write("<h4>File upload</h4>\n"); |
| 192 | 46 | output.write("<p>\n"); |
| 193 | 46 | output.write("A <b>PoemFileDataAdaptor</b> "); |
| 194 | 46 | output.write("obtains the name of the file upload directory from "); |
| 195 | 46 | output.write("a setting in the settings table.\n"); |
| 196 | 46 | output.write("</p>\n"); |
| 197 | |
|
| 198 | 46 | output.write( |
| 199 | |
"<form method=\"post\" action=\"Upload" + |
| 200 | |
"\" enctype=\"multipart/form-data\" target='Upload'>" + |
| 201 | |
p("You can upload a file here:") + |
| 202 | |
"<input type=hidden name='upload' value='yes'>\n" + |
| 203 | |
"<input type=\"file\" name=\"file\" enctype=\"multipart/form-data\">\n" + |
| 204 | |
"<input type=\"submit\" name=\"Submit\" value=\"Upload file\">\n" + |
| 205 | |
getUploadMessage(melati) + |
| 206 | |
"</form>\n"); |
| 207 | |
|
| 208 | 46 | output.write("<h4>Melati Exception handling</h4\n>"); |
| 209 | 46 | output.write("<p>An exception is rendered to the output.</p>\n"); |
| 210 | 46 | output.write("<p>An access violation provokes a login challenge.</p>\n"); |
| 211 | 46 | output.write("<p>You curently have the access token " + melati.getUser() + ".</p>\n<"); |
| 212 | 46 | output.write("<ul>\n"); |
| 213 | 46 | output.write("<li>\n"); |
| 214 | 46 | output.write("<a href='" + |
| 215 | |
melati.getSameURL() + |
| 216 | |
"/Exception'>Exception</a>\n"); |
| 217 | 46 | output.write("</li>\n"); |
| 218 | 46 | output.write("<li>\n"); |
| 219 | 46 | output.write("<a href='" + |
| 220 | |
melati.getSameURL() + |
| 221 | |
"/AccessPoemException'>Access Poem " + |
| 222 | |
"Exception</a> (requiring you to log-in as an administrator)\n"); |
| 223 | 46 | output.write("</li>\n"); |
| 224 | 46 | output.write("</ul>\n"); |
| 225 | |
|
| 226 | 46 | if (method != null) { |
| 227 | 20 | if (method.equals("Exception")) |
| 228 | 4 | throw new MelatiBugMelatiException("It got caught!"); |
| 229 | 16 | if (method.equals("AccessPoemException")) { |
| 230 | 8 | Capability admin = PoemThread.database().administerCapability(); |
| 231 | 8 | AccessToken token = PoemThread.accessToken(); |
| 232 | 8 | output.write("<p>You are logged in as "+token+" and have " + admin + " capability.</p>\n"); |
| 233 | 8 | if (!token.givesCapability(admin)) |
| 234 | 4 | throw new AccessPoemException(token, admin); |
| 235 | |
} |
| 236 | |
} |
| 237 | |
|
| 238 | 38 | output.write("<h3>Further Testing</h3>\n"); |
| 239 | 38 | output.write("<h4>Template Engine Testing</h4>\n"); |
| 240 | 38 | output.write(p("You are currently using: <b>" + |
| 241 | |
melati.getConfig().getTemplateEngine().getClass().getName() + |
| 242 | |
"</b>.")); |
| 243 | 38 | output.write(p("You can test your WebMacro installation by clicking <a href=" + |
| 244 | |
melati.getZoneURL() + |
| 245 | |
"/org.melati.test.WebmacroStandalone/>WebmacroStandalone</a>")); |
| 246 | 38 | output.write(p("You can test your Template Engine working with " + |
| 247 | |
"Melati by clicking <a href=" + |
| 248 | |
melati.getZoneURL() + |
| 249 | |
"/org.melati.test.TemplateServletTest/" + |
| 250 | |
melati.getPoemContext().getLogicalDatabase() + |
| 251 | |
">" + |
| 252 | |
"org.melati.test.TemplateServletTest/" + |
| 253 | |
melati.getPoemContext().getLogicalDatabase() + "</a>")); |
| 254 | |
|
| 255 | 38 | output.write(p("Make sure the <a href='"+ |
| 256 | |
melati.getZoneURL() + |
| 257 | |
"/org.melati.admin.Admin/" + |
| 258 | |
melati.getPoemContext().getLogicalDatabase() + |
| 259 | |
"/Main'>Admin System</a> is working." + |
| 260 | |
"\n")); |
| 261 | |
|
| 262 | 38 | output.write("</body></html>"); |
| 263 | |
|
| 264 | |
|
| 265 | 38 | } |
| 266 | |
|
| 267 | |
private void doUpload(Melati melati) throws IOException { |
| 268 | |
|
| 269 | 12 | Hashtable fields = null; |
| 270 | 12 | InputStream in = melati.getRequest().getInputStream(); |
| 271 | 12 | MultipartFormDataDecoder decoder = |
| 272 | |
new MultipartFormDataDecoder(melati, |
| 273 | |
in, |
| 274 | |
melati.getRequest().getContentType(), |
| 275 | |
melati.getConfig().getFormDataAdaptorFactory()); |
| 276 | 12 | fields = decoder.parseData(); |
| 277 | 12 | MultipartFormField field = (MultipartFormField)fields.get("file"); |
| 278 | 12 | byte[] data = field.getData(); |
| 279 | 12 | if (data.length == 0) { |
| 280 | 4 | melati.getWriter().write(p("No file was uploaded")); |
| 281 | 4 | return; |
| 282 | |
} |
| 283 | 8 | melati.getResponse().setContentType(field.getContentType()); |
| 284 | 8 | OutputStream output = melati.getResponse().getOutputStream(); |
| 285 | 8 | output.write(data); |
| 286 | 8 | output.close(); |
| 287 | 8 | return; |
| 288 | |
} |
| 289 | |
|
| 290 | |
|
| 291 | |
protected String getUploadMessage(Melati melati) { |
| 292 | 46 | return p("This will save your file in a file at a path specified in the database's Settings table.") + |
| 293 | |
p(" Try saving a file in your " + |
| 294 | |
"/tmp directory <a href='" + melati.getZoneURL() + |
| 295 | |
"/org.melati.test.ConfigServletTestOverride/'>here</a>."); |
| 296 | |
} |
| 297 | |
|
| 298 | |
private String p(String sentence) { |
| 299 | 294 | return "<p>" + sentence + "</p>\n"; |
| 300 | |
} |
| 301 | |
|
| 302 | |
} |
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|