forked from eclipse/kapua
-
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.
- Loading branch information
angelo.andreussi
committed
Sep 23, 2024
1 parent
3a2e61f
commit c42e530
Showing
4 changed files
with
115 additions
and
22 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
rest-api/web/src/main/java/moxycom/MoxyJsonFeatureCustom.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,83 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Eurotech - initial API and implementation | ||
*******************************************************************************/ | ||
package moxycom; | ||
|
||
import org.glassfish.jersey.CommonProperties; | ||
import org.glassfish.jersey.internal.InternalProperties; | ||
import org.glassfish.jersey.internal.util.PropertiesHelper; | ||
import org.glassfish.jersey.message.filtering.EntityFilteringFeature; | ||
import org.glassfish.jersey.moxy.internal.MoxyFilteringFeature; | ||
import org.glassfish.jersey.moxy.json.internal.FilteringMoxyJsonProvider; | ||
|
||
import javax.ws.rs.Priorities; | ||
import javax.ws.rs.core.Configuration; | ||
import javax.ws.rs.core.Feature; | ||
import javax.ws.rs.core.FeatureContext; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import javax.ws.rs.ext.Provider; | ||
import javax.xml.bind.JAXBException; | ||
import javax.xml.bind.Unmarshaller; | ||
import javax.xml.bind.helpers.DefaultValidationEventHandler; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
|
||
public class MoxyJsonFeatureCustom implements Feature { | ||
//Basically this is the org.glassfish.jersey.moxy.json.MoxyJsonFeature but with some tweaks to set a custom JsonProvider | ||
//Here, I already tried to extend that class but this leads to a bad behaviour since it will register a jsonProvider that interferes with the CustomMoxyJsonProvider here set | ||
|
||
private static final String JSON_FEATURE = MoxyJsonFeatureCustom.class.getSimpleName(); | ||
|
||
@Override | ||
public boolean configure(final FeatureContext context) { | ||
final Configuration config = context.getConfiguration(); | ||
|
||
if (CommonProperties.getValue(config.getProperties(), config.getRuntimeType(), | ||
CommonProperties.MOXY_JSON_FEATURE_DISABLE, Boolean.FALSE, Boolean.class)) { | ||
return false; | ||
} | ||
|
||
final String jsonFeature = CommonProperties.getValue(config.getProperties(), config.getRuntimeType(), | ||
InternalProperties.JSON_FEATURE, JSON_FEATURE, String.class); | ||
// Other JSON providers registered. | ||
if (!JSON_FEATURE.equalsIgnoreCase(jsonFeature)) { | ||
return false; | ||
} | ||
|
||
// Disable other JSON providers. | ||
context.property(PropertiesHelper.getPropertyNameForRuntime(InternalProperties.JSON_FEATURE, config.getRuntimeType()), | ||
JSON_FEATURE); | ||
|
||
// Set a slightly lower priority of workers than JSON-P so MOXy is not pick-ed up for JsonStructures (if both are used). | ||
final int workerPriority = Priorities.USER + 2000; | ||
|
||
if (EntityFilteringFeature.enabled(config)) { | ||
context.register(MoxyFilteringFeature.class); | ||
context.register(FilteringMoxyJsonProvider.class, workerPriority); | ||
} else { | ||
context.register(CustomMoxyJsonProvider.class, workerPriority); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Provider | ||
public static class CustomMoxyJsonProvider extends org.glassfish.jersey.moxy.json.internal.ConfigurableMoxyJsonProvider { | ||
//A custom moxyJsonProvider that sets the unmarshaller validationEventHandler to the default one. This one return false (and hence, jaxb then stops execution and propagate exception to the stack) when error or fatal error is found (when excpetion has been thrown from one of our custom xmlAdapters for example) | ||
@Override | ||
protected void preReadFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, Unmarshaller unmarshaller) throws JAXBException { | ||
super.preReadFrom(type, genericType, annotations, mediaType, httpHeaders, unmarshaller); | ||
unmarshaller.setEventHandler(new DefaultValidationEventHandler()); | ||
} | ||
} | ||
} |
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
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