Skip to content

Commit

Permalink
feat(impl):[#359] add new helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
ds-ext-kmassalski committed Mar 27, 2024
1 parent ca34021 commit 7a990b2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,12 @@
import static org.eclipse.tractusx.irs.util.TestMother.batchAspectName;
import static org.eclipse.tractusx.irs.util.TestMother.singleLevelBomAsBuiltAspectName;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.eclipse.edc.spi.types.domain.edr.EndpointDataReference;
import org.eclipse.tractusx.irs.component.PartChainIdentificationKey;
import org.eclipse.tractusx.irs.component.RegisterJob;
Expand All @@ -53,6 +48,7 @@
import org.eclipse.tractusx.irs.data.StringMapper;
import org.eclipse.tractusx.irs.edc.client.configuration.JsonLdConfiguration;
import org.eclipse.tractusx.irs.edc.client.model.EDRAuthCode;
import org.eclipse.tractusx.irs.registryclient.util.SerializationHelper;
import org.eclipse.tractusx.irs.semanticshub.SemanticHubWireMockSupport;
import org.eclipse.tractusx.irs.testing.wiremock.DiscoveryServiceWiremockSupport;
import org.eclipse.tractusx.irs.testing.wiremock.DtrWiremockSupport;
Expand Down Expand Up @@ -108,20 +104,7 @@ static String encodedAssetIds(final String assetIds) {
.name("globalAssetId")
.value(assetIds)
.build();
return Base64.getEncoder().encodeToString(serialize(globalAssetId));
}

private static byte[] serialize(final IdentifierKeyValuePair assetIds) {
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
final ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.writeValue(stream, assetIds);
return stream.toByteArray();
} catch (final IOException e) {
return new byte[0];
}
return Base64.getEncoder().encodeToString(new SerializationHelper().serialize(globalAssetId));
}

static void verifyDiscoveryCalls(final int times) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,16 @@
********************************************************************************/
package org.eclipse.tractusx.irs.registryclient.decentral;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import io.github.resilience4j.retry.annotation.Retry;
import org.eclipse.edc.spi.types.domain.edr.EndpointDataReference;
import org.eclipse.tractusx.irs.component.assetadministrationshell.AssetAdministrationShellDescriptor;
import org.eclipse.tractusx.irs.component.assetadministrationshell.IdentifierKeyValuePair;
import org.eclipse.tractusx.irs.registryclient.util.SerializationHelper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
Expand All @@ -54,18 +49,12 @@ public class DecentralDigitalTwinRegistryClient {
private static final String PLACEHOLDER_AAS_IDENTIFIER = "aasIdentifier";
private static final String PLACEHOLDER_ASSET_IDS = "assetIds";

private static final ObjectWriter WRITER;
static {
final ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
WRITER = mapper.writer();
}

private final RestTemplate edcRestTemplate;
private final String shellDescriptorTemplate;
private final String lookupShellsTemplate;

private final SerializationHelper serializationHelper = new SerializationHelper();

public DecentralDigitalTwinRegistryClient(final RestTemplate edcRestTemplate,
@Value("${digitalTwinRegistry.shellDescriptorTemplate:}") final String shellDescriptorTemplate,
@Value("${digitalTwinRegistry.lookupShellsTemplate:}") final String lookupShellsTemplate) {
Expand Down Expand Up @@ -95,22 +84,12 @@ public LookupShellsResponse getAllAssetAdministrationShellIdsByAssetLink(
new HttpEntity<>(null, headers(endpointDataReference)), LookupShellsResponse.class).getBody();
}

private static String encodeWithBase64(final String aasIdentifier) {
private String encodeWithBase64(final String aasIdentifier) {
return Base64.getEncoder().encodeToString(aasIdentifier.getBytes(StandardCharsets.UTF_8));
}

private static String encodeWithBase64(final IdentifierKeyValuePair assetIds) {
return Base64.getEncoder().encodeToString(serialize(assetIds));
}

private static byte[] serialize(final IdentifierKeyValuePair assetIds) {
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
WRITER.writeValue(stream, assetIds);
return stream.toByteArray();
} catch (final IOException e) {
return new byte[0];
}
private String encodeWithBase64(final IdentifierKeyValuePair assetIds) {
return Base64.getEncoder().encodeToString(serializationHelper.serialize(assetIds));
}

private HttpHeaders headers(final EndpointDataReference dataReference) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/********************************************************************************
* Copyright (c) 2022,2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
package org.eclipse.tractusx.irs.registryclient.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;

/**
* Serializer helper
*/
public class SerializationHelper {

private static final ObjectWriter WRITER;
static {
final ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
WRITER = mapper.writer();
}

public byte[] serialize(final Object object) {
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
WRITER.writeValue(stream, object);
return stream.toByteArray();
} catch (final IOException e) {
return new byte[0];
}
}
}

0 comments on commit 7a990b2

Please sign in to comment.