use of jakarta.servlet.ServletException in project spring-framework by spring-projects.
the class HttpRequestHandlerTests method testHttpRequestHandlerServletPassThrough.
@Test
public void testHttpRequestHandlerServletPassThrough() throws Exception {
MockServletContext servletContext = new MockServletContext();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.getBeanFactory().registerSingleton("myHandler", (HttpRequestHandler) (req, res) -> {
assertThat(req).isSameAs(request);
assertThat(res).isSameAs(response);
String exception = request.getParameter("exception");
if ("ServletException".equals(exception)) {
throw new ServletException("test");
}
if ("IOException".equals(exception)) {
throw new IOException("test");
}
res.getWriter().write("myResponse");
});
wac.setServletContext(servletContext);
wac.refresh();
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
Servlet servlet = new HttpRequestHandlerServlet();
servlet.init(new MockServletConfig(servletContext, "myHandler"));
servlet.service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myResponse");
request.setParameter("exception", "ServletException");
assertThatExceptionOfType(ServletException.class).isThrownBy(() -> servlet.service(request, response)).withMessage("test");
request.setParameter("exception", "IOException");
assertThatIOException().isThrownBy(() -> servlet.service(request, response)).withMessage("test");
}
use of jakarta.servlet.ServletException in project spring-framework by spring-projects.
the class SpringServletContainerInitializer method onStartup.
/**
* Delegate the {@code ServletContext} to any {@link WebApplicationInitializer}
* implementations present on the application classpath.
* <p>Because this class declares @{@code HandlesTypes(WebApplicationInitializer.class)},
* Servlet containers will automatically scan the classpath for implementations of
* Spring's {@code WebApplicationInitializer} interface and provide the set of all
* such types to the {@code webAppInitializerClasses} parameter of this method.
* <p>If no {@code WebApplicationInitializer} implementations are found on the classpath,
* this method is effectively a no-op. An INFO-level log message will be issued notifying
* the user that the {@code ServletContainerInitializer} has indeed been invoked but that
* no {@code WebApplicationInitializer} implementations were found.
* <p>Assuming that one or more {@code WebApplicationInitializer} types are detected,
* they will be instantiated (and <em>sorted</em> if the @{@link
* org.springframework.core.annotation.Order @Order} annotation is present or
* the {@link org.springframework.core.Ordered Ordered} interface has been
* implemented). Then the {@link WebApplicationInitializer#onStartup(ServletContext)}
* method will be invoked on each instance, delegating the {@code ServletContext} such
* that each instance may register and configure servlets such as Spring's
* {@code DispatcherServlet}, listeners such as Spring's {@code ContextLoaderListener},
* or any other Servlet API features such as filters.
* @param webAppInitializerClasses all implementations of
* {@link WebApplicationInitializer} found on the application classpath
* @param servletContext the servlet context to be initialized
* @see WebApplicationInitializer#onStartup(ServletContext)
* @see AnnotationAwareOrderComparator
*/
@Override
public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext) throws ServletException {
List<WebApplicationInitializer> initializers = Collections.emptyList();
if (webAppInitializerClasses != null) {
initializers = new ArrayList<>(webAppInitializerClasses.size());
for (Class<?> waiClass : webAppInitializerClasses) {
// no matter what @HandlesTypes says...
if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) && WebApplicationInitializer.class.isAssignableFrom(waiClass)) {
try {
initializers.add((WebApplicationInitializer) ReflectionUtils.accessibleConstructor(waiClass).newInstance());
} catch (Throwable ex) {
throw new ServletException("Failed to instantiate WebApplicationInitializer class", ex);
}
}
}
}
if (initializers.isEmpty()) {
servletContext.log("No Spring WebApplicationInitializer types detected on classpath");
return;
}
servletContext.log(initializers.size() + " Spring WebApplicationInitializers detected on classpath");
AnnotationAwareOrderComparator.sort(initializers);
for (WebApplicationInitializer initializer : initializers) {
initializer.onStartup(servletContext);
}
}
use of jakarta.servlet.ServletException in project spring-framework by spring-projects.
the class ServletForwardingController method handleRequestInternal.
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
ServletContext servletContext = getServletContext();
Assert.state(servletContext != null, "No ServletContext");
RequestDispatcher rd = servletContext.getNamedDispatcher(this.servletName);
if (rd == null) {
throw new ServletException("No servlet with name '" + this.servletName + "' defined in web.xml");
}
// If already included, include again, else forward.
if (useInclude(request, response)) {
rd.include(request, response);
if (logger.isTraceEnabled()) {
logger.trace("Included servlet [" + this.servletName + "] in ServletForwardingController '" + this.beanName + "'");
}
} else {
rd.forward(request, response);
if (logger.isTraceEnabled()) {
logger.trace("Forwarded to servlet [" + this.servletName + "] in ServletForwardingController '" + this.beanName + "'");
}
}
return null;
}
use of jakarta.servlet.ServletException in project spring-framework by spring-projects.
the class DispatcherServletTests method notDetectAllHandlerAdapters.
@Test
public void notDetectAllHandlerAdapters() throws ServletException, IOException {
DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
complexDispatcherServlet.setNamespace("test");
complexDispatcherServlet.setDetectAllHandlerAdapters(false);
complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
// only ServletHandlerAdapter with bean name "handlerAdapter" detected
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/servlet.do");
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertThat(response.getContentAsString()).isEqualTo("body");
// SimpleControllerHandlerAdapter not detected
request = new MockHttpServletRequest(getServletContext(), "GET", "/form.do");
response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertThat(response.getForwardedUrl()).as("forwarded to failed").isEqualTo("failed0.jsp");
assertThat(request.getAttribute("exception").getClass().equals(ServletException.class)).as("Exception exposed").isTrue();
}
use of jakarta.servlet.ServletException in project spring-framework by spring-projects.
the class DispatcherServletTests method simpleMappingExceptionResolverWithAllHandlers2.
@Test
public void simpleMappingExceptionResolverWithAllHandlers2() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/loc.do");
request.addPreferredLocale(Locale.CANADA);
request.addUserRole("role1");
request.addParameter("servlet", "yes");
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertThat(response.getStatus()).isEqualTo(500);
assertThat(response.getForwardedUrl()).as("forwarded to failed").isEqualTo("failed1.jsp");
assertThat(request.getAttribute("exception") instanceof ServletException).as("Exception exposed").isTrue();
}
Aggregations