| 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.OutputStream; |
| 49 | |
|
| 50 | |
import org.melati.servlet.Form; |
| 51 | |
import org.melati.servlet.TemplateServlet; |
| 52 | |
import org.melati.servlet.TemporaryFileFormDataAdaptorFactory; |
| 53 | |
import org.melati.Melati; |
| 54 | |
import org.melati.MelatiConfig; |
| 55 | |
import org.melati.servlet.PathInfoException; |
| 56 | |
import org.melati.PoemContext; |
| 57 | |
import org.melati.servlet.MultipartFormField; |
| 58 | |
import org.melati.template.ServletTemplateContext; |
| 59 | |
import org.melati.util.MelatiBugMelatiException; |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | 10 | public class TemplateServletTest extends TemplateServlet { |
| 69 | |
private static final long serialVersionUID = 1L; |
| 70 | |
|
| 71 | |
protected String doTemplateRequest(Melati melati, |
| 72 | |
ServletTemplateContext templateContext) |
| 73 | |
throws Exception { |
| 74 | |
|
| 75 | 62 | String method = melati.getMethod(); |
| 76 | 62 | templateContext.put("RestrictedAccessObject", |
| 77 | |
new RestrictedAccessObject()); |
| 78 | 62 | templateContext.put("servletName", this.getClass().getName()); |
| 79 | 62 | if (method != null) { |
| 80 | |
|
| 81 | 20 | if (method.equals("Upload")) { |
| 82 | 8 | MultipartFormField field = templateContext.getMultipartFormField("file"); |
| 83 | 8 | byte[] data = field.getData(); |
| 84 | 8 | melati.getResponse().setContentType(field.getContentType()); |
| 85 | 8 | OutputStream output = melati.getResponse().getOutputStream(); |
| 86 | 8 | output.write(data); |
| 87 | 8 | output.close(); |
| 88 | 8 | return null; |
| 89 | |
} |
| 90 | |
|
| 91 | 12 | if (method.equals("Redirect")) { |
| 92 | 4 | melati.getResponse().sendRedirect("http://www.melati.org"); |
| 93 | 4 | return null; |
| 94 | |
} |
| 95 | 8 | if (method.equals("Exception")) { |
| 96 | |
|
| 97 | 4 | throw new MelatiBugMelatiException("It got caught!"); |
| 98 | |
} |
| 99 | |
} |
| 100 | 46 | if (Form.getFieldNulled(melati.getServletTemplateContext(), "passback") != null) |
| 101 | 6 | melati.setPassbackExceptionHandling(); |
| 102 | |
else |
| 103 | |
|
| 104 | 40 | melati.setPropagateExceptionHandling(); |
| 105 | |
|
| 106 | 46 | return("org/melati/test/TemplateServletTest"); |
| 107 | |
} |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
protected PoemContext poemContext(Melati melati) |
| 117 | |
throws PathInfoException { |
| 118 | 62 | String[] parts = melati.getPathInfoParts(); |
| 119 | 62 | if (parts.length == 0) |
| 120 | 6 | return poemContextWithLDB(melati,"melatitest"); |
| 121 | |
else |
| 122 | 56 | return super.poemContext(melati); |
| 123 | |
} |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
protected MelatiConfig melatiConfig() { |
| 131 | 10 | MelatiConfig config = super.melatiConfig(); |
| 132 | 10 | TemporaryFileFormDataAdaptorFactory factory = |
| 133 | |
new TemporaryFileFormDataAdaptorFactory(); |
| 134 | 10 | config.setFormDataAdaptorFactory(factory); |
| 135 | 10 | return config; |
| 136 | |
} |
| 137 | |
|
| 138 | |
} |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|