From 93f6e688364f564ef2c514ffb6727629f0df77b4 Mon Sep 17 00:00:00 2001
From: RIHTARSIC Joze <joze.rihtarsic@ext.ec.europa.eu>
Date: Wed, 14 Jun 2023 15:41:41 +0200
Subject: [PATCH] Fix url generation

---
 .../OasisSMPServiceGroup10Handler.java        | 13 +++--
 .../ec/edelivery/smp/utils/SmpUrlBuilder.java | 49 ++-----------------
 .../smp/controllers/ResourceController.java   |  7 ++-
 smp-webapp/src/main/webapp/WEB-INF/web.xml    |  2 -
 ...rTest.java => ResourceControllerTest.java} |  7 ++-
 .../src/test/resources/logback-test.xml       |  4 +-
 .../webapp_integration_test_data.sql          |  6 +++
 7 files changed, 23 insertions(+), 65 deletions(-)
 rename smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/controllers/{ServiceGroupControllerTest.java => ResourceControllerTest.java} (99%)

diff --git a/smp-resource-extensions/oasis-smp-spi/src/main/java/eu/europa/ec/smp/spi/handler/OasisSMPServiceGroup10Handler.java b/smp-resource-extensions/oasis-smp-spi/src/main/java/eu/europa/ec/smp/spi/handler/OasisSMPServiceGroup10Handler.java
index 01aed3273..08a37ae8c 100644
--- a/smp-resource-extensions/oasis-smp-spi/src/main/java/eu/europa/ec/smp/spi/handler/OasisSMPServiceGroup10Handler.java
+++ b/smp-resource-extensions/oasis-smp-spi/src/main/java/eu/europa/ec/smp/spi/handler/OasisSMPServiceGroup10Handler.java
@@ -107,7 +107,7 @@ public class OasisSMPServiceGroup10Handler extends AbstractOasisSMPHandler {
         for (ResourceIdentifier subresId : subResourceIdentifier) {
             URI url = buildSMPURLForParticipantAndDocumentIdentifier(resourceIdentifier, subresId);
             ServiceMetadataReferenceType referenceType = new ServiceMetadataReferenceType();
-            referenceType.setHref(url.getPath());
+            referenceType.setHref(url.toString());
             referenceIds.add(referenceType);
         }
         return referenceIds;
@@ -116,19 +116,18 @@ public class OasisSMPServiceGroup10Handler extends AbstractOasisSMPHandler {
     public URI buildSMPURLForParticipantAndDocumentIdentifier(ResourceIdentifier resourceIdentifier, ResourceIdentifier subresourceIdentifier) throws ResourceException {
         LOG.debug("Build SMP url for participant identifier: [{}] and document identifier [{}].", resourceIdentifier, subresourceIdentifier);
 
-
         String pathSegment = smpDataApi.getURIPathSegmentForSubresource(OasisSMPServiceMetadata10.RESOURCE_IDENTIFIER);
         String baseUrl = smpDataApi.getResourceUrl();
-        String urlEncodedFormatParticipant = smpIdentifierApi.getURLEncodedResourceIdentifier(resourceIdentifier);
-        String urlEncodedFormatDocument = smpIdentifierApi.getURLEncodedSubresourceIdentifier(subresourceIdentifier);
+        String formattedParticipant = smpIdentifierApi.formatResourceIdentifier(resourceIdentifier);
+        String formattedDocument = smpIdentifierApi.formatSubresourceIdentifier(subresourceIdentifier);
 
         LOG.debug("Build SMP url from base path [{}], participant identifier: [{}] and document identifier [{}].",
-                baseUrl, urlEncodedFormatParticipant, urlEncodedFormatDocument);
+                baseUrl, formattedParticipant, formattedDocument);
         try {
             return new URIBuilder(baseUrl)
-                    .appendPathSegments(urlEncodedFormatParticipant)
+                    .appendPathSegments(formattedParticipant)
                     .appendPathSegments(pathSegment)
-                    .appendPathSegments(urlEncodedFormatDocument).build();
+                    .appendPathSegments(formattedDocument).build();
         } catch (URISyntaxException e) {
             throw new ResourceException(INTERNAL_ERROR, "Can not build SMP document URL path! " + ExceptionUtils.getMessage(e), e);
         }
diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/utils/SmpUrlBuilder.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/utils/SmpUrlBuilder.java
index d152b288e..f7229c913 100644
--- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/utils/SmpUrlBuilder.java
+++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/utils/SmpUrlBuilder.java
@@ -56,12 +56,7 @@ public class SmpUrlBuilder {
         this.configurationService = configurationService;
     }
 
-    public URI getCurrentUri() {
-        return ServletUriComponentsBuilder.fromCurrentRequestUri().build().toUri();
-    }
-
     public String buildSMPUrlForApplication() {
-        LOG.debug("Build SMP url for Application.");
         HttpServletRequest req = getCurrentRequest();
         HttpForwardedHeaders fh = new HttpForwardedHeaders(req);
         LOG.debug("Generate response uri with headers data: [{}]", fh);
@@ -78,48 +73,10 @@ public class SmpUrlBuilder {
             }
             uriBuilder = uriBuilder.scheme(fh.getProto());
         } else {
-            LOG.debug("Ignore settings header because host is null!");
+            LOG.info("Ignore settings header because host is null!");
         }
+        return uriBuilder.build().toUriString();
 
-        return uriBuilder
-                .toUriString();
-    }
-
-    /*
-        public String buildSMPUrlForParticipantAndDocumentIdentifier(ParticipantIdentifierType participantId, DocumentIdentifier docId) {
-            LOG.debug("Build SMP url for participant identifier: [{}] and document identifier [{}].", participantId, docId);
-            HttpServletRequest req = getCurrentRequest();
-            HttpForwardedHeaders fh = new HttpForwardedHeaders(req);
-            LOG.debug("Generate response uri with headers data: [{}]", fh);
-            UriComponentsBuilder uriBuilder = getSMPUrlBuilder();//
-            if (fh.getHost() != null) {
-                LOG.debug("Set response uri for forwarded headers: [{}]", fh);
-                uriBuilder = uriBuilder.host(fh.getHost());
-                String port = fh.getNonDefaultPort();
-                if (!StringUtils.isBlank(port)) {
-                    uriBuilder = uriBuilder.port(port);
-                } else if (!StringUtils.isBlank(fh.getPort())) {
-                    LOG.debug("Set port to null because it is default port: [{}]", fh);
-                    uriBuilder = uriBuilder.port(null);
-                }
-                uriBuilder = uriBuilder.scheme(fh.getProto());
-            } else {
-                LOG.debug("Ignore settings header because host is null!");
-            }
-            String urlEncodedFormatParticipant = identifierService.urlEncodedFormatParticipant(participantId);
-            String urlEncodedFormatDocument = identifierService.urlEncodedFormatDocument(docId);
-
-            return uriBuilder
-                    .path(SMP_DOCUMENT_RESOURCE_TEMPLATE)
-                    .buildAndExpand(urlEncodedFormatParticipant, urlEncodedFormatDocument)
-                    .toUriString();
-        }
-    */
-    public String buildSMPUrlForPath(String path) {
-        LOG.debug("Build SMP url for path: [{}].", path);
-
-        UriComponentsBuilder uriBuilder = getSMPUrlBuilder();
-        return uriBuilder.path(path).build().toUriString();
     }
 
     /**
@@ -131,7 +88,7 @@ public class SmpUrlBuilder {
     public UriComponentsBuilder getSMPUrlBuilder() {
 
         UriComponentsBuilder uriBuilder = ServletUriComponentsBuilder.fromCurrentRequestUri();
-        // uriBuilder = uriBuilder.replacePath(getUrlContext());
+        uriBuilder = uriBuilder.replacePath(getUrlContext());
         return uriBuilder;
     }
 
diff --git a/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/controllers/ResourceController.java b/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/controllers/ResourceController.java
index eeae04b83..ab940997b 100644
--- a/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/controllers/ResourceController.java
+++ b/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/controllers/ResourceController.java
@@ -35,7 +35,6 @@ import java.util.stream.Collectors;
 import static eu.europa.ec.edelivery.smp.exceptions.ErrorCode.INTERNAL_ERROR;
 import static eu.europa.ec.edelivery.smp.exceptions.ErrorCode.INVALID_REQUEST;
 import static eu.europa.ec.edelivery.smp.servlet.WebConstants.*;
-import static java.net.URLDecoder.decode;
 import static org.apache.commons.lang3.StringUtils.lowerCase;
 
 /**
@@ -163,11 +162,11 @@ public class ResourceController {
         return new ResourceRequest(resourceAction, headersMap, pathParameters, inputStream);
     }
 
-    public static String  urlDecode(String header){
+    public static String urlDecode(String header) {
         try {
-            return header==null?null: URLDecoder.decode(header, UTF_8);
+            return header == null ? null : URLDecoder.decode(header, UTF_8);
         } catch (UnsupportedEncodingException e) {
-            throw new SMPRuntimeException(INTERNAL_ERROR,"DecodeHeader", ExceptionUtils.getRootCauseMessage( e));
+            throw new SMPRuntimeException(INTERNAL_ERROR, "DecodeHeader", ExceptionUtils.getRootCauseMessage(e));
         }
     }
 
diff --git a/smp-webapp/src/main/webapp/WEB-INF/web.xml b/smp-webapp/src/main/webapp/WEB-INF/web.xml
index 6c79c4a1a..851c2abeb 100644
--- a/smp-webapp/src/main/webapp/WEB-INF/web.xml
+++ b/smp-webapp/src/main/webapp/WEB-INF/web.xml
@@ -90,10 +90,8 @@
     -->
     <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
-
     </listener>
     <listener>
         <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
     </listener>
-
 </web-app>
diff --git a/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/controllers/ServiceGroupControllerTest.java b/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/controllers/ResourceControllerTest.java
similarity index 99%
rename from smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/controllers/ServiceGroupControllerTest.java
rename to smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/controllers/ResourceControllerTest.java
index c1680476e..677df1bed 100644
--- a/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/controllers/ServiceGroupControllerTest.java
+++ b/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/controllers/ResourceControllerTest.java
@@ -59,8 +59,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
                 "update SMP_CONFIGURATION set PROPERTY_VALUE='true', LAST_UPDATED_ON=NOW() where PROPERTY_NAME='smp.automation.authentication.external.tls.clientCert.enabled';"
         },
         executionPhase = BEFORE_TEST_METHOD)
-@Ignore
-public class ServiceGroupControllerTest {
+public class ResourceControllerTest {
 
     private static final String IDENTIFIER_SCHEME = "ehealth-participantid-qns";
     private static final String PARTICIPANT_ID = "urn:poland:ncpb";
@@ -166,7 +165,7 @@ public class ServiceGroupControllerTest {
     public void existingServiceMetadataCanBeRetrievedByEverybody() throws Exception {
 
         String xmlSG = getSampleServiceGroupBody(IDENTIFIER_SCHEME, PARTICIPANT_ID);
-        String xmlMD = generateServiceMetadata(PARTICIPANT_ID, IDENTIFIER_SCHEME, DOCUMENT_ID, IDENTIFIER_SCHEME, "test");
+        String xmlMD = generateServiceMetadata(PARTICIPANT_ID, IDENTIFIER_SCHEME, DOCUMENT_ID, DOCUMENT_SCHEME, "test");
         // crate service group
         mvc.perform(put(URL_PATH)
                 .with(ADMIN_CREDENTIALS)
@@ -421,7 +420,7 @@ public class ServiceGroupControllerTest {
 
     public void prepareForGet() throws Exception {
         String xmlSG = getSampleServiceGroupBody(IDENTIFIER_SCHEME, PARTICIPANT_ID);
-        String xmlMD = generateServiceMetadata(PARTICIPANT_ID, IDENTIFIER_SCHEME, DOCUMENT_ID, IDENTIFIER_SCHEME, "test");
+        String xmlMD = generateServiceMetadata(PARTICIPANT_ID, IDENTIFIER_SCHEME, DOCUMENT_ID, DOCUMENT_SCHEME, "test");
         // crate service group
         mvc.perform(put(URL_PATH)
                 .header(HTTP_HEADER_KEY_DOMAIN, HTTP_DOMAIN_VALUE)
diff --git a/smp-webapp/src/test/resources/logback-test.xml b/smp-webapp/src/test/resources/logback-test.xml
index 5ea50260f..646509a14 100644
--- a/smp-webapp/src/test/resources/logback-test.xml
+++ b/smp-webapp/src/test/resources/logback-test.xml
@@ -35,10 +35,10 @@
         </encoder>
     </appender>
 
-    <logger name="eu.europa.ec.edelivery" level="DEBUG" />
+    <logger name="eu.europa.ec" level="DEBUG" />
     <logger name="org.springframework.security.cas" level="DEBUG" />
     <root level="WARN">
         <appender-ref ref="file"/>
         <appender-ref ref="stdout"/>
     </root>
-</configuration>
\ No newline at end of file
+</configuration>
diff --git a/smp-webapp/src/test/resources/webapp_integration_test_data.sql b/smp-webapp/src/test/resources/webapp_integration_test_data.sql
index cb98cb6b1..2bb475c36 100644
--- a/smp-webapp/src/test/resources/webapp_integration_test_data.sql
+++ b/smp-webapp/src/test/resources/webapp_integration_test_data.sql
@@ -71,6 +71,12 @@ insert into SMP_CREDENTIAL (ID, FK_USER_ID, CREDENTIAL_ACTIVE, CREDENTIAL_NAME,
 (17, 7, 1, 'CN=EHEALTH_SMP_TEST_BRAZIL,O=European Commission,C=BE:48b681ee8e0dcc08', null,'CERTIFICATE','REST_API' , NOW(),  NOW());
 insert into SMP_CERTIFICATE (ID, CERTIFICATE_ID, VALID_FROM, VALID_TO, CREATED_ON, LAST_UPDATED_ON) values
 (17, 'CN=EHEALTH_SMP_TEST_BRAZIL,O=European Commission,C=BE:48b681ee8e0dcc08', null,null, NOW(),  NOW());
+insert into SMP_CREDENTIAL (ID, FK_USER_ID, CREDENTIAL_ACTIVE, CREDENTIAL_NAME, CREDENTIAL_VALUE, CREDENTIAL_TYPE, CREDENTIAL_TARGET, CREATED_ON, LAST_UPDATED_ON) values
+(18, 7, 1, 'CN=utf-8_z_SMP,O=EC,C=BE:0000000000000666', null,'CERTIFICATE','REST_API' , NOW(),  NOW());
+insert into SMP_CERTIFICATE (ID, CERTIFICATE_ID, VALID_FROM, VALID_TO, CREATED_ON, LAST_UPDATED_ON) values
+(18, 'CN=utf-8_z_SMP,O=EC,C=BE:0000000000000666', null,null, NOW(),  NOW());
+
+
 
 -- insert into SMP_USER(ID, USERNAME, ROLE, ACTIVE, CREATED_ON, LAST_UPDATED_ON) values (8, 'Cert3', 'SMP_ADMIN', 1, NOW(),  NOW());
 -- insert into SMP_CERTIFICATE (ID, CERTIFICATE_ID, VALID_FROM, VALID_TO, CREATED_ON, LAST_UPDATED_ON) values (8, 'CN=utf-8_ż_SMP,O=EC,C=BE:0000000000000666', null,null, NOW(),  NOW());
-- 
GitLab