View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of 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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.filter;
17  
18  import static org.seasar.cubby.CubbyConstants.ATTR_ACTION;
19  import static org.seasar.cubby.CubbyConstants.ATTR_CONTEXT_PATH;
20  import static org.seasar.cubby.CubbyConstants.ATTR_MESSAGES;
21  
22  import java.util.ArrayList;
23  import java.util.Enumeration;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletRequestWrapper;
29  
30  import org.seasar.cubby.controller.ActionContext;
31  import org.seasar.cubby.controller.ThreadContext;
32  import org.seasar.framework.beans.BeanDesc;
33  import org.seasar.framework.beans.PropertyDesc;
34  import org.seasar.framework.beans.factory.BeanDescFactory;
35  import org.seasar.framework.container.ComponentDef;
36  
37  /**
38   * 
39   * @author baba
40   */
41  public class CubbyHttpServletRequestWrapper extends HttpServletRequestWrapper {
42  
43  	private final ActionContext context;
44  
45  	public CubbyHttpServletRequestWrapper(final HttpServletRequest request,
46  			final ActionContext context) {
47  		super(request);
48  
49  		this.context = context;
50  	}
51  
52  	@Override
53  	public Object getAttribute(final String name) {
54  		final Object attribute;
55  		if (ATTR_CONTEXT_PATH.equals(name)) {
56  			attribute = this.getContextPath();
57  		} else if (ATTR_ACTION.equals(name)) {
58  			attribute = context.getAction();
59  		} else if (ATTR_MESSAGES.equals(name)) {
60  			attribute = ThreadContext.getMessagesMap();
61  		} else {
62  			if (context.isInitialized()) {
63  				final ComponentDef componentDef = context.getComponentDef();
64  				final Class<?> concreteClass = componentDef.getConcreteClass();
65  				final BeanDesc beanDesc = BeanDescFactory
66  						.getBeanDesc(concreteClass);
67  				if (beanDesc.hasPropertyDesc(name)) {
68  					final PropertyDesc propertyDesc = beanDesc
69  							.getPropertyDesc(name);
70  					if (propertyDesc.isReadable()) {
71  						attribute = propertyDesc.getValue(context.getAction());
72  					} else {
73  						attribute = super.getAttribute(name);
74  					}
75  				} else {
76  					attribute = super.getAttribute(name);
77  				}
78  			} else {
79  				attribute = super.getAttribute(name);
80  			}
81  		}
82  		return attribute;
83  	}
84  
85  	@SuppressWarnings("unchecked")
86  	@Override
87  	public Enumeration getAttributeNames() {
88  		final List attributeNames = new ArrayList();
89  
90  		attributeNames.add(ATTR_ACTION);
91  
92  		final Class<?> concreteClass = context.getComponentDef()
93  				.getConcreteClass();
94  		final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(concreteClass);
95  		for (int i = 0; i < beanDesc.getPropertyDescSize(); i++) {
96  			PropertyDesc propertyDesc = beanDesc.getPropertyDesc(i);
97  			if (propertyDesc.isReadable()) {
98  				attributeNames.add(propertyDesc.getPropertyName());
99  			}
100 		}
101 
102 		Enumeration defaultAttributeNames = super.getAttributeNames();
103 		while (defaultAttributeNames.hasMoreElements()) {
104 			attributeNames.add((String) defaultAttributeNames.nextElement());
105 		}
106 		return new IteratorEnumeration(attributeNames.iterator());
107 	}
108 
109 	static class IteratorEnumeration<T> implements Enumeration<T> {
110 
111 		private final Iterator<T> iterator;
112 
113 		public IteratorEnumeration(Iterator<T> iterator) {
114 			this.iterator = iterator;
115 		}
116 
117 		public boolean hasMoreElements() {
118 			return iterator.hasNext();
119 		}
120 
121 		public T nextElement() {
122 			return iterator.next();
123 		}
124 
125 	}
126 
127 }