1 16 17 package org.springframework.web.portlet.handler; 18 19 import javax.portlet.PortletRequest; 20 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 39 public abstract class AbstractHandlerMapping extends ApplicationObjectSupport 40 implements HandlerMapping, Ordered { 41 42 private int order = Integer.MAX_VALUE; 44 private Object defaultHandler; 45 46 private Object [] interceptors; 47 48 private boolean applyWebRequestInterceptorsToRenderPhaseOnly = true; 49 50 private HandlerInterceptor[] adaptedInterceptors; 51 52 53 public final void setOrder(int order) { 54 this.order = order; 55 } 56 57 public final int getOrder() { 58 return order; 59 } 60 61 66 public void setDefaultHandler(Object defaultHandler) { 67 this.defaultHandler = defaultHandler; 68 } 69 70 74 public Object getDefaultHandler() { 75 return this.defaultHandler; 76 } 77 78 87 public final void setInterceptors(Object [] interceptors) { 88 this.interceptors = interceptors; 89 } 90 91 107 public void setApplyWebRequestInterceptorsToRenderPhaseOnly(boolean applyWebRequestInterceptorsToRenderPhaseOnly) { 108 this.applyWebRequestInterceptorsToRenderPhaseOnly = applyWebRequestInterceptorsToRenderPhaseOnly; 109 } 110 111 112 116 protected void initApplicationContext() throws BeansException { 117 initInterceptors(); 118 } 119 120 125 protected void initInterceptors() { 126 if (this.interceptors != null) { 127 this.adaptedInterceptors = new HandlerInterceptor[this.interceptors.length]; 128 for (int i = 0; i < this.interceptors.length; i++) { 129 if (this.interceptors[i] == null) { 130 throw new IllegalArgumentException ("Entry number " + i + " in interceptors array is null"); 131 } 132 this.adaptedInterceptors[i] = adaptInterceptor(this.interceptors[i]); 133 } 134 } 135 } 136 137 149 protected HandlerInterceptor adaptInterceptor(Object interceptor) { 150 if (interceptor instanceof HandlerInterceptor) { 151 return (HandlerInterceptor) interceptor; 152 } 153 else if (interceptor instanceof WebRequestInterceptor) { 154 return new WebRequestHandlerInterceptorAdapter( 155 (WebRequestInterceptor) interceptor, this.applyWebRequestInterceptorsToRenderPhaseOnly); 156 } 157 else { 158 throw new IllegalArgumentException ("Interceptor type not supported: " + interceptor.getClass().getName()); 159 } 160 } 161 162 166 protected final HandlerInterceptor[] getAdaptedInterceptors() { 167 return this.adaptedInterceptors; 168 } 169 170 171 178 public final HandlerExecutionChain getHandler(PortletRequest request) throws Exception { 179 Object handler = getHandlerInternal(request); 180 if (handler == null) { 181 handler = getDefaultHandler(); 182 } 183 if (handler == null) { 184 return null; 185 } 186 if (handler instanceof String ) { 188 String handlerName = (String ) handler; 189 handler = getApplicationContext().getBean(handlerName); 190 } 191 return getHandlerExecutionChain(handler, request); 192 } 193 194 203 protected abstract Object getHandlerInternal(PortletRequest request) throws Exception ; 204 205 215 protected HandlerExecutionChain getHandlerExecutionChain(Object handler, PortletRequest request) { 216 return new HandlerExecutionChain(handler, getAdaptedInterceptors()); 217 } 218 219 } 220 | Popular Tags |