From 21032c4e8d2e8b996e43a4dc562a2214970c235c Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Sat, 21 Mar 2020 16:34:56 +0100 Subject: [PATCH] fixed ORC certificate loading for DH / non-ORC certificates --- .../orc/TestORCPublicCertificateDatabase.java | 4 +- .../orc/ORCPublicCertificateDatabase.java | 63 +++++++++++++++---- .../ORCPublicCertificateDatabaseImpl.java | 28 ++++++--- .../sap/sse/replication/impl/Activator.java | 2 +- 4 files changed, 74 insertions(+), 23 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index 4290490fe5f..ea13dabacd8 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -165,8 +165,8 @@ public class TestORCPublicCertificateDatabase { " extracted from certificates "+certificates, certificateHandle.isPresent()); final String referenceNumber = certificateHandle.get().getReferenceNumber(); final CertificateHandle handle = db.getCertificateHandle(referenceNumber); - final ORCCertificate result = db.getCertificate(referenceNumber); - assertNotNull("Unable to load certificate for reference number "+referenceNumber+" from handle "+certificateHandle); + final ORCCertificate result = db.getCertificate(referenceNumber, handle.getFamily()); + assertNotNull("Unable to load certificate for reference number "+referenceNumber+" from handle "+certificateHandle, result); assertEquals(handle.getGPH(), result.getGPH().asSeconds(), 0.00001); // Use some tolerance as we found differences as much as 5s between the dxtDate in the handle coming from the XML search result // and the IssueDate field in the JSON. Both suggest to report millisecond accuracy, but dxtDate always seems to have the diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java index cc77e5fde0a..39ed892c3d2 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java @@ -3,6 +3,8 @@ package com.sap.sailing.domain.orc; import java.time.format.DateTimeParseException; import java.util.Arrays; import java.util.Date; +import java.util.HashMap; +import java.util.Map; import java.util.Set; import java.util.UUID; import java.util.concurrent.Future; @@ -95,6 +97,37 @@ import com.sap.sse.common.Util; public interface ORCPublicCertificateDatabase { ORCPublicCertificateDatabase INSTANCE = new ORCPublicCertificateDatabaseImpl(); + public enum CertificateFamily { + UNKNOWN(0, ""), ORC(1, "ORC"), SUPER_YACHT(2, "SY"), DOUBLE_HANDED(3, "DH"), MULTI_HULL(4, "Mu"); + + private final int familyId; + private final String familyQueryParamValue; + private final static Map familyById; + + static { + familyById = new HashMap<>(); + for (final CertificateFamily family : values()) { + familyById.put(family.getFamilyId(), family); + } + } + + private CertificateFamily(int familyId, String familyQueryParamValue) { + this.familyId = familyId; + this.familyQueryParamValue = familyQueryParamValue; + } + + public int getFamilyId() { + return familyId; + } + + public String getFamilyQueryParamValue() { + return familyQueryParamValue; + } + + public static CertificateFamily fromId(int familyId) { + return familyById.get(familyId); + } + } /** * Equality and hash code of such handles is based on the {@link #getReferenceNumber() reference number} only. * @@ -108,6 +141,7 @@ public interface ORCPublicCertificateDatabase { String getSSSID(); UUID getDatInGID(); String getReferenceNumber(); + CertificateFamily getFamily(); String getYachtName(); String getSailNumber(); String getBoatClassName(); @@ -140,15 +174,7 @@ public interface ORCPublicCertificateDatabase { Iterable search(CountryCode country, Integer yearOfIssuance, String referenceNumber, String yachtName, String sailNumber, String boatClassName, boolean includeInvalid) throws Exception; - default Iterable getCertificates(Iterable handles) throws Exception { - return Util.map(handles, handle->{ - try { - return getCertificate(handle.getReferenceNumber()); - } catch (Exception e) { - throw new RuntimeException(e); - } - }); - } + Iterable getCertificates(Iterable handles) throws Exception; default Iterable getCertificates(CertificateHandle... handles) throws Exception { return getCertificates(Arrays.asList(handles)); @@ -160,10 +186,23 @@ public interface ORCPublicCertificateDatabase { ORCCertificate searchForUpdate(ORCCertificate certificate) throws Exception; - ORCCertificate searchForUpdate(CertificateHandle certificate) throws Exception; - - ORCCertificate getCertificate(String referenceNumber) throws Exception; + ORCCertificate searchForUpdate(CertificateHandle certificateHandle) throws Exception; + /** + * Searches for a default {@link CertificateFamily#ORC} certificate by the given {@code referenceNumber}. + * Note that this may not find certificates of other families. + */ + default ORCCertificate getCertificate(String referenceNumber) throws Exception { + return getCertificate(referenceNumber, CertificateFamily.ORC); + } + + ORCCertificate getCertificate(String referenceNumber, CertificateFamily family) throws Exception; + + default ORCCertificate getCertificate(CertificateHandle certificateHandle) throws Exception { + final Iterable certificates = getCertificates(certificateHandle); + return certificates.iterator().hasNext() ? certificates.iterator().next() : null; + } + /** * Creates a lookup job that is forked into the background. The job will first try an exact lookup * with the parameters as specified. If one or more results are obtained this way, they are returned. If no diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java index 7a39f5e315f..e476a4b4c2a 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java @@ -109,6 +109,7 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat private final Double gph; private final UUID datInGID; private final String referenceNumber; + private final CertificateFamily family; private final String yachtName; private final String sailNumber; private final String boatClassName; @@ -122,9 +123,9 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat private final Boolean isValid; public CertificateHandleImpl(CountryCode issuingCountry, String fileId, Double gph, String sssid, - UUID datInGID, String referenceNumber, String yachtName, String sailNumber, String boatClassName, - String designer, String builder, Integer yearBuilt, TimePoint issueDate, Integer certType, - Boolean oneDesign, Boolean isProvisional, Boolean isValid) { + UUID datInGID, String referenceNumber, CertificateFamily family, String yachtName, String sailNumber, + String boatClassName, String designer, String builder, Integer yearBuilt, TimePoint issueDate, + Integer certType, Boolean oneDesign, Boolean isProvisional, Boolean isValid) { super(); this.issuingCountry = issuingCountry; this.fileId = fileId; @@ -132,6 +133,7 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat this.sssid = sssid; this.datInGID = datInGID; this.referenceNumber = referenceNumber; + this.family = family; this.yachtName = yachtName; this.sailNumber = sailNumber; this.boatClassName = boatClassName; @@ -200,6 +202,11 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat return referenceNumber; } + @Override + public CertificateFamily getFamily() { + return family; + } + @Override public String getYachtName() { return yachtName; @@ -330,6 +337,7 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat Double gph = null; UUID datInGID = null; String referenceNumber = null; + CertificateFamily family = CertificateFamily.UNKNOWN; String yachtName = null; String sailNumber = null; String boatClassName = null; @@ -359,6 +367,9 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat case "RefNo": referenceNumber = child.getTextContent().trim(); break; + case "Family": + family = child.getTextContent().trim().isEmpty() ? CertificateFamily.UNKNOWN : CertificateFamily.fromId(Integer.valueOf(child.getTextContent().trim())); + break; case "YachtName": yachtName = child.getTextContent(); break; @@ -400,8 +411,9 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat break; } } - return new CertificateHandleImpl(issuingCountry, fileId, gph, sssid, datInGID, referenceNumber, - yachtName, sailNumber, boatClassName, designer, builder, yearBuilt, issueDate, certType, isOneDesign, isProvisional, isValid); + return new CertificateHandleImpl(issuingCountry, fileId, gph, sssid, datInGID, referenceNumber, family, + yachtName, sailNumber, boatClassName, designer, builder, yearBuilt, issueDate, certType, isOneDesign, + isProvisional, isValid); } @Override @@ -471,8 +483,8 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat } @Override - public ORCCertificate getCertificate(String referenceNumber) throws Exception { - final String queryParameters = "ext=json&RefNo="+referenceNumber; + public ORCCertificate getCertificate(String referenceNumber, CertificateFamily family) throws Exception { + final String queryParameters = "ext=json&RefNo="+referenceNumber+"&family="+family.getFamilyQueryParamValue(); logger.fine("Obtaining certificate for reference number "+referenceNumber); final ORCCertificate result = getSingleCertificate(queryParameters); if (result == null) { @@ -501,7 +513,7 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat final Map> futures = new HashMap<>(); for (final CertificateHandle handle : handles) { if (!handle.getReferenceNumber().trim().isEmpty()) { - final FutureTask task = new FutureTask(()->getCertificate(handle.getReferenceNumber())); + final FutureTask task = new FutureTask(()->getCertificate(handle.getReferenceNumber(), handle.getFamily())); final Thread backgroundExecutor = new Thread(task, "ORC certificate background download thread for "+handle.getReferenceNumber()); backgroundExecutor.setDaemon(true); backgroundExecutor.start(); diff --git a/java/com.sap.sse.replication/src/com/sap/sse/replication/impl/Activator.java b/java/com.sap.sse.replication/src/com/sap/sse/replication/impl/Activator.java index ccdf4e0238b..9782f82699e 100755 --- a/java/com.sap.sse.replication/src/com/sap/sse/replication/impl/Activator.java +++ b/java/com.sap.sse.replication/src/com/sap/sse/replication/impl/Activator.java @@ -175,7 +175,7 @@ public class Activator implements BundleActivator { " before firing up replication automatically..."); final List> replicables = new ArrayList<>(); for (String replicableIdAsString : replicableIdsAsStrings) { - logger.info("Trying to obtain Replicable " + replicableIdAsString+", waiting if necessary"); + logger.info("Trying to obtain Replicable " + replicableIdAsString+", waiting if necessary"); Replicable replicable = replicablesProvider.getReplicable(replicableIdAsString, /* wait */true); logger.info("Obtained Replicable " + replicableIdAsString); replicables.add(replicable);