001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2007
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.common.web.interceptor;
025:
026: import java.util.List;
027:
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030:
031: import org.springframework.beans.factory.InitializingBean;
032: import org.springframework.web.context.request.WebRequestInterceptor;
033: import org.springframework.web.servlet.HandlerInterceptor;
034: import org.springframework.web.servlet.ModelAndView;
035: import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter;
036:
037: /**
038: * HandlerInterceptor that delegates calls to a list of interceptors.
039: * Supported interceptor types are HandlerInterceptor and WebRequestInterceptor.
040: *
041: * @see #setInterceptors(List)
042: * @author Felix Gnass [fgnass at neteye dot de]
043: * @since 6.5
044: */
045: public class ChainedHandlerInterceptor implements HandlerInterceptor,
046: InitializingBean {
047:
048: private List interceptors;
049:
050: private HandlerInterceptor[] adaptedInterceptors;
051:
052: /**
053: * Set the interceptors to apply in a chain.
054: * <p>Supported interceptor types are HandlerInterceptor and WebRequestInterceptor.
055: * @param interceptors array of handler interceptors, or <code>null</code> if none
056: * @see org.springframework.web.servlet.HandlerInterceptor
057: * @see org.springframework.web.context.request.WebRequestInterceptor
058: */
059: public void setInterceptors(List interceptors) {
060: this .interceptors = interceptors;
061: }
062:
063: /**
064: * Calls the {@link #initInterceptors()} method.
065: */
066: public void afterPropertiesSet() throws Exception {
067: initInterceptors();
068: }
069:
070: /**
071: * Initialize the specified interceptors, adapting them where necessary.
072: * @see #setInterceptors
073: * @see #adaptInterceptor
074: */
075: protected void initInterceptors() {
076: if (interceptors != null) {
077: adaptedInterceptors = new HandlerInterceptor[interceptors
078: .size()];
079: for (int i = 0; i < adaptedInterceptors.length; i++) {
080: Object interceptor = interceptors.get(i);
081: if (interceptor == null) {
082: throw new IllegalArgumentException("Entry number "
083: + i + " in interceptors list is null");
084: }
085: adaptedInterceptors[i] = adaptInterceptor(interceptor);
086: }
087: } else {
088: adaptedInterceptors = new HandlerInterceptor[0];
089: }
090: }
091:
092: /**
093: * Adapt the given interceptor object to the HandlerInterceptor interface.
094: * <p>Supported interceptor types are HandlerInterceptor and WebRequestInterceptor.
095: * Each given WebRequestInterceptor will be wrapped in a WebRequestHandlerInterceptorAdapter.
096: * Can be overridden in subclasses.
097: * @param interceptor the specified interceptor object
098: * @return the interceptor wrapped as HandlerInterceptor
099: * @see org.springframework.web.servlet.HandlerInterceptor
100: * @see org.springframework.web.context.request.WebRequestInterceptor
101: * @see WebRequestHandlerInterceptorAdapter
102: */
103: protected HandlerInterceptor adaptInterceptor(Object interceptor) {
104: if (interceptor instanceof HandlerInterceptor) {
105: return (HandlerInterceptor) interceptor;
106: } else if (interceptor instanceof WebRequestInterceptor) {
107: return new WebRequestHandlerInterceptorAdapter(
108: (WebRequestInterceptor) interceptor);
109: } else {
110: throw new IllegalArgumentException(
111: "Interceptor type not supported: "
112: + interceptor.getClass().getName());
113: }
114: }
115:
116: /**
117: * Delegates the call to all configured interceptors. If an interceptor
118: * returns <code>false</code> the chain is not further processes and the
119: * method returns <code>false</code> itself. Otherwise <code>true</code>
120: * is returned after all interceptors have been called.
121: */
122: public boolean preHandle(HttpServletRequest request,
123: HttpServletResponse response, Object handler)
124: throws Exception {
125:
126: for (int i = 0; i < adaptedInterceptors.length; i++) {
127: HandlerInterceptor interceptor = adaptedInterceptors[i];
128: if (!interceptor.preHandle(request, response, handler)) {
129: return false;
130: }
131: }
132: return true;
133: }
134:
135: /**
136: * Delegates the call to all configured interceptors.
137: */
138: public void postHandle(HttpServletRequest request,
139: HttpServletResponse response, Object handler,
140: ModelAndView modelAndView) throws Exception {
141:
142: for (int i = 0; i < adaptedInterceptors.length; i++) {
143: HandlerInterceptor interceptor = adaptedInterceptors[i];
144: interceptor.postHandle(request, response, handler,
145: modelAndView);
146: }
147: }
148:
149: /**
150: * Delegates the call to all configured interceptors.
151: */
152: public void afterCompletion(HttpServletRequest request,
153: HttpServletResponse response, Object handler, Exception ex)
154: throws Exception {
155:
156: for (int i = 0; i < adaptedInterceptors.length; i++) {
157: HandlerInterceptor interceptor = adaptedInterceptors[i];
158: interceptor.afterCompletion(request, response, handler, ex);
159: }
160: }
161:
162: }
|