From 70f436144b9e0880dd704d2098cbd1b1bdf95190 Mon Sep 17 00:00:00 2001
From: RIHTARSIC Joze <joze.rihtarsic@ext.ec.europa.eu>
Date: Wed, 7 Feb 2024 09:08:46 +0100
Subject: [PATCH] [EDELIVERY-12625] Fix formatting on store Oasis SMP resource

---
 .../handler/OasisSMPResource10Handler.java    | 42 +++++++++++------
 .../handler/OasisSMPResource20Handler.java    | 46 +++++++++++++------
 2 files changed, 60 insertions(+), 28 deletions(-)

diff --git a/smp-resource-extensions/oasis-smp-spi/src/main/java/eu/europa/ec/smp/spi/handler/OasisSMPResource10Handler.java b/smp-resource-extensions/oasis-smp-spi/src/main/java/eu/europa/ec/smp/spi/handler/OasisSMPResource10Handler.java
index 10d105d53..a2fb0a759 100644
--- a/smp-resource-extensions/oasis-smp-spi/src/main/java/eu/europa/ec/smp/spi/handler/OasisSMPResource10Handler.java
+++ b/smp-resource-extensions/oasis-smp-spi/src/main/java/eu/europa/ec/smp/spi/handler/OasisSMPResource10Handler.java
@@ -8,9 +8,9 @@
  * versions of the EUPL (the "Licence");
  * You may not use this work except in compliance with the Licence.
  * You may obtain a copy of the Licence at:
- * 
+ *
  * [PROJECT_HOME]\license\eupl-1.2\license.txt or https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software distributed under the Licence is
  * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the Licence for the specific language governing permissions and limitations under the Licence.
@@ -33,11 +33,13 @@ import gen.eu.europa.ec.ddc.api.smp10.ParticipantIdentifierType;
 import gen.eu.europa.ec.ddc.api.smp10.ServiceGroup;
 import gen.eu.europa.ec.ddc.api.smp10.ServiceMetadataReferenceCollectionType;
 import gen.eu.europa.ec.ddc.api.smp10.ServiceMetadataReferenceType;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.apache.hc.core5.net.URIBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
+import org.springframework.util.StreamUtils;
 
 import java.io.BufferedInputStream;
 import java.io.ByteArrayInputStream;
@@ -154,6 +156,7 @@ public class OasisSMPResource10Handler extends AbstractOasisSMPHandler {
 
     @Override
     public void storeResource(RequestData resourceData, ResponseData responseData) throws ResourceException {
+        LOG.info("Store resource for identifier [{}].", resourceData.getResourceIdentifier());
         InputStream inputStream = resourceData.getResourceInputStream();
         // reading resource multiple time make sure it can be rest
         if (!inputStream.markSupported()) {
@@ -168,18 +171,29 @@ public class OasisSMPResource10Handler extends AbstractOasisSMPHandler {
                 && !resource.getServiceMetadataReferenceCollection().getServiceMetadataReferences().isEmpty()) {
             throw new ResourceException(INVALID_PARAMETERS, "ServiceMetadataReferenceCollection must be empty!");
         }
-        // set participant to "lowercase" to match it as is saved in the database
-        // this is just for back-compatibility issue!
-        resource.getParticipantIdentifier().setValue(resourceData.getResourceIdentifier().getValue());
-        resource.getParticipantIdentifier().setScheme(resourceData.getResourceIdentifier().getScheme());
-
-        try {
-            //inputStream.reset();
-            //StreamUtils.copy(inputStream, responseData.getOutputStream());
-            // need to save resource because of the update on the resource identifier values
-            reader.serializeNative(resource, responseData.getOutputStream(), true);
-        } catch (TechnicalException e) {
-            throw new ResourceException(PARSE_ERROR, "Error occurred while copying the ServiceGroup", e);
+        // back-compatibility issue: set participant to "lowercase" to match it as is saved in the database
+        ParticipantIdentifierType orgResourceId = resource.getParticipantIdentifier();
+        ResourceIdentifier nrmResourceId = resourceData.getResourceIdentifier();
+        boolean isSame = StringUtils.equals(orgResourceId.getValue(), nrmResourceId.getValue())
+                && StringUtils.equals(orgResourceId.getScheme(), nrmResourceId.getScheme());
+
+        if (isSame) {
+            try {
+                inputStream.reset();
+                StreamUtils.copy(inputStream, responseData.getOutputStream());
+            } catch (IOException e) {
+                throw new ResourceException(PARSE_ERROR, "Error occurred while copying the ServiceGroup", e);
+            }
+        } else {
+            LOG.info("Update ServiceGroup identifier before saving. Old: [{}], New: [{}]", orgResourceId, nrmResourceId);
+            orgResourceId.setValue(nrmResourceId.getValue());
+            orgResourceId.setScheme(nrmResourceId.getScheme());
+            try {
+                // need to save resource because of the update on the resource identifier values
+                reader.serializeNative(resource, responseData.getOutputStream(), true);
+            } catch (TechnicalException e) {
+                throw new ResourceException(PARSE_ERROR, "Error occurred while copying the ServiceGroup", e);
+            }
         }
     }
 
diff --git a/smp-resource-extensions/oasis-smp-spi/src/main/java/eu/europa/ec/smp/spi/handler/OasisSMPResource20Handler.java b/smp-resource-extensions/oasis-smp-spi/src/main/java/eu/europa/ec/smp/spi/handler/OasisSMPResource20Handler.java
index bcbe0a46c..2e07ad2c2 100644
--- a/smp-resource-extensions/oasis-smp-spi/src/main/java/eu/europa/ec/smp/spi/handler/OasisSMPResource20Handler.java
+++ b/smp-resource-extensions/oasis-smp-spi/src/main/java/eu/europa/ec/smp/spi/handler/OasisSMPResource20Handler.java
@@ -8,9 +8,9 @@
  * versions of the EUPL (the "Licence");
  * You may not use this work except in compliance with the Licence.
  * You may obtain a copy of the Licence at:
- * 
+ *
  * [PROJECT_HOME]\license\eupl-1.2\license.txt or https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software distributed under the Licence is
  * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the Licence for the specific language governing permissions and limitations under the Licence.
@@ -37,10 +37,12 @@ import gen.eu.europa.ec.ddc.api.smp20.aggregate.ServiceReference;
 import gen.eu.europa.ec.ddc.api.smp20.basic.ID;
 import gen.eu.europa.ec.ddc.api.smp20.basic.ParticipantID;
 import gen.eu.europa.ec.ddc.api.smp20.basic.SMPVersionID;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
+import org.springframework.util.StreamUtils;
 import org.w3c.dom.Document;
 
 import javax.xml.transform.TransformerException;
@@ -80,7 +82,6 @@ public class OasisSMPResource20Handler extends AbstractOasisSMPHandler {
     public void generateResource(RequestData resourceData, ResponseData responseData, List<String> fields) throws ResourceException {
         ResourceIdentifier identifier = getResourceIdentifier(resourceData);
 
-
         ServiceGroup resource = new ServiceGroup();
         resource.setSMPVersionID(new SMPVersionID());
         resource.getSMPVersionID().setValue("2.0");
@@ -145,6 +146,7 @@ public class OasisSMPResource20Handler extends AbstractOasisSMPHandler {
 
     @Override
     public void storeResource(RequestData resourceData, ResponseData responseData) throws ResourceException {
+        LOG.info("Store resource for identifier [{}].", resourceData.getResourceIdentifier());
         InputStream inputStream = resourceData.getResourceInputStream();
         // reading resource multiple time make sure it can be rest
         if (!inputStream.markSupported()) {
@@ -154,18 +156,34 @@ public class OasisSMPResource20Handler extends AbstractOasisSMPHandler {
         ServiceGroup resource = validateAndParse(resourceData);
 
         // ServiceMetadataReferenceCollection must be empty because they are automatically generated
-        if (!resource.getServiceReferences().isEmpty()) {
-            throw new ResourceException(INVALID_PARAMETERS, "ServiceReferences must be empty!");
+        if (resource.getServiceReferences() != null
+                && !resource.getServiceReferences().isEmpty()) {
+            throw new ResourceException(INVALID_PARAMETERS, "Service references must be empty!");
         }
-        // set participant to "lowercase" to match it as is saved in the database
-        // this is just for back-compatibility issue!
-        resource.getParticipantID().setValue(resourceData.getResourceIdentifier().getValue());
-        resource.getParticipantID().setSchemeID(resourceData.getResourceIdentifier().getScheme());
-
-        try {
-            reader.serializeNative(resource, responseData.getOutputStream(), false);
-        } catch (TechnicalException e) {
-            throw new ResourceException(PARSE_ERROR, "Error occurred while copying the ServiceGroup", e);
+        // back-compatibility issue: set participant to "lowercase" to match it as is saved in the database
+        ParticipantID orgResourceId = resource.getParticipantID();
+        ResourceIdentifier nrmResourceId = resourceData.getResourceIdentifier();
+        boolean isSame = StringUtils.equals(orgResourceId.getValue(), nrmResourceId.getValue())
+                && StringUtils.equals(orgResourceId.getSchemeID(), nrmResourceId.getScheme());
+
+        if (isSame) {
+            try {
+                inputStream.reset();
+                StreamUtils.copy(inputStream, responseData.getOutputStream());
+            } catch (IOException e) {
+                throw new ResourceException(PARSE_ERROR, "Error occurred while copying the ServiceGroup", e);
+            }
+
+        } else {
+            LOG.info("Update ServiceGroup identifier before saving. Old: [{}], New: [{}]", orgResourceId, nrmResourceId);
+            orgResourceId.setValue(nrmResourceId.getValue());
+            orgResourceId.setSchemeID(nrmResourceId.getScheme());
+            try {
+                // need to save resource because of the update on the resource identifier values
+                reader.serializeNative(resource, responseData.getOutputStream(), true);
+            } catch (TechnicalException e) {
+                throw new ResourceException(PARSE_ERROR, "Error occurred while copying the ServiceGroup", e);
+            }
         }
     }
 
-- 
GitLab