Coverage Report - org.apache.tapestry.multipart.AbstractMultipartDecoder
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractMultipartDecoder
0%
0/48
0%
0/14
2.5
 
 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  
 package org.apache.tapestry.multipart;
 15  
 
 16  
 import org.apache.commons.fileupload.FileItem;
 17  
 import org.apache.hivemind.ApplicationRuntimeException;
 18  
 import org.apache.tapestry.request.IUploadFile;
 19  
 
 20  
 import java.io.UnsupportedEncodingException;
 21  
 import java.util.HashMap;
 22  
 import java.util.Iterator;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 
 26  
 /**
 27  
  * @author Raphael Jean
 28  
  */
 29  0
 public abstract class AbstractMultipartDecoder
 30  
 {
 31  
 
 32  0
     protected int _maxSize = 10000000;
 33  
 
 34  0
     protected int _thresholdSize = 1024;
 35  
 
 36  0
     protected String _repositoryPath = System.getProperty("java.io.tmpdir");
 37  
 
 38  
     protected String _encoding;
 39  
     
 40  
     /**
 41  
      * Map of UploadPart (which implements IUploadFile), keyed on parameter
 42  
      * name.
 43  
      */
 44  0
     protected Map _uploadParts = new HashMap();
 45  
 
 46  
     /**
 47  
      * Map of ValuePart, keyed on parameter name.
 48  
      */
 49  0
     private Map _valueParts = new HashMap();
 50  
 
 51  
     public IUploadFile getFileUpload(String parameterName)
 52  
     {
 53  0
         return (IUploadFile) _uploadParts.get(parameterName);
 54  
     }
 55  
 
 56  
     public void cleanup()
 57  
     {
 58  0
         Iterator i = _uploadParts.values().iterator();
 59  
 
 60  0
         while(i.hasNext())
 61  
         {
 62  0
             UploadPart part = (UploadPart) i.next();
 63  
 
 64  0
             part.cleanup();
 65  0
         }
 66  0
     }
 67  
 
 68  
     protected Map buildParameterMap()
 69  
     {
 70  0
         Map result = new HashMap();
 71  
 
 72  0
         Iterator i = _valueParts.entrySet().iterator();
 73  0
         while(i.hasNext())
 74  
         {
 75  0
             Map.Entry e = (Map.Entry) i.next();
 76  
 
 77  0
             String name = (String) e.getKey();
 78  0
             ValuePart part = (ValuePart) e.getValue();
 79  
 
 80  0
             result.put(name, part.getValues());
 81  0
         }
 82  
 
 83  0
         return result;
 84  
     }
 85  
 
 86  
     protected void processFileItems(List parts)
 87  
     {
 88  0
         if (parts == null) return;
 89  
 
 90  0
         Iterator i = parts.iterator();
 91  
 
 92  0
         while(i.hasNext())
 93  
         {
 94  0
             FileItem item = (FileItem) i.next();
 95  
 
 96  0
             processFileItem(item);
 97  0
         }
 98  0
     }
 99  
 
 100  
     private void processFileItem(FileItem item)
 101  
     {
 102  0
         if (item.isFormField())
 103  
         {
 104  0
             processFormFieldItem(item);
 105  0
             return;
 106  
         }
 107  
 
 108  0
         processUploadFileItem(item);
 109  0
     }
 110  
 
 111  
     private void processUploadFileItem(FileItem item)
 112  
     {
 113  0
         String name = item.getFieldName();
 114  
 
 115  0
         UploadPart part = new UploadPart(item);
 116  
 
 117  0
         _uploadParts.put(name, part);
 118  0
     }
 119  
 
 120  
     void processFormFieldItem(FileItem item)
 121  
     {
 122  0
         String name = item.getFieldName();
 123  
 
 124  0
         String value = extractFileItemValue(item);
 125  
 
 126  0
         ValuePart part = (ValuePart) _valueParts.get(name);
 127  
 
 128  0
         if (part == null)
 129  0
             _valueParts.put(name, new ValuePart(value));
 130  
         else 
 131  0
             part.add(value);
 132  0
     }
 133  
 
 134  
     private String extractFileItemValue(FileItem item)
 135  
     {
 136  
         try
 137  
         {
 138  0
             return (_encoding == null) ? item.getString() : item.getString(_encoding);
 139  
         }
 140  0
         catch (UnsupportedEncodingException ex)
 141  
         {
 142  0
             throw new ApplicationRuntimeException(MultipartMessages
 143  
                     .unsupportedEncoding(_encoding, ex), ex);
 144  
         }
 145  
     }
 146  
 }