Package org.springframework.web.context.request

Examples of org.springframework.web.context.request.ServletRequestAttributes


                    "Not operating in context of a Spring-processed Request?");
        }

        if (requestAttributes instanceof ServletRequestAttributes) {

            final ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
            final HttpServletRequest request = servletRequestAttributes.getRequest();
            return request.getContextPath();

        } else if (requestAttributes instanceof PortletRequestAttributes) {

            final PortletRequestAttributes portletRequestAttributes = (PortletRequestAttributes) requestAttributes;
View Full Code Here


    LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
    LocaleContextHolder.setLocaleContext(buildLocaleContext(request), this.threadContextInheritable);

    // Expose current RequestAttributes to current thread.
    RequestAttributes previousRequestAttributes = RequestContextHolder.getRequestAttributes();
    ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);

    if (logger.isDebugEnabled()) {
      logger.debug("Bound request context to thread: " + request);
    }
   
    try {
      ModelAndView mv = null;
      try {
        processedRequest = checkMultipart(request);

        // Determine handler for the current request.
        mappedHandler = getHandler(processedRequest, false);
        if (mappedHandler == null || mappedHandler.getHandler() == null) {
          noHandlerFound(processedRequest, response);
          return;
        }

        // Apply preHandle methods of registered interceptors.
        HandlerInterceptor[] interceptors = mappedHandler.getInterceptors();
        if (interceptors != null) {
          for (int i = 0; i < interceptors.length; i++) {
            HandlerInterceptor interceptor = interceptors[i];
            if (!interceptor.preHandle(processedRequest, response, mappedHandler.getHandler())) {
              triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null);
              return;
            }
            interceptorIndex = i;
          }
        }

        // Actually invoke the handler.
        HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
        mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

        // Do we need view name translation?
        if (mv != null && !mv.hasView()) {
          mv.setViewName(getDefaultViewName(request));
        }

        // Apply postHandle methods of registered interceptors.
        if (interceptors != null) {
          for (int i = interceptors.length - 1; i >= 0; i--) {
            HandlerInterceptor interceptor = interceptors[i];
            interceptor.postHandle(processedRequest, response, mappedHandler.getHandler(), mv);
          }
        }
      }
      catch (ModelAndViewDefiningException ex) {
        logger.debug("ModelAndViewDefiningException encountered", ex);
        mv = ex.getModelAndView();
      }
      catch (Exception ex) {
        Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
        mv = processHandlerException(processedRequest, response, handler, ex);
      }

      // Did the handler return a view to render?
      if (mv != null && !mv.wasCleared()) {
        render(mv, processedRequest, response);
      }
      else {
        if (logger.isDebugEnabled()) {
          logger.debug("Null ModelAndView returned to DispatcherServlet with name '" +
              getServletName() + "': assuming HandlerAdapter completed request handling");
        }
      }

      // Trigger after-completion for successful outcome.
      triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null);
    }

    catch (Exception ex) {
      // Trigger after-completion for thrown exception.
      triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);
      throw ex;
    }
    catch (Error err) {
      ServletException ex = new NestedServletException("Handler processing failed", err);
      // Trigger after-completion for thrown exception.
      triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);
      throw ex;
    }

    finally {
      // Clean up any resources used by a multipart request.
      if (processedRequest != request) {
        cleanupMultipart(processedRequest);
      }

      // Reset thread-bound context.
      RequestContextHolder.setRequestAttributes(previousRequestAttributes, this.threadContextInheritable);
      LocaleContextHolder.setLocaleContext(previousLocaleContext, this.threadContextInheritable);

      // Clear request attributes.
      requestAttributes.requestCompleted();
      if (logger.isDebugEnabled()) {
        logger.debug("Cleared thread-bound request context: " + request);
      }
    }
  }
View Full Code Here

    LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
    LocaleContextHolder.setLocaleContext(buildLocaleContext(request), this.threadContextInheritable);

    // Expose current RequestAttributes to current thread.
    RequestAttributes previousRequestAttributes = RequestContextHolder.getRequestAttributes();
    ServletRequestAttributes requestAttributes = null;
    if (previousRequestAttributes == null || previousRequestAttributes.getClass().equals(ServletRequestAttributes.class)) {
      requestAttributes = new ServletRequestAttributes(request);
      RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);
    }

    if (logger.isTraceEnabled()) {
      logger.trace("Bound request context to thread: " + request);
    }

    try {
      doService(request, response);
    }
    catch (ServletException ex) {
      failureCause = ex;
      throw ex;
    }
    catch (IOException ex) {
      failureCause = ex;
      throw ex;
    }
    catch (Throwable ex) {
      failureCause = ex;
      throw new NestedServletException("Request processing failed", ex);
    }

    finally {
      // Clear request attributes and reset thread-bound context.
      LocaleContextHolder.setLocaleContext(previousLocaleContext, this.threadContextInheritable);
      if (requestAttributes != null) {
        RequestContextHolder.setRequestAttributes(previousRequestAttributes, this.threadContextInheritable);
        requestAttributes.requestCompleted();
      }
      if (logger.isTraceEnabled()) {
        logger.trace("Cleared thread-bound request context: " + request);
      }
View Full Code Here

   * Thank you SO: http://stackoverflow.com/a/5140022/878361
   */
  private void startRequest() {
    request = new MockHttpServletRequest();
    request.setSession(session);
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

    // creating session
    ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
    attr.getRequest().getSession(true);
  }
View Full Code Here

    Map<String, String[]> parameters = (Map<String, String[]>) requestWrapper
        .getAttribute(Constants.EDITABLE_PARAMETER_ERROR);
    assertEquals(1, parameters.size());

    // Set request attributes on threadlocal
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(requestWrapper));

    // New Editable instance
    EditableParameterValidator validator = new EditableParameterValidator();
    Errors errors = new MapBindingResult(new HashMap<String, String>(), "");
    assertFalse(errors.hasErrors());
View Full Code Here

  @Override
  @RequestMapping(value = "${error.path:/error}", produces = "text/html")
  public ModelAndView errorHtml(HttpServletRequest request) {

    Integer status = (Integer) new ServletRequestAttributes(request).getAttribute(
        "javax.servlet.error.status_code", RequestAttributes.SCOPE_REQUEST);

    // TODO: better to toss this class completely and use an error page for 404
    switch (status) {
      case 404:
View Full Code Here

  // {!begin supportHateoas}
  @Before
  public void setup() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
  }
View Full Code Here

        CommitMonitor monitor = findRequestContext(requestContext, SimpleRequestContext.class);
        return assertNotNull(monitor, "no monitor");
    }

    private void setupSpringWebEnvironment(HttpServletRequest request) {
        ServletRequestAttributes attributes = new ServletRequestAttributes(request);
        LocaleContextHolder.setLocale(request.getLocale(), threadContextInheritable);
        RequestContextHolder.setRequestAttributes(attributes, threadContextInheritable);

        getLogger().debug("Bound request context to thread: {}", request);
    }
View Full Code Here

        request.setServletPath(servletPath);
        request.addHeader("host", host);

        request.setPathInfo(url.substring(pathLength));

      ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
      RequestContextHolder.setRequestAttributes(requestAttributes);

        HTTPRequestAdapter.parseRequest(request);
    }
View Full Code Here

        request = new MockHttpServletRequest(this.wac.getServletContext(),method, url);
        request.setContextPath(this.contextPath);
        request.setServletPath(this.servletPath);
        request.addHeader("host", host);

      ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
      RequestContextHolder.setRequestAttributes(requestAttributes);

        for (HTTPHeader header : headers) {
            request.addHeader(header.name, header.value);
        }
View Full Code Here

TOP

Related Classes of org.springframework.web.context.request.ServletRequestAttributes

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.