From 68eb241bef83485c44c9245e3ff4d048efb4e927 Mon Sep 17 00:00:00 2001
From: Pawel GUTOWSKI <Pawel.GUTOWSKI@ext.ec.europa.eu>
Date: Wed, 28 Feb 2018 16:19:00 +0100
Subject: [PATCH] EDELIVERY-3184 Added URL-encoding for ServiceMetadata
 references

---
 .../eu/europa/ec/smp/api/Identifiers.java     | 21 +++++++++++++++++++
 .../eu/europa/ec/smp/api/IdentifiersTest.java | 18 ++++++++++++++++
 .../ServiceMetadataPathBuilder.java           |  3 ++-
 3 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/smp-api/src/main/java/eu/europa/ec/smp/api/Identifiers.java b/smp-api/src/main/java/eu/europa/ec/smp/api/Identifiers.java
index 42cf810fd..e8104baf4 100644
--- a/smp-api/src/main/java/eu/europa/ec/smp/api/Identifiers.java
+++ b/smp-api/src/main/java/eu/europa/ec/smp/api/Identifiers.java
@@ -18,9 +18,15 @@ import org.oasis_open.docs.bdxr.ns.smp._2016._05.DocumentIdentifier;
 import org.oasis_open.docs.bdxr.ns.smp._2016._05.ParticipantIdentifierType;
 import org.oasis_open.docs.bdxr.ns.smp._2016._05.ProcessIdentifier;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import static java.nio.charset.StandardCharsets.UTF_8;
+
 /**
  * Factory and utility methods for API classes generated from OASIS XSD.
  *
@@ -56,6 +62,21 @@ public class Identifiers {
         return String.format("%s::%s", docId.getScheme(), docId.getValue());
     }
 
+    public static String asUrlEncodedString(ParticipantIdentifierType participantId){
+        try {
+            return URLEncoder.encode(asString(participantId), UTF_8.name());
+        } catch (UnsupportedEncodingException e) {
+            throw new IllegalStateException(e);
+        }
+    }
+
+    public static String asUrlEncodedString(DocumentIdentifier docId){
+        try {
+            return URLEncoder.encode(asString(docId), UTF_8.name());
+        } catch (UnsupportedEncodingException e) {
+            throw new IllegalStateException(e);
+        }
+    }
 
     private static String extract(String doubleColonDelimitedId, String groupName) {
         try {
diff --git a/smp-api/src/test/java/eu/europa/ec/smp/api/IdentifiersTest.java b/smp-api/src/test/java/eu/europa/ec/smp/api/IdentifiersTest.java
index 8d639f045..64a37e8dd 100644
--- a/smp-api/src/test/java/eu/europa/ec/smp/api/IdentifiersTest.java
+++ b/smp-api/src/test/java/eu/europa/ec/smp/api/IdentifiersTest.java
@@ -165,4 +165,22 @@ public class IdentifiersTest {
         assertEquals(MALFORMED_INPUT_MSG + negativeInput, e.getMessage());
     }
 
+    @Test
+    public void testUrlEncodingParticipantId(){
+        //given
+        ParticipantIdentifierType participantId = new ParticipantIdentifierType("0088:conformance:sg01#", "ehealth:actorid:qns");
+
+        //when-then
+        assertEquals("ehealth%3Aactorid%3Aqns%3A%3A0088%3Aconformance%3Asg01%23", Identifiers.asUrlEncodedString(participantId));
+    }
+
+    @Test
+    public void testUrlEncodingDocumentId(){
+        //given
+        DocumentIdentifier docId = new DocumentIdentifier("urn::ehealth##services:extended:epsos01::101", "busdox:docid:qns");
+
+        //when-then
+        assertEquals("busdox%3Adocid%3Aqns%3A%3Aurn%3A%3Aehealth%23%23services%3Aextended%3Aepsos01%3A%3A101", Identifiers.asUrlEncodedString(docId));
+    }
+
 }
diff --git a/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/controllers/ServiceMetadataPathBuilder.java b/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/controllers/ServiceMetadataPathBuilder.java
index 0c692555c..f3e84c855 100644
--- a/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/controllers/ServiceMetadataPathBuilder.java
+++ b/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/controllers/ServiceMetadataPathBuilder.java
@@ -28,6 +28,7 @@ import javax.servlet.http.HttpServletRequest;
 import java.net.URI;
 
 import static eu.europa.ec.smp.api.Identifiers.asString;
+import static eu.europa.ec.smp.api.Identifiers.asUrlEncodedString;
 
 /**
  * Created by gutowpa on 13/07/2017.
@@ -47,7 +48,7 @@ public class ServiceMetadataPathBuilder {
         String path = ServletUriComponentsBuilder.fromCurrentRequestUri()
                 .replacePath(getUrlContext())
                 .path("/{participantId}/services/{docId}")
-                .buildAndExpand(asString(participantId), asString(docId))
+                .buildAndExpand(asUrlEncodedString(participantId), asUrlEncodedString(docId))
                 .toUriString();
 
         return path;
-- 
GitLab