Search in sources :

Example 1 with NestedServletException

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

the class HttpInvokerServiceExporter method handleRequest.

/**
	 * Reads a remote invocation from the request, executes it,
	 * and writes the remote invocation result to the response.
	 * @see #readRemoteInvocation(HttpServletRequest)
	 * @see #invokeAndCreateResult(org.springframework.remoting.support.RemoteInvocation, Object)
	 * @see #writeRemoteInvocationResult(HttpServletRequest, HttpServletResponse, RemoteInvocationResult)
	 */
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        RemoteInvocation invocation = readRemoteInvocation(request);
        RemoteInvocationResult result = invokeAndCreateResult(invocation, getProxy());
        writeRemoteInvocationResult(request, response, result);
    } catch (ClassNotFoundException ex) {
        throw new NestedServletException("Class not found during deserialization", ex);
    }
}
Also used : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) NestedServletException(org.springframework.web.util.NestedServletException) RemoteInvocationResult(org.springframework.remoting.support.RemoteInvocationResult)

Example 2 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 3 with NestedServletException

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

the class MetricFilterAutoConfigurationTests method controllerMethodThatThrowsUnhandledException.

@Test
public void controllerMethodThatThrowsUnhandledException() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class, MetricFilterAutoConfiguration.class);
    Filter filter = context.getBean(Filter.class);
    MockMvc mvc = MockMvcBuilders.standaloneSetup(new MetricFilterTestController()).addFilter(filter).build();
    try {
        mvc.perform(get("/unhandledException")).andExpect(status().isInternalServerError());
    } catch (NestedServletException ex) {
    // Expected
    }
    verify(context.getBean(CounterService.class)).increment("status.500.unhandledException");
    verify(context.getBean(GaugeService.class)).submit(eq("response.unhandledException"), anyDouble());
    context.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) NestedServletException(org.springframework.web.util.NestedServletException) Filter(javax.servlet.Filter) OncePerRequestFilter(org.springframework.web.filter.OncePerRequestFilter) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 4 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 5 with NestedServletException

use of org.springframework.web.util.NestedServletException in project spring-cloud-sleuth by spring-cloud.

the class MyFilter method should_log_tracing_information_when_500_exception_was_thrown.

@Test
public void should_log_tracing_information_when_500_exception_was_thrown() throws Exception {
    Long expectedTraceId = new Random().nextLong();
    try {
        whenSentToExceptionThrowingEndpoint(expectedTraceId);
        fail("Should fail");
    } catch (NestedServletException e) {
        then(e).hasRootCauseInstanceOf(RuntimeException.class);
    }
    // we need to dump the span cause it's not in TraceFilter since TF
    // has also error dispatch and the ErrorController would report the span
    then(this.reporter.getSpans()).hasSize(1);
    then(this.reporter.getSpans().get(0).tags()).containsEntry("error", "Request processing failed; nested exception is java.lang.RuntimeException");
}
Also used : NestedServletException(org.springframework.web.util.NestedServletException) Random(java.util.Random) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Aggregations

NestedServletException (org.springframework.web.util.NestedServletException)22 IOException (java.io.IOException)5 Test (org.junit.Test)5 RemoteInvocation (org.springframework.remoting.support.RemoteInvocation)4 RemoteInvocationResult (org.springframework.remoting.support.RemoteInvocationResult)4 ServletException (javax.servlet.ServletException)3 Test (org.junit.jupiter.api.Test)3 WebAsyncManager (org.springframework.web.context.request.async.WebAsyncManager)3 ServletException (jakarta.servlet.ServletException)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Map (java.util.Map)2 Transactional (javax.transaction.Transactional)2 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)2 SerializationException (com.haulmont.cuba.core.sys.serialization.SerializationException)1 User (com.serotonin.m2m2.vo.User)1 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)1 RequestDispatcher (jakarta.servlet.RequestDispatcher)1