Skip to content

Commit

Permalink
fix: TEST
Browse files Browse the repository at this point in the history
  • Loading branch information
angelo.andreussi committed Sep 23, 2024
1 parent 3a2e61f commit c42e530
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 22 deletions.
83 changes: 83 additions & 0 deletions rest-api/web/src/main/java/moxycom/MoxyJsonFeatureCustom.java
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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXBException;

import moxycom.MoxyJsonFeatureCustom;
import org.eclipse.kapua.app.api.core.KapuaSerializableBodyWriter;
import org.eclipse.kapua.app.api.core.ListBodyWriter;
import org.eclipse.kapua.app.api.core.MoxyJsonConfigContextResolver;
Expand All @@ -26,6 +27,7 @@
import org.eclipse.kapua.locator.guice.GuiceLocatorImpl;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.utilities.binding.AbstractBinder;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.server.filter.UriConnegFilter;
Expand All @@ -34,6 +36,8 @@
import org.jvnet.hk2.guice.bridge.api.GuiceBridge;
import org.jvnet.hk2.guice.bridge.api.GuiceIntoHK2Bridge;



public class RestApisApplication extends ResourceConfig {

public RestApisApplication() throws JAXBException {
Expand All @@ -60,12 +64,12 @@ protected void configure() {
property(ServerProperties.WADL_FEATURE_DISABLE, true);

//Manually adding MOXyJSONFeature
register(org.glassfish.jersey.moxy.json.MoxyJsonFeature.class);
register(MoxyJsonConfigContextResolver.class);
register(UriConnegFilter.class);
register(JaxbContextResolver.class);
register(KapuaSerializableBodyWriter.class);
register(ListBodyWriter.class);
register(MoxyJsonFeatureCustom.class);

register(new ContainerLifecycleListener() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.model.xml.adapters;
import org.eclipse.kapua.KapuaIllegalNullArgumentException;
import org.eclipse.kapua.model.xml.XmlPropertyAdapted;

import javax.xml.bind.annotation.adapters.XmlAdapter;
Expand All @@ -35,26 +36,30 @@ public XmlPropertiesAdapter(Class<V> propertyClass, Supplier<V> adaptedPropertyF
}

@Override
public Map<String, Object> unmarshal(V[] properties) {
public Map<String, Object> unmarshal(V[] properties) throws KapuaIllegalNullArgumentException {
Map<String, Object> unmarshalledProperties;
unmarshalledProperties = Optional.ofNullable(properties)
.map(Arrays::asList)
.orElse(Collections.emptyList())
.stream()
.peek(adaptedProp -> {
if (adaptedProp.getType() == null) {
throw new InternalError("null value for property.type parameter");
}
})
.filter(adaptedProp -> xmlPropertyAdapters.containsKey((adaptedProp.getType())))
.collect(Collectors.toMap(
XmlPropertyAdapted::getName,
adaptedProp -> {
final XmlPropertyAdapter xmlPropertyAdapter = xmlPropertyAdapters.get(adaptedProp.getType());
return xmlPropertyAdapter.unmarshallValues(adaptedProp);
}));
try {
unmarshalledProperties = Optional.ofNullable(properties)
.map(Arrays::asList)
.orElse(Collections.emptyList())
.stream()
.peek(adaptedProp -> {
if (adaptedProp.getType() == null) {
throw new InternalError("null value for property.type parameter");
}
})
.filter(adaptedProp -> xmlPropertyAdapters.containsKey((adaptedProp.getType())))
.collect(Collectors.toMap(
XmlPropertyAdapted::getName,
adaptedProp -> {
final XmlPropertyAdapter xmlPropertyAdapter = xmlPropertyAdapters.get(adaptedProp.getType());
return xmlPropertyAdapter.unmarshallValues(adaptedProp);
}));

return unmarshalledProperties;
return unmarshalledProperties;
} catch (InternalError e) {
throw new KapuaIllegalNullArgumentException("fooo");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.kapua.model.xml.adapters;

import org.eclipse.kapua.KapuaIllegalNullArgumentException;
import org.eclipse.kapua.model.xml.XmlPropertyAdapted;
import org.eclipse.kapua.qa.markers.junit.JUnitTests;
import org.junit.Assert;
Expand Down Expand Up @@ -187,7 +188,7 @@ public void testMarshalling() {
}

@Test
public void testUnmarshallingMissingProperties() {
public void testUnmarshallingMissingProperties() throws KapuaIllegalNullArgumentException {
//Given adapters
final StringPropertyAdapter stringAdapter = Mockito.spy(new StringPropertyAdapter());
final HashMap<TestTypes, XmlPropertyAdapter> adapters = new HashMap<TestTypes, XmlPropertyAdapter>() {
Expand All @@ -204,7 +205,7 @@ public void testUnmarshallingMissingProperties() {
}

@Test
public void testUnmarshalling() {
public void testUnmarshalling() throws KapuaIllegalNullArgumentException {
//Given adapters
final StringPropertyAdapter stringAdapter = Mockito.spy(new StringPropertyAdapter());
final BooleanPropertyAdapter booleanAdapter = Mockito.spy(new BooleanPropertyAdapter());
Expand Down Expand Up @@ -254,7 +255,7 @@ public void testUnmarshallingMissingType() {
//and an instance
final XmlPropertiesAdapter instance = new TestPropertiesAdapter(adapters);
//When I unmarshal
Assert.assertThrows(InternalError.class, () -> instance.unmarshal(new TestPropertyAdapted[]{
Assert.assertThrows(KapuaIllegalNullArgumentException.class, () -> instance.unmarshal(new TestPropertyAdapted[]{
new TestPropertyAdapted("aString", TestTypes.First, "TheString"),
new TestPropertyAdapted("aBoolean", TestTypes.Second, "false", "true"),
new TestPropertyAdapted("anotherValue", null, "42")
Expand Down

0 comments on commit c42e530

Please sign in to comment.