Move DMF message converter in amqp-api (#3143)

- move in amqp to be in single place
- public DmfMessageConverter that could be used everywhere directly (instead of factory methods)
- defualt amqpMessageConverter bean renamed to dmfMessageConverter
- trusted packages configured (for dmfMessageConverter) with hawkbit.dmf.trusted-packages - default hwakbit dmf model package

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2026-06-15 13:51:25 +03:00
committed by GitHub
parent fcb9679796
commit a76e62f431
10 changed files with 89 additions and 69 deletions

View File

@@ -30,5 +30,22 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<!-- DmfMessageConverter dependencies -->
<dependency>
<groupId>tools.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-amqp</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,45 @@
/**
* Copyright (c) 2026 Contributors to the Eclipse Foundation
*
* 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
*/
package org.eclipse.hawkbit.dmf;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.support.converter.JacksonJsonMessageConverter;
import tools.jackson.databind.json.JsonMapper;
// - since spring-amqp 4.0.4 not all packages are assumed trusted for type converter (only java.land and java.util)
// so e need to add hawkbit DMF model package (and eventual extension packages) as trusted
// - also (again since spring-amqp 4.0.4) the conversion from empty payload fail (which probably is fine since it is JSON)
// however, (for backward compatibility, e.g. THING_REMOVED doesn't define payload and could be empty byte[]) we assume that
// empty payload is empty byte[] and not try to convert it to Object (which fail since it is not JSON)
public class DmfMessageConverter extends JacksonJsonMessageConverter {
public static final String DMF_JSON_MODEL_PACKAGE = "org.eclipse.hawkbit.dmf.json.model";
public DmfMessageConverter(final String... trustedPackages) {
this(new JsonMapper(), trustedPackages);
}
public DmfMessageConverter(final JsonMapper jsonMapper, final String... trustedPackages) {
super(jsonMapper, trustedPackages.length == 0 ? new String[] { DMF_JSON_MODEL_PACKAGE } : trustedPackages);
}
@Override
public @NonNull Object fromMessage(@NonNull final Message message, final @Nullable Object conversionHint) {
// default converter tries to convert empty body payload to Object (since rabbit 4.0.4)
// which probably is correct since it has to be JSON - however, in this case we assume - empty byte[]
if (message.getBody().length == 0) {
return message.getBody();
} else {
return super.fromMessage(message, conversionHint);
}
}
}