/* $$ Clover has instrumented this file $$ */// Copyright 2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.tapestry.multipart;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.tapestry.Tapestry;
import org.apache.tapestry.request.IUploadFile;
/**
* Decodes the data in a <code>multipart/form-data</code> HTTP request, handling
* file uploads and multi-valued parameters. After decoding, the class is used
* to access the parameter values.
*
* <p>This implementation is a thin wrapper around the Apache Jakarta
* <a href="http://jakarta.apache.org/commons/fileupload/">FileUpload</a>.
*
* <p>Supports single valued parameters, multi-valued parameters and individual
* file uploads. That is, for file uploads, each upload must be a unique parameter
* (that is all the {@link org.apache.tapestry.form.Upload} component needs).
*
* @author Joe Panico
* @since 2.0.1
*
**/
public class DefaultMultipartDecoder implements IMultipartDecoder
{public static com.cortexeb.tools.clover.d __CLOVER_185_0 = com.cortexeb.tools.clover.aq.getRecorder(new char[] {67,58,92,119,111,114,107,115,112,97,99,101,92,106,97,107,97,114,116,97,45,116,97,112,101,115,116,114,121,92,102,114,97,109,101,119,111,114,107,92,116,97,114,103,101,116,92,99,108,111,118,101,114,45,100,98},1096998272901L);
/**
* Request attribute key used to store the part map for this request.
* The part map is created in {@link #decode(HttpServletRequest)}. By storing
* the part map in the request instead of an instance variable, DefaultMultipartDecoder
* becomes threadsafe (no client-specific state in instance variables).
*
**/
public static final String PART_MAP_ATTRIBUTE_NAME = "org.apache.tapestry.multipart.part-map";
private int _maxSize = 10000000;
private int _thresholdSize = 1024;
private String _repositoryPath = System.getProperty("java.io.tmpdir");
private static DefaultMultipartDecoder _shared;
public static DefaultMultipartDecoder getSharedInstance()
{try { __CLOVER_185_0.M[922]++;
__CLOVER_185_0.S[4376]++;if ((((_shared == null) && (++__CLOVER_185_0.CT[810] != 0)) || (++__CLOVER_185_0.CF[810] == 0))){
__CLOVER_185_0.S[4377]++;_shared = new DefaultMultipartDecoder();}
__CLOVER_185_0.S[4378]++;return _shared;
} finally { }}
public void setMaxSize(int maxSize)
{try { __CLOVER_185_0.M[923]++;
__CLOVER_185_0.S[4379]++;_maxSize = maxSize;
} finally { }}
public int getMaxSize()
{try { __CLOVER_185_0.M[924]++;
__CLOVER_185_0.S[4380]++;return _maxSize;
} finally { }}
public void setThresholdSize(int thresholdSize)
{try { __CLOVER_185_0.M[925]++;
__CLOVER_185_0.S[4381]++;_thresholdSize = thresholdSize;
} finally { }}
public int getThresholdSize()
{try { __CLOVER_185_0.M[926]++;
__CLOVER_185_0.S[4382]++;return _thresholdSize;
} finally { }}
public void setRepositoryPath(String repositoryPath)
{try { __CLOVER_185_0.M[927]++;
__CLOVER_185_0.S[4383]++;_repositoryPath = repositoryPath;
} finally { }}
public String getRepositoryPath()
{try { __CLOVER_185_0.M[928]++;
__CLOVER_185_0.S[4384]++;return _repositoryPath;
} finally { }}
public static boolean isMultipartRequest(HttpServletRequest request)
{try { __CLOVER_185_0.M[929]++;
__CLOVER_185_0.S[4385]++;return FileUpload.isMultipartContent(request);
} finally { }}
/**
* Invokes {@link IPart#cleanup()} on each part.
*
**/
public void cleanup(HttpServletRequest request)
{try { __CLOVER_185_0.M[930]++;
__CLOVER_185_0.S[4386]++;Map partMap = getPartMap(request);
__CLOVER_185_0.S[4387]++;Iterator i = partMap.values().iterator();
__CLOVER_185_0.S[4388]++;while ((((i.hasNext()) && (++__CLOVER_185_0.CT[811] != 0)) || (++__CLOVER_185_0.CF[811] == 0))){
{
__CLOVER_185_0.S[4389]++;IPart part = (IPart) i.next();
__CLOVER_185_0.S[4390]++;part.cleanup();
}}
} finally { }}
/**
* Decodes the request, storing the part map (keyed on query parameter name,
* value is {@link IPart} into the request as an attribute.
*
* @throws ApplicationRuntimeException if decode fails, for instance the
* request exceeds getMaxSize()
*
**/
public void decode(HttpServletRequest request)
{try { __CLOVER_185_0.M[931]++;
__CLOVER_185_0.S[4391]++;Map partMap = new HashMap();
__CLOVER_185_0.S[4392]++;request.setAttribute(PART_MAP_ATTRIBUTE_NAME, partMap);
// The encoding that will be used to decode the string parameters
// It should NOT be null at this point, but it may be
// if the older Servlet API 2.2 is used
__CLOVER_185_0.S[4393]++;String encoding = request.getCharacterEncoding();
// DiskFileUpload is not quite threadsafe, so we create a new instance
// for each request.
__CLOVER_185_0.S[4394]++;DiskFileUpload upload = new DiskFileUpload();
__CLOVER_185_0.S[4395]++;List parts = null;
__CLOVER_185_0.S[4396]++;try
{
__CLOVER_185_0.S[4397]++;if ((((encoding != null) && (++__CLOVER_185_0.CT[812] != 0)) || (++__CLOVER_185_0.CF[812] == 0))){
__CLOVER_185_0.S[4398]++;upload.setHeaderEncoding(encoding);}
__CLOVER_185_0.S[4399]++;parts = upload.parseRequest(request, _thresholdSize, _maxSize, _repositoryPath);
}
catch (FileUploadException ex)
{
__CLOVER_185_0.S[4400]++;throw new ApplicationRuntimeException(
Tapestry.format("DefaultMultipartDecoder.unable-to-decode", ex.getMessage()),
ex);
}
__CLOVER_185_0.S[4401]++;int count = Tapestry.size(parts);
__CLOVER_185_0.S[4402]++;for (int i = 0; (((i < count) && (++__CLOVER_185_0.CT[813] != 0)) || (++__CLOVER_185_0.CF[813] == 0)); i++){
{
__CLOVER_185_0.S[4403]++;FileItem uploadItem = (FileItem) parts.get(i);
__CLOVER_185_0.S[4404]++;if ((((uploadItem.isFormField()) && (++__CLOVER_185_0.CT[814] != 0)) || (++__CLOVER_185_0.CF[814] == 0))){
{
__CLOVER_185_0.S[4405]++;try
{
__CLOVER_185_0.S[4406]++;String name = uploadItem.getFieldName();
__CLOVER_185_0.S[4407]++;String value;
__CLOVER_185_0.S[4408]++;if ((((encoding == null) && (++__CLOVER_185_0.CT[815] != 0)) || (++__CLOVER_185_0.CF[815] == 0))){
__CLOVER_185_0.S[4409]++;value = uploadItem.getString();}
else{
__CLOVER_185_0.S[4410]++;value = uploadItem.getString(encoding);}
__CLOVER_185_0.S[4411]++;ValuePart valuePart = (ValuePart) partMap.get(name);
__CLOVER_185_0.S[4412]++;if ((((valuePart != null) && (++__CLOVER_185_0.CT[816] != 0)) || (++__CLOVER_185_0.CF[816] == 0))){
{
__CLOVER_185_0.S[4413]++;valuePart.add(value);
}}
else{
{
__CLOVER_185_0.S[4414]++;valuePart = new ValuePart(value);
__CLOVER_185_0.S[4415]++;partMap.put(name, valuePart);
}}
}
catch (UnsupportedEncodingException ex)
{
__CLOVER_185_0.S[4416]++;throw new ApplicationRuntimeException(
Tapestry.format("illegal-encoding", encoding),
ex);
}
}}
else{
{
__CLOVER_185_0.S[4417]++;UploadPart uploadPart = new UploadPart(uploadItem);
__CLOVER_185_0.S[4418]++;partMap.put(uploadItem.getFieldName(), uploadPart);
}}
}}
} finally { }}
public String getString(HttpServletRequest request, String name)
{try { __CLOVER_185_0.M[932]++;
__CLOVER_185_0.S[4419]++;Map partMap = getPartMap(request);
__CLOVER_185_0.S[4420]++;ValuePart part = (ValuePart) partMap.get(name);
__CLOVER_185_0.S[4421]++;if ((((part != null) && (++__CLOVER_185_0.CT[817] != 0)) || (++__CLOVER_185_0.CF[817] == 0))){
__CLOVER_185_0.S[4422]++;return part.getValue();}
__CLOVER_185_0.S[4423]++;return null;
} finally { }}
public String[] getStrings(HttpServletRequest request, String name)
{try { __CLOVER_185_0.M[933]++;
__CLOVER_185_0.S[4424]++;Map partMap = getPartMap(request);
__CLOVER_185_0.S[4425]++;ValuePart part = (ValuePart) partMap.get(name);
__CLOVER_185_0.S[4426]++;if ((((part != null) && (++__CLOVER_185_0.CT[818] != 0)) || (++__CLOVER_185_0.CF[818] == 0))){
__CLOVER_185_0.S[4427]++;return part.getValues();}
__CLOVER_185_0.S[4428]++;return null;
} finally { }}
public IUploadFile getUploadFile(HttpServletRequest request, String name)
{try { __CLOVER_185_0.M[934]++;
__CLOVER_185_0.S[4429]++;Map partMap = getPartMap(request);
__CLOVER_185_0.S[4430]++;return (IUploadFile) partMap.get(name);
} finally { }}
private Map getPartMap(HttpServletRequest request)
{try { __CLOVER_185_0.M[935]++;
__CLOVER_185_0.S[4431]++;return (Map) request.getAttribute(PART_MAP_ATTRIBUTE_NAME);
} finally { }}
}