01: /*
02: * Copyright 2005-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
05: * in compliance with the License. You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software distributed under the License
10: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11: * or implied. See the License for the specific language governing permissions and limitations under
12: * the License.
13: */
14:
15: package org.strecks.injection.handler;
16:
17: import javax.servlet.ServletContext;
18: import javax.servlet.http.HttpServletRequest;
19: import javax.servlet.http.HttpSession;
20:
21: import org.strecks.context.ActionContext;
22: import org.strecks.util.Assert;
23:
24: /**
25: * Injection handler which searches for named attribute in the following scopes (listed in order)
26: * <i>request</i>, <i>session</i> then <i>application</i>. Use this instead of
27: * <code>RequestAttributeInjectionHandler</code>, <code>SessionAttributeInjectionHandler</code>
28: * or <code>ContextAttributeInjectionHandler</code> if your application does not know what scope
29: * to find the attribute in
30: * @author Phil Zoio
31: */
32: public class ScopedAttributeInjectionHandler implements
33: InjectionHandler {
34:
35: private String attributeName;
36:
37: public ScopedAttributeInjectionHandler(String attributeName) {
38: super ();
39: Assert.notNull(attributeName);
40: this .attributeName = attributeName;
41: }
42:
43: public String getAttributeName() {
44: return attributeName;
45: }
46:
47: public Object getValue(ActionContext injectionContext) {
48:
49: HttpServletRequest request = injectionContext.getRequest();
50:
51: Object attribute = request.getAttribute(attributeName);
52: if (attribute == null) {
53: HttpSession session = request.getSession();
54: attribute = session.getAttribute(attributeName);
55: }
56: if (attribute == null) {
57: ServletContext context = injectionContext.getContext();
58: attribute = context.getAttribute(attributeName);
59: }
60: return attribute;
61: }
62:
63: }
|