1+ /*******************************************************************************
2+ * Copyright (C) 2025 the Eclipse BaSyx Authors
3+ *
4+ * Permission is hereby granted, free of charge, to any person obtaining
5+ * a copy of this software and associated documentation files (the
6+ * "Software"), to deal in the Software without restriction, including
7+ * without limitation the rights to use, copy, modify, merge, publish,
8+ * distribute, sublicense, and/or sell copies of the Software, and to
9+ * permit persons to whom the Software is furnished to do so, subject to
10+ * the following conditions:
11+ *
12+ * The above copyright notice and this permission notice shall be
13+ * included in all copies or substantial portions of the Software.
14+ *
15+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+ *
23+ * SPDX-License-Identifier: MIT
24+ ******************************************************************************/
25+
26+ package org .eclipse .digitaltwin .basyx .aasenvironment .client ;
27+
28+ import java .io .IOException ;
29+ import java .nio .charset .StandardCharsets ;
30+ import java .util .List ;
31+
32+ import org .apache .commons .lang3 .NotImplementedException ;
33+ import org .eclipse .digitaltwin .aas4j .v3 .dataformat .core .SerializationException ;
34+ import org .eclipse .digitaltwin .basyx .aasenvironment .AasEnvironment ;
35+ import org .eclipse .digitaltwin .basyx .aasenvironment .client .internal .SerializationApi ;
36+ import org .eclipse .digitaltwin .basyx .aasenvironment .environmentloader .CompleteEnvironment ;
37+ import org .springframework .core .io .ByteArrayResource ;
38+ import org .springframework .core .io .Resource ;
39+
40+ public class ConnectedAasEnvironment implements AasEnvironment {
41+ protected static final String ENVIRONMENT_BASE_PATH = "http://localhost:8081" ;
42+
43+ private final SerializationApi serializationApi ;
44+
45+ ConnectedAasEnvironment () {
46+ this .serializationApi = new SerializationApi (ENVIRONMENT_BASE_PATH );
47+ }
48+
49+ public ConnectedAasEnvironment (String environmentBaseUrl ) {
50+ this .serializationApi = new SerializationApi (environmentBaseUrl );
51+ }
52+
53+ ConnectedAasEnvironment (SerializationApi serializationApi ) {
54+ this .serializationApi = serializationApi ;
55+ }
56+
57+ /**
58+ * Generates serialization by IDs
59+ *
60+ * @param serializationFormat the format in which the serialization is required
61+ * @param aasIds the list of AAS IDs to be serialized
62+ * @param submodelIds the list of Submodel IDs to be serialized
63+ * @param includeConceptDescriptions whether to include concept descriptions
64+ * @return The serialized object as a byte array or stream
65+ */
66+ public Resource generateSerializationByIds (String serializationFormat , List <String > aasIds , List <String > submodelIds , boolean includeConceptDescriptions ) {
67+ try {
68+ return serializationApi .generateSerializationByIds (serializationFormat , aasIds , submodelIds , includeConceptDescriptions );
69+ } catch (Exception e ) {
70+ // Handle the exception, wrap it, or rethrow it as appropriate
71+ throw new RuntimeException ("Error generating serialization" , e );
72+ }
73+ }
74+
75+ @ Override
76+ public String createJSONAASEnvironmentSerialization (List <String > aasIds , List <String > submodelIds ,
77+ boolean includeConceptDescriptions ) throws SerializationException {
78+ try {
79+ Resource res = serializationApi .generateSerializationByIds ("application/json" , aasIds , submodelIds , includeConceptDescriptions );
80+
81+ String jsonStr = new String (((ByteArrayResource ) res ).getByteArray (), StandardCharsets .UTF_8 );
82+
83+ return jsonStr ;
84+ } catch (Exception e ) {
85+ throw new RuntimeException ("Error generating serialization" , e );
86+ }
87+ }
88+
89+ @ Override
90+ public String createXMLAASEnvironmentSerialization (List <String > aasIds , List <String > submodelIds ,
91+ boolean includeConceptDescriptions ) throws SerializationException {
92+ try {
93+ Resource res = serializationApi .generateSerializationByIds ("application/xml" , aasIds , submodelIds , includeConceptDescriptions );
94+
95+ String jsonStr = new String (((ByteArrayResource ) res ).getByteArray (), StandardCharsets .UTF_8 );
96+
97+ return jsonStr ;
98+ } catch (Exception e ) {
99+ throw new RuntimeException ("Error generating serialization" , e );
100+ }
101+ }
102+
103+ @ Override
104+ public byte [] createAASXAASEnvironmentSerialization (List <String > aasIds , List <String > submodelIds ,
105+ boolean includeConceptDescriptions ) throws SerializationException , IOException {
106+ try {
107+ Resource res = serializationApi .generateSerializationByIds ("application/asset-administration-shell-package+xml" , aasIds , submodelIds , includeConceptDescriptions );
108+ return ((ByteArrayResource ) res ).getByteArray ();
109+ } catch (Exception e ) {
110+ throw new RuntimeException ("Error generating serialization" , e );
111+ }
112+ }
113+
114+ @ Override
115+ public void loadEnvironment (CompleteEnvironment completeEnvironment , boolean ignoreDuplicates ) {
116+ throw new NotImplementedException (
117+ "loadEnvironment(CompleteEnvironment completeEnvironment, boolean ignoreDuplicates) is not yet implemented" );
118+ }
119+
120+ @ Override
121+ public void loadEnvironment (CompleteEnvironment completeEnvironment ) {
122+ throw new NotImplementedException (
123+ "loadEnvironment(CompleteEnvironment completeEnvironment) is not yet implemented" );
124+ }
125+ }
0 commit comments