List of usage examples for org.springframework.web.servlet HandlerExecutionChain getHandler
public Object getHandler()
From source file:au.com.gaiaresources.bdrs.controller.test.TestDataCreator.java
private ModelAndView handle(HttpServletRequest request, HttpServletResponse response) throws Exception { final HandlerMapping handlerMapping = appContext.getBean(HandlerMapping.class); final HandlerAdapter handlerAdapter = appContext.getBean(HandlerAdapter.class); final HandlerExecutionChain handler = handlerMapping.getHandler(request); Object controller = handler.getHandler(); // if you want to override any injected attributes do it here HandlerInterceptor[] interceptors = handlerMapping.getHandler(request).getInterceptors(); for (HandlerInterceptor interceptor : interceptors) { if (handleInterceptor(interceptor)) { final boolean carryOn = interceptor.preHandle(request, response, controller); if (!carryOn) { return null; }//from www. j a va2 s . co m } } ModelAndView mv = handlerAdapter.handle(request, response, controller); return mv; }
From source file:com.mystudy.source.spring.mvc.DispatcherServlet.java
/** * Handle the result of handler selection and handler invocation, which is * either a ModelAndView or an Exception to be resolved to a ModelAndView. *//*ww w .ja v a 2 s . c o m*/ private void processDispatchResult(HttpServletRequest request, HttpServletResponse response, HandlerExecutionChain mappedHandler, ModelAndView mv, Exception exception) throws Exception { boolean errorView = false; if (exception != null) { if (exception instanceof ModelAndViewDefiningException) { logger.debug("ModelAndViewDefiningException encountered", exception); mv = ((ModelAndViewDefiningException) exception).getModelAndView(); } else { Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null); mv = processHandlerException(request, response, handler, exception); errorView = (mv != null); } } // Did the handler return a view to render? if (mv != null && !mv.wasCleared()) { render(mv, request, response); if (errorView) { WebUtils.clearErrorRequestAttributes(request); } } else { if (logger.isDebugEnabled()) { logger.debug("Null ModelAndView returned to DispatcherServlet with name '" + getServletName() + "': assuming HandlerAdapter completed request handling"); } } if (WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) { // Concurrent handling started during a forward return; } if (mappedHandler != null) { mappedHandler.triggerAfterCompletion(request, response, null); } }
From source file:com.mystudy.source.spring.mvc.DispatcherServlet.java
/** 1:?. * 2:HandlerExecutionChain,Handler,handlerHandlerInterceptor? * 3:?HandlerExecutionChain.handlerHandlerAdapter,HadnlerAdapter.supports()????Handler * 4:?lastModified?servlet???,?,?/*from w w w . ja va2 s . c o m*/ * 5:mappedHandler.applyPreHandle?? * 6:ha.handle(processedRequest, response, mappedHandler.getHandler())?? * 7:asyncManager.isConcurrentHandlingStarted()?? * 9? * 10:?postHandle * WebAsyncManager:?web * ???? * 1: * HandlerExecutionChain:?Handler. * getHandler()?? * ???handlerMappings???, * for (HandlerMapping hm : this.handlerMappings) { if (logger.isTraceEnabled()) { logger.trace( "Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'"); } HandlerExecutionChain handler = hm.getHandler(request); if (handler != null) { return handler; } } return null; } HandlerExecutionChain: : private final Object handler; private HandlerInterceptor[] interceptors; private List<HandlerInterceptor> interceptorList; 1handler?HandlerInterceptor???? 2preHandle??handler 3handler??????HttpServletResponse?SpringMVCpostHandle????afterCompletion?web?? ???????? ?HandlerMapping,handlerMappings.?? HandlerInterceptor? HandlerInterceptor HandlerInterceptorSpringMVC????????????? LocaleChangeInterceptor?? HandlerAdapter boolean supports(Object handler); ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception; long getLastModified(HttpServletRequest request, Object handler); 1:RequestMappingHandlerAdapter?? * Process the actual dispatching to the handler. * <p>The handler will be obtained by applying the servlet's HandlerMappings in order. * The HandlerAdapter will be obtained by querying the servlet's installed HandlerAdapters * to find the first that supports the handler class. * <p>All HTTP methods are handled by this method. It's up to HandlerAdapters or handlers * themselves to decide which methods are acceptable. * @param request current HTTP request * @param response current HTTP response * @throws Exception in case of any kind of processing failure */ protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception { HttpServletRequest processedRequest = request; HandlerExecutionChain mappedHandler = null; boolean multipartRequestParsed = false; WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request); try { ModelAndView mv = null; Exception dispatchException = null; try { //? processedRequest = checkMultipart(request); multipartRequestParsed = processedRequest != request; //??handler // Determine handler for the current request. mappedHandler = getHandler(processedRequest, false); if (mappedHandler == null || mappedHandler.getHandler() == null) { noHandlerFound(processedRequest, response); return; } // ??HandlerAdapter Determine handler adapter for the current request. HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler()); // Process last-modified header, if supported by the handler. String method = request.getMethod(); boolean isGet = "GET".equals(method); if (isGet || "HEAD".equals(method)) { long lastModified = ha.getLastModified(request, mappedHandler.getHandler()); if (logger.isDebugEnabled()) { String requestUri = urlPathHelper.getRequestUri(request); logger.debug("Last-Modified value for [" + requestUri + "] is: " + lastModified); } if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) { return; } } //? if (!mappedHandler.applyPreHandle(processedRequest, response)) { return; } try { //?? Actually invoke the handler. mv = ha.handle(processedRequest, response, mappedHandler.getHandler()); } finally { if (asyncManager.isConcurrentHandlingStarted()) { return; } } applyDefaultViewName(request, mv); mappedHandler.applyPostHandle(processedRequest, response, mv); } catch (Exception ex) { dispatchException = ex; } processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException); } catch (Exception ex) { triggerAfterCompletion(processedRequest, response, mappedHandler, ex); } catch (Error err) { triggerAfterCompletionWithError(processedRequest, response, mappedHandler, err); } finally { if (asyncManager.isConcurrentHandlingStarted()) { // Instead of postHandle and afterCompletion mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response); return; } // Clean up any resources used by a multipart request. if (multipartRequestParsed) { cleanupMultipart(processedRequest); } } }
From source file:org.cloudifysource.rest.AttributesContollerTest.java
/** * This method finds the handler for a given request URI. It will also * ensure that the URI Parameters i.e. /context/test/{name} are added to the * request/*from w ww. java 2 s .c om*/ * * @param request * The request object to be used * @return The correct handler for the request * @throws Exception * Indicates a matching handler could not be found */ private Object getHandlerToRequest(final MockHttpServletRequest request) throws Exception { HandlerExecutionChain chain = null; final Map<String, HandlerMapping> map = applicationContext.getBeansOfType(HandlerMapping.class); for (HandlerMapping mapping : map.values()) { chain = mapping.getHandler(request); if (chain != null) { break; } } if (chain == null) { throw new InvalidParameterException( "Unable to find handler for request URI: " + request.getRequestURI()); } return chain.getHandler(); }
From source file:org.cloudifysource.rest.ControllerTest.java
private MockHttpServletResponse testRequest(final MockHttpServletRequest request, final HandlerMethod expectedHandlerMethod) throws Exception { final MockHttpServletResponse response = new MockHttpServletResponse(); final HandlerExecutionChain handlerExecutionChain = getHandlerToRequest(request); Object handler = handlerExecutionChain.getHandler(); Assert.assertEquals("Wrong handler selected for request uri: " + request.getRequestURI(), expectedHandlerMethod.toString(), handler.toString()); HandlerInterceptor[] interceptors = handlerExecutionChain.getInterceptors(); // pre handle for (HandlerInterceptor handlerInterceptor : interceptors) { handlerInterceptor.preHandle(request, response, handler); }//www.ja va2s. c o m // handle the request ModelAndView modelAndView = handlerAdapter.handle(request, response, handler); // post handle for (HandlerInterceptor handlerInterceptor : interceptors) { handlerInterceptor.postHandle(request, response, handler, modelAndView); } // validate the response Assert.assertTrue("Wrong response status: " + response.getStatus(), response.getStatus() == HttpStatus.OK.value()); Assert.assertTrue(response.getContentType().contains(MediaType.APPLICATION_JSON)); return response; }
From source file:org.fao.geonet.kernel.SpringLocalServiceInvoker.java
public Object invoke(HttpServletRequest request, HttpServletResponse response) throws Exception { HandlerExecutionChain handlerExecutionChain = requestMappingHandlerMapping.getHandler(request); HandlerMethod handlerMethod = (HandlerMethod) handlerExecutionChain.getHandler(); ServletInvocableHandlerMethod servletInvocableHandlerMethod = new ServletInvocableHandlerMethod( handlerMethod);/* w w w . j ava 2s. co m*/ servletInvocableHandlerMethod.setHandlerMethodArgumentResolvers(argumentResolvers); servletInvocableHandlerMethod.setHandlerMethodReturnValueHandlers(returnValueHandlers); servletInvocableHandlerMethod.setDataBinderFactory(webDataBinderFactory); Object o = servletInvocableHandlerMethod.invokeForRequest(new ServletWebRequest(request, response), null, new Object[0]); // check whether we need to further process a "forward:" response if (o instanceof String) { String checkForward = (String) o; if (checkForward.startsWith("forward:")) { // // if the original url ends with the first component of the fwd url, then concatenate them, otherwise // just invoke it and hope for the best... // eg. local://srv/api/records/urn:marlin.csiro.au:org:1_organisation_name // returns forward:urn:marlin.csiro.au:org:1_organisation_name/formatters/xml // so we join the original url and the forwarded url as: // /api/records/urn:marlin.csiro.au:org:1_organisation_name/formatters/xml and invoke it. // String fwdUrl = StringUtils.substringAfter(checkForward, "forward:"); String lastComponent = StringUtils.substringAfterLast(request.getRequestURI(), "/"); if (lastComponent.length() > 0 && StringUtils.startsWith(fwdUrl, lastComponent)) { return invoke(request.getRequestURI() + StringUtils.substringAfter(fwdUrl, lastComponent)); } else { return invoke(fwdUrl); } } } return o; }
From source file:org.gwtwidgets.server.spring.test.TestHandler.java
public void testServiceInvocation() throws Exception { logger.info("Testing simple invocation"); HandlerExecutionChain chain = handler.getHandler(requestService); GWTRPCServiceExporter exporter = (GWTRPCServiceExporter) chain.getHandler(); ServiceTest service = (ServiceTest) exporter.getService(); assertEquals(service.add(3, -5), -2); }
From source file:org.gwtwidgets.server.spring.test.TestHandler.java
public void testExceptionTranslation() throws Exception { logger.info("Testing exception translation for plain service"); HandlerExecutionChain chain = handler.getHandler(requestService); GWTRPCServiceExporter exporter = (GWTRPCServiceExporter) chain.getHandler(); ServiceTest service = (ServiceTest) exporter.getService(); try {//w w w . j av a2 s. co m service.throwDeclaredException(); } catch (Throwable t) { assertTrue(t instanceof CustomException); } }
From source file:org.gwtwidgets.server.spring.test.TestHandler.java
public void testExceptionTranslationTx() throws Exception { logger.info("Testing exception translation for proxied service"); HandlerExecutionChain chain = handler.getHandler(requestService); GWTRPCServiceExporter exporter = (GWTRPCServiceExporter) chain.getHandler(); ServiceTest service = (ServiceTest) exporter.getService(); try {//from w w w. j av a2 s . c o m service.throwDeclaredException(); } catch (Throwable t) { assertTrue(t instanceof CustomException); } }
From source file:org.gwtwidgets.server.spring.test.TestRPCExporter.java
public void testExporter() throws Exception { try {//from ww w .jav a 2s . co m logger.info("stressTestExporter: testing RPC"); HttpServletRequest serviceRequest = getServiceRequest(); HandlerExecutionChain chain = handlerMapping.getHandler(serviceRequest); GWTRPCServiceExporter exporter = (GWTRPCServiceExporter) chain.getHandler(); long start = System.currentTimeMillis(); for (int i = 0; i < testCount; i++) { MockHttpServletResponse serviceResponse = getServiceResponse(); exporter.handleRequest(serviceRequest, serviceResponse); String sResponse = serviceResponse.getContentAsString(); assertEquals(responsePayload, sResponse); } long end = System.currentTimeMillis(); logger.info("stressTestExporter: " + testCount + " invocations in " + (end - start) + " ms"); } catch (Throwable t) { logger.error(t.getMessage(), t); fail(); } }