001// Copyright 2007, 2008 The Apache Software Foundation 002// 003// Licensed under the Apache License, Version 2.0 (the "License"); 004// you may not use this file except in compliance with the License. 005// You may obtain a copy of the License at 006// 007// http://www.apache.org/licenses/LICENSE-2.0 008// 009// Unless required by applicable law or agreed to in writing, software 010// distributed under the License is distributed on an "AS IS" BASIS, 011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012// See the License for the specific language governing permissions and 013// limitations under the License. 014 015package org.apache.tapestry5.upload.internal.services; 016 017import javax.servlet.http.HttpServletRequest; 018import javax.servlet.http.HttpServletRequestWrapper; 019 020import static org.apache.tapestry5.commons.util.CollectionFactory.newMap; 021 022import java.io.UnsupportedEncodingException; 023import java.util.Collections; 024import java.util.Enumeration; 025import java.util.Map; 026 027/** 028 * Wrapper for HttpServletRequest that overrides the parameter methods of the wrapped request. i.e. parameters are 029 * retrieved from the wrapper rather than the real request. 030 */ 031public class ParametersServletRequestWrapper extends HttpServletRequestWrapper 032{ 033 private final Map<String, ParameterValue> parameters = newMap(); 034 035 public ParametersServletRequestWrapper(HttpServletRequest httpServletRequest) 036 { 037 super(httpServletRequest); 038 } 039 040 @Override 041 public String getParameter(String name) 042 { 043 return getValueFor(name).single(); 044 } 045 046 @Override 047 public Map<String, String[]> getParameterMap() 048 { 049 Map<String, String[]> paramMap = newMap(); 050 051 for (Map.Entry<String, ParameterValue> e : parameters.entrySet()) 052 { 053 ParameterValue value = e.getValue(); 054 055 paramMap.put(e.getKey(), value.multi()); 056 } 057 058 return paramMap; 059 } 060 061 @Override 062 public Enumeration<String> getParameterNames() 063 { 064 return Collections.enumeration(parameters.keySet()); 065 } 066 067 @Override 068 public String[] getParameterValues(String name) 069 { 070 return getValueFor(name).multi(); 071 } 072 073 public void addParameter(String name, String value) 074 { 075 ParameterValue pv = parameters.get(name); 076 if (pv == null) 077 { 078 pv = new ParameterValue(value); 079 parameters.put(name, pv); 080 } 081 else 082 { 083 pv.add(value); 084 } 085 } 086 087 ParameterValue getValueFor(String name) 088 { 089 ParameterValue value = parameters.get(name); 090 091 return value == null ? ParameterValue.NULL : value; 092 } 093 094 /** 095 * Ignores any attempt to set the character encoding, as it already has been set before the request content was 096 * parsed. 097 * 098 * @see org.apache.tapestry5.http.TapestryHttpSymbolConstants#CHARSET 099 */ 100 @Override 101 public void setCharacterEncoding(String enc) throws UnsupportedEncodingException 102 { 103 104 } 105}