diff --git a/servicetalk-examples/http/jaxrs/src/main/java/io/servicetalk/examples/http/jaxrs/HelloWorldExceptionMapper.java b/servicetalk-examples/http/jaxrs/src/main/java/io/servicetalk/examples/http/jaxrs/HelloWorldExceptionMapper.java new file mode 100644 index 0000000000..fc95f3be4c --- /dev/null +++ b/servicetalk-examples/http/jaxrs/src/main/java/io/servicetalk/examples/http/jaxrs/HelloWorldExceptionMapper.java @@ -0,0 +1,30 @@ +/* + * Copyright © 2023 Apple Inc. and the ServiceTalk project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.servicetalk.examples.http.jaxrs; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; + +public class HelloWorldExceptionMapper implements ExceptionMapper { + @Override + public Response toResponse(final Throwable exception) { + if (exception instanceof IllegalStateException) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + + return Response.serverError().build(); + } +} diff --git a/servicetalk-examples/http/jaxrs/src/main/java/io/servicetalk/examples/http/jaxrs/HelloWorldJaxRsApplication.java b/servicetalk-examples/http/jaxrs/src/main/java/io/servicetalk/examples/http/jaxrs/HelloWorldJaxRsApplication.java index 63d7860fdb..3c32dd3581 100644 --- a/servicetalk-examples/http/jaxrs/src/main/java/io/servicetalk/examples/http/jaxrs/HelloWorldJaxRsApplication.java +++ b/servicetalk-examples/http/jaxrs/src/main/java/io/servicetalk/examples/http/jaxrs/HelloWorldJaxRsApplication.java @@ -31,7 +31,8 @@ public final class HelloWorldJaxRsApplication extends Application { public Set> getClasses() { return new HashSet<>(asList( MultiPartFeature.class, - HelloWorldJaxRsResource.class + HelloWorldJaxRsResource.class, + HelloWorldExceptionMapper.class ) ); } diff --git a/servicetalk-examples/http/jaxrs/src/main/java/io/servicetalk/examples/http/jaxrs/HelloWorldJaxRsResource.java b/servicetalk-examples/http/jaxrs/src/main/java/io/servicetalk/examples/http/jaxrs/HelloWorldJaxRsResource.java index c4bad85664..e1d118aac1 100644 --- a/servicetalk-examples/http/jaxrs/src/main/java/io/servicetalk/examples/http/jaxrs/HelloWorldJaxRsResource.java +++ b/servicetalk-examples/http/jaxrs/src/main/java/io/servicetalk/examples/http/jaxrs/HelloWorldJaxRsResource.java @@ -145,6 +145,28 @@ public CompletionStage slowHello(@DefaultValue("world") @QueryParam("who return delayedResponse; } + /** + * Resource that uses Java's CompletionStage to produce an error response. + * Note that the {@link ConnectionContext} could also be injected into a class-level {@code @Context} field. + *

+ * Test with: + *

+     * curl -v http://localhost:8080/greetings/error-hello
+     * curl -v http://localhost:8080/greetings/error-hello?mapped=false
+     * 
+ * + * @param mapped whether the exception is mapped or not. + * @param ctx the {@link ConnectionContext}. + * @return future greetings as a {@link CompletionStage} of {@link String}. + */ + @GET + @Path("error-hello") + @Produces(TEXT_PLAIN) + public CompletionStage errorHello(@DefaultValue("true") @QueryParam("mapped") final boolean mapped, + @Context final ConnectionContext ctx) { + return Single.failed(mapped ? new IllegalStateException() : new Exception()).toCompletionStage(); + } + /** * Resource that only relies on {@link Single}s for consuming and producing data, and operators for processing it. * No OIO adaptation is involved when requests are dispatched to it,