Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit 03e28e0e authored by Joze RIHTARSIC's avatar Joze RIHTARSIC
Browse files

Merge pull request #167 in EDELIVERY/smp from...

Merge pull request #167 in EDELIVERY/smp from bugfix/EDELIVERY-6583-problem-with-workaround-for-the-grp-space-test-in-the-metadata-url to bugfix/EDELIVERY-6343-set-csrf-tokens-for-the-smp-console

* commit 'ed3eebf70ecbaace7433f96bd5b26d530b4102c7':
  Fix path segment '%' encoding
parents 93dedb34 5be3c787
No related branches found
No related tags found
No related merge requests found
...@@ -34,6 +34,10 @@ ...@@ -34,6 +34,10 @@
<groupId>org.apache.cxf.xjc-utils</groupId> <groupId>org.apache.cxf.xjc-utils</groupId>
<artifactId>cxf-xjc-runtime</artifactId> <artifactId>cxf-xjc-runtime</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency> <dependency>
<groupId>pl.pragmatists</groupId> <groupId>pl.pragmatists</groupId>
<artifactId>JUnitParams</artifactId> <artifactId>JUnitParams</artifactId>
......
...@@ -18,11 +18,7 @@ import org.apache.commons.lang3.StringUtils; ...@@ -18,11 +18,7 @@ import org.apache.commons.lang3.StringUtils;
import org.oasis_open.docs.bdxr.ns.smp._2016._05.DocumentIdentifier; 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.ParticipantIdentifierType;
import org.oasis_open.docs.bdxr.ns.smp._2016._05.ProcessIdentifier; import org.oasis_open.docs.bdxr.ns.smp._2016._05.ProcessIdentifier;
import org.springframework.web.util.UriUtils;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
...@@ -34,12 +30,11 @@ import static java.nio.charset.StandardCharsets.UTF_8; ...@@ -34,12 +30,11 @@ import static java.nio.charset.StandardCharsets.UTF_8;
public class Identifiers { public class Identifiers {
public static final String EBCORE_IDENTIFIER_PREFIX = "urn:oasis:names:tc:ebcore:partyid-type:"; public static final String EBCORE_IDENTIFIER_PREFIX = "urn:oasis:names:tc:ebcore:partyid-type:";
public static final String EBCORE_IDENTIFIER_FORMAT="%s:%s"; public static final String EBCORE_IDENTIFIER_FORMAT = "%s:%s";
public static final String EBCORE_IDENTIFIER_ISO6523_SCHEME="iso6523"; public static final String EBCORE_IDENTIFIER_ISO6523_SCHEME = "iso6523";
public static final String DOUBLE_COLON_IDENTIFIER_FORMAT="%s::%s"; public static final String DOUBLE_COLON_IDENTIFIER_FORMAT = "%s::%s";
private static final String EMPTY_IDENTIFIER="Null/Empty";
private static final String EMPTY_IDENTIFIER = "Null/Empty";
public static ParticipantIdentifierType asParticipantId(String participantIDentifier) { public static ParticipantIdentifierType asParticipantId(String participantIDentifier) {
...@@ -58,20 +53,20 @@ public class Identifiers { ...@@ -58,20 +53,20 @@ public class Identifiers {
} }
public static String asString(ParticipantIdentifierType participantId) { public static String asString(ParticipantIdentifierType participantId) {
if(StringUtils.isBlank(participantId.getScheme())) { if (StringUtils.isBlank(participantId.getScheme())) {
// if scheme is empty just return value (for OASIS SMP 1.0 must start with :: ) // if scheme is empty just return value (for OASIS SMP 1.0 must start with :: )
return (StringUtils.startsWithIgnoreCase(participantId.getScheme(), EBCORE_IDENTIFIER_PREFIX)? return (StringUtils.startsWithIgnoreCase(participantId.getScheme(), EBCORE_IDENTIFIER_PREFIX) ?
"":"::") + participantId.getValue(); "" : "::") + participantId.getValue();
} }
String format = String format =
StringUtils.startsWithIgnoreCase(participantId.getScheme(), EBCORE_IDENTIFIER_PREFIX)? StringUtils.startsWithIgnoreCase(participantId.getScheme(), EBCORE_IDENTIFIER_PREFIX) ?
EBCORE_IDENTIFIER_FORMAT:DOUBLE_COLON_IDENTIFIER_FORMAT; EBCORE_IDENTIFIER_FORMAT : DOUBLE_COLON_IDENTIFIER_FORMAT;
return String.format(format, participantId.getScheme(), participantId.getValue()); return String.format(format, participantId.getScheme(), participantId.getValue());
} }
public static String asString(DocumentIdentifier docId) { public static String asString(DocumentIdentifier docId) {
return String.format(DOUBLE_COLON_IDENTIFIER_FORMAT, docId.getScheme()!=null?docId.getScheme():"", docId.getValue()); return String.format(DOUBLE_COLON_IDENTIFIER_FORMAT, docId.getScheme() != null ? docId.getScheme() : "", docId.getValue());
} }
public static String asUrlEncodedString(ParticipantIdentifierType participantId) { public static String asUrlEncodedString(ParticipantIdentifierType participantId) {
...@@ -83,20 +78,17 @@ public class Identifiers { ...@@ -83,20 +78,17 @@ public class Identifiers {
} }
private static String urlEncode(String s) { private static String urlEncode(String s) {
try { return UriUtils.encode(s, UTF_8.name());
return URLEncoder.encode(s, UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
} }
private static String[] splitParticipantIdentifier(String participantIdentifier) { private static String[] splitParticipantIdentifier(String participantIdentifier) {
String[] idResult; String[] idResult;
if (StringUtils.isBlank(participantIdentifier)){ if (StringUtils.isBlank(participantIdentifier)) {
throw new MalformedIdentifierException(EMPTY_IDENTIFIER, null); throw new MalformedIdentifierException(EMPTY_IDENTIFIER, null);
} }
String identifier = participantIdentifier.trim(); String identifier = participantIdentifier.trim();
if(identifier.startsWith(EBCORE_IDENTIFIER_PREFIX) if (identifier.startsWith(EBCORE_IDENTIFIER_PREFIX)
|| identifier.startsWith("::" + EBCORE_IDENTIFIER_PREFIX)) { || identifier.startsWith("::" + EBCORE_IDENTIFIER_PREFIX)) {
idResult = splitEbCoreIdentifier(identifier); idResult = splitEbCoreIdentifier(identifier);
} else { } else {
...@@ -110,25 +102,26 @@ public class Identifiers { ...@@ -110,25 +102,26 @@ public class Identifiers {
/** /**
* Method splits identifier at first occurrence of double colon :: and returns array size of 2. The first value is * Method splits identifier at first occurrence of double colon :: and returns array size of 2. The first value is
* schema and the second is identifier. If identifier is blank or with missing :: MalformedIdentifierException is thrown * schema and the second is identifier. If identifier is blank or with missing :: MalformedIdentifierException is thrown
*
* @param doubleColonDelimitedId * @param doubleColonDelimitedId
* @return array with two elements. First is schema and second is id * @return array with two elements. First is schema and second is id
*/ */
private static String[] splitDoubleColonIdentifier(String doubleColonDelimitedId) { private static String[] splitDoubleColonIdentifier(String doubleColonDelimitedId) {
if (StringUtils.isBlank(doubleColonDelimitedId)){ if (StringUtils.isBlank(doubleColonDelimitedId)) {
throw new MalformedIdentifierException(EMPTY_IDENTIFIER, null); throw new MalformedIdentifierException(EMPTY_IDENTIFIER, null);
} }
String[] idResult = new String[2]; String[] idResult = new String[2];
int delimiterIndex = doubleColonDelimitedId.indexOf("::"); int delimiterIndex = doubleColonDelimitedId.indexOf("::");
if (delimiterIndex<0){ if (delimiterIndex < 0) {
throw new MalformedIdentifierException(doubleColonDelimitedId, null); throw new MalformedIdentifierException(doubleColonDelimitedId, null);
} }
idResult[0] = delimiterIndex==0?null:doubleColonDelimitedId.substring(0,delimiterIndex); idResult[0] = delimiterIndex == 0 ? null : doubleColonDelimitedId.substring(0, delimiterIndex);
idResult[1] = doubleColonDelimitedId.substring(delimiterIndex+2); idResult[1] = doubleColonDelimitedId.substring(delimiterIndex + 2);
if (StringUtils.isBlank(idResult[1])){ if (StringUtils.isBlank(idResult[1])) {
throw new MalformedIdentifierException(doubleColonDelimitedId, null); throw new MalformedIdentifierException(doubleColonDelimitedId, null);
} }
...@@ -136,26 +129,26 @@ public class Identifiers { ...@@ -136,26 +129,26 @@ public class Identifiers {
} }
public static String[] splitEbCoreIdentifier(final String partyId) { public static String[] splitEbCoreIdentifier(final String partyId) {
String partyIdPrivate = partyId.trim(); String partyIdPrivate = partyId.trim();
if (partyIdPrivate.startsWith("::")) { if (partyIdPrivate.startsWith("::")) {
partyIdPrivate = StringUtils.removeStart(partyIdPrivate, "::"); partyIdPrivate = StringUtils.removeStart(partyIdPrivate, "::");
} }
if (!partyIdPrivate.startsWith(EBCORE_IDENTIFIER_PREFIX)){ if (!partyIdPrivate.startsWith(EBCORE_IDENTIFIER_PREFIX)) {
throw new MalformedIdentifierException(partyId, null); throw new MalformedIdentifierException(partyId, null);
} }
boolean isIso6523 = partyIdPrivate.startsWith(EBCORE_IDENTIFIER_PREFIX+EBCORE_IDENTIFIER_ISO6523_SCHEME +":"); boolean isIso6523 = partyIdPrivate.startsWith(EBCORE_IDENTIFIER_PREFIX + EBCORE_IDENTIFIER_ISO6523_SCHEME + ":");
int isSchemeDelimiter = partyIdPrivate.indexOf(':',EBCORE_IDENTIFIER_PREFIX.length()); int isSchemeDelimiter = partyIdPrivate.indexOf(':', EBCORE_IDENTIFIER_PREFIX.length());
if (isSchemeDelimiter < 0){ if (isSchemeDelimiter < 0) {
// invalid scheme // invalid scheme
throw new IllegalArgumentException(String.format("Invalid ebCore id [%s] ebcoreId must have prefix 'urn:oasis:names:tc:ebcore:partyid-type', "+ throw new IllegalArgumentException(String.format("Invalid ebCore id [%s] ebcoreId must have prefix 'urn:oasis:names:tc:ebcore:partyid-type', " +
"and parts <catalog-identifier>, <scheme-in-catalog>, <scheme-specific-identifier> separated by colon. " + "and parts <catalog-identifier>, <scheme-in-catalog>, <scheme-specific-identifier> separated by colon. " +
"Example: urn:oasis:names:tc:ebcore:partyid-type:<catalog-identifier>:(<scheme-in-catalog>)?:<scheme-specific-identifier>.", partyIdPrivate)); "Example: urn:oasis:names:tc:ebcore:partyid-type:<catalog-identifier>:(<scheme-in-catalog>)?:<scheme-specific-identifier>.", partyIdPrivate));
} }
int isPartDelimiter = partyIdPrivate.indexOf(':',isSchemeDelimiter+1); int isPartDelimiter = partyIdPrivate.indexOf(':', isSchemeDelimiter + 1);
String[] result = new String[2]; String[] result = new String[2];
if (isPartDelimiter < 0 && isIso6523) { // for iso scheme-in-catalog is mandatory if (isPartDelimiter < 0 && isIso6523) { // for iso scheme-in-catalog is mandatory
...@@ -163,10 +156,10 @@ public class Identifiers { ...@@ -163,10 +156,10 @@ public class Identifiers {
throw new IllegalArgumentException(String.format("Invalid ebCore id [%s] ebcoreId must have prefix 'urn:oasis:names:tc:ebcore:partyid-type', " + throw new IllegalArgumentException(String.format("Invalid ebCore id [%s] ebcoreId must have prefix 'urn:oasis:names:tc:ebcore:partyid-type', " +
"and parts <catalog-identifier>, <scheme-in-catalog>, <scheme-specific-identifier> separated by colon. " + "and parts <catalog-identifier>, <scheme-in-catalog>, <scheme-specific-identifier> separated by colon. " +
"Example: urn:oasis:names:tc:ebcore:partyid-type:<catalog-identifier>:(<scheme-in-catalog>)?:<scheme-specific-identifier>.", partyIdPrivate)); "Example: urn:oasis:names:tc:ebcore:partyid-type:<catalog-identifier>:(<scheme-in-catalog>)?:<scheme-specific-identifier>.", partyIdPrivate));
} else if (isPartDelimiter < 0){ } else if (isPartDelimiter < 0) {
result[0] = partyIdPrivate.substring(0, isSchemeDelimiter).trim(); result[0] = partyIdPrivate.substring(0, isSchemeDelimiter).trim();
result[1] = partyIdPrivate.substring(isSchemeDelimiter + 1).trim(); result[1] = partyIdPrivate.substring(isSchemeDelimiter + 1).trim();
}else { } else {
result[0] = partyIdPrivate.substring(0, isPartDelimiter).trim(); result[0] = partyIdPrivate.substring(0, isPartDelimiter).trim();
result[1] = partyIdPrivate.substring(isPartDelimiter + 1).trim(); result[1] = partyIdPrivate.substring(isPartDelimiter + 1).trim();
} }
...@@ -177,11 +170,8 @@ public class Identifiers { ...@@ -177,11 +170,8 @@ public class Identifiers {
} }
//check if double colon was used for identifier separator in ebecoreid //check if double colon was used for identifier separator in ebecoreid
if (result[0].endsWith(":")) { if (result[0].endsWith(":")) {
result[0] = StringUtils.removeEnd(result[0] , ":"); result[0] = StringUtils.removeEnd(result[0], ":");
} }
return result; return result;
} }
} }
...@@ -174,7 +174,6 @@ public class IdentifiersTest { ...@@ -174,7 +174,6 @@ public class IdentifiersTest {
return res; return res;
} }
@Test @Test
@Parameters(method = "participantIdentifierPositiveCases") @Parameters(method = "participantIdentifierPositiveCases")
@TestCaseName("{0}") @TestCaseName("{0}")
...@@ -248,7 +247,6 @@ public class IdentifiersTest { ...@@ -248,7 +247,6 @@ public class IdentifiersTest {
assertEquals(value, processId.getValue()); assertEquals(value, processId.getValue());
} }
@Test @Test
@Parameters(method = "negativeCases") @Parameters(method = "negativeCases")
public void testProcessIdNegative(String negativeInput) { public void testProcessIdNegative(String negativeInput) {
...@@ -277,7 +275,6 @@ public class IdentifiersTest { ...@@ -277,7 +275,6 @@ public class IdentifiersTest {
fail(); fail();
} }
private void negativeAssertions(String negativeInput, Exception e) { private void negativeAssertions(String negativeInput, Exception e) {
assertTrue(e instanceof MalformedIdentifierException); assertTrue(e instanceof MalformedIdentifierException);
assertEquals(MALFORMED_INPUT_MSG + (StringUtils.isBlank(negativeInput) ? "Null/Empty" : negativeInput), e.getMessage()); assertEquals(MALFORMED_INPUT_MSG + (StringUtils.isBlank(negativeInput) ? "Null/Empty" : negativeInput), e.getMessage());
...@@ -292,6 +289,16 @@ public class IdentifiersTest { ...@@ -292,6 +289,16 @@ public class IdentifiersTest {
assertEquals("ehealth%3Aactorid%3Aqns%3A%3A0088%3Aconformance%3Asg01%23", Identifiers.asUrlEncodedString(participantId)); assertEquals("ehealth%3Aactorid%3Aqns%3A%3A0088%3Aconformance%3Asg01%23", Identifiers.asUrlEncodedString(participantId));
} }
@Test
public void testUrlEncodingParticipantIdWithSpace() {
//given
ParticipantIdentifierType participantId = new ParticipantIdentifierType("GPR: 0088:conformance:sg01#", "ehealth:actorid:qns");
//when-then
//Because this is path segment spaces must be percent encoded (not with +)!
assertEquals("ehealth%3Aactorid%3Aqns%3A%3AGPR%3A%200088%3Aconformance%3Asg01%23", Identifiers.asUrlEncodedString(participantId));
}
@Test @Test
public void testUrlEncodingDocumentId() { public void testUrlEncodingDocumentId() {
//given //given
...@@ -301,5 +308,14 @@ public class IdentifiersTest { ...@@ -301,5 +308,14 @@ public class IdentifiersTest {
assertEquals("busdox%3Adocid%3Aqns%3A%3Aurn%3A%3Aehealth%23%23services%3Aextended%3Aepsos01%3A%3A101", Identifiers.asUrlEncodedString(docId)); assertEquals("busdox%3Adocid%3Aqns%3A%3Aurn%3A%3Aehealth%23%23services%3Aextended%3Aepsos01%3A%3A101", Identifiers.asUrlEncodedString(docId));
} }
@Test
public void testUrlEncodingDocumentIdWithSpace() {
//given
DocumentIdentifier docId = new DocumentIdentifier("urn::ehealth##services:extended:epsos01:: 101", "busdox:docid:qns");
//when-then
//Because this is path segment spaces must be percent encoded (not with +)!
assertEquals("busdox%3Adocid%3Aqns%3A%3Aurn%3A%3Aehealth%23%23services%3Aextended%3Aepsos01%3A%3A%20101", Identifiers.asUrlEncodedString(docId));
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment