-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to configure Jackson's JaxRSFeature on Jersey DefaultJacksonJax…
…bJsonProvider (#5816) Signed-off-by: jansupol <jan.supol@oracle.com>
- Loading branch information
Showing
6 changed files
with
284 additions
and
5 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
67 changes: 67 additions & 0 deletions
67
media/json-jackson/src/main/java/org/glassfish/jersey/jackson/JaxRSFeatureObjectMapper.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,67 @@ | ||
/* | ||
* Copyright (c) 2024 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.jackson; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.glassfish.jersey.jackson.internal.AbstractObjectMapper; | ||
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.cfg.JaxRSFeature; | ||
|
||
|
||
/** | ||
* The Jackson {@link ObjectMapper} supporting {@link JaxRSFeature}s. | ||
*/ | ||
public class JaxRSFeatureObjectMapper extends AbstractObjectMapper { | ||
|
||
public JaxRSFeatureObjectMapper() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Method for changing state of an on/off {@link org.glassfish.jersey.jackson.internal.jackson.jaxrs.cfg.JaxRSFeature} | ||
* features. | ||
*/ | ||
public ObjectMapper configure(JaxRSFeature f, boolean state) { | ||
jaxrsFeatureBag.jaxrsFeature(f, state); | ||
return this; | ||
} | ||
|
||
/** | ||
* Method for enabling specified {@link org.glassfish.jersey.jackson.internal.jackson.jaxrs.cfg.JaxRSFeature}s | ||
* for parser instances this object mapper creates. | ||
*/ | ||
public ObjectMapper enable(JaxRSFeature... features) { | ||
if (features != null) { | ||
for (JaxRSFeature f : features) { | ||
jaxrsFeatureBag.jaxrsFeature(f, true); | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Method for disabling specified {@link org.glassfish.jersey.jackson.internal.jackson.jaxrs.cfg.JaxRSFeature}s | ||
* for parser instances this object mapper creates. | ||
*/ | ||
public ObjectMapper disable(JaxRSFeature... features) { | ||
if (features != null) { | ||
for (JaxRSFeature f : features) { | ||
jaxrsFeatureBag.jaxrsFeature(f, false); | ||
} | ||
} | ||
return this; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...son-jackson/src/main/java/org/glassfish/jersey/jackson/internal/AbstractObjectMapper.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,29 @@ | ||
/* | ||
* Copyright (c) 2024 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.jackson.internal; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
/** | ||
* Internal ObjectMapper with {@link JaxrsFeatureBag}. | ||
*/ | ||
public abstract class AbstractObjectMapper extends ObjectMapper { | ||
protected AbstractObjectMapper() { | ||
|
||
} | ||
protected JaxrsFeatureBag jaxrsFeatureBag = new JaxrsFeatureBag(); | ||
} |
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
58 changes: 58 additions & 0 deletions
58
media/json-jackson/src/main/java/org/glassfish/jersey/jackson/internal/JaxrsFeatureBag.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,58 @@ | ||
/* | ||
* Copyright (c) 2024 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.jackson.internal; | ||
|
||
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.base.ProviderBase; | ||
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.cfg.JaxRSFeature; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Internal holder class for {@link JaxRSFeature} settings and their values. | ||
*/ | ||
public class JaxrsFeatureBag<T extends JaxrsFeatureBag> { | ||
protected static final String JAXRS_FEATURE = "jersey.config.jackson.jaxrs.feature"; | ||
|
||
private static class JaxRSFeatureState { | ||
/* package */ final JaxRSFeature feature; | ||
/* package */ final boolean state; | ||
public JaxRSFeatureState(JaxRSFeature feature, boolean state) { | ||
this.feature = feature; | ||
this.state = state; | ||
} | ||
} | ||
|
||
private Optional<List<JaxRSFeatureState>> jaxRSFeature = Optional.empty(); | ||
|
||
public T jaxrsFeature(JaxRSFeature feature, boolean state) { | ||
if (!jaxRSFeature.isPresent()) { | ||
jaxRSFeature = Optional.of(new ArrayList<>()); | ||
} | ||
jaxRSFeature.ifPresent(list -> list.add(new JaxrsFeatureBag.JaxRSFeatureState(feature, state))); | ||
return (T) this; | ||
} | ||
|
||
protected boolean hasJaxrsFeature() { | ||
return jaxRSFeature.isPresent(); | ||
} | ||
|
||
/* package */ void configureJaxrsFeatures(ProviderBase providerBase) { | ||
jaxRSFeature.ifPresent(list -> list.stream().forEach(state -> providerBase.configure(state.feature, state.state))); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
media/json-jackson/src/test/java/org/glassfish/jersey/jackson/internal/JaxRSFeatureTest.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,96 @@ | ||
/* | ||
* Copyright (c) 2024 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.jackson.internal; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.glassfish.jersey.jackson.JacksonFeature; | ||
import org.glassfish.jersey.jackson.JaxRSFeatureObjectMapper; | ||
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.cfg.JaxRSFeature; | ||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import javax.inject.Inject; | ||
import javax.ws.rs.client.Client; | ||
import javax.ws.rs.client.ClientBuilder; | ||
import javax.ws.rs.client.ClientRequestContext; | ||
import javax.ws.rs.client.ClientRequestFilter; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.ContextResolver; | ||
import javax.ws.rs.ext.Providers; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
|
||
public class JaxRSFeatureTest { | ||
@Test | ||
public void testJaxrsFeatureOnJacksonFeature() { | ||
Client client = ClientBuilder.newClient() | ||
.register(new JacksonFeature().jaxrsFeature(JaxRSFeature.READ_FULL_STREAM, false)) | ||
.register(JaxrsFeatureFilter.class); | ||
|
||
try (Response r = client.target("http://xxx.yyy").request().get()) { | ||
MatcherAssert.assertThat(r.getStatus(), Matchers.is(200)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testJaxrsFeatureOnContextResolver() { | ||
Client client = ClientBuilder.newClient() | ||
.register(JacksonFeature.class) | ||
.register(JaxrsFetureContextResolver.class) | ||
.register(JaxrsFeatureFilter.class); | ||
|
||
try (Response r = client.target("http://xxx.yyy").request().get()) { | ||
MatcherAssert.assertThat(r.getStatus(), Matchers.is(200)); | ||
} | ||
} | ||
|
||
|
||
public static class JaxrsFeatureFilter implements ClientRequestFilter { | ||
private final DefaultJacksonJaxbJsonProvider jacksonProvider; | ||
@Inject | ||
public JaxrsFeatureFilter(Providers allProviders) { | ||
jacksonProvider = (DefaultJacksonJaxbJsonProvider) | ||
allProviders.getMessageBodyReader(Object.class, Object.class, null, MediaType.APPLICATION_JSON_TYPE); | ||
try { | ||
jacksonProvider.readFrom(Object.class, Object.class, null, MediaType.APPLICATION_JSON_TYPE, null, | ||
new ByteArrayInputStream("{}".getBytes())); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}; | ||
|
||
@Override | ||
public void filter(ClientRequestContext requestContext) throws IOException { | ||
Response.Status status = jacksonProvider.isEnabled(JaxRSFeature.READ_FULL_STREAM) | ||
? Response.Status.FORBIDDEN | ||
: Response.Status.OK; | ||
requestContext.abortWith(Response.status(status).build()); | ||
} | ||
} | ||
|
||
public static class JaxrsFetureContextResolver implements ContextResolver<ObjectMapper> { | ||
|
||
@Override | ||
public ObjectMapper getContext(Class<?> type) { | ||
JaxRSFeatureObjectMapper objectMapper = new JaxRSFeatureObjectMapper(); | ||
objectMapper.disable(JaxRSFeature.READ_FULL_STREAM); | ||
return objectMapper; | ||
} | ||
} | ||
} |