01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.http.server;
07:
08: import javax.servlet.http.HttpServletRequest;
09: import javax.servlet.http.HttpServletResponse;
10:
11: import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
12:
13: /**
14: * Base class for single-use request interceptors. This implementation sets the
15: * thread name to something sensible at the start of the request handling and
16: * resets the name at the end. This is useful for logging frameworks that make
17: * use of thread names, such as Log4J. Should not be a singleton bean! Configure
18: * as inner bean in openrdf-servlet.xml
19: *
20: * @author Herko ter Horst
21: */
22: public abstract class ServerInterceptor extends
23: HandlerInterceptorAdapter {
24:
25: private String origThreadName;
26:
27: @Override
28: public boolean preHandle(HttpServletRequest request,
29: HttpServletResponse response, Object handler)
30: throws Exception {
31: origThreadName = Thread.currentThread().getName();
32: Thread.currentThread().setName(getThreadName());
33:
34: setRequestAttributes(request);
35:
36: return super .preHandle(request, response, handler);
37: }
38:
39: @Override
40: public void afterCompletion(HttpServletRequest request,
41: HttpServletResponse response, Object handler,
42: Exception exception) throws Exception {
43: cleanUpResources();
44: Thread.currentThread().setName(origThreadName);
45: }
46:
47: /**
48: * Determine the thread name to use. Called before the request is forwarded
49: * to a handler.
50: *
51: * @param request
52: * the request
53: * @return a name that makes sense based on the request
54: * @throws ServerHTTPException
55: * if it was impossible to determine a name due to an internal error
56: */
57: protected abstract String getThreadName()
58: throws ServerHTTPException;
59:
60: /**
61: * Set attributes for this request. Called before the request is forwarded to
62: * a handler. By default, this method does nothing.
63: *
64: * @param request
65: * the request
66: * @throws ClientHTTPException
67: * if it was impossible to set one or more attributes due to a bad
68: * request on the part of the client
69: * @throws ServerHTTPException
70: * if it was impossible to set one or more attributes due to an
71: * internal error
72: */
73: protected void setRequestAttributes(HttpServletRequest request)
74: throws ClientHTTPException, ServerHTTPException {
75: }
76:
77: /**
78: * Clean up resources used in handling this request. Called after the request
79: * is handled and a the view is rendered (or an exception has occurred). By
80: * default, this method does nothing.
81: *
82: * @throws ServerHTTPException
83: * if some resources could not be cleaned up because of an internal
84: * error
85: */
86: protected void cleanUpResources() throws ServerHTTPException {
87: }
88: }
|