- if (switchCertificate != null) {
- SwitchCertificateBuilder switchCertificateBuilder = new SwitchCertificateBuilder();
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'-00:00'");
- formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
- try {
- Map<String, String> subjectMap = new ConcurrentHashMap<>();
- Map<String, String> issuerMap = new ConcurrentHashMap<>();
- subjectMap.putAll(new LdapName(switchCertificate.getSubjectX500Principal().getName())
- .getRdns().stream()
- .collect(Collectors.toMap(rdn -> rdn.getType(), rdn -> rdn.getValue().toString())));
- issuerMap.putAll(new LdapName(switchCertificate.getIssuerX500Principal().getName())
- .getRdns().stream()
- .collect(Collectors.toMap(rdn -> rdn.getType(), rdn -> rdn.getValue().toString())));
- SubjectBuilder subjectBuilder = new SubjectBuilder();
- subjectBuilder.setCommonName(subjectMap.get("CN"));
- subjectBuilder.setCountry(subjectMap.get("C"));
- subjectBuilder.setLocality(subjectMap.get("L"));
- subjectBuilder.setOrganization(subjectMap.get("O"));
- subjectBuilder.setOrganizationUnit(subjectMap.get("OU"));
- subjectBuilder.setState(subjectMap.get("ST"));
- IssuerBuilder issuerBuilder = new IssuerBuilder();
- issuerBuilder.setCommonName(issuerMap.get("CN"));
- issuerBuilder.setCountry(issuerMap.get("C"));
- issuerBuilder.setLocality(issuerMap.get("L"));
- issuerBuilder.setOrganization(issuerMap.get("O"));
- issuerBuilder.setOrganizationUnit(issuerMap.get("OU"));
- issuerBuilder.setState(issuerMap.get("ST"));
- switchCertificateBuilder.setSubject(subjectBuilder.build());
- switchCertificateBuilder.setIssuer(issuerBuilder.build());
- } catch (InvalidNameException e) {
- LOG.error("Exception ", e);
- }
- switchCertificateBuilder.setSerialNumber(switchCertificate.getSerialNumber().toString());
- try {
- if (switchCertificate.getSubjectAlternativeNames() != null) {
- List<String> subjectAlternateNames = new ArrayList<>();
- switchCertificate.getSubjectAlternativeNames().forEach(generalName -> {
- final Object value = generalName.get(1);
- if (value instanceof String) {
- subjectAlternateNames.add((String) value);
- }
- });
- switchCertificateBuilder.setSubjectAlternateNames(subjectAlternateNames);
- } else {
- switchCertificateBuilder.setSubjectAlternateNames(null);
- }
- } catch (CertificateParsingException e) {
- LOG.error("Error encountered while parsing certificate ", e);
- }
- switchCertificateBuilder.setValidFrom(new DateAndTime(formatter.format(switchCertificate.getNotBefore())));
- switchCertificateBuilder.setValidTo(new DateAndTime(formatter.format(switchCertificate.getNotAfter())));
- return switchCertificateBuilder.build();
+ if (switchCertificate == null) {
+ return null;
+ }
+
+ final var builder = new SwitchCertificateBuilder();
+ final var subjectMap = indexRds(switchCertificate.getSubjectX500Principal());
+ if (subjectMap != null) {
+ builder.setSubject(new SubjectBuilder()
+ .setCommonName(subjectMap.get("CN"))
+ .setCountry(subjectMap.get("C"))
+ .setLocality(subjectMap.get("L"))
+ .setOrganization(subjectMap.get("O"))
+ .setOrganizationUnit(subjectMap.get("OU"))
+ .setState(subjectMap.get("ST"))
+ .build());