Skip to content

Commit

Permalink
Convert NotFoundExceptionMapper to annotation style
Browse files Browse the repository at this point in the history
This make it possible to avoid having to retrieve the
header from the Arc Request Context
  • Loading branch information
geoand authored and cemnura committed Dec 21, 2020
1 parent 4df88d5 commit 3be32c4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
import java.util.Objects;
import java.util.stream.Collectors;

import javax.ws.rs.NotFoundException;
import javax.ws.rs.Priorities;

import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.ApplicationArchivesBuildItem;
import io.quarkus.resteasy.reactive.server.runtime.ExceptionMapperRecorder;
import io.quarkus.resteasy.reactive.server.runtime.NotFoundExceptionMapper;
import io.quarkus.resteasy.reactive.spi.ExceptionMapperBuildItem;
import io.quarkus.resteasy.reactive.spi.CustomExceptionMapperBuildItem;
import io.quarkus.vertx.http.deployment.HttpRootPathBuildItem;
import io.quarkus.vertx.http.deployment.devmode.NotFoundPageDisplayableEndpointBuildItem;
import io.quarkus.vertx.http.deployment.devmode.RouteDescriptionBuildItem;
Expand All @@ -29,10 +26,10 @@ public class ResteasyReactiveDevModeProcessor {

@Record(STATIC_INIT)
@BuildStep(onlyIf = IsDevelopment.class)
void setupExceptionMapper(BuildProducer<ExceptionMapperBuildItem> providers, HttpRootPathBuildItem httpRoot,
void setupExceptionMapper(BuildProducer<CustomExceptionMapperBuildItem> customExceptionMappers,
HttpRootPathBuildItem httpRoot,
ExceptionMapperRecorder recorder) {
providers.produce(new ExceptionMapperBuildItem(NotFoundExceptionMapper.class.getName(),
NotFoundException.class.getName(), Priorities.USER + 1, true));
customExceptionMappers.produce(new CustomExceptionMapperBuildItem(NotFoundExceptionMapper.class.getName()));
recorder.setHttpRoot(httpRoot.getRootPath());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
import java.util.stream.Stream;

import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Context;
import javax.ws.rs.Priorities;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.Variant;
import javax.ws.rs.ext.ExceptionMapper;

import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.common.util.ServerMediaType;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
import org.jboss.resteasy.reactive.server.core.request.ServerDrivenNegotiation;
import org.jboss.resteasy.reactive.server.handlers.RestInitialHandler;
import org.jboss.resteasy.reactive.server.mapping.RequestMapper;
Expand All @@ -44,9 +44,8 @@
import io.quarkus.runtime.util.ClassPathUtils;
import io.quarkus.vertx.http.runtime.devmode.RouteDescription;

public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundException> {
public class NotFoundExceptionMapper {

protected static final String META_INF_RESOURCES_SLASH = "META-INF/resources/";
protected static final String META_INF_RESOURCES = "META-INF/resources";

private final static Variant JSON_VARIANT = new Variant(MediaType.APPLICATION_JSON_TYPE, (String) null, null);
Expand All @@ -62,9 +61,6 @@ public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundExceptio

private static final Logger LOG = Logger.getLogger(NotFoundExceptionMapper.class);

@Context
private HttpHeaders headers;

public static void setHttpRoot(String rootPath) {
httpRoot = rootPath;
}
Expand All @@ -84,15 +80,15 @@ public MethodDescription(String method, String fullPath, String produces, String
}
}

@Override
public Response toResponse(NotFoundException exception) {
@ServerExceptionMapper(value = NotFoundException.class, priority = Priorities.USER + 1)
public Response toResponse(HttpHeaders headers) {
if ((classMappers == null) || classMappers.isEmpty()) {
return respond();
return respond(headers);
}
return respond(ResourceDescription.fromClassMappers(classMappers));
return respond(ResourceDescription.fromClassMappers(classMappers), headers);
}

private Response respond() {
private Response respond(HttpHeaders headers) {
Variant variant = selectVariant(headers);

if (variant == JSON_VARIANT) {
Expand All @@ -107,7 +103,7 @@ private Response respond() {
return Response.status(Status.NOT_FOUND).build();
}

private Response respond(List<ResourceDescription> descriptions) {
private Response respond(List<ResourceDescription> descriptions, HttpHeaders headers) {
Variant variant = selectVariant(headers);

if (variant == JSON_VARIANT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.HttpHeaders;
Expand Down Expand Up @@ -45,4 +46,9 @@
public @interface ServerExceptionMapper {

Class<? extends Throwable>[] value() default {};

/**
* The priority with which the exception mapper will be executed
*/
int priority() default Priorities.USER;
}

0 comments on commit 3be32c4

Please sign in to comment.