Skip to content

Commit fc291b8

Browse files
committed
DefaultExceptionMapper
Signed-off-by: Maxim Nesen <maxim.nesen@oracle.com>
1 parent b9479ae commit fc291b8

File tree

19 files changed

+125
-71
lines changed

19 files changed

+125
-71
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.internal;
18+
19+
import jakarta.ws.rs.WebApplicationException;
20+
import jakarta.ws.rs.core.Response;
21+
import jakarta.ws.rs.ext.ExceptionMapper;
22+
23+
public class DefaultExceptionMapper implements ExceptionMapper<Throwable> {
24+
@Override
25+
public Response toResponse(Throwable exception) {
26+
return (exception instanceof WebApplicationException)
27+
? processWebApplicationException((WebApplicationException) exception)
28+
: processDefaultException(exception);
29+
}
30+
31+
private static Response processWebApplicationException(WebApplicationException exception) {
32+
return (exception.getResponse() == null)
33+
? processDefaultException(exception)
34+
: exception.getResponse();
35+
}
36+
37+
private static Response processDefaultException(Throwable exception) {
38+
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(exception.getMessage()).build();
39+
}
40+
}

core-common/src/main/java/org/glassfish/jersey/internal/ExceptionMapperFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -58,6 +58,8 @@ public class ExceptionMapperFactory implements ExceptionMappers {
5858

5959
private static final Logger LOGGER = Logger.getLogger(ExceptionMapperFactory.class.getName());
6060

61+
private static final ExceptionMapper<Throwable> DEFAULT_EXCEPTION_MAPPER = new DefaultExceptionMapper();
62+
6163
/**
6264
* Configurator which initializes and register {@link ExceptionMappers} instance into {@link InjectionManager} and
6365
* {@link BootstrapBag}.
@@ -127,7 +129,7 @@ private <T extends Throwable> ExceptionMapper<T> find(final Class<T> type, final
127129
}
128130
}
129131
}
130-
return mapper;
132+
return mapper == null ? (ExceptionMapper<T>) DEFAULT_EXCEPTION_MAPPER : mapper;
131133
}
132134

133135
/**

core-server/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,6 @@
196196
<artifactId>jakarta.validation-api</artifactId>
197197
</dependency>
198198

199-
<dependency>
200-
<groupId>jakarta.xml.bind</groupId>
201-
<artifactId>jakarta.xml.bind-api</artifactId>
202-
</dependency>
203-
204199
<dependency>
205200
<groupId>com.sun.xml.bind</groupId>
206201
<artifactId>jaxb-osgi</artifactId>

core-server/src/main/java/org/glassfish/jersey/server/ServerRuntime.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -54,6 +54,7 @@
5454

5555
import jakarta.inject.Provider;
5656

57+
import org.glassfish.jersey.internal.DefaultExceptionMapper;
5758
import org.glassfish.jersey.internal.guava.Preconditions;
5859
import org.glassfish.jersey.internal.inject.InjectionManager;
5960
import org.glassfish.jersey.internal.inject.Injections;
@@ -503,7 +504,7 @@ private Response mapException(final Throwable originalThrowable) throws Throwabl
503504
&& throwable.getCause() instanceof MessageBodyProviderNotFoundException) {
504505
throw throwable;
505506
}
506-
Response waeResponse = null;
507+
Response waeResponse;
507508

508509
if (throwable instanceof WebApplicationException) {
509510
final WebApplicationException webApplicationException = (WebApplicationException) throwable;
@@ -512,10 +513,11 @@ private Response mapException(final Throwable originalThrowable) throws Throwabl
512513
processingContext.routingContext().setMappedThrowable(throwable);
513514

514515
waeResponse = webApplicationException.getResponse();
515-
if (waeResponse.hasEntity()) {
516-
LOGGER.log(Level.FINE, LocalizationMessages
517-
.EXCEPTION_MAPPING_WAE_ENTITY(waeResponse.getStatus()), throwable);
518-
return waeResponse;
516+
if (waeResponse != null) {
517+
LOGGER.log(Level.FINE, waeResponse.hasEntity()
518+
? LocalizationMessages.EXCEPTION_MAPPING_WAE_ENTITY(waeResponse.getStatus())
519+
: LocalizationMessages.EXCEPTION_MAPPING_WAE_NO_ENTITY(waeResponse.getStatus()),
520+
throwable);
519521
}
520522
}
521523

@@ -527,7 +529,7 @@ private Response mapException(final Throwable originalThrowable) throws Throwabl
527529
try {
528530
final Response mappedResponse = mapper.toResponse(throwable);
529531

530-
if (tracingLogger.isLogEnabled(ServerTraceEvent.EXCEPTION_MAPPING)) {
532+
if (isTracingLoggingEnabled(mapper, throwable, tracingLogger)) {
531533
tracingLogger.logDuration(ServerTraceEvent.EXCEPTION_MAPPING,
532534
timestamp, mapper, throwable, throwable.getLocalizedMessage(),
533535
mappedResponse != null ? mappedResponse.getStatusInfo() : "-no-response-");
@@ -561,13 +563,6 @@ private Response mapException(final Throwable originalThrowable) throws Throwabl
561563
return Response.serverError().build();
562564
}
563565
}
564-
565-
if (waeResponse != null) {
566-
LOGGER.log(Level.FINE, LocalizationMessages
567-
.EXCEPTION_MAPPING_WAE_NO_ENTITY(waeResponse.getStatus()), throwable);
568-
569-
return waeResponse;
570-
}
571566
}
572567
// internal mapping
573568
if (throwable instanceof HeaderValueException) {
@@ -740,6 +735,14 @@ private void release(final ContainerResponse responseContext) {
740735
processingContext.triggerEvent(RequestEvent.Type.FINISHED);
741736
}
742737
}
738+
739+
private static boolean isTracingLoggingEnabled(ExceptionMapper mapper, Throwable throwable, TracingLogger tracingLogger) {
740+
boolean defaultLoggingState = mapper instanceof DefaultExceptionMapper
741+
&& throwable instanceof WebApplicationException;
742+
return !defaultLoggingState
743+
&& tracingLogger.isLogEnabled(ServerTraceEvent.EXCEPTION_MAPPING);
744+
745+
}
743746
}
744747

745748
private static class AsyncResponder implements AsyncContext, ContainerResponseWriter.TimeoutHandler, CompletionCallback {

core-server/src/test/java/org/glassfish/jersey/server/ApplicationHandlerTest.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -130,9 +130,13 @@ public String doPost() {
130130
public void testReturnBadRequestOnIllHeaderValue() throws Exception {
131131
ApplicationHandler app = createApplication(Resource.class);
132132

133-
assertEquals(400,
134-
app.apply(RequestContextBuilder.from("/", "GET").header(HttpHeaders.CONTENT_LENGTH, "text").build())
135-
.get().getStatus());
133+
final ContainerResponse response =
134+
app.apply(RequestContextBuilder.from("/", "GET")
135+
.header(HttpHeaders.CONTENT_LENGTH, "text").build())
136+
.get();
137+
assertEquals("Unable to parse \"Content-Length\" header value: \"text\"", response.getEntity());
138+
assertEquals(500,
139+
response.getStatus());
136140
}
137141

138142
@Test
@@ -558,12 +562,14 @@ public void testMapResponseErrorForMbw() throws Exception {
558562
/**
559563
* Test that un-mapped response errors are tried to be processed only once (MBW).
560564
*/
561-
@Test(expected = ExecutionException.class)
565+
@Test
562566
public void testMapCyclicResponseErrorForMbw() throws Exception {
563567
final ApplicationHandler handler = new ApplicationHandler(MapResponseErrorApplication.class);
564568

565569
final ContainerRequest context = RequestContextBuilder.from("/foobar", "GET").build();
566570

567-
handler.apply(context).get();
571+
final ContainerResponse containerResponse = handler.apply(context).get();
572+
assertEquals(500, containerResponse.getStatus());
573+
assertEquals("Cannot do that!", containerResponse.getEntity());
568574
}
569575
}

core-server/src/test/java/org/glassfish/jersey/server/AsyncCallbackServerTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -35,6 +35,7 @@
3535

3636
import jakarta.inject.Singleton;
3737

38+
import org.junit.Ignore;
3839
import org.junit.Test;
3940
import static org.junit.Assert.assertEquals;
4041
import static org.junit.Assert.assertFalse;
@@ -71,6 +72,7 @@ public void testCompletionCallback() throws ExecutionException, InterruptedExcep
7172
}
7273

7374
@Test
75+
@Ignore //since JAXRS 3.1 failure is not processed by callbacks
7476
public void testCompletionFail() throws ExecutionException, InterruptedException {
7577
final Flags flags = new Flags();
7678

core-server/src/test/java/org/glassfish/jersey/server/filter/ApplicationFilterTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -318,11 +318,7 @@ public Response apply(final ContainerRequestContext request) {
318318
});
319319
resourceConfig.registerResources(rb.build());
320320
final ApplicationHandler application = new ApplicationHandler(resourceConfig);
321-
try {
322-
application.apply(RequestContextBuilder.from("/test", "GET").build()).get().getStatus();
323-
Assert.fail("should throw an exception");
324-
} catch (final Exception e) {
325-
// ok
326-
}
321+
int status = application.apply(RequestContextBuilder.from("/test", "GET").build()).get().getStatus();
322+
Assert.assertEquals(500, status);
327323
}
328324
}

ext/entity-filtering/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@
5252
<scope>provided</scope>
5353
</dependency>
5454

55-
<dependency>
56-
<groupId>jakarta.xml.bind</groupId>
57-
<artifactId>jakarta.xml.bind-api</artifactId>
58-
<scope>provided</scope>
59-
</dependency>
60-
6155
<dependency>
6256
<groupId>org.glassfish.jersey.test-framework</groupId>
6357
<artifactId>jersey-test-framework-core</artifactId>

ext/wadl-doclet/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,6 @@
207207
<artifactId>jersey-server</artifactId>
208208
<version>${project.version}</version>
209209
</dependency>
210-
<dependency>
211-
<groupId>jakarta.xml.bind</groupId>
212-
<artifactId>jakarta.xml.bind-api</artifactId>
213-
</dependency>
214210
</dependencies>
215211

216212
<build>

incubator/declarative-linking/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@
5252
<artifactId>jakarta.xml.bind-api</artifactId>
5353
<scope>provided</scope>
5454
</dependency>
55-
<dependency>
56-
<groupId>jakarta.xml.bind</groupId>
57-
<artifactId>jakarta.xml.bind-api</artifactId>
58-
<scope>provided</scope>
59-
</dependency>
6055
<dependency>
6156
<groupId>jakarta.el</groupId>
6257
<artifactId>jakarta.el-api</artifactId>

0 commit comments

Comments
 (0)