Skip to content

Commit

Permalink
Fix servlet error mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Apr 30, 2024
1 parent db219a8 commit 1524537
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.jboss.metadata.web.spec.CookieConfigMetaData;
import org.jboss.metadata.web.spec.DispatcherType;
import org.jboss.metadata.web.spec.EmptyRoleSemanticType;
import org.jboss.metadata.web.spec.ErrorPageMetaData;
import org.jboss.metadata.web.spec.FilterMappingMetaData;
import org.jboss.metadata.web.spec.FilterMetaData;
import org.jboss.metadata.web.spec.FiltersMetaData;
Expand Down Expand Up @@ -651,6 +652,16 @@ public ServletDeploymentManagerBuildItem build(List<ServletBuildItem> servlets,
recorder.addServletContainerInitializer(deployment,
(Class<? extends ServletContainerInitializer>) context.classProxy(sci.sciClass), handlesTypes);
}
if (webMetaData.getErrorPages() != null) {
for (ErrorPageMetaData errorPage : webMetaData.getErrorPages()) {
if (errorPage.getErrorCode() != null && !errorPage.getErrorCode().isBlank()) {
recorder.addErrorPage(deployment, errorPage.getLocation(), Integer.parseInt(errorPage.getErrorCode()));
} else if (errorPage.getExceptionType() != null && !errorPage.getExceptionType().isBlank()) {
recorder.addErrorPage(deployment, errorPage.getLocation(),
(Class<? extends Throwable>) context.classProxy(errorPage.getExceptionType()));
}
}
}
SessionConfigMetaData sessionConfig = webMetaData.getSessionConfig();
if (sessionConfig != null) {
if (sessionConfig.getSessionTimeoutSet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public class ServletWebXmlTestCase {
" <mime-mapping>\n" +
" <extension>wasm</extension>\n" +
" <mime-type>application/wasm</mime-type>\n" +
" </mime-mapping>" +
" </mime-mapping>\n" +
" <error-page>\n" +
" <error-code>404</error-code>\n" +
" <location>/mapped</location>\n" +
" </error-page>\n" +
"</web-app>";

@RegisterExtension
Expand All @@ -56,4 +60,11 @@ public void testMimeMapping() {
.statusCode(200)
.contentType(is("application/wasm"));
}

@Test
public void test404Mapping() {
RestAssured.when().get("/missing").then()
.statusCode(404)
.body(is("web xml servlet"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,16 @@ public void setSessionCookieConfig(ServletSessionConfig config, String name, Str
}
}

public void addErrorPage(RuntimeValue<DeploymentInfo> deployment, String location, int errorCode) {
deployment.getValue().addErrorPage(new ErrorPage(location, errorCode));

}

public void addErrorPage(RuntimeValue<DeploymentInfo> deployment, String location,
Class<? extends Throwable> exceptionType) {
deployment.getValue().addErrorPage(new ErrorPage(location, exceptionType));
}

/**
* we can't have SecureRandom in the native image heap, so we need to lazy init
*/
Expand Down

0 comments on commit 1524537

Please sign in to comment.