Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TemplateServletTest |
|
| 4.333333333333333;4.333 |
1 | /* | |
2 | * $Source: /usr/cvsroot/melati/melati/src/site/resources/withWebmacro/org.melati.test.TemplateServletTest.html,v $ | |
3 | * $Revision: 1.1 $ | |
4 | * | |
5 | * Copyright (C) 2000 Tim Joyce | |
6 | * | |
7 | * Part of Melati (http://melati.org), a framework for the rapid | |
8 | * development of clean, maintainable web applications. | |
9 | * | |
10 | * Melati is free software; Permission is granted to copy, distribute | |
11 | * and/or modify this software under the terms either: | |
12 | * | |
13 | * a) the GNU General Public License as published by the Free Software | |
14 | * Foundation; either version 2 of the License, or (at your option) | |
15 | * any later version, | |
16 | * | |
17 | * or | |
18 | * | |
19 | * b) any version of the Melati Software License, as published | |
20 | * at http://melati.org | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License and | |
23 | * the Melati Software License along with this program; | |
24 | * if not, write to the Free Software Foundation, Inc., | |
25 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the | |
26 | * GNU General Public License and visit http://melati.org to obtain the | |
27 | * Melati Software License. | |
28 | * | |
29 | * Feel free to contact the Developers of Melati (http://melati.org), | |
30 | * if you would like to work out a different arrangement than the options | |
31 | * outlined here. It is our intention to allow Melati to be used by as | |
32 | * wide an audience as possible. | |
33 | * | |
34 | * This program is distributed in the hope that it will be useful, | |
35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
37 | * GNU General Public License for more details. | |
38 | * | |
39 | * Contact details for copyright holder: | |
40 | * | |
41 | * Tim Joyce <timj@paneris.org> | |
42 | * http://paneris.org/ | |
43 | * 68 Sandbanks Rd, Poole, Dorset. BH14 8BY. UK | |
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 | * Test a Melati configuration with a Template Engine and a | |
63 | * temporary file form data adaptor. | |
64 | * | |
65 | * @author Tim Joyce | |
66 | * $Revision: 1.1 $ | |
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 | // No need to explicitly do this as it is the default, other than test coverage | |
104 | 40 | melati.setPropagateExceptionHandling(); |
105 | ||
106 | 46 | return("org/melati/test/TemplateServletTest"); |
107 | } | |
108 | ||
109 | /** | |
110 | * Set up the POEM context so we don't have to specify the | |
111 | * logicaldatabase on the pathinfo. | |
112 | * | |
113 | * Useful when writing appications where you are typically only accessing | |
114 | * a single database. | |
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 | * Set the form data adaptor. | |
127 | * | |
128 | * @see org.melati.servlet.ConfigServlet#melatiConfig() | |
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 |