Search in sources :

Example 6 with NestedServletException

use of org.springframework.web.util.NestedServletException in project spring-framework by spring-projects.

the class FrameworkServlet method processRequest.

/**
	 * Process this request, publishing an event regardless of the outcome.
	 * <p>The actual event handling is performed by the abstract
	 * {@link #doService} template method.
	 */
protected final void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    long startTime = System.currentTimeMillis();
    Throwable failureCause = null;
    LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
    LocaleContext localeContext = buildLocaleContext(request);
    RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
    ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());
    initContextHolders(request, localeContext, requestAttributes);
    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 {
        resetContextHolders(request, previousLocaleContext, previousAttributes);
        if (requestAttributes != null) {
            requestAttributes.requestCompleted();
        }
        if (logger.isDebugEnabled()) {
            if (failureCause != null) {
                this.logger.debug("Could not complete request", failureCause);
            } else {
                if (asyncManager.isConcurrentHandlingStarted()) {
                    logger.debug("Leaving response open for concurrent processing");
                } else {
                    this.logger.debug("Successfully completed request");
                }
            }
        }
        publishRequestHandledEvent(request, response, startTime, failureCause);
    }
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) NestedServletException(org.springframework.web.util.NestedServletException) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) IOException(java.io.IOException)

Example 7 with NestedServletException

use of org.springframework.web.util.NestedServletException in project opennms by OpenNMS.

the class ServiceRegistryHttpInvokerServiceExporter method handleRequest.

public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        RemoteInvocation invocation = readRemoteInvocation(request);
        Serializable interfaceNameObject = invocation.getAttribute(ServiceRegistryHttpInvokerProxyFactoryBean.ATTRIBUTE_INTERFACE_NAME);
        if (interfaceNameObject == null) {
            throw new NestedServletException("Interface name attribute not found. This class can only service requests to a " + ServiceRegistryHttpInvokerProxyFactoryBean.class.getSimpleName() + " client.");
        } else {
            String interfaceName = (String) interfaceNameObject;
            try {
                //RemoteInvocationResult result = invokeAndCreateResult(invocation, getProxy());
                // TODO: Use a method similar to {@link RemoteExporter#getProxyForService()} to create an
                // interface proxy that masks any other methods on the remotely invoked object.
                RemoteInvocationResult result = invokeAndCreateResult(invocation, serviceRegistry.findProvider(Class.forName(interfaceName)));
                writeRemoteInvocationResult(request, response, result);
            } catch (IllegalArgumentException e) {
                throw new NestedServletException("No provider registered for interface " + interfaceName, e);
            }
        }
    } catch (ClassNotFoundException e) {
        throw new NestedServletException("Class not found during deserialization", e);
    }
}
Also used : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) Serializable(java.io.Serializable) NestedServletException(org.springframework.web.util.NestedServletException) RemoteInvocationResult(org.springframework.remoting.support.RemoteInvocationResult)

Example 8 with NestedServletException

use of org.springframework.web.util.NestedServletException in project spring-framework by spring-projects.

the class ExceptionHandlerExceptionResolverTests method resolveExceptionWithAssertionError.

@Test
public void resolveExceptionWithAssertionError() throws Exception {
    AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyConfig.class);
    this.resolver.setApplicationContext(cxt);
    this.resolver.afterPropertiesSet();
    AssertionError err = new AssertionError("argh");
    HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle");
    ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, new NestedServletException("Handler dispatch failed", err));
    assertNotNull("Exception was not handled", mav);
    assertTrue(mav.isEmpty());
    assertEquals(err.toString(), this.response.getContentAsString());
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) NestedServletException(org.springframework.web.util.NestedServletException) ModelAndView(org.springframework.web.servlet.ModelAndView) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 9 with NestedServletException

use of org.springframework.web.util.NestedServletException in project spring-security-oauth by spring-projects.

the class OAuth2ClientContextFilter method doFilter.

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    request.setAttribute(CURRENT_URI, calculateCurrentUri(request));
    try {
        chain.doFilter(servletRequest, servletResponse);
    } catch (IOException ex) {
        throw ex;
    } catch (Exception ex) {
        // Try to extract a SpringSecurityException from the stacktrace
        Throwable[] causeChain = throwableAnalyzer.determineCauseChain(ex);
        UserRedirectRequiredException redirect = (UserRedirectRequiredException) throwableAnalyzer.getFirstThrowableOfType(UserRedirectRequiredException.class, causeChain);
        if (redirect != null) {
            redirectUser(redirect, request, response);
        } else {
            if (ex instanceof ServletException) {
                throw (ServletException) ex;
            }
            if (ex instanceof RuntimeException) {
                throw (RuntimeException) ex;
            }
            throw new NestedServletException("Unhandled exception", ex);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) NestedServletException(org.springframework.web.util.NestedServletException) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) UserRedirectRequiredException(org.springframework.security.oauth2.client.resource.UserRedirectRequiredException) NestedServletException(org.springframework.web.util.NestedServletException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UserRedirectRequiredException(org.springframework.security.oauth2.client.resource.UserRedirectRequiredException)

Aggregations

NestedServletException (org.springframework.web.util.NestedServletException)9 IOException (java.io.IOException)4 ServletException (javax.servlet.ServletException)4 Test (org.junit.Test)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 RemoteInvocation (org.springframework.remoting.support.RemoteInvocation)2 RemoteInvocationResult (org.springframework.remoting.support.RemoteInvocationResult)2 WebAsyncManager (org.springframework.web.context.request.async.WebAsyncManager)2 Serializable (java.io.Serializable)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Filter (javax.servlet.Filter)1 ServletRequest (javax.servlet.ServletRequest)1 ServletResponse (javax.servlet.ServletResponse)1 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)1 BeanWrapper (org.springframework.beans.BeanWrapper)1 BeansException (org.springframework.beans.BeansException)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 PropertyValues (org.springframework.beans.PropertyValues)1