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

Skip to content
Snippets Groups Projects
Commit 55612da4 authored by Pawel GUTOWSKI's avatar Pawel GUTOWSKI
Browse files

Merge pull request #81 in EDELIVERY/smp from EDELIVERY-3049_UTF8_extension_bug to development

* commit '2109190822eb5de8cb9bf852d740ef03edb2ca15':
  EDELIVERY-3049 EDELIVERY-2304 Fixed UTF-8 issue in ServiceGroup Extension element, romoved util package since all classes were already refactored in a meanwhile
parents dd6c9f86 cb3925da
No related branches found
No related tags found
No related merge requests found
Showing
with 76 additions and 61 deletions
/*
* Copyright 2017 European Commission | CEF eDelivery
* Copyright 2018 European Commission | CEF eDelivery
*
* Licensed under the EUPL, Version 1.2 or as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence");
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European Commission - subsequent 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 attached in file: LICENCE-EUPL-v1.2.pdf
......@@ -11,11 +11,13 @@
* See the Licence for the specific language governing permissions and limitations under the Licence.
*/
package eu.europa.ec.cipa.smp.server.security;
package eu.europa.ec.edelivery.smp;
import org.springframework.security.crypto.bcrypt.BCrypt;
/**
* Utilitiy class that can be used from commandline by SMP administrators if they want to calculate BCrypt hash.
*
* Created by gutowpa on 22/02/2017.
*/
public class BCryptPasswordHash {
......
/*
* Copyright 2017 European Commission | CEF eDelivery
* Copyright 2018 European Commission | CEF eDelivery
*
* Licensed under the EUPL, Version 1.2 or as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence");
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European Commission - subsequent 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 attached in file: LICENCE-EUPL-v1.2.pdf
......@@ -11,16 +11,13 @@
* See the Licence for the specific language governing permissions and limitations under the Licence.
*/
package eu.europa.ec.cipa.smp.server.util;
package eu.europa.ec.edelivery.smp.conversion;
import org.apache.cxf.staxutils.PrettyPrintXMLStreamWriter;
import org.oasis_open.docs.bdxr.ns.smp._2016._05.ExtensionType;
import org.springframework.util.StreamUtils;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.*;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.namespace.QName;
......@@ -31,15 +28,16 @@ import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.List;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* Created by migueti on 13/02/2017.
*/
public class ExtensionUtils {
public class ExtensionConverter {
private static final String WRAPPED_FORMAT = "<ExtensionsWrapper xmlns=\"http://docs.oasis-open.org/bdxr/ns/SMP/2016/05\">%s</ExtensionsWrapper>";
......@@ -51,24 +49,24 @@ public class ExtensionUtils {
private static final QName EXT_TYPE_QNAME = new QName("http://docs.oasis-open.org/bdxr/ns/SMP/2016/05", "Extension");
public static String marshalExtensions(List<ExtensionType> extensions) throws JAXBException, XMLStreamException {
protected static String marshalExtensions(List<ExtensionType> extensions) throws JAXBException, XMLStreamException, UnsupportedEncodingException {
if (extensions == null) {
return null;
}
StringBuilder stringBuilder = new StringBuilder();
for (ExtensionType aExtension : extensions) {
stringBuilder.append(ExtensionUtils.marshalExtension(aExtension));
stringBuilder.append(ExtensionConverter.marshalExtension(aExtension));
}
return stringBuilder.toString();
}
private static String marshalExtension(ExtensionType extension) throws JAXBException, XMLStreamException {
if(extension == null) {
private static String marshalExtension(ExtensionType extension) throws JAXBException, XMLStreamException, UnsupportedEncodingException {
if (extension == null) {
return null;
}
JAXBContext jaxbContext = JAXBContext.newInstance(ExtensionType.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
JAXBElement aJaxbElement = new JAXBElement(EXT_TYPE_QNAME, ExtensionType.class, extension);
JAXBElement jaxbElement = new JAXBElement(EXT_TYPE_QNAME, ExtensionType.class, extension);
jaxbMarshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLOutputFactory xof = XMLOutputFactory.newFactory();
......@@ -77,19 +75,19 @@ public class ExtensionUtils {
try {
xmlStreamWriter = xof.createXMLStreamWriter(baos);
xsw = new PrettyPrintXMLStreamWriter(xmlStreamWriter, 4);
jaxbMarshaller.marshal(aJaxbElement, xsw);
jaxbMarshaller.marshal(jaxbElement, xsw);
} finally {
if(xmlStreamWriter != null) {
if (xmlStreamWriter != null) {
xmlStreamWriter.close();
}
if (xsw != null) {
xsw.close();
}
}
return baos.toString();
return baos.toString(UTF_8.name());
}
public static List<ExtensionType> unmarshalExtensions(String xml) throws JAXBException {
protected static List<ExtensionType> unmarshalExtensions(String xml) throws JAXBException {
String wrappedExtensionsStr = String.format(WRAPPED_FORMAT, xml);
InputStream inStream = new ByteArrayInputStream(wrappedExtensionsStr.getBytes(UTF_8));
JAXBContext jaxbContext = JAXBContext.newInstance(ExtensionsWrapper.class);
......
......@@ -16,7 +16,6 @@ package eu.europa.ec.edelivery.smp.conversion;
import eu.europa.ec.edelivery.smp.data.model.DBServiceGroupId;
import eu.europa.ec.edelivery.smp.exceptions.ConversionException;
import eu.europa.ec.edelivery.smp.exceptions.XmlParsingException;
import eu.europa.ec.cipa.smp.server.util.ExtensionUtils;
import eu.europa.ec.edelivery.smp.data.model.DBServiceGroup;
import org.oasis_open.docs.bdxr.ns.smp._2016._05.*;
import org.w3c.dom.Document;
......@@ -32,6 +31,7 @@ import javax.xml.stream.XMLStreamException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
......@@ -72,7 +72,7 @@ public class ServiceGroupConverter {
ParticipantIdentifierType identifier = new ParticipantIdentifierType(dbServiceGroup.getId().getBusinessIdentifier(), dbServiceGroup.getId().getBusinessIdentifierScheme());
serviceGroup.setParticipantIdentifier(identifier);
try {
List<ExtensionType> extensions = ExtensionUtils.unmarshalExtensions(dbServiceGroup.getExtension());
List<ExtensionType> extensions = ExtensionConverter.unmarshalExtensions(dbServiceGroup.getExtension());
serviceGroup.getExtensions().addAll(extensions);
} catch (JAXBException e) {
throw new ConversionException(e);
......@@ -95,8 +95,8 @@ public class ServiceGroupConverter {
public static String extractExtensionsPayload(ServiceGroup serviceGroup) {
try {
return ExtensionUtils.marshalExtensions(serviceGroup.getExtensions());
} catch (JAXBException | XMLStreamException e) {
return ExtensionConverter.marshalExtensions(serviceGroup.getExtensions());
} catch (JAXBException | XMLStreamException | UnsupportedEncodingException e) {
throw new ConversionException(e);
}
}
......
/*
* Copyright 2017 European Commission | CEF eDelivery
* Copyright 2018 European Commission | CEF eDelivery
*
* Licensed under the EUPL, Version 1.2 or as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence");
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European Commission - subsequent 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 attached in file: LICENCE-EUPL-v1.2.pdf
......@@ -11,8 +11,9 @@
* See the Licence for the specific language governing permissions and limitations under the Licence.
*/
package eu.europa.ec.cipa.smp.server.security;
package eu.europa.ec.edelivery.smp;
import eu.europa.ec.edelivery.smp.BCryptPasswordHash;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
......
/*
* Copyright 2017 European Commission | CEF eDelivery
* Copyright 2018 European Commission | CEF eDelivery
*
* Licensed under the EUPL, Version 1.2 or as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence");
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European Commission - subsequent 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 attached in file: LICENCE-EUPL-v1.2.pdf
......@@ -11,8 +11,9 @@
* See the Licence for the specific language governing permissions and limitations under the Licence.
*/
package eu.europa.ec.cipa.smp.server.util;
package eu.europa.ec.edelivery.smp.conversion;
import eu.europa.ec.edelivery.smp.testutil.XmlTestUtils;
import org.junit.Assert;
import org.junit.Test;
import org.oasis_open.docs.bdxr.ns.smp._2016._05.ExtensionType;
......@@ -22,21 +23,26 @@ import org.xmlunit.matchers.CompareMatcher;
import javax.xml.bind.JAXBException;
import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
/**
* Created by migueti on 13/02/2017.
*/
public class ExtensionUtilsTest {
public class ExtensionConverterTest {
private static final String WRAPPED_FORMAT = "<ExtensionsWrapper xmlns=\"http://docs.oasis-open.org/bdxr/ns/SMP/2016/05\">%s</ExtensionsWrapper>";
private static final String RES_PATH = "/eu/europa/ec/cipa/smp/server/util/";
private static final String UTF8_SEQUENCE = "ẞßÄäËëÏïÖöÜüẄẅŸÿЁёЇїӜӝ-Zażółć gęślą jaźń-ÆæØøÅå-ÀÆÇßãÿαΩƒ";
@Test
public void testMarshalOneExtension() throws JAXBException, XMLStreamException, IOException, SAXException {
// given
......@@ -44,7 +50,7 @@ public class ExtensionUtilsTest {
String inputDoc = XmlTestUtils.loadDocumentAsString(RES_PATH + "extensionMarshal.xml");
// when
String xmlResult = ExtensionUtils.marshalExtensions(list);
String xmlResult = ExtensionConverter.marshalExtensions(list);
// then
assertThat(xmlResult, CompareMatcher.isIdenticalTo(inputDoc));
......@@ -57,7 +63,7 @@ public class ExtensionUtilsTest {
String inputDoc = XmlTestUtils.loadDocumentAsString(RES_PATH + "extensionMarshalMore.xml");
// when
String xmlResult = ExtensionUtils.marshalExtensions(list);
String xmlResult = ExtensionConverter.marshalExtensions(list);
// then
String wrappedXmlResult = String.format(WRAPPED_FORMAT, xmlResult);
......@@ -65,13 +71,28 @@ public class ExtensionUtilsTest {
assertThat(wrappedXmlResult, CompareMatcher.isIdenticalTo(wrappedInputDoc));
}
@Test
public void testUtf8Handling() throws JAXBException, XMLStreamException, UnsupportedEncodingException {
// given
ExtensionType extension = new ExtensionType();
extension.setExtensionName(UTF8_SEQUENCE);
List<ExtensionType> extensions = Arrays.asList(extension);
//when
String extensionsXml = ExtensionConverter.marshalExtensions(extensions);
List<ExtensionType> resultExtensions = ExtensionConverter.unmarshalExtensions(extensionsXml);
//then
assertEquals(UTF8_SEQUENCE, resultExtensions.get(0).getExtensionName());
}
@Test
public void testUnmarshal() throws IOException, JAXBException {
// given
String inputDoc = XmlTestUtils.loadDocumentAsString(RES_PATH + "extensionMarshal.xml");
// when
List<ExtensionType> extensions = ExtensionUtils.unmarshalExtensions(inputDoc);
List<ExtensionType> extensions = ExtensionConverter.unmarshalExtensions(inputDoc);
// then
checkExtensions(extensions, 1);
......@@ -83,7 +104,7 @@ public class ExtensionUtilsTest {
String inputDoc = XmlTestUtils.loadDocumentAsString(RES_PATH + "extensionMarshalMore.xml");
// when
List<ExtensionType> extensions = ExtensionUtils.unmarshalExtensions(inputDoc);
List<ExtensionType> extensions = ExtensionConverter.unmarshalExtensions(inputDoc);
// then
checkExtensions(extensions, 2);
......
......@@ -14,7 +14,7 @@
package eu.europa.ec.edelivery.smp.conversion;
import eu.europa.ec.edelivery.smp.exceptions.XmlParsingException;
import eu.europa.ec.cipa.smp.server.util.XmlTestUtils;
import eu.europa.ec.edelivery.smp.testutil.XmlTestUtils;
import org.junit.Test;
import org.oasis_open.docs.bdxr.ns.smp._2016._05.ServiceGroup;
import org.xml.sax.SAXParseException;
......
......@@ -14,7 +14,7 @@
package eu.europa.ec.edelivery.smp.conversion;
import eu.europa.ec.edelivery.smp.exceptions.XmlParsingException;
import eu.europa.ec.cipa.smp.server.util.XmlTestUtils;
import eu.europa.ec.edelivery.smp.testutil.XmlTestUtils;
import org.junit.Test;
import org.oasis_open.docs.bdxr.ns.smp._2016._05.RedirectType;
import org.oasis_open.docs.bdxr.ns.smp._2016._05.ServiceEndpointList;
......
......@@ -41,8 +41,8 @@ import java.util.List;
import static eu.europa.ec.edelivery.smp.conversion.ServiceGroupConverter.toDbModel;
import static eu.europa.ec.edelivery.smp.conversion.ServiceGroupConverter.unmarshal;
import static eu.europa.ec.cipa.smp.server.util.XmlTestUtils.loadDocumentAsString;
import static eu.europa.ec.cipa.smp.server.util.XmlTestUtils.marshall;
import static eu.europa.ec.edelivery.smp.testutil.XmlTestUtils.loadDocumentAsString;
import static eu.europa.ec.edelivery.smp.testutil.XmlTestUtils.marshall;
import static eu.europa.ec.smp.api.Identifiers.asParticipantId;
import static org.junit.Assert.*;
......
......@@ -40,8 +40,8 @@ import java.io.IOException;
import java.util.List;
import static eu.europa.ec.edelivery.smp.conversion.ServiceMetadataConverter.unmarshal;
import static eu.europa.ec.cipa.smp.server.util.XmlTestUtils.loadDocumentAsString;
import static eu.europa.ec.cipa.smp.server.util.XmlTestUtils.marshall;
import static eu.europa.ec.edelivery.smp.testutil.XmlTestUtils.loadDocumentAsString;
import static eu.europa.ec.edelivery.smp.testutil.XmlTestUtils.marshall;
import static eu.europa.ec.smp.api.Identifiers.asDocumentId;
import static eu.europa.ec.smp.api.Identifiers.asParticipantId;
import static org.junit.Assert.*;
......
......@@ -13,22 +13,17 @@
package eu.europa.ec.edelivery.smp.services;
import eu.europa.ec.cipa.smp.server.security.SignatureUtil;
import eu.europa.ec.edelivery.smp.config.PropertiesTestConfig;
import eu.europa.ec.edelivery.smp.testutil.SignatureUtil;
import eu.europa.ec.edelivery.smp.config.SmpServicesTestConfig;
import eu.europa.ec.edelivery.smp.services.ServiceMetadataSigner;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import static eu.europa.ec.cipa.smp.server.util.XmlTestUtils.loadDocument;
import static eu.europa.ec.edelivery.smp.testutil.XmlTestUtils.loadDocument;
/**
* Created by rodrfla on 20/02/2017.
......
/*
* Copyright 2017 European Commission | CEF eDelivery
* Copyright 2018 European Commission | CEF eDelivery
*
* Licensed under the EUPL, Version 1.2 or as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence");
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European Commission - subsequent 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 attached in file: LICENCE-EUPL-v1.2.pdf
......@@ -11,9 +11,8 @@
* See the Licence for the specific language governing permissions and limitations under the Licence.
*/
package eu.europa.ec.cipa.smp.server.security;
package eu.europa.ec.edelivery.smp.testutil;
import eu.europa.ec.cipa.smp.server.util.XmlTestUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
......
/*
* Copyright 2017 European Commission | CEF eDelivery
* Copyright 2018 European Commission | CEF eDelivery
*
* Licensed under the EUPL, Version 1.2 or as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence");
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European Commission - subsequent 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 attached in file: LICENCE-EUPL-v1.2.pdf
......@@ -11,7 +11,7 @@
* See the Licence for the specific language governing permissions and limitations under the Licence.
*/
package eu.europa.ec.cipa.smp.server.security;
package eu.europa.ec.edelivery.smp.testutil;
import javax.xml.crypto.*;
import javax.xml.crypto.dsig.SignatureMethod;
......
/*
* Copyright 2017 European Commission | CEF eDelivery
* Copyright 2018 European Commission | CEF eDelivery
*
* Licensed under the EUPL, Version 1.2 or as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence");
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European Commission - subsequent 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 attached in file: LICENCE-EUPL-v1.2.pdf
......@@ -11,9 +11,8 @@
* See the Licence for the specific language governing permissions and limitations under the Licence.
*/
package eu.europa.ec.cipa.smp.server.util;
package eu.europa.ec.edelivery.smp.testutil;
import eu.europa.ec.cipa.smp.server.security.SignatureUtil;
import org.apache.commons.io.IOUtils;
import org.oasis_open.docs.bdxr.ns.smp._2016._05.ServiceGroup;
import org.oasis_open.docs.bdxr.ns.smp._2016._05.ServiceMetadata;
......
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