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 | 5 | 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 | 31 | String method = melati.getMethod(); |
76 | 31 | templateContext.put("RestrictedAccessObject", |
77 | |
new RestrictedAccessObject()); |
78 | 31 | templateContext.put("servletName", this.getClass().getName()); |
79 | 31 | if (method != null) { |
80 | |
|
81 | 10 | if (method.equals("Upload")) { |
82 | 4 | MultipartFormField field = templateContext.getMultipartFormField("file"); |
83 | 4 | byte[] data = field.getData(); |
84 | 4 | melati.getResponse().setContentType(field.getContentType()); |
85 | 4 | OutputStream output = melati.getResponse().getOutputStream(); |
86 | 4 | output.write(data); |
87 | 4 | output.close(); |
88 | 4 | return null; |
89 | |
} |
90 | |
|
91 | 6 | if (method.equals("Redirect")) { |
92 | 2 | melati.getResponse().sendRedirect("http://www.melati.org"); |
93 | 2 | return null; |
94 | |
} |
95 | 4 | if (method.equals("Exception")) { |
96 | |
|
97 | 2 | throw new MelatiBugMelatiException("It got caught!"); |
98 | |
} |
99 | |
} |
100 | 23 | if (Form.getFieldNulled(melati.getServletTemplateContext(), "passback") != null) |
101 | 3 | melati.setPassbackExceptionHandling(); |
102 | |
else |
103 | |
|
104 | 20 | melati.setPropagateExceptionHandling(); |
105 | |
|
106 | 23 | return("org/melati/test/TemplateServletTest"); |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
protected PoemContext poemContext(Melati melati) |
117 | |
throws PathInfoException { |
118 | 31 | String[] parts = melati.getPathInfoParts(); |
119 | 31 | if (parts.length == 0) |
120 | 3 | return poemContextWithLDB(melati,"melatitest"); |
121 | |
else |
122 | 28 | return super.poemContext(melati); |
123 | |
} |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
protected MelatiConfig melatiConfig() { |
131 | 5 | MelatiConfig config = super.melatiConfig(); |
132 | 5 | TemporaryFileFormDataAdaptorFactory factory = |
133 | |
new TemporaryFileFormDataAdaptorFactory(); |
134 | 5 | config.setFormDataAdaptorFactory(factory); |
135 | 5 | return config; |
136 | |
} |
137 | |
|
138 | |
} |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|