forked from eclipse-ee4j/jersey
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix for JAX-RS SPI resource finder (eclipse-ee4j#4143)
Signed-off-by: Maxim Nesen <[email protected]> Signed-off-by: Jan Supol <[email protected]>
- Loading branch information
Showing
5 changed files
with
305 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...-client/src/main/java/org/glassfish/jersey/client/internal/ClientExceptionMapperUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
|
||
package org.glassfish.jersey.client.internal; | ||
|
||
import org.glassfish.jersey.spi.ExceptionMappers; | ||
|
||
import javax.ws.rs.ConstrainedTo; | ||
import javax.ws.rs.RuntimeType; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
|
||
public abstract class ClientExceptionMapperUtil { | ||
private ClientExceptionMapperUtil() { | ||
throw new IllegalStateException("Cannot instantiate"); | ||
} | ||
|
||
/** | ||
* Searches for a mapping of {@link ExceptionMapper} of a given Exception by calling | ||
* {@link ExceptionMappers#findMapping(Throwable)}. If not found, it searches recursively for | ||
* an exception mapper of a cause of the Exception. For instance, for a | ||
* {@link javax.ws.rs.ProcessingException ProcessingException} an | ||
* {@code ExceptionMapper<ProcessingException>} would work, but an {@code ExceptionMapper} | ||
* for a cause of the {@code ProcessingException} would work, too. | ||
* <p/> | ||
* Note that the {@code ExceptionMapper} needs to be annotated with | ||
* {@link ConstrainedTo ConstrainedTo(RuntimeType.Client)} | ||
* | ||
* @param exceptionMappers the {@link ExceptionMappers} instance | ||
* @param exceptionInstance the actual exception for which the {@link ExceptionMapper} is to be find | ||
* @return A pair of an {@link ExceptionMapper} and the corresponding exception (or cause), | ||
* or {@code null} when none found. | ||
*/ | ||
public static <T extends Throwable> ExceptionCauseMapper findMappingIncludingCause(final ExceptionMappers exceptionMappers, | ||
final T exceptionInstance) { | ||
final ExceptionMapper<T> mapper = exceptionMappers.findMapping(exceptionInstance); | ||
if (mapper != null) { | ||
final ConstrainedTo annotation = mapper.getClass().getAnnotation(ConstrainedTo.class); | ||
if (annotation != null && annotation.value().equals(RuntimeType.CLIENT)) { | ||
return new ExceptionCauseMapper(mapper, exceptionInstance); | ||
} | ||
} | ||
return exceptionInstance.getCause() == null | ||
? null : findMappingIncludingCause(exceptionMappers, exceptionInstance.getCause()); | ||
} | ||
|
||
/** | ||
* A holder class for for a pair of {@link ExceptionMapper} and the corresponding {@code Exception} | ||
* | ||
* @param <EXCEPTION> a {@link Throwable} | ||
*/ | ||
public static class ExceptionCauseMapper<EXCEPTION extends Throwable> { | ||
public ExceptionMapper<EXCEPTION> exceptionMapper; | ||
public EXCEPTION exception; | ||
|
||
public ExceptionCauseMapper(ExceptionMapper<EXCEPTION> exceptionMapper, EXCEPTION exception) { | ||
this.exceptionMapper = exceptionMapper; | ||
this.exception = exception; | ||
} | ||
} | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
core-client/src/test/java/org/glassfish/jersey/client/ClientExceptionMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
|
||
package org.glassfish.jersey.client; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import javax.ws.rs.ConstrainedTo; | ||
import javax.ws.rs.ProcessingException; | ||
import javax.ws.rs.RuntimeType; | ||
import javax.ws.rs.client.ClientBuilder; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
import javax.ws.rs.ext.Providers; | ||
|
||
import java.net.ConnectException; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
|
||
public class ClientExceptionMapperTest { | ||
private static final String expectedResponse = "HelloFromExceptionMapper"; | ||
|
||
@Test | ||
public void exceptionMapperNoConstraintToTest() { | ||
try { | ||
final String response = ClientBuilder.newClient() | ||
.register(new ExceptionMapper<Throwable>() { | ||
|
||
@Override | ||
public Response toResponse(Throwable exception) { | ||
return Response.ok(expectedResponse).build(); | ||
} | ||
}) | ||
.target("http://localhost:9900/doesnotexist").request().get(String.class); | ||
Assert.fail("ProcessingException has not been thrown"); | ||
} catch (ProcessingException pe) { | ||
// expected | ||
} | ||
} | ||
|
||
@Test | ||
public void exceptionMapperConstraintClientToTest() { | ||
final String response = ClientBuilder.newClient() | ||
.register(ClientExceptionMapper.class) | ||
.target("http://localhost:9900/doesnotexist").request().get(String.class); | ||
Assert.assertThat(response, is(expectedResponse)); | ||
} | ||
|
||
@Test | ||
public void exceptionMapperConstraintToServerTest() { | ||
try { | ||
final String response = ClientBuilder.newClient() | ||
.register(ServerExceptionMapper.class) | ||
.target("http://localhost:9900/doesnotexist").request().get(String.class); | ||
Assert.assertThat(response, is(expectedResponse)); | ||
Assert.fail("ProcessingException has not been thrown"); | ||
} catch (ProcessingException pe) { | ||
// expected | ||
} | ||
} | ||
|
||
@ConstrainedTo(RuntimeType.CLIENT) | ||
public static class ClientExceptionMapper implements ExceptionMapper<ConnectException> { | ||
@Override | ||
public Response toResponse(ConnectException exception) { | ||
return Response.ok(expectedResponse).build(); | ||
} | ||
} | ||
|
||
@ConstrainedTo(RuntimeType.SERVER) | ||
public static class ServerExceptionMapper implements ExceptionMapper<ConnectException> { | ||
@Override | ||
public Response toResponse(ConnectException exception) { | ||
return Response.ok(expectedResponse).build(); | ||
} | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...client/src/test/java/org/glassfish/jersey/tests/e2e/client/ClientExceptionMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
|
||
package org.glassfish.jersey.tests.e2e.client; | ||
|
||
import org.glassfish.jersey.server.ResourceConfig; | ||
import org.glassfish.jersey.test.JerseyTest; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import javax.ws.rs.ConstrainedTo; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.NotFoundException; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.RuntimeType; | ||
import javax.ws.rs.client.Entity; | ||
import javax.ws.rs.core.Application; | ||
import javax.ws.rs.core.GenericType; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
|
||
public class ClientExceptionMapperTest extends JerseyTest { | ||
|
||
@ConstrainedTo(RuntimeType.CLIENT) | ||
public static class NotFoundExceptionMapper implements ExceptionMapper<NotFoundException> { | ||
|
||
@Override | ||
public Response toResponse(NotFoundException exception) { | ||
return Response.ok("404").build(); | ||
} | ||
} | ||
|
||
@Path("/test") | ||
public static class TestResource { | ||
@GET | ||
public String ok() { | ||
return "ok"; | ||
} | ||
} | ||
|
||
@Override | ||
protected Application configure() { | ||
return new ResourceConfig().register(TestResource.class); | ||
} | ||
|
||
@Test | ||
public void test404get() { | ||
String entity = target().path("nowhere") | ||
.register(NotFoundExceptionMapper.class) | ||
.request().get(String.class); | ||
Assert.assertThat(entity, is("404")); | ||
} | ||
|
||
@Test | ||
public void test404getGenericType() { | ||
String entity = target().path("nowhere") | ||
.register(NotFoundExceptionMapper.class) | ||
.request().get(new GenericType<String>(String.class){}); | ||
Assert.assertThat(entity, is("404")); | ||
} | ||
|
||
@Test | ||
public void test404post() { | ||
String entity = target().path("nowhere") | ||
.register(NotFoundExceptionMapper.class) | ||
.request().post(Entity.entity("something", MediaType.TEXT_PLAIN_TYPE), String.class); | ||
Assert.assertThat(entity, is("404")); | ||
} | ||
} |