-
Notifications
You must be signed in to change notification settings - Fork 356
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Allow to configure Jackson's JaxRSFeature on Jersey DefaultJacksonJaxbJsonProvider #5816
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it makes sense to remove Optional and just have the list?:
private final List<JaxRSFeatureState> jaxRSFeatures = new ArrayList<>();
If empty,
hasJaxrsFeature
returns falseI found this is more understandable, although this is only a personal opinion.