Package org.springframework.web.context.request

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


    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


  protected void doFilterInternal(
      HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
      throws ServletException, IOException {

    ServletRequestAttributes attributes = new ServletRequestAttributes(request);
    LocaleContextHolder.setLocale(request.getLocale());
    RequestContextHolder.setRequestAttributes(attributes);
    if (logger.isDebugEnabled()) {
      logger.debug("Bound request context to thread: " + request);
    }
    try {
      filterChain.doFilter(request, response);
    }
    finally {
      RequestContextHolder.setRequestAttributes(null);
      LocaleContextHolder.setLocale(null);
      attributes.requestCompleted();
      if (logger.isDebugEnabled()) {
        logger.debug("Cleared thread-bound request context: " + request);
      }
    }
  }
View Full Code Here

          String value = ((String)appList.get(key)).trim();
          String jsp = "StartJNLPApplication.jsp?appName=" + key + "&appStartClass=" + value;
          /*
           * create launch URL
           */
          ServletRequestAttributes requestAttributes;
          try {
            requestAttributes = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
            /*
             * we are running distributed
             */
            HttpServletRequest request = requestAttributes.getRequest();
            value = request.getScheme() + "://" + InetAddress.getLocalHost().getHostName() + ":" + request.getServerPort() + request.getContextPath() + "/" + jsp;

          } catch (IllegalStateException e1) {
            /*
             * we are running locally
View Full Code Here

        String jsp = "StartJNLPApplication.jsp?appName=" + key
            + "&appStartClass=" + value;
        /*
         * create launch URL
         */
        ServletRequestAttributes requestAttributes;
        try {
          requestAttributes = (ServletRequestAttributes) RequestContextHolder
              .currentRequestAttributes();
          /*
           * we are running distributed
           */
          HttpServletRequest request = requestAttributes.getRequest();
          value = request.getScheme() + "://"
              + InetAddress.getLocalHost().getHostName() + ":"
              + request.getServerPort()
              + request.getContextPath() + "/" + jsp;

View Full Code Here

  protected void doFilterInternal(
      HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
      throws ServletException, IOException {

    ServletRequestAttributes attributes = new ServletRequestAttributes(request);
    LocaleContextHolder.setLocale(request.getLocale(), this.threadContextInheritable);
    RequestContextHolder.setRequestAttributes(attributes, this.threadContextInheritable);
    if (logger.isDebugEnabled()) {
      logger.debug("Bound request context to thread: " + request);
    }
    try {
      filterChain.doFilter(request, response);
    }
    finally {
      RequestContextHolder.resetRequestAttributes();
      LocaleContextHolder.resetLocaleContext();
      attributes.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 = 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());

        // 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

  protected void doFilterInternal(
      HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
      throws ServletException, IOException {

    ServletRequestAttributes attributes = new ServletRequestAttributes(request);
    LocaleContextHolder.setLocale(request.getLocale());
    RequestContextHolder.setRequestAttributes(attributes);
    if (logger.isDebugEnabled()) {
      logger.debug("Bound request context to thread: " + request);
    }
    try {
      filterChain.doFilter(request, response);
    }
    finally {
      RequestContextHolder.setRequestAttributes(null);
      LocaleContextHolder.setLocale(null);
      attributes.requestCompleted();
      if (logger.isDebugEnabled()) {
        logger.debug("Cleared thread-bound request context: " + request);
      }
    }
  }
View Full Code Here

    bd.getPropertyValues().addPropertyValue("name", "abc");
    wac.registerBeanDefinition(targetBeanName, bd);
    wac.refresh();

    HttpServletRequest request = new MockHttpServletRequest();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
    TestBean target = (TestBean) wac.getBean(targetBeanName);
    assertEquals("abc", target.getName());
    assertSame(target, request.getAttribute(targetBeanName));

    TestBean target2 = (TestBean) wac.getBean(targetBeanName);
    assertEquals("abc", target2.getName());
    assertSame(target2, target);
    assertSame(target2, request.getAttribute(targetBeanName));

    request = new MockHttpServletRequest();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
    TestBean target3 = (TestBean) wac.getBean(targetBeanName);
    assertEquals("abc", target3.getName());
    assertSame(target3, request.getAttribute(targetBeanName));
    assertNotSame(target3, target);
View Full Code Here

  }
 
  public void testPutBeanInSession() throws Exception {
    String targetBeanName = "target";
    HttpServletRequest request = new MockHttpServletRequest();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.setScope(WebApplicationContext.SCOPE_SESSION);
    bd.getPropertyValues().addPropertyValue("name", "abc");
View Full Code Here

    httpRequest.addPreferredLocale(Locale.GERMAN);

    // see RequestContextListener.requestInitialized()
    try {
      LocaleContextHolder.setLocale(httpRequest.getLocale());
      RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(httpRequest));

      LocaleContext servletLocaleContext = LocaleContextHolder.getLocaleContext();
      RequestAttributes servletRequestAttrs = RequestContextHolder.getRequestAttributes();

      MockActionRequest request = new MockActionRequest();
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.