From be3960661f366a9528e8f6f793474680b6ff24e0 Mon Sep 17 00:00:00 2001 From: "angelo.andreussi" Date: Mon, 21 Oct 2024 12:11:43 +0200 Subject: [PATCH] fix: changed the way CustomMoxyJsonProvider is set in order to solve "Error deserializing object from entity stream" error message upon auth --- .../app/api/web/CustomMoxyJsonProvider.java | 34 ------------ .../MoxyJsonFeatureCustomJsonProvider.java | 53 +++++++++++++++++++ .../app/api/web/RestApisApplication.java | 2 +- 3 files changed, 54 insertions(+), 35 deletions(-) delete mode 100644 rest-api/web/src/main/java/org/eclipse/kapua/app/api/web/CustomMoxyJsonProvider.java create mode 100644 rest-api/web/src/main/java/org/eclipse/kapua/app/api/web/MoxyJsonFeatureCustomJsonProvider.java diff --git a/rest-api/web/src/main/java/org/eclipse/kapua/app/api/web/CustomMoxyJsonProvider.java b/rest-api/web/src/main/java/org/eclipse/kapua/app/api/web/CustomMoxyJsonProvider.java deleted file mode 100644 index 40dcb76a54d..00000000000 --- a/rest-api/web/src/main/java/org/eclipse/kapua/app/api/web/CustomMoxyJsonProvider.java +++ /dev/null @@ -1,34 +0,0 @@ -/******************************************************************************* - * 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 org.eclipse.kapua.app.api.web; - -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; - @Provider - public class CustomMoxyJsonProvider extends org.glassfish.jersey.moxy.json.internal.ConfigurableMoxyJsonProvider { - /** - * This class is a custom moxyJsonProvider that sets the unmarshaller validationEventHandler to the default one - * This one allows to propagate exceptions to the stack when an error is found (for example, when an exception has been thrown from one of our custom "xmlAdapters") - */ - @Override - protected void preReadFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, Unmarshaller unmarshaller) throws JAXBException { - super.preReadFrom(type, genericType, annotations, mediaType, httpHeaders, unmarshaller); - unmarshaller.setEventHandler(new DefaultValidationEventHandler()); - } - } diff --git a/rest-api/web/src/main/java/org/eclipse/kapua/app/api/web/MoxyJsonFeatureCustomJsonProvider.java b/rest-api/web/src/main/java/org/eclipse/kapua/app/api/web/MoxyJsonFeatureCustomJsonProvider.java new file mode 100644 index 00000000000..13853559347 --- /dev/null +++ b/rest-api/web/src/main/java/org/eclipse/kapua/app/api/web/MoxyJsonFeatureCustomJsonProvider.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * 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 org.eclipse.kapua.app.api.web; + +import org.glassfish.jersey.internal.InternalProperties; +import org.glassfish.jersey.internal.util.PropertiesHelper; +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 MoxyJsonFeatureCustomJsonProvider implements Feature { + //A custom feature used to set a custom moxyJsonProvider + + private static final String JSON_FEATURE = MoxyJsonFeatureCustomJsonProvider.class.getSimpleName(); + + @Override + public boolean configure(final FeatureContext context) { + final Configuration config = context.getConfiguration(); + // Disable other JSON providers. In this way the org.glassfish.jersey.moxy.json.MoxyJsonFeature (registered as default by MOXy) will skip the registration of the default provider + context.property(PropertiesHelper.getPropertyNameForRuntime(InternalProperties.JSON_FEATURE, config.getRuntimeType()), + JSON_FEATURE); + context.register(CustomMoxyJsonProvider.class); + 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 allows to propagate exceptions to the stack when an error is found (for example, when an exception has been thrown from one of our custom "xmlAdapters") + @Override + protected void preReadFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, Unmarshaller unmarshaller) throws JAXBException { + super.preReadFrom(type, genericType, annotations, mediaType, httpHeaders, unmarshaller); + unmarshaller.setEventHandler(new DefaultValidationEventHandler()); + } + } +} diff --git a/rest-api/web/src/main/java/org/eclipse/kapua/app/api/web/RestApisApplication.java b/rest-api/web/src/main/java/org/eclipse/kapua/app/api/web/RestApisApplication.java index 18392d7bd2b..ff0fc5a5e62 100644 --- a/rest-api/web/src/main/java/org/eclipse/kapua/app/api/web/RestApisApplication.java +++ b/rest-api/web/src/main/java/org/eclipse/kapua/app/api/web/RestApisApplication.java @@ -65,7 +65,7 @@ protected void configure() { register(JaxbContextResolver.class); register(KapuaSerializableBodyWriter.class); register(ListBodyWriter.class); - register(CustomMoxyJsonProvider.class); + register(MoxyJsonFeatureCustomJsonProvider.class); register(new ContainerLifecycleListener() {