Coverage Report - org.melati.test.PoemServletTest
 
Classes in this File Line Coverage Branch Coverage Complexity
PoemServletTest
100%
120/120
100%
16/16
2.714
PoemServletTest$1
100%
8/8
N/A
2.714
 
 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 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.Hashtable;
 52  
 
 53  
 import javax.servlet.ServletException;
 54  
 
 55  
 import org.melati.Melati;
 56  
 import org.melati.MelatiConfig;
 57  
 import org.melati.poem.Database;
 58  
 import org.melati.poem.PoemTask;
 59  
 import org.melati.poem.Table;
 60  
 import org.melati.poem.Capability;
 61  
 import org.melati.poem.AccessToken;
 62  
 import org.melati.poem.AccessPoemException;
 63  
 import org.melati.poem.PoemThread;
 64  
 import org.melati.servlet.MultipartFormDataDecoder;
 65  
 import org.melati.servlet.MultipartFormField;
 66  
 import org.melati.servlet.PoemServlet;
 67  
 import org.melati.util.MelatiBugMelatiException;
 68  
 import org.melati.util.MelatiWriter;
 69  
 
 70  
 /**
 71  
  * Test a Melati configuration which accesses a POEM database 
 72  
  * without using a Template Engine.
 73  
  */
 74  
 public class PoemServletTest extends PoemServlet {
 75  
 
 76  
   private static final long serialVersionUID = -2216872878288661630L;
 77  
 
 78  
   /**
 79  
    * Constructor.
 80  
    */
 81  
    public PoemServletTest() {
 82  10
      super();
 83  10
    }
 84  
    
 85  
 
 86  
   /**
 87  
    * Normally one would ensure that these settings are present in 
 88  
    * the database, but they are ensured here so that everything 
 89  
    * is in one place.
 90  
    * {@inheritDoc}
 91  
    * @see org.melati.servlet.PoemServlet#prePoemSession(org.melati.Melati)
 92  
    */
 93  
   protected void prePoemSession(Melati melati) throws Exception {
 94  29
     final Database db = melati.getDatabase();
 95  29
     final MelatiConfig mc = melati.getConfig();
 96  29
     db.inSession(AccessToken.root, new PoemTask() {
 97  
       public void run() {
 98  29
         db.getSettingTable().
 99  29
         ensure("UploadDir", 
 100  29
                mc.getStaticURL(), 
 101  
                "Upload Directory",
 102  
                "Directory to upload to");
 103  29
         db.getSettingTable().
 104  29
         ensure("UploadURL",
 105  29
                mc.getStaticURL(), 
 106  
                "Uploaded URL",
 107  
                "URL of uploaded files, defaults to Melati Static ");
 108  29
       }
 109  
     });
 110  
     
 111  29
   }
 112  
 
 113  
 
 114  
   protected void doPoemRequest(Melati melati)
 115  
      throws ServletException, IOException {
 116  29
      String method = melati.getMethod();
 117  29
      if (method != null && method.equals("Upload")) {
 118  6
        doUpload(melati);
 119  6
       return;
 120  
      }
 121  
 
 122  23
      melati.getResponse().setContentType("text/html");
 123  23
      MelatiWriter output = melati.getWriter();
 124  
 
 125  23
      output.write(
 126  
      "<html><head><title>PoemServlet Test</title></head>\n");
 127  23
      output.write("<body>\n");
 128  23
      output.write("<h2>PoemServlet " +
 129  
      "Test</h2>\n");
 130  23
      output.write("<p>This servlet tests your melati/poem configuration. " +
 131  
      "</p>\n");
 132  23
      output.write("<p>If you can read this message, it means that you have " +
 133  
                   "successfully created a  POEM session, \n");
 134  23
      output.write("using the configurations given in " +
 135  
                   "org.melati.LogicalDatabase.properties. </p>\n");
 136  23
      output.write("<p><b>Note</b> that this " +
 137  
                   "servlet does not initialise a template engine.</p>\n");
 138  23
      output.write("<h4>The PoemContext</h4>\n");
 139  23
      output.write("<h4>The PoemContext enables access to a database, a table, a record and a method.</h4>\n");
 140  46
      output.write("<p>Your <b>PoemContext</b> is set up as: " +
 141  23
                   melati.getPoemContext() +
 142  
                   ".</p> \n");
 143  23
      output.write("<p>Method:" + method + "</p>\n");
 144  
      
 145  23
      output.write("<p>\nThe PoemContext can be setup using the servlet's PathInfo.</p>\n");
 146  23
      output.write("<ul>\n");
 147  23
      output.write("<li>\n");
 148  46
      output.write("<a href=" +
 149  23
          melati.getZoneURL() +
 150  
          "/org.melati.test.PoemServletTest/" +
 151  23
          melati.getPoemContext().getLogicalDatabase() +
 152  
          "/tableinfo/0/View>" + 
 153  
          "tableinfo/0/View" +
 154  
          "</a>)\n");
 155  23
      output.write("</li>\n");
 156  23
      output.write("<li>\n");
 157  46
      output.write("<a href=" +
 158  23
          melati.getZoneURL() +
 159  
          "/org.melati.test.PoemServletTest/" +
 160  23
          melati.getPoemContext().getLogicalDatabase() +
 161  
          "/columninfo/0/View>" + 
 162  
          "columninfo/0/View" +
 163  
          "</a>)\n");
 164  23
      output.write("</li>\n");
 165  23
      output.write("<li>\n");
 166  46
      output.write("<a href=" +
 167  23
          melati.getZoneURL() +
 168  
          "/org.melati.test.PoemServletTest/" +
 169  23
          melati.getPoemContext().getLogicalDatabase() +
 170  
          "/user/1/View>user/1/View" +
 171  
          "</a>)\n");
 172  23
      output.write("</li>\n");
 173  23
      output.write("</ul>\n");
 174  23
      output.write("");
 175  23
      output.write("<table>");
 176  23
      output.write("<tr><th colspan=2>Tables in the Database " + melati.getDatabaseName() + "</th></tr>\n");
 177  
 
 178  23
      for (Table<?> t : melati.getDatabase().getDisplayTables()) { 
 179  243
        output.write("<tr>\n <td>");
 180  243
        output.write(t.getDisplayName());
 181  243
        output.write("</td>\n <td>");
 182  243
        output.write(t.getDescription());
 183  243
        output.write("</td>\n</tr>\n");
 184  243
      }
 185  23
      output.write("</table>\n");
 186  
 
 187  
 
 188  23
      output.write("<h4>File upload</h4>\n");
 189  23
      output.write("<p>\n");
 190  23
      output.write("A <b>PoemFileDataAdaptor</b> ");
 191  23
      output.write("obtains the name of the file upload directory from ");
 192  23
      output.write("a setting in the settings table.\n");
 193  23
      output.write("</p>\n");
 194  
      
 195  46
      output.write(
 196  
          "<form method=\"post\" action=\"Upload" + 
 197  
          "\" enctype=\"multipart/form-data\" target='Upload'>" +
 198  23
          p("You can upload a file here:") +
 199  
          "<input type=hidden name='upload' value='yes'>\n" +
 200  
          "<input type=\"file\" name=\"file\" enctype=\"multipart/form-data\">\n" +
 201  
          "<input type=\"submit\" name=\"Submit\" value=\"Upload file\">\n" +
 202  23
          getUploadMessage(melati) +
 203  
          "</form>\n");
 204  
      
 205  23
      output.write("<h4>Melati Exception handling</h4\n>");
 206  23
      output.write("<p>An exception is rendered to the output.</p>\n");     
 207  23
      output.write("<p>An access violation provokes a login challenge.</p>\n");     
 208  23
      output.write("<p>You curently have the access token " + melati.getUser() + ".</p>\n<");     
 209  23
      output.write("<ul>\n");
 210  23
      output.write("<li>\n");
 211  46
      output.write("<a href='" + 
 212  23
              melati.getSameURL() +
 213  
              "/Exception'>Exception</a>\n");
 214  23
      output.write("</li>\n");
 215  23
      output.write("<li>\n");
 216  46
      output.write("<a href='" +
 217  23
              melati.getSameURL() +
 218  
              "/AccessPoemException'>Access Poem " +
 219  
      "Exception</a> (requiring you to log-in as an administrator)\n");
 220  23
      output.write("</li>\n");
 221  23
      output.write("</ul>\n");
 222  
      
 223  23
     if (method != null) {
 224  10
       if (method.equals("Exception")) 
 225  2
         throw new MelatiBugMelatiException("It got caught!");
 226  8
       if (method.equals("AccessPoemException")) {
 227  4
         Capability admin = PoemThread.database().administerCapability();
 228  4
         AccessToken token = PoemThread.accessToken();
 229  4
         output.write("<p>You are logged in as "+token+" and have " + admin + " capability.</p>\n");
 230  4
         if (!token.givesCapability(admin))
 231  2
           throw new AccessPoemException(token, admin);
 232  
       }
 233  
     }
 234  
 
 235  19
     output.write("<h3>Further Testing</h3>\n");
 236  19
     output.write("<h4>Template Engine Testing</h4>\n");
 237  38
     output.write(p("You are currently using: <b>" + 
 238  19
     melati.getConfig().getTemplateEngine().getClass().getName() + 
 239  
     "</b>."));
 240  38
     output.write(p("You can test your WebMacro installation by clicking <a href=" + 
 241  19
     melati.getZoneURL() + 
 242  
     "/org.melati.test.WebmacroStandalone/>WebmacroStandalone</a>"));
 243  38
     output.write(p("You can test your Template Engine working with " +
 244  
     "Melati by clicking <a href=" + 
 245  19
     melati.getZoneURL() + 
 246  
     "/org.melati.test.TemplateServletTest/" + 
 247  19
     melati.getPoemContext().getLogicalDatabase() + 
 248  
     ">" + 
 249  
     "org.melati.test.TemplateServletTest/" + 
 250  19
     melati.getPoemContext().getLogicalDatabase() + "</a>"));
 251  
 
 252  38
     output.write(p("Make sure the <a href='"+ 
 253  19
       melati.getZoneURL() + 
 254  
       "/org.melati.admin.Admin/" + 
 255  19
       melati.getPoemContext().getLogicalDatabase() + 
 256  
       "/Main'>Admin System</a> is working." + 
 257  
       "\n"));
 258  
     
 259  19
     output.write("</body></html>");
 260  
 
 261  
    
 262  19
    }
 263  
 
 264  
   private void doUpload(Melati melati) throws IOException {
 265  
 
 266  6
     Hashtable<String, MultipartFormField> fields = null;
 267  6
     InputStream in = melati.getRequest().getInputStream();
 268  6
     MultipartFormDataDecoder decoder =
 269  
         new MultipartFormDataDecoder(melati,
 270  
             in,
 271  6
             melati.getRequest().getContentType(),
 272  6
             melati.getConfig().getFormDataAdaptorFactory());
 273  6
     fields = decoder.parseData();
 274  6
     MultipartFormField field = (MultipartFormField)fields.get("file");
 275  6
     byte[] data = field.getData();
 276  6
     if (data.length == 0) {
 277  2
       melati.getWriter().write(p("No file was uploaded"));
 278  2
       return;
 279  
     }
 280  4
     melati.getResponse().setContentType(field.getContentType());
 281  4
     OutputStream output = melati.getResponse().getOutputStream();
 282  4
     output.write(data);
 283  4
     output.close();
 284  4
     return;
 285  
   }
 286  
   
 287  
 
 288  
   protected String getUploadMessage(Melati melati) {
 289  23
     return p("This will save your file in a file at a path specified in the database's Settings table.") + 
 290  23
     p("  Try saving a file in your " +
 291  23
            "/tmp directory <a href='" + melati.getZoneURL() +
 292  
            "/org.melati.test.ConfigServletTestOverride/'>here</a>.");
 293  
   }
 294  
   
 295  
   private String p(String sentence) { 
 296  147
     return "<p>" + sentence + "</p>\n";
 297  
   }
 298  
 
 299  
 }
 300  
 
 301  
 
 302  
 
 303  
 
 304  
 
 305