Clover coverage report - Code Coverage for tapestry release 4.0-beta-8
Coverage timestamp: Sat Sep 24 2005 11:50:34 EDT
file stats: LOC: 191   Methods: 10
NCLOC: 119   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MultipartDecoderImpl.java 81.2% 93.6% 100% 91.8%
coverage coverage
 1    // Copyright 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.Iterator;
 20    import java.util.List;
 21    import java.util.Map;
 22   
 23    import javax.servlet.http.HttpServletRequest;
 24   
 25    import org.apache.commons.fileupload.DiskFileUpload;
 26    import org.apache.commons.fileupload.FileItem;
 27    import org.apache.commons.fileupload.FileUploadException;
 28    import org.apache.hivemind.ApplicationRuntimeException;
 29    import org.apache.tapestry.request.IUploadFile;
 30   
 31    /**
 32    * Implementation of {@link org.apache.tapestry.multipart.MultipartDecoder}that is based on <a
 33    * href="http://jakarta.apache.org/commons/fileupload/">Jakarta FileUpload </a>.
 34    *
 35    * @author Howard M. Lewis Ship
 36    * @author Joe Panico
 37    * @since 4.0
 38    */
 39    public class MultipartDecoderImpl implements MultipartDecoder
 40    {
 41    /**
 42    * Map of UploadPart (which implements IUploadFile), keyed on parameter name.
 43    */
 44   
 45    private Map _uploadParts = new HashMap();
 46   
 47    /**
 48    * Map of ValuePart, keyed on parameter name.
 49    */
 50    private Map _valueParts = new HashMap();
 51   
 52    private int _maxSize = 10000000;
 53   
 54    private int _thresholdSize = 1024;
 55   
 56    private String _repositoryPath = System.getProperty("java.io.tmpdir");
 57   
 58    private String _encoding;
 59   
 60  5 public HttpServletRequest decode(HttpServletRequest request)
 61    {
 62  5 _encoding = request.getCharacterEncoding();
 63   
 64  5 DiskFileUpload upload = createUpload(request);
 65   
 66  5 try
 67    {
 68  5 List fileItems = upload
 69    .parseRequest(request, _thresholdSize, _maxSize, _repositoryPath);
 70   
 71  5 processFileItems(fileItems);
 72    }
 73    catch (FileUploadException ex)
 74    {
 75  0 throw new ApplicationRuntimeException(MultipartMessages.unableToDecode(ex), ex);
 76    }
 77   
 78  5 Map parameterMap = buildParameterMap();
 79   
 80  5 return new UploadFormParametersWrapper(request, parameterMap);
 81    }
 82   
 83  5 private Map buildParameterMap()
 84    {
 85  5 Map result = new HashMap();
 86   
 87  5 Iterator i = _valueParts.entrySet().iterator();
 88  5 while (i.hasNext())
 89    {
 90  22 Map.Entry e = (Map.Entry) i.next();
 91   
 92  22 String name = (String) e.getKey();
 93  22 ValuePart part = (ValuePart) e.getValue();
 94   
 95  22 result.put(name, part.getValues());
 96    }
 97   
 98  5 return result;
 99    }
 100   
 101  5 private void processFileItems(List parts)
 102    {
 103  5 if (parts == null)
 104  0 return;
 105   
 106  5 Iterator i = parts.iterator();
 107   
 108  5 while (i.hasNext())
 109    {
 110  27 FileItem item = (FileItem) i.next();
 111   
 112  27 processFileItem(item);
 113    }
 114    }
 115   
 116  27 private void processFileItem(FileItem item)
 117    {
 118  27 if (item.isFormField())
 119    {
 120  23 processFormFieldItem(item);
 121  23 return;
 122    }
 123   
 124  4 processUploadFileItem(item);
 125    }
 126   
 127  4 private void processUploadFileItem(FileItem item)
 128    {
 129  4 String name = item.getFieldName();
 130   
 131  4 UploadPart part = new UploadPart(item);
 132   
 133  4 _uploadParts.put(name, part);
 134    }
 135   
 136  23 void processFormFieldItem(FileItem item)
 137    {
 138  23 String name = item.getFieldName();
 139   
 140  23 String value = extractFileItemValue(item);
 141   
 142  23 ValuePart part = (ValuePart) _valueParts.get(name);
 143   
 144  23 if (part == null)
 145  22 _valueParts.put(name, new ValuePart(value));
 146    else
 147  1 part.add(value);
 148    }
 149   
 150  23 private String extractFileItemValue(FileItem item)
 151    {
 152  23 try
 153    {
 154  23 return (_encoding == null) ? item.getString() : item.getString(_encoding);
 155    }
 156    catch (UnsupportedEncodingException ex)
 157    {
 158  0 throw new ApplicationRuntimeException(MultipartMessages.unsupportedEncoding(
 159    _encoding,
 160    ex), ex);
 161    }
 162    }
 163   
 164  5 protected DiskFileUpload createUpload(HttpServletRequest request)
 165    {
 166  5 DiskFileUpload upload = new DiskFileUpload();
 167   
 168  5 if (_encoding != null)
 169  5 upload.setHeaderEncoding(_encoding);
 170   
 171  5 return upload;
 172    }
 173   
 174  3 public IUploadFile getFileUpload(String parameterName)
 175    {
 176  3 return (IUploadFile) _uploadParts.get(parameterName);
 177    }
 178   
 179  5 public void cleanup()
 180    {
 181  5 Iterator i = _uploadParts.values().iterator();
 182   
 183  5 while (i.hasNext())
 184    {
 185  4 UploadPart part = (UploadPart) i.next();
 186   
 187  4 part.cleanup();
 188    }
 189    }
 190   
 191    }