Skip to content

Commit

Permalink
Fix Servlet/Rest ExceptionMapper
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Kruger <[email protected]>
  • Loading branch information
phillip-kruger committed Apr 5, 2024
1 parent ce368d4 commit 05471d9
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ public void setupDeployment(BeanContainerBuildItem beanContainerBuildItem,
RuntimeValue<Deployment> deployment = recorder.createDeployment(deploymentInfo,
beanContainerBuildItem.getValue(), shutdownContext, vertxConfig,
requestContextFactoryBuildItem.map(RequestContextFactoryBuildItem::getFactory).orElse(null),
initClassFactory, launchModeBuildItem.getLaunchMode());
initClassFactory, launchModeBuildItem.getLaunchMode(), servletPresent);

quarkusRestDeploymentBuildItemBuildProducer
.produce(new ResteasyReactiveDeploymentBuildItem(deployment, deploymentPath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ public RuntimeValue<Deployment> createDeployment(DeploymentInfo info,
ShutdownContext shutdownContext, HttpBuildTimeConfig vertxConfig,
RequestContextFactory contextFactory,
BeanFactory<ResteasyReactiveInitialiser> initClassFactory,
LaunchMode launchMode) {
LaunchMode launchMode,
boolean servletPresent) {

info.setServletPresent(servletPresent);

CurrentRequestManager
.setCurrentRequestInstance(new QuarkusCurrentRequest(beanContainer.beanInstance(CurrentVertxRequest.class)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ public class Deployment {
private final ArrayList<RequestMapper.RequestPath<RestInitialHandler.InitialMatch>> classMappers;
private final List<GenericRuntimeConfigurableServerRestHandler<?>> runtimeConfigurableServerRestHandlers;
private final RuntimeExceptionMapper exceptionMapper;
private final boolean servletPresent;
private final ResteasyReactiveConfig resteasyReactiveConfig;
private final Map<String, List<String>> disabledEndpoints;
//this is not final, as it is set after startup
private RuntimeConfiguration runtimeConfiguration;

public Deployment(ExceptionMapping exceptionMapping, ContextResolvers contextResolvers,
public Deployment(ExceptionMapping exceptionMapping,
ContextResolvers contextResolvers,
ServerSerialisers serialisers,
ServerRestHandler[] abortHandlerChain,
EntityWriter dynamicEntityWriter, String prefix, ParamConverterProviders paramConverterProviders,
Expand All @@ -62,6 +64,7 @@ public Deployment(ExceptionMapping exceptionMapping, ContextResolvers contextRes
ArrayList<RequestMapper.RequestPath<RestInitialHandler.InitialMatch>> classMappers,
List<GenericRuntimeConfigurableServerRestHandler<?>> runtimeConfigurableServerRestHandlers,
RuntimeExceptionMapper exceptionMapper,
boolean servletPresent,
ResteasyReactiveConfig resteasyReactiveConfig,
Map<String, List<String>> disabledEndpoints) {
this.exceptionMapping = exceptionMapping;
Expand All @@ -79,6 +82,7 @@ public Deployment(ExceptionMapping exceptionMapping, ContextResolvers contextRes
this.classMappers = classMappers;
this.runtimeConfigurableServerRestHandlers = runtimeConfigurableServerRestHandlers;
this.exceptionMapper = exceptionMapper;
this.servletPresent = servletPresent;
this.resteasyReactiveConfig = resteasyReactiveConfig;
this.disabledEndpoints = disabledEndpoints;
}
Expand All @@ -103,6 +107,10 @@ public ExceptionMapping getExceptionMapping() {
return exceptionMapping;
}

public boolean isServletPresent() {
return servletPresent;
}

public ContextResolvers getContextResolvers() {
return contextResolvers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class DeploymentInfo {
private String applicationPath;
private List<HandlerChainCustomizer> globalHandlerCustomizers = new ArrayList<>();
private boolean developmentMode;
private boolean servletPresent = false;

public ResourceInterceptors getInterceptors() {
return interceptors;
Expand Down Expand Up @@ -191,4 +192,13 @@ public DeploymentInfo setDevelopmentMode(boolean developmentMode) {
this.developmentMode = developmentMode;
return this;
}

public boolean isServletPresent() {
return servletPresent;
}

public DeploymentInfo setServletPresent(boolean servletPresent) {
this.servletPresent = servletPresent;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ public BeanFactory.BeanInstance<?> apply(Class<?> aClass) {
abortHandlingChain.toArray(EMPTY_REST_HANDLER_ARRAY), dynamicEntityWriter,
prefix, paramConverterProviders, configurationImpl, applicationSupplier,
threadSetupAction, requestContextFactory, preMatchHandlers, classMappers,
runtimeConfigurableServerRestHandlers, exceptionMapper, info.getResteasyReactiveConfig(),
runtimeConfigurableServerRestHandlers, exceptionMapper, info.isServletPresent(),
info.getResteasyReactiveConfig(),
disabledEndpoints);
}

Expand All @@ -243,7 +244,7 @@ private void forEachMapperEntry(MappersKey key,
int classTemplateNameCount = key.path.countPathParamNames();
RuntimeMappingDeployment runtimeMappingDeployment = new RuntimeMappingDeployment(classTemplates);
ClassRoutingHandler classRoutingHandler = new ClassRoutingHandler(runtimeMappingDeployment.buildClassMapper(),
classTemplateNameCount);
classTemplateNameCount, info.isServletPresent());
classMappers.add(new RequestMapper.RequestPath<>(true, key.path,
new RestInitialHandler.InitialMatch(new ServerRestHandler[] { classRoutingHandler },
runtimeMappingDeployment.getMaxMethodTemplateNameCount() + classTemplateNameCount)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ public class ClassRoutingHandler implements ServerRestHandler {

private final Map<String, RequestMapper<RuntimeResource>> mappers;
private final int parameterOffset;
final boolean servletPresent;

public ClassRoutingHandler(Map<String, RequestMapper<RuntimeResource>> mappers, int parameterOffset) {
public ClassRoutingHandler(Map<String, RequestMapper<RuntimeResource>> mappers, int parameterOffset,
boolean servletPresent) {
this.mappers = mappers;
this.parameterOffset = parameterOffset;
this.servletPresent = servletPresent;
}

@Override
Expand Down Expand Up @@ -226,7 +229,7 @@ private void throwNotFound(ResteasyReactiveRequestContext requestContext) {
ProvidersImpl providers = requestContext.getProviders();
ExceptionMapper<NotFoundException> exceptionMapper = providers.getExceptionMapper(NotFoundException.class);

if (exceptionMapper == null) {
if (exceptionMapper == null || servletPresent) {
if (requestContext.resumeExternalProcessing()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ public void handle(ResteasyReactiveRequestContext requestContext) throws Excepti
ProvidersImpl providers = requestContext.getProviders();
ExceptionMapper<NotFoundException> exceptionMapper = providers.getExceptionMapper(NotFoundException.class);

if (exceptionMapper != null) {
if (exceptionMapper != null && !deployment.isServletPresent()) {
// the NotFoundExceptionMapper needs access to the headers so we need to activate the scope
requestContext.requireCDIRequestScope();
// we want to engage the NotFoundExceptionMapper when nothing is found
requestContext.handleException(new NotFoundException());
return;
} else if (requestContext.resumeExternalProcessing()) {
Expand Down

0 comments on commit 05471d9

Please sign in to comment.