Coverage Report - org.melati.test.PoemServletTest
 
Classes in this File Line Coverage Branch Coverage Complexity
PoemServletTest
100%
100/100
100%
16/16
2.714
PoemServletTest$1
100%
4/4
N/A
2.714
 
 1  
 /*
 2  
  * $Source: /usr/cvsroot/melati/melati/src/site/resources/withWebmacro/org.melati.test.PoemServletTest.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 At 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.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  
  * Test a Melati configuration which accesses a POEM database 
 73  
  * without using a Template Engine.
 74  
  */
 75  
 public class PoemServletTest extends PoemServlet {
 76  
 
 77  
   private static final long serialVersionUID = -2216872878288661630L;
 78  
 
 79  
   /**
 80  
    * Constructor.
 81  
    */
 82  
    public PoemServletTest() {
 83  20
      super();
 84  20
    }
 85  
    
 86  
 
 87  
   /**
 88  
    * Normally one would ensure that these settings are present in 
 89  
    * the database, but they are ensured here so that everything 
 90  
    * is in one place.
 91  
    * {@inheritDoc}
 92  
    * @see org.melati.servlet.PoemServlet#prePoemSession(org.melati.Melati)
 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