PageRenderTime 44ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/springframework-3.0.5/projects/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/handler/AbstractHandlerMapping.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 262 lines | 97 code | 28 blank | 137 comment | 15 complexity | 554638b4ebba53741272e4ba36e7b63a MD5 | raw file
  1. /*
  2. * Copyright 2002-2008 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.web.portlet.handler;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.List;
  20. import javax.portlet.PortletRequest;
  21. import org.springframework.beans.BeansException;
  22. import org.springframework.context.support.ApplicationObjectSupport;
  23. import org.springframework.core.Ordered;
  24. import org.springframework.web.context.request.WebRequestInterceptor;
  25. import org.springframework.web.portlet.HandlerExecutionChain;
  26. import org.springframework.web.portlet.HandlerInterceptor;
  27. import org.springframework.web.portlet.HandlerMapping;
  28. /**
  29. * Abstract base class for {@link org.springframework.web.portlet.HandlerMapping}
  30. * implementations. Supports ordering, a default handler, and handler interceptors.
  31. *
  32. * @author Juergen Hoeller
  33. * @author John A. Lewis
  34. * @since 2.0
  35. * @see #getHandlerInternal
  36. * @see #setDefaultHandler
  37. * @see #setInterceptors
  38. * @see org.springframework.web.portlet.HandlerInterceptor
  39. */
  40. public abstract class AbstractHandlerMapping extends ApplicationObjectSupport
  41. implements HandlerMapping, Ordered {
  42. private int order = Integer.MAX_VALUE; // default: same as non-Ordered
  43. private Object defaultHandler;
  44. private final List<Object> interceptors = new ArrayList<Object>();
  45. private boolean applyWebRequestInterceptorsToRenderPhaseOnly = true;
  46. private HandlerInterceptor[] adaptedInterceptors;
  47. /**
  48. * Specify the order value for this HandlerMapping bean.
  49. * <p>Default value is <code>Integer.MAX_VALUE</code>, meaning that it's non-ordered.
  50. * @see org.springframework.core.Ordered#getOrder()
  51. */
  52. public final void setOrder(int order) {
  53. this.order = order;
  54. }
  55. public final int getOrder() {
  56. return this.order;
  57. }
  58. /**
  59. * Set the default handler for this handler mapping.
  60. * This handler will be returned if no specific mapping was found.
  61. * <p>Default is <code>null</code>, indicating no default handler.
  62. */
  63. public void setDefaultHandler(Object defaultHandler) {
  64. this.defaultHandler = defaultHandler;
  65. }
  66. /**
  67. * Return the default handler for this handler mapping,
  68. * or <code>null</code> if none.
  69. */
  70. public Object getDefaultHandler() {
  71. return this.defaultHandler;
  72. }
  73. /**
  74. * Set the interceptors to apply for all handlers mapped by this handler mapping.
  75. * <p>Supported interceptor types are HandlerInterceptor and WebRequestInterceptor.
  76. * Each given WebRequestInterceptor will be wrapped in a WebRequestHandlerInterceptorAdapter.
  77. * @param interceptors array of handler interceptors, or <code>null</code> if none
  78. * @see #adaptInterceptor
  79. * @see org.springframework.web.portlet.HandlerInterceptor
  80. * @see org.springframework.web.context.request.WebRequestInterceptor
  81. */
  82. public void setInterceptors(Object[] interceptors) {
  83. this.interceptors.addAll(Arrays.asList(interceptors));
  84. }
  85. /**
  86. * Specify whether to apply WebRequestInterceptors to the Portlet render phase
  87. * only ("true", or whether to apply them to the Portlet action phase as well
  88. * ("false").
  89. * <p>Default is "true", since WebRequestInterceptors are usually built for
  90. * MVC-style handler execution plus rendering process (which is, for example,
  91. * the primary target scenario for "Open Session in View" interceptors,
  92. * offering lazy loading of persistent objects during view rendering).
  93. * Set this to "false" to have WebRequestInterceptors apply to the action
  94. * phase as well (for example, in case of an "Open Session in View" interceptor,
  95. * to allow for lazy loading outside of a transaction during the action phase).
  96. * @see #setInterceptors
  97. * @see org.springframework.web.context.request.WebRequestInterceptor
  98. * @see WebRequestHandlerInterceptorAdapter#WebRequestHandlerInterceptorAdapter(WebRequestInterceptor, boolean)
  99. * @see org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor
  100. */
  101. public void setApplyWebRequestInterceptorsToRenderPhaseOnly(boolean applyWebRequestInterceptorsToRenderPhaseOnly) {
  102. this.applyWebRequestInterceptorsToRenderPhaseOnly = applyWebRequestInterceptorsToRenderPhaseOnly;
  103. }
  104. /**
  105. * Initializes the interceptors.
  106. * @see #extendInterceptors(java.util.List)
  107. * @see #initInterceptors()
  108. */
  109. @Override
  110. protected void initApplicationContext() throws BeansException {
  111. extendInterceptors(this.interceptors);
  112. initInterceptors();
  113. }
  114. /**
  115. * Extension hook that subclasses can override to register additional interceptors,
  116. * given the configured interceptors (see {@link #setInterceptors}).
  117. * <p>Will be invoked before {@link #initInterceptors()} adapts the specified
  118. * interceptors into {@link HandlerInterceptor} instances.
  119. * <p>The default implementation is empty.
  120. * @param interceptors the configured interceptor List (never <code>null</code>),
  121. * allowing to add further interceptors before as well as after the existing
  122. * interceptors
  123. */
  124. protected void extendInterceptors(List interceptors) {
  125. }
  126. /**
  127. * Initialize the specified interceptors, adapting them where necessary.
  128. * @see #setInterceptors
  129. * @see #adaptInterceptor
  130. */
  131. protected void initInterceptors() {
  132. if (!this.interceptors.isEmpty()) {
  133. this.adaptedInterceptors = new HandlerInterceptor[this.interceptors.size()];
  134. for (int i = 0; i < this.interceptors.size(); i++) {
  135. Object interceptor = this.interceptors.get(i);
  136. if (interceptor == null) {
  137. throw new IllegalArgumentException("Entry number " + i + " in interceptors array is null");
  138. }
  139. this.adaptedInterceptors[i] = adaptInterceptor(interceptor);
  140. }
  141. }
  142. }
  143. /**
  144. * Adapt the given interceptor object to the HandlerInterceptor interface.
  145. * <p>Supported interceptor types are HandlerInterceptor and WebRequestInterceptor.
  146. * Each given WebRequestInterceptor will be wrapped in a WebRequestHandlerInterceptorAdapter.
  147. * Can be overridden in subclasses.
  148. * @param interceptor the specified interceptor object
  149. * @return the interceptor wrapped as HandlerInterceptor
  150. * @see #setApplyWebRequestInterceptorsToRenderPhaseOnly
  151. * @see org.springframework.web.portlet.HandlerInterceptor
  152. * @see org.springframework.web.context.request.WebRequestInterceptor
  153. * @see WebRequestHandlerInterceptorAdapter
  154. */
  155. protected HandlerInterceptor adaptInterceptor(Object interceptor) {
  156. if (interceptor instanceof HandlerInterceptor) {
  157. return (HandlerInterceptor) interceptor;
  158. }
  159. else if (interceptor instanceof WebRequestInterceptor) {
  160. return new WebRequestHandlerInterceptorAdapter(
  161. (WebRequestInterceptor) interceptor, this.applyWebRequestInterceptorsToRenderPhaseOnly);
  162. }
  163. else {
  164. throw new IllegalArgumentException("Interceptor type not supported: " + interceptor.getClass().getName());
  165. }
  166. }
  167. /**
  168. * Return the adapted interceptors as HandlerInterceptor array.
  169. * @return the array of HandlerInterceptors, or <code>null</code> if none
  170. */
  171. protected final HandlerInterceptor[] getAdaptedInterceptors() {
  172. return this.adaptedInterceptors;
  173. }
  174. /**
  175. * Look up a handler for the given request, falling back to the default
  176. * handler if no specific one is found.
  177. * @param request current portlet request
  178. * @return the corresponding handler instance, or the default handler
  179. * @see #getHandlerInternal
  180. */
  181. public final HandlerExecutionChain getHandler(PortletRequest request) throws Exception {
  182. Object handler = getHandlerInternal(request);
  183. if (handler == null) {
  184. handler = getDefaultHandler();
  185. }
  186. if (handler == null) {
  187. return null;
  188. }
  189. // Bean name or resolved handler?
  190. if (handler instanceof String) {
  191. String handlerName = (String) handler;
  192. handler = getApplicationContext().getBean(handlerName);
  193. }
  194. return getHandlerExecutionChain(handler, request);
  195. }
  196. /**
  197. * Look up a handler for the given request, returning <code>null</code> if no
  198. * specific one is found. This method is called by {@link #getHandler};
  199. * a <code>null</code> return value will lead to the default handler, if one is set.
  200. * <p>Note: This method may also return a pre-built {@link HandlerExecutionChain},
  201. * combining a handler object with dynamically determined interceptors.
  202. * Statically specified interceptors will get merged into such an existing chain.
  203. * @param request current portlet request
  204. * @return the corresponding handler instance, or <code>null</code> if none found
  205. * @throws Exception if there is an internal error
  206. * @see #getHandler
  207. */
  208. protected abstract Object getHandlerInternal(PortletRequest request) throws Exception;
  209. /**
  210. * Build a HandlerExecutionChain for the given handler, including applicable interceptors.
  211. * <p>The default implementation simply builds a standard HandlerExecutionChain with
  212. * the given handler and this handler mapping's common interceptors. Subclasses may
  213. * override this in order to extend/rearrange the list of interceptors.
  214. * <p><b>NOTE:</b> The passed-in handler object may be a raw handler or a pre-built
  215. * HandlerExecutionChain. This method should handle those two cases explicitly,
  216. * either building a new HandlerExecutionChain or extending the existing chain.
  217. * <p>For simply adding an interceptor, consider calling <code>super.getHandlerExecutionChain</code>
  218. * and invoking {@link HandlerExecutionChain#addInterceptor} on the returned chain object.
  219. * @param handler the resolved handler instance (never <code>null</code>)
  220. * @param request current HTTP request
  221. * @return the HandlerExecutionChain (never <code>null</code>)
  222. * @see #getAdaptedInterceptors()
  223. */
  224. protected HandlerExecutionChain getHandlerExecutionChain(Object handler, PortletRequest request) {
  225. if (handler instanceof HandlerExecutionChain) {
  226. HandlerExecutionChain chain = (HandlerExecutionChain) handler;
  227. chain.addInterceptors(getAdaptedInterceptors());
  228. return chain;
  229. }
  230. else {
  231. return new HandlerExecutionChain(handler, getAdaptedInterceptors());
  232. }
  233. }
  234. }