Clover coverage report - Code Coverage for tapestry release 3.1-alpha-1
Coverage timestamp: Mon Feb 21 2005 09:16:14 EST
file stats: LOC: 274   Methods: 17
NCLOC: 165   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
DefaultMultipartDecoder.java 61.1% 77% 70.6% 72.9%
coverage coverage
 1   
 // Copyright 2004, 2005 The Apache Software Foundation
 2   
 //
 3   
 // Licensed under the Apache License, Version 2.0 (the "License");
 4   
 // you may not use this file except in compliance with the License.
 5   
 // You may obtain a copy of the License at
 6   
 //
 7   
 //     http://www.apache.org/licenses/LICENSE-2.0
 8   
 //
 9   
 // Unless required by applicable law or agreed to in writing, software
 10   
 // distributed under the License is distributed on an "AS IS" BASIS,
 11   
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12   
 // See the License for the specific language governing permissions and
 13   
 // limitations under the License.
 14   
 
 15   
 package org.apache.tapestry.multipart;
 16   
 
 17   
 import java.io.UnsupportedEncodingException;
 18   
 import java.util.HashMap;
 19   
 import java.util.HashSet;
 20   
 import java.util.Iterator;
 21   
 import java.util.List;
 22   
 import java.util.Map;
 23   
 import java.util.Set;
 24   
 
 25   
 import javax.servlet.http.HttpServletRequest;
 26   
 
 27   
 import org.apache.commons.fileupload.DiskFileUpload;
 28   
 import org.apache.commons.fileupload.FileItem;
 29   
 import org.apache.commons.fileupload.FileUpload;
 30   
 import org.apache.commons.fileupload.FileUploadException;
 31   
 import org.apache.hivemind.ApplicationRuntimeException;
 32   
 import org.apache.tapestry.Tapestry;
 33   
 import org.apache.tapestry.request.IUploadFile;
 34   
 
 35   
 /**
 36   
  * Decodes the data in a <code>multipart/form-data</code> HTTP request, handling file uploads and
 37   
  * multi-valued parameters. After decoding, the class is used to access the parameter values.
 38   
  * <p>
 39   
  * This implementation is a thin wrapper around the Apache Jakarta <a
 40   
  * href="http://jakarta.apache.org/commons/fileupload/">FileUpload </a>.
 41   
  * <p>
 42   
  * Supports single valued parameters, multi-valued parameters and individual file uploads. That is,
 43   
  * for file uploads, each upload must be a unique parameter (that is all the
 44   
  * {@link org.apache.tapestry.form.Upload}component needs).
 45   
  * 
 46   
  * @author Joe Panico
 47   
  * @since 2.0.1
 48   
  */
 49   
 public class DefaultMultipartDecoder implements IMultipartDecoder
 50   
 {
 51   
     /**
 52   
      * Request attribute key used to store the part map for this request. The part map is created in
 53   
      * {@link #decode(HttpServletRequest)}. By storing the part map in the request instead of an
 54   
      * instance variable, DefaultMultipartDecoder becomes threadsafe (no client-specific state in
 55   
      * instance variables).
 56   
      */
 57   
 
 58   
     public static final String PART_MAP_ATTRIBUTE_NAME = "org.apache.tapestry.multipart.part-map";
 59   
 
 60   
     public static final String STRING_NAMES_ATTRIBUTE_NAME = "org.apache.tapestry.multipart.parameter-names";
 61   
 
 62   
     private int _maxSize = 10000000;
 63   
 
 64   
     private int _thresholdSize = 1024;
 65   
 
 66   
     private String _repositoryPath = System.getProperty("java.io.tmpdir");
 67   
 
 68   
     private static DefaultMultipartDecoder _shared;
 69   
 
 70  0
     public static DefaultMultipartDecoder getSharedInstance()
 71   
     {
 72  0
         if (_shared == null)
 73  0
             _shared = new DefaultMultipartDecoder();
 74   
 
 75  0
         return _shared;
 76   
     }
 77   
 
 78  1
     public void setMaxSize(int maxSize)
 79   
     {
 80  1
         _maxSize = maxSize;
 81   
     }
 82   
 
 83  0
     public int getMaxSize()
 84   
     {
 85  0
         return _maxSize;
 86   
     }
 87   
 
 88  2
     public void setThresholdSize(int thresholdSize)
 89   
     {
 90  2
         _thresholdSize = thresholdSize;
 91   
     }
 92   
 
 93  0
     public int getThresholdSize()
 94   
     {
 95  0
         return _thresholdSize;
 96   
     }
 97   
 
 98  1
     public void setRepositoryPath(String repositoryPath)
 99   
     {
 100  1
         _repositoryPath = repositoryPath;
 101   
     }
 102   
 
 103  0
     public String getRepositoryPath()
 104   
     {
 105  0
         return _repositoryPath;
 106   
     }
 107   
 
 108  187
     public static boolean isMultipartRequest(HttpServletRequest request)
 109   
     {
 110  187
         return FileUpload.isMultipartContent(request);
 111   
     }
 112   
 
 113   
     /**
 114   
      * Invokes {@link IPart#cleanup()}on each part.
 115   
      */
 116  6
     public void cleanup(HttpServletRequest request)
 117   
     {
 118  6
         Map partMap = getPartMap(request);
 119   
 
 120  6
         Iterator i = partMap.values().iterator();
 121  6
         while (i.hasNext())
 122   
         {
 123  33
             IPart part = (IPart) i.next();
 124  33
             part.cleanup();
 125   
         }
 126   
     }
 127   
 
 128   
     /**
 129   
      * Decodes the request, storing the part map (keyed on query parameter name, value is
 130   
      * {@link IPart}into the request as an attribute.
 131   
      * 
 132   
      * @throws ApplicationRuntimeException
 133   
      *             if decode fails, for instance the request exceeds getMaxSize()
 134   
      */
 135   
 
 136  6
     public void decode(HttpServletRequest request)
 137   
     {
 138  6
         Map partMap = new HashMap();
 139   
 
 140  6
         request.setAttribute(PART_MAP_ATTRIBUTE_NAME, partMap);
 141   
 
 142   
         // The encoding that will be used to decode the string parameters
 143   
         // It should NOT be null at this point, but it may be
 144   
         // if the older Servlet API 2.2 is used
 145  6
         String encoding = request.getCharacterEncoding();
 146   
 
 147   
         // DiskFileUpload is not quite threadsafe, so we create a new instance
 148   
         // for each request.
 149   
 
 150  6
         DiskFileUpload upload = new DiskFileUpload();
 151   
 
 152  6
         List parts = null;
 153   
 
 154  6
         try
 155   
         {
 156  6
             if (encoding != null)
 157  6
                 upload.setHeaderEncoding(encoding);
 158  6
             parts = upload.parseRequest(request, _thresholdSize, _maxSize, _repositoryPath);
 159   
         }
 160   
         catch (FileUploadException ex)
 161   
         {
 162  0
             throw new ApplicationRuntimeException(Tapestry.format(
 163   
                     "DefaultMultipartDecoder.unable-to-decode",
 164   
                     ex.getMessage()), ex);
 165   
         }
 166   
 
 167  6
         int count = Tapestry.size(parts);
 168   
 
 169  6
         Set names = new HashSet();
 170   
 
 171  6
         for (int i = 0; i < count; i++)
 172   
         {
 173  34
             FileItem uploadItem = (FileItem) parts.get(i);
 174   
 
 175  34
             if (uploadItem.isFormField())
 176   
             {
 177  30
                 try
 178   
                 {
 179  30
                     String name = uploadItem.getFieldName();
 180   
 
 181  30
                     names.add(name);
 182   
 
 183  30
                     String value = extractFileItemValue(uploadItem, encoding);
 184   
 
 185  30
                     ValuePart valuePart = (ValuePart) partMap.get(name);
 186   
 
 187  30
                     if (valuePart != null)
 188   
                     {
 189  1
                         valuePart.add(value);
 190   
                     }
 191   
                     else
 192   
                     {
 193  29
                         valuePart = new ValuePart(value);
 194  29
                         partMap.put(name, valuePart);
 195   
                     }
 196   
                 }
 197   
                 catch (UnsupportedEncodingException ex)
 198   
                 {
 199  0
                     throw new ApplicationRuntimeException(Tapestry.format(
 200   
                             "illegal-encoding",
 201   
                             encoding), ex);
 202   
                 }
 203   
             }
 204   
             else
 205   
             {
 206  4
                 UploadPart uploadPart = new UploadPart(uploadItem);
 207   
 
 208  4
                 partMap.put(uploadItem.getFieldName(), uploadPart);
 209   
             }
 210   
         }
 211   
 
 212  6
         storeNamesIntoRequest(request, names);
 213   
     }
 214   
 
 215   
     /** @since 3.1 */
 216   
 
 217  6
     private void storeNamesIntoRequest(HttpServletRequest request, Set names)
 218   
     {
 219  6
         int count = names.size();
 220   
 
 221  6
         String[] array = (String[]) names.toArray(new String[count]);
 222   
 
 223  6
         request.setAttribute(STRING_NAMES_ATTRIBUTE_NAME, array);
 224   
     }
 225   
 
 226   
     /** @since 3.1 */
 227   
 
 228  30
     private String extractFileItemValue(FileItem uploadItem, String encoding)
 229   
             throws UnsupportedEncodingException
 230   
     {
 231  30
         return (encoding == null) ? uploadItem.getString() : uploadItem.getString(encoding);
 232   
     }
 233   
 
 234  0
     public String getString(HttpServletRequest request, String name)
 235   
     {
 236  0
         Map partMap = getPartMap(request);
 237   
 
 238  0
         ValuePart part = (ValuePart) partMap.get(name);
 239  0
         if (part != null)
 240  0
             return part.getValue();
 241   
 
 242  0
         return null;
 243   
     }
 244   
 
 245  30
     public String[] getStrings(HttpServletRequest request, String name)
 246   
     {
 247  30
         Map partMap = getPartMap(request);
 248   
 
 249  30
         ValuePart part = (ValuePart) partMap.get(name);
 250  30
         if (part != null)
 251  30
             return part.getValues();
 252   
 
 253  0
         return null;
 254   
     }
 255   
 
 256   
     /** @since 3.1 */
 257  6
     public String[] getStringParameterNames(HttpServletRequest request)
 258   
     {
 259  6
         return (String[]) request.getAttribute(STRING_NAMES_ATTRIBUTE_NAME);
 260   
     }
 261   
 
 262  3
     public IUploadFile getUploadFile(HttpServletRequest request, String name)
 263   
     {
 264  3
         Map partMap = getPartMap(request);
 265   
 
 266  3
         return (IUploadFile) partMap.get(name);
 267   
     }
 268   
 
 269  39
     private Map getPartMap(HttpServletRequest request)
 270   
     {
 271  39
         return (Map) request.getAttribute(PART_MAP_ATTRIBUTE_NAME);
 272   
     }
 273   
 
 274   
 }