View Javadoc

1   /*
2    * Copyright 2004-2005 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.morph.reflect.reflectors;
17  
18  import javax.servlet.ServletRequest;
19  
20  import net.sf.composite.util.ObjectUtils;
21  import net.sf.morph.reflect.ReflectionException;
22  import net.sf.morph.util.ContainerUtils;
23  
24  /**
25   * Exposes servlet request parameters.
26   * 
27   * @author Matt Sgarlata
28   * @since Nov 21, 2004
29   */
30  public class ServletRequestParameterReflector extends BaseServletReflector {
31  
32  	private static final Class[] REFLECTABLE_TYPES = new Class[] {
33  		ServletRequest.class
34  	};
35  	
36  	private ServletRequest getRequest(Object bean) {
37  		return (ServletRequest) bean;
38  	}
39  
40  	protected String[] getPropertyNamesImpl(Object bean) throws Exception {
41  		return enumerationToStringArray(getRequest(bean).getParameterNames());
42  	}
43  
44  	protected Class getTypeImpl(Object bean, String propertyName)
45  		throws Exception {
46  		String[] values = getRequest(bean).getParameterValues(propertyName);
47  		return ObjectUtils.isEmpty(values) || values.length == 1 ? String.class : String[].class;
48  	}
49  
50  	protected boolean isReadableImpl(Object bean, String propertyName)
51  		throws Exception {
52  		// all possible property names are readable.  If the property was not
53  		// part of the request, the Servlet API specifies that null will be
54  		// returned if request.getParameter is called
55  		// return ContainerUtils.contains(getPropertyNames(bean), propertyName);
56  		return true;
57  	}
58  	
59  	protected boolean isWriteableImpl(Object bean, String propertyName)
60  		throws Exception {
61  		return false;
62  	}
63  
64  	protected Object getImpl(Object bean, String propertyName) throws Exception {
65  		String[] values = getRequest(bean).getParameterValues(propertyName);
66  		return ObjectUtils.isEmpty(values) || values.length == 1 ? (Object) getRequest(bean).getParameter(propertyName) : values;
67  	}
68  
69  	protected void setImpl(Object bean, String propertyName, Object value)
70  		throws Exception {
71  		// won't actually ever throw this exception since isWriteable
72  		// always returns false
73  		throw new ReflectionException("Can't set HTTP request parameters");
74  	}
75  	
76  	public Class[] getReflectableClassesImpl() {
77  		return REFLECTABLE_TYPES;
78  	}
79  	
80  }