View Javadoc
1   /*
2    * $Source$
3    * $Revision$
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$
67   */
68  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      String method = melati.getMethod();
76      templateContext.put("RestrictedAccessObject", 
77              new RestrictedAccessObject());
78      templateContext.put("servletName", this.getClass().getName());
79      if (method != null) {
80  
81        if (method.equals("Upload")) {
82          MultipartFormField field = templateContext.getMultipartFormField("file");
83          byte[] data = field.getData();
84          melati.getResponse().setContentType(field.getContentType());
85          OutputStream output = melati.getResponse().getOutputStream();
86          output.write(data);
87          output.close();
88          return null;
89        }
90  
91        if (method.equals("Redirect")) {
92          melati.getResponse().sendRedirect("http://www.melati.org");
93          return null;
94        }
95        if (method.equals("Exception")) {  
96  
97          throw new MelatiBugMelatiException("It got caught!");
98        }
99      }
100     if (Form.getFieldNulled(melati.getServletTemplateContext(), "passback") != null)
101       melati.setPassbackExceptionHandling();
102     else
103       // No need to explicitly do this as it is the default, other than test coverage
104       melati.setPropagateExceptionHandling(); 
105 
106     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 applications where you are typically only accessing
114  * a single database.
115  */
116   protected PoemContext poemContext(Melati melati)
117   throws PathInfoException {
118     PoemContext pc = super.poemContext(melati);
119     if (pc.getLogicalDatabase().equals(""))
120       pc = poemContextWithLDB(melati,"melatitest");
121     return pc;
122   }
123 
124   /** 
125    * Set the form data adaptor.
126    * 
127    * @see org.melati.servlet.ConfigServlet#melatiConfig()
128    */
129   protected MelatiConfig melatiConfig() {
130     MelatiConfig config = super.melatiConfig();
131     TemporaryFileFormDataAdaptorFactory factory = 
132         new TemporaryFileFormDataAdaptorFactory();
133     config.setFormDataAdaptorFactory(factory);
134     return config;
135   }
136   
137 }
138 
139 
140 
141 
142 
143