01: /******************************************************************************
02: * JBoss, a division of Red Hat *
03: * Copyright 2006, Red Hat Middleware, LLC, and individual *
04: * contributors as indicated by the @authors tag. See the *
05: * copyright.txt in the distribution for a full listing of *
06: * individual contributors. *
07: * *
08: * This is free software; you can redistribute it and/or modify it *
09: * under the terms of the GNU Lesser General Public License as *
10: * published by the Free Software Foundation; either version 2.1 of *
11: * the License, or (at your option) any later version. *
12: * *
13: * This software is distributed in the hope that it will be useful, *
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16: * Lesser General Public License for more details. *
17: * *
18: * You should have received a copy of the GNU Lesser General Public *
19: * License along with this software; if not, write to the Free *
20: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
21: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
22: ******************************************************************************/package org.jboss.portal.wsrp.servlet;
23:
24: import javax.servlet.http.HttpServletRequest;
25: import javax.servlet.http.HttpServletResponse;
26: import java.lang.reflect.InvocationHandler;
27: import java.lang.reflect.Method;
28:
29: /**
30: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
31: * @version $Revision: 8784 $
32: */
33: public class ServletAccess implements InvocationHandler {
34:
35: public static final ThreadLocal local = new ThreadLocal();
36:
37: protected Object next;
38:
39: public Object invoke(Object proxy, Method method, Object[] args)
40: throws Throwable {
41: String mname = method.getName();
42: if ("backgroundProcess".equals(mname)) {
43: // Noop
44: return null;
45: } else if ("setNext".equals(mname)) {
46: next = args[0];
47: return null;
48: } else if ("getNext".equals(mname)) {
49: return next;
50: } else if ("invoke".equals(mname)) {
51: try {
52: Object req = args[0];
53: Object resp = args[1];
54: Invocation invocation = new Invocation(req, resp);
55: local.set(invocation);
56: return method.invoke(next, args);
57: } finally {
58: local.set(null);
59: }
60: } else {
61: // getInfo()
62: return "A valve that setup a thread local assocation with request and response";
63: }
64: }
65:
66: public static void setRequestAndResponse(
67: HttpServletRequest request, HttpServletResponse response) {
68: local.set(new Invocation(request, response));
69: }
70:
71: public static HttpServletRequest getRequest() {
72: Invocation invocation = (Invocation) local.get();
73: return invocation != null ? (HttpServletRequest) invocation.req
74: : null;
75: }
76:
77: public static HttpServletResponse getResponse() {
78: return (HttpServletResponse) ((Invocation) local.get()).resp;
79: }
80:
81: private static class Invocation {
82: private final Object req;
83: private final Object resp;
84:
85: public Invocation(Object req, Object resp) {
86: this.req = req;
87: this.resp = resp;
88: }
89: }
90:
91: }
|