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.List;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletRequestWrapper;
28  
29  import org.seasar.cubby.CubbyConstants;
30  import org.seasar.cubby.action.Action;
31  import org.seasar.cubby.controller.ThreadContext;
32  import org.seasar.cubby.util.IteratorEnumeration;
33  import org.seasar.framework.beans.BeanDesc;
34  import org.seasar.framework.beans.PropertyDesc;
35  import org.seasar.framework.beans.factory.BeanDescFactory;
36  
37  /**
38   * 特別な属性を取得するためのリクエストのラッパです。
39   * <p>
40   * 以下のような属性を使用することができます。 <table><thead>
41   * <tr>
42   * <th>属性名</th>
43   * <th>値</th>
44   * <th>型</th>
45   * </tr>
46   * </thead><tbody>
47   * <tr>
48   * <td>{@link CubbyConstants#ATTR_CONTEXT_PATH}</td>
49   * <td>コンテキストパス</td>
50   * <td>{@link String}</td>
51   * </tr>
52   * <tr>
53   * <td>{@link CubbyConstants#ATTR_ACTION}</td>
54   * <td>アクション</td>
55   * <td>{@link org.seasar.cubby.action.Action}</td>
56   * </tr>
57   * <tr>
58   * <td>{@link CubbyConstants#ATTR_MESSAGES}</td>
59   * <td>メッセージリソース</td>
60   * <td>{@link java.util.Map}</td>
61   * </tr>
62   * <tr>
63   * <td>アクションのプロパティ名</td>
64   * <td>アクションのプロパティ値</td>
65   * <td>任意</td>
66   * </tr>
67   * </table> これらの属性は通常の属性よりも優先されるのでご注意ください。
68   * </p>
69   * 
70   * @author baba
71   * @since 1.0.0
72   */
73  class CubbyHttpServletRequestWrapper extends HttpServletRequestWrapper {
74  
75  	/**
76  	 * インスタンス化します。
77  	 * 
78  	 * @param request
79  	 *            ラップするリクエスト
80  	 */
81  	public CubbyHttpServletRequestWrapper(final HttpServletRequest request) {
82  		super(request);
83  	}
84  
85  	/**
86  	 * リクエストの属性を取得します。
87  	 * 
88  	 * @param name
89  	 *            属性名
90  	 */
91  	@Override
92  	public Object getAttribute(final String name) {
93  		final Object attribute;
94  		if (ATTR_CONTEXT_PATH.equals(name)) {
95  			attribute = this.getContextPath();
96  		} else if (ATTR_MESSAGES.equals(name)) {
97  			attribute = ThreadContext.getMessagesMap();
98  		} else {
99  			final Action action = (Action) super.getAttribute(ATTR_ACTION);
100 			if (action != null) {
101 				final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(action
102 						.getClass());
103 				if (beanDesc.hasPropertyDesc(name)) {
104 					final PropertyDesc propertyDesc = beanDesc
105 							.getPropertyDesc(name);
106 					if (propertyDesc.isReadable()) {
107 						attribute = propertyDesc.getValue(action);
108 					} else {
109 						attribute = super.getAttribute(name);
110 					}
111 				} else {
112 					attribute = super.getAttribute(name);
113 				}
114 			} else {
115 				attribute = super.getAttribute(name);
116 			}
117 		}
118 		return attribute;
119 	}
120 
121 	/**
122 	 * 属性名の列挙を返します。
123 	 * 
124 	 * @return 属性名の列挙
125 	 */
126 	@SuppressWarnings("unchecked")
127 	@Override
128 	public Enumeration getAttributeNames() {
129 		final List attributeNames = new ArrayList();
130 
131 		attributeNames.add(ATTR_CONTEXT_PATH);
132 		attributeNames.add(ATTR_ACTION);
133 		attributeNames.add(ATTR_MESSAGES);
134 
135 		final Action action = (Action) super.getAttribute(ATTR_ACTION);
136 		if (action != null) {
137 			final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(action
138 					.getClass());
139 			for (int i = 0; i < beanDesc.getPropertyDescSize(); i++) {
140 				final PropertyDesc propertyDesc = beanDesc.getPropertyDesc(i);
141 				if (propertyDesc.isReadable()) {
142 					attributeNames.add(propertyDesc.getPropertyName());
143 				}
144 			}
145 		}
146 
147 		final Enumeration defaultAttributeNames = super.getAttributeNames();
148 		while (defaultAttributeNames.hasMoreElements()) {
149 			attributeNames.add(defaultAttributeNames.nextElement());
150 		}
151 		return new IteratorEnumeration(attributeNames.iterator());
152 	}
153 
154 }