Coverage Report - org.melati.template.MultipartTemplateContext
 
Classes in this File Line Coverage Branch Coverage Complexity
MultipartTemplateContext
43%
16/37
100%
2/2
1.545
 
 1  
 /*
 2  
  * $Source: /usr/cvsroot/melati/melati/src/site/resources/withWebmacro/org.melati.template.MultipartTemplateContext.html,v $
 3  
  * $Revision: 1.1 $
 4  
  *
 5  
  * Copyright (C) 2000 Myles Chippendale
 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  
  *     Myles Chippendale <mylesc At paneris.org>
 42  
  *     http://paneris.org/
 43  
  *     29 Stanley Road, Oxford, OX4 1QY, UK
 44  
  */
 45  
 
 46  
 
 47  
 package org.melati.template;
 48  
 
 49  
 import java.io.InputStream;
 50  
 import java.io.IOException;
 51  
 import java.util.Hashtable;
 52  
 import java.util.Enumeration;
 53  
 import javax.servlet.http.HttpSession;
 54  
 import org.melati.Melati;
 55  
 import org.melati.servlet.MultipartFormField;
 56  
 import org.melati.servlet.MultipartFormDataDecoder;
 57  
 
 58  
 
 59  
 /**
 60  
  * A {@link ServletTemplateContext} which allows access to the filename and
 61  
  * body of any file which is uploaded from a HTML form field.
 62  
  *
 63  
  * (by setting its ENCTYPE to ``multipart/form-data'' and 
 64  
  * setting the field's type to * ``file'').
 65  
  * <p>
 66  
  * You can retrieve the value of any field variable as usual by
 67  
  * using getForm(s).
 68  
  * <p>
 69  
  * Note that you need to pass in a {@link ServletTemplateContext} to the constructor.
 70  
  */
 71  
 public class MultipartTemplateContext implements ServletTemplateContext {
 72  
   ServletTemplateContext peer;
 73  
   Hashtable fields;
 74  
   Melati melati;
 75  
 
 76  
   /**
 77  
    * Constructor.
 78  
    * @param melati our Melati
 79  
    * @param context the ServletTemplateContext
 80  
    */
 81  
   public MultipartTemplateContext(Melati melati, ServletTemplateContext context)
 82  14
       throws IOException {
 83  14
     peer = context;
 84  14
     this.melati = melati;
 85  
     try {
 86  14
       InputStream in = melati.getRequest().getInputStream();
 87  14
       MultipartFormDataDecoder decoder=
 88  
         new MultipartFormDataDecoder(
 89  
                              melati,
 90  
                              in,
 91  
                              melati.getRequest().getContentType(),
 92  
                              melati.getConfig().getFormDataAdaptorFactory());
 93  14
       fields = decoder.parseData();
 94  
     }
 95  0
     catch (IOException e) {
 96  0
       fields = new Hashtable();
 97  0
       throw e;
 98  14
     }
 99  14
   }
 100  
 
 101  
   /**
 102  
    * Constructor.
 103  
    * @param melati our Melati
 104  
    * @param context the ServletTemplateContext
 105  
    * @param maxSize maximum allowed size 
 106  
    */
 107  
   public MultipartTemplateContext(Melati melati, ServletTemplateContext context,
 108  
                                   int maxSize)
 109  0
       throws IOException {
 110  0
     peer = context;
 111  0
     this.melati = melati;
 112  
     try {
 113  0
       InputStream in = melati.getRequest().getInputStream();
 114  0
       MultipartFormDataDecoder decoder=
 115  
         new MultipartFormDataDecoder(melati,
 116  
                              in,
 117  
                              melati.getRequest().getContentType(),
 118  
                              melati.getConfig().getFormDataAdaptorFactory(),
 119  
                              maxSize);
 120  0
       fields = decoder.parseData();
 121  
     }
 122  0
     catch (IOException e) {
 123  0
       fields = new Hashtable();
 124  0
       throw e;
 125  0
     }
 126  0
   }
 127  
 
 128  
   /**
 129  
    * {@inheritDoc}
 130  
    * @see org.melati.template.TemplateContext#put(java.lang.String, java.lang.Object)
 131  
    */
 132  
   public void put(String s, Object o) {
 133  60
     peer.put(s,o);
 134  60
   }
 135  
 
 136  
   /**
 137  
    * {@inheritDoc}
 138  
    * @see org.melati.template.ServletTemplateContext#getFormField(java.lang.String)
 139  
    */
 140  
   public String getFormField(String s) {
 141  30
     MultipartFormField field = (MultipartFormField)fields.get(s);
 142  30
     if (field == null)
 143  8
       return peer.getFormField(s);
 144  22
     return field.getDataString(melati.getResponse().getCharacterEncoding());
 145  
   }
 146  
 
 147  
   /**
 148  
    * {@inheritDoc}
 149  
    * @see org.melati.template.ServletTemplateContext#getMultipartFormField(java.lang.String)
 150  
    */
 151  
   public MultipartFormField getMultipartFormField(String s) {
 152  18
     return (MultipartFormField)fields.get(s);
 153  
   }
 154  
 
 155  
   /**
 156  
    * @return an Enumeration of Field names
 157  
    */
 158  
   public Enumeration getFieldNames() {
 159  0
     return fields.elements();
 160  
   }
 161  
 
 162  
   /**
 163  
    * {@inheritDoc}
 164  
    * @see org.melati.template.TemplateContext#get(java.lang.String)
 165  
    */
 166  
   public Object get(String s) {
 167  0
     return peer.get(s);
 168  
   }
 169  
 
 170  
   /**
 171  
    * {@inheritDoc}
 172  
    * @see org.melati.template.ServletTemplateContext#getSession()
 173  
    */
 174  
   public HttpSession getSession() {
 175  0
     return peer.getSession();
 176  
   }
 177  
 
 178  
   /**
 179  
    * {@inheritDoc}
 180  
    * @see org.melati.template.TemplateContext#getContext()
 181  
    */
 182  
   public Object getContext() {
 183  6
     return peer.getContext();
 184  
   }
 185  
 
 186  
   /**
 187  
    * {@inheritDoc}
 188  
    * @see org.melati.template.TemplateContext#setPassbackExceptionHandling()
 189  
    */
 190  
   public void setPassbackExceptionHandling() {
 191  0
     peer.setPassbackExceptionHandling();    
 192  0
   }
 193  
 
 194  
   /**
 195  
    * {@inheritDoc}
 196  
    * @see org.melati.template.TemplateContext#setPropagateExceptionHandling()
 197  
    */
 198  
   public void setPropagateExceptionHandling() {
 199  0
     peer.setPropagateExceptionHandling();    
 200  0
   }
 201  
 
 202  
   
 203  
 }
 204  
 
 205  
 
 206  
 
 207  
 
 208  
 
 209