Skip to content

Commit

Permalink
Merge pull request #918 from FraunhoferIOSB/bugfix/improve-standard-c…
Browse files Browse the repository at this point in the history
…ompliance

Improve standard compliance
  • Loading branch information
mjacoby authored Oct 9, 2024
2 parents fb0f287 + 9d9093e commit 0ed0782
Show file tree
Hide file tree
Showing 58 changed files with 1,566 additions and 397 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import de.fraunhofer.iosb.ilt.faaast.service.dataformat.SerializationException;
import de.fraunhofer.iosb.ilt.faaast.service.dataformat.json.JsonApiDeserializer;
import de.fraunhofer.iosb.ilt.faaast.service.dataformat.json.JsonApiSerializer;
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.UnsupportedModifierException;
import de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue;
import de.fraunhofer.iosb.ilt.faaast.service.model.value.Datatype;
import de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo;
Expand Down Expand Up @@ -123,7 +124,7 @@ public String write(DataElementValue value) throws AssetConnectionException {
try {
return serializer.write(value);
}
catch (SerializationException e) {
catch (SerializationException | UnsupportedModifierException e) {
throw new AssetConnectionException("serializing value to JSON failed", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package de.fraunhofer.iosb.ilt.faaast.service.dataformat;

import de.fraunhofer.iosb.ilt.faaast.service.model.api.modifier.OutputModifier;
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.UnsupportedModifierException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -43,8 +44,9 @@ public interface ApiSerializer {
* @param modifier output modifier defining how to serialize
* @return string serialization of obj
* @throws SerializationException if serialization fails
* @throws UnsupportedModifierException if the modifier is not supported for this element
*/
public String write(Object obj, OutputModifier modifier) throws SerializationException;
public String write(Object obj, OutputModifier modifier) throws SerializationException, UnsupportedModifierException;


/**
Expand All @@ -54,8 +56,9 @@ public interface ApiSerializer {
* @param obj object to serialize
* @return string serialization of obj
* @throws SerializationException if serialization fails
* @throws UnsupportedModifierException if the modifier is not supported for this element
*/
public default String write(Object obj) throws SerializationException {
public default String write(Object obj) throws SerializationException, UnsupportedModifierException {
return write(obj, OutputModifier.DEFAULT);
}

Expand All @@ -68,8 +71,9 @@ public default String write(Object obj) throws SerializationException {
* @param modifier output modifier defining how to serialize
* @throws IOException if writing to the stream fails
* @throws SerializationException if serialization fails
* @throws UnsupportedModifierException if the modifier is not supported for this element
*/
public default void write(OutputStream out, Object obj, OutputModifier modifier) throws IOException, SerializationException {
public default void write(OutputStream out, Object obj, OutputModifier modifier) throws IOException, SerializationException, UnsupportedModifierException {
write(out, DEFAULT_CHARSET, obj, modifier);
}

Expand All @@ -82,8 +86,9 @@ public default void write(OutputStream out, Object obj, OutputModifier modifier)
* @param obj object to serialize
* @throws IOException if writing to the stream fails
* @throws SerializationException if serialization fails
* @throws UnsupportedModifierException if the modifier is not supported for this element
*/
public default void write(OutputStream out, Object obj) throws IOException, SerializationException {
public default void write(OutputStream out, Object obj) throws IOException, SerializationException, UnsupportedModifierException {
write(out, obj, OutputModifier.DEFAULT);
}

Expand All @@ -97,8 +102,9 @@ public default void write(OutputStream out, Object obj) throws IOException, Seri
* @param modifier output modifier defining how to serialize
* @throws IOException if writing to the stream fails
* @throws SerializationException if serialization fails
* @throws UnsupportedModifierException if the modifier is not supported for this element
*/
public default void write(OutputStream out, Charset charset, Object obj, OutputModifier modifier) throws IOException, SerializationException {
public default void write(OutputStream out, Charset charset, Object obj, OutputModifier modifier) throws IOException, SerializationException, UnsupportedModifierException {
try (OutputStreamWriter writer = new OutputStreamWriter(out, charset)) {
writer.write(write(obj, modifier));
}
Expand All @@ -114,8 +120,9 @@ public default void write(OutputStream out, Charset charset, Object obj, OutputM
* @param obj object to serialize
* @throws IOException if writing to the stream fails
* @throws SerializationException if serialization fails
* @throws UnsupportedModifierException if the modifier is not supported for this element
*/
public default void write(OutputStream out, Charset charset, Object obj) throws IOException, SerializationException {
public default void write(OutputStream out, Charset charset, Object obj) throws IOException, SerializationException, UnsupportedModifierException {
write(out, charset, obj, OutputModifier.DEFAULT);
}

Expand All @@ -130,8 +137,9 @@ public default void write(OutputStream out, Charset charset, Object obj) throws
* @throws FileNotFoundException if file is not found
* @throws IOException if writing to the stream fails
* @throws SerializationException if serialization fails
* @throws UnsupportedModifierException if the modifier is not supported for this element
*/
public default void write(File file, Charset charset, Object obj, OutputModifier modifier) throws IOException, SerializationException {
public default void write(File file, Charset charset, Object obj, OutputModifier modifier) throws IOException, SerializationException, UnsupportedModifierException {
try (OutputStream out = new FileOutputStream(file)) {
write(out, charset, obj, modifier);
}
Expand All @@ -148,8 +156,9 @@ public default void write(File file, Charset charset, Object obj, OutputModifier
* @throws FileNotFoundException if file is not found
* @throws IOException if writing to the stream fails
* @throws SerializationException if serialization fails
* @throws UnsupportedModifierException if the modifier is not supported for this element
*/
public default void write(File file, Charset charset, Object obj) throws IOException, SerializationException {
public default void write(File file, Charset charset, Object obj) throws IOException, SerializationException, UnsupportedModifierException {
write(file, charset, obj, OutputModifier.DEFAULT);
}

Expand All @@ -163,8 +172,9 @@ public default void write(File file, Charset charset, Object obj) throws IOExcep
* @throws FileNotFoundException if file is not found
* @throws IOException if writing to the stream fails
* @throws SerializationException if serialization fails
* @throws UnsupportedModifierException if the modifier is not supported for this element
*/
public default void write(File file, Object obj, OutputModifier modifier) throws IOException, SerializationException {
public default void write(File file, Object obj, OutputModifier modifier) throws IOException, SerializationException, UnsupportedModifierException {
write(file, DEFAULT_CHARSET, obj, modifier);
}

Expand All @@ -178,8 +188,9 @@ public default void write(File file, Object obj, OutputModifier modifier) throws
* @throws FileNotFoundException if file is not found
* @throws IOException if writing to the stream fails
* @throws SerializationException if serialization fails
* @throws UnsupportedModifierException if the modifier is not supported for this element
*/
public default void write(File file, Object obj) throws IOException, SerializationException {
public default void write(File file, Object obj) throws IOException, SerializationException, UnsupportedModifierException {
write(file, obj, OutputModifier.DEFAULT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import de.fraunhofer.iosb.ilt.faaast.service.model.serialization.DataFormat;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Objects;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.InMemoryFile;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonSerializer;
import org.eclipse.digitaltwin.aas4j.v3.model.Environment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
Expand All @@ -31,6 +34,7 @@
@SupportedDataformat(DataFormat.JSON)
public class JsonEnvironmentSerializer implements EnvironmentSerializer {

private static final Logger LOGGER = LoggerFactory.getLogger(JsonEnvironmentSerializer.class);
private final JsonSerializer serializer;

public JsonEnvironmentSerializer() {
Expand All @@ -40,8 +44,8 @@ public JsonEnvironmentSerializer() {

@Override
public byte[] write(Charset charset, Environment environment, Collection<InMemoryFile> files) throws SerializationException {
if (files != null && !files.isEmpty()) {
throw new UnsupportedOperationException("serializing file content is not supported for data format JSON");
if (Objects.nonNull(files) && !files.isEmpty()) {
LOGGER.debug("embedded files are ignored when serializing to JSON");
}
try {
return serializer.write(environment).getBytes(charset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
@SupportedDataformat(DataFormat.JSONLD)
public class JsonLDEnvironmentSerializer implements EnvironmentSerializer {

// private static final Logger LOGGER = LoggerFactory.getLogger(JsonLDEnvironmentSerializer.class);
// private final Serializer serializer;

public JsonLDEnvironmentSerializer() {
Expand All @@ -40,8 +41,8 @@ public JsonLDEnvironmentSerializer() {
@Override
public byte[] write(Charset charset, Environment environment, Collection<InMemoryFile> files) throws SerializationException {
throw new UnsupportedOperationException("Current version of AAS4j library does not support RDF/JSON-LD de-/serialization");
// if (files != null && !files.isEmpty()) {
// throw new UnsupportedOperationException("serializing file content is not supported for data format RDF");
// if (Objects.nonNull(files) && !files.isEmpty()) {
// LOGGER.debug("embedded files are ignored when serializing to JSON-LD");
// }
// try {
// return serializer.write(environment, Lang.JSONLD).getBytes(charset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
@SupportedDataformat(DataFormat.RDF)
public class RdfEnvironmentSerializer implements EnvironmentSerializer {

// private static final Logger LOGGER = LoggerFactory.getLogger(RdfEnvironmentSerializer.class);
public static final Lang DEFAULT_RDF_LANGUAGE = Lang.TTL;
// private final Serializer serializer;

Expand All @@ -56,8 +57,8 @@ public RdfEnvironmentSerializer() {
*/
public byte[] write(Charset charset, Environment environment, Collection<InMemoryFile> files, Lang rdfLanguage) throws SerializationException {
throw new UnsupportedOperationException("Current version of AAS4j library does not support RDF/JSON-LD de-/serialization");
// if (files != null && !files.isEmpty()) {
// throw new UnsupportedOperationException("serializing file content is not supported for data format RDF");
// if (Objects.nonNull(files) && !files.isEmpty()) {
// LOGGER.debug("embedded files are ignored when serializing to RDF");
// }
// try {
// return serializer.write(environment, rdfLanguage).getBytes(charset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import de.fraunhofer.iosb.ilt.faaast.service.model.serialization.DataFormat;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Objects;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.InMemoryFile;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.xml.XmlSerializer;
import org.eclipse.digitaltwin.aas4j.v3.model.Environment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
Expand All @@ -31,6 +34,7 @@
@SupportedDataformat(DataFormat.XML)
public class XmlEnvironmentSerializer implements EnvironmentSerializer {

private static final Logger LOGGER = LoggerFactory.getLogger(XmlEnvironmentSerializer.class);
private final XmlSerializer serializer;

public XmlEnvironmentSerializer() {
Expand All @@ -40,8 +44,8 @@ public XmlEnvironmentSerializer() {

@Override
public byte[] write(Charset charset, Environment environment, Collection<InMemoryFile> files) throws SerializationException {
if (files != null && !files.isEmpty()) {
throw new UnsupportedOperationException("serializing file content is not supported for data format XML");
if (Objects.nonNull(files) && !files.isEmpty()) {
LOGGER.debug("embedded files are ignored when serializing to XML");
}
try {
return serializer.write(environment).getBytes(charset);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2021 Fraunhofer IOSB, eine rechtlich nicht selbstaendige
* Einrichtung der Fraunhofer-Gesellschaft zur Foerderung der angewandten
* Forschung e.V.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.fraunhofer.iosb.ilt.faaast.service.request.handler.aasrepository;

import de.fraunhofer.iosb.ilt.faaast.service.exception.MessageBusException;
import de.fraunhofer.iosb.ilt.faaast.service.model.api.paging.Page;
import de.fraunhofer.iosb.ilt.faaast.service.model.api.request.aasrepository.GetAllAssetAdministrationShellsByAssetIdReferenceRequest;
import de.fraunhofer.iosb.ilt.faaast.service.model.api.response.aasrepository.GetAllAssetAdministrationShellsByAssetIdReferenceResponse;
import de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.access.ElementReadEventMessage;
import de.fraunhofer.iosb.ilt.faaast.service.persistence.AssetAdministrationShellSearchCriteria;
import de.fraunhofer.iosb.ilt.faaast.service.request.handler.AbstractRequestHandler;
import de.fraunhofer.iosb.ilt.faaast.service.request.handler.RequestExecutionContext;
import de.fraunhofer.iosb.ilt.faaast.service.util.LambdaExceptionHelper;
import de.fraunhofer.iosb.ilt.faaast.service.util.ReferenceBuilder;
import java.util.List;
import java.util.stream.Collectors;
import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell;
import org.eclipse.digitaltwin.aas4j.v3.model.Reference;


/**
* Class to handle a
* {@link de.fraunhofer.iosb.ilt.faaast.service.model.api.request.aasrepository.GetAllAssetAdministrationShellsByAssetIdRequest}
* in the service and to send the corresponding response
* {@link de.fraunhofer.iosb.ilt.faaast.service.model.api.response.aasrepository.GetAllAssetAdministrationShellsByAssetIdResponse}.
* Is responsible for communication with the persistence and sends the corresponding events to the message bus.
*/
public class GetAllAssetAdministrationShellsByAssetIdReferenceRequestHandler
extends AbstractRequestHandler<GetAllAssetAdministrationShellsByAssetIdReferenceRequest, GetAllAssetAdministrationShellsByAssetIdReferenceResponse> {

public GetAllAssetAdministrationShellsByAssetIdReferenceRequestHandler(RequestExecutionContext context) {
super(context);
}


@Override
public GetAllAssetAdministrationShellsByAssetIdReferenceResponse process(GetAllAssetAdministrationShellsByAssetIdReferenceRequest request) throws MessageBusException {
Page<AssetAdministrationShell> page = context.getPersistence().findAssetAdministrationShells(
AssetAdministrationShellSearchCriteria.builder()
.assetIds(parseSpecificAssetIds(request.getAssetIds()))
.build(),
request.getOutputModifier(),
request.getPagingInfo());
if (!request.isInternal()) {
page.getContent().forEach(LambdaExceptionHelper.rethrowConsumer(
x -> context.getMessageBus().publish(ElementReadEventMessage.builder()
.element(x)
.value(x)
.build())));
}
List<Reference> result = page.getContent().stream()
.map(ReferenceBuilder::forAas)
.collect(Collectors.toList());
return GetAllAssetAdministrationShellsByAssetIdReferenceResponse.builder()
.payload(Page.of(result, page.getMetadata()))
.success()
.build();
}

}
Loading

0 comments on commit 0ed0782

Please sign in to comment.