1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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.CubbyConstants; |
31 | |
import org.seasar.cubby.controller.ActionContext; |
32 | |
import org.seasar.cubby.controller.ThreadContext; |
33 | |
import org.seasar.framework.beans.BeanDesc; |
34 | |
import org.seasar.framework.beans.PropertyDesc; |
35 | |
import org.seasar.framework.beans.factory.BeanDescFactory; |
36 | |
import org.seasar.framework.container.ComponentDef; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public class CubbyHttpServletRequestWrapper extends HttpServletRequestWrapper { |
75 | |
|
76 | |
|
77 | |
private final ActionContext context; |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public CubbyHttpServletRequestWrapper(final HttpServletRequest request, |
88 | |
final ActionContext context) { |
89 | 0 | super(request); |
90 | |
|
91 | 0 | this.context = context; |
92 | 0 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
@Override |
101 | |
public Object getAttribute(final String name) { |
102 | |
final Object attribute; |
103 | 0 | if (ATTR_CONTEXT_PATH.equals(name)) { |
104 | 0 | attribute = this.getContextPath(); |
105 | 0 | } else if (ATTR_ACTION.equals(name)) { |
106 | 0 | attribute = context.getAction(); |
107 | 0 | } else if (ATTR_MESSAGES.equals(name)) { |
108 | 0 | attribute = ThreadContext.getMessagesMap(); |
109 | |
} else { |
110 | 0 | if (context.isInitialized()) { |
111 | 0 | final ComponentDef componentDef = context.getComponentDef(); |
112 | 0 | final Class<?> concreteClass = componentDef.getConcreteClass(); |
113 | 0 | final BeanDesc beanDesc = BeanDescFactory |
114 | |
.getBeanDesc(concreteClass); |
115 | 0 | if (beanDesc.hasPropertyDesc(name)) { |
116 | 0 | final PropertyDesc propertyDesc = beanDesc |
117 | |
.getPropertyDesc(name); |
118 | 0 | if (propertyDesc.isReadable()) { |
119 | 0 | attribute = propertyDesc.getValue(context.getAction()); |
120 | |
} else { |
121 | 0 | attribute = super.getAttribute(name); |
122 | |
} |
123 | 0 | } else { |
124 | 0 | attribute = super.getAttribute(name); |
125 | |
} |
126 | 0 | } else { |
127 | 0 | attribute = super.getAttribute(name); |
128 | |
} |
129 | |
} |
130 | 0 | return attribute; |
131 | |
} |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
@SuppressWarnings("unchecked") |
139 | |
@Override |
140 | |
public Enumeration getAttributeNames() { |
141 | 0 | final List attributeNames = new ArrayList(); |
142 | |
|
143 | 0 | attributeNames.add(ATTR_ACTION); |
144 | |
|
145 | 0 | final Class<?> concreteClass = context.getComponentDef() |
146 | |
.getConcreteClass(); |
147 | 0 | final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(concreteClass); |
148 | 0 | for (int i = 0; i < beanDesc.getPropertyDescSize(); i++) { |
149 | 0 | final PropertyDesc propertyDesc = beanDesc.getPropertyDesc(i); |
150 | 0 | if (propertyDesc.isReadable()) { |
151 | 0 | attributeNames.add(propertyDesc.getPropertyName()); |
152 | |
} |
153 | |
} |
154 | |
|
155 | 0 | final Enumeration defaultAttributeNames = super.getAttributeNames(); |
156 | 0 | while (defaultAttributeNames.hasMoreElements()) { |
157 | 0 | attributeNames.add(defaultAttributeNames.nextElement()); |
158 | |
} |
159 | 0 | return new IteratorEnumeration(attributeNames.iterator()); |
160 | |
} |
161 | |
|
162 | 0 | private static class IteratorEnumeration<T> implements Enumeration<T> { |
163 | |
|
164 | |
private final Iterator<T> iterator; |
165 | |
|
166 | 0 | private IteratorEnumeration(final Iterator<T> iterator) { |
167 | 0 | this.iterator = iterator; |
168 | 0 | } |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
public boolean hasMoreElements() { |
174 | 0 | return iterator.hasNext(); |
175 | |
} |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
public T nextElement() { |
181 | 0 | return iterator.next(); |
182 | |
} |
183 | |
|
184 | |
} |
185 | |
|
186 | |
} |