Example usage for org.springframework.web.servlet.mvc.method.annotation ServletInvocableHandlerMethod invokeForRequest

List of usage examples for org.springframework.web.servlet.mvc.method.annotation ServletInvocableHandlerMethod invokeForRequest

Introduction

In this page you can find the example usage for org.springframework.web.servlet.mvc.method.annotation ServletInvocableHandlerMethod invokeForRequest.

Prototype

@Nullable
public Object invokeForRequest(NativeWebRequest request, @Nullable ModelAndViewContainer mavContainer,
        Object... providedArgs) throws Exception 

Source Link

Document

Invoke the method after resolving its argument values in the context of the given request.

Usage

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);//  ww  w.ja va  2 s  .c  o  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;
}