Source:
public SimpleCertificateResponseMessageType importToKeystore(ImportToKeystoreRequestMessageType request) { String alias = request.getAlias(); if (StringUtils.isBlank(alias) || null == request.getServerCert()) { return buildSimpleResponse(false, "Alias and Server Certificate are required."); } PrivateKey privateKey = readPrivateKey(alias, getPasswordKeystore()); if (null == privateKey) { return buildSimpleResponse(false, "No privatekey recorded for alias: " + alias); } tempKeystore = null; if (!copyFile(FILE_JKS_GATEWAY, FILE_JKS_GATEWAY_NEW)) { return buildSimpleResponse(false, "Unable to copy keystore to temporary file."); } try { Certificate publicKey = CertificateUtil.createCertificate(request.getServerCert()); if (null == publicKey) { return buildSimpleResponse(false, "Server certificate was not upload."); } if (null != request.getRootCert() && CollectionUtils.isNotEmpty(request.getIntermediateList())) { Certificate oldCert = CertificateManagerImpl.getInstance().getCertificateBy(alias); List<ListCertificateType> oldChain = buildChain(oldCert, getTempKeystore()); for (ListCertificateType item : oldChain) { if (!alias.equalsIgnoreCase(item.getAlias())) { getTempKeystore().deleteEntry(item.getAlias()); } } Certificate caRoot = CertificateUtil.createCertificate(request.getRootCert()); getTempKeystore().setCertificateEntry(MessageFormat.format("{0}_root", alias), caRoot); int indexInt = 0; for (DataHandler cert : request.getIntermediateList()) { Certificate caIntermediate = CertificateUtil.createCertificate(cert); getTempKeystore().setCertificateEntry( MessageFormat.format("{0}_intermediate_{1}", alias, indexInt++), caIntermediate); } } getTempKeystore().setKeyEntry(alias, privateKey, getPasswordKeystore().toCharArray(), CoreHelpUtils.getCertificateChain(publicKey)); CoreHelpUtils.saveJksTo(getTempKeystore(), getPasswordKeystore(), FILE_JKS_GATEWAY_NEW); } catch (CertificateManagerException | UtilException | KeyStoreException | CertificateEncodingException e) { LOG.error("Error occurred importing to keystore: {}", e.getLocalizedMessage(), e); return buildSimpleResponse(false, "Error occurred importing to keystore."); } return buildSimpleResponse(true, "Import to Keystore is successful."); }

Target:
public SimpleCertificateResponseMessageType importToKeystore(ImportToKeystoreRequestMessageType request) { String alias = request.getAlias(); if (StringUtils.isBlank(alias) || null == request.getServerCert()) { return buildSimpleResponse(false, "Alias and Server Certificate are required."); } PrivateKey privateKey = readPrivateKey(alias, getPasswordKeystore()); if (null == privateKey) { return buildSimpleResponse(false, "No privatekey recorded for alias: " + alias); } tempKeystore = null; if (!copyFile(FILE_JKS_GATEWAY, FILE_JKS_GATEWAY_NEW)) { return buildSimpleResponse(false, "Unable to copy KeyStore to temporary file."); } try { Certificate publicKey = CertificateUtil.createCertificate(request.getServerCert()); if (null == publicKey) { return buildSimpleResponse(false, "Server certificate was not uploaded."); } if (null != request.getRootCert() && CollectionUtils.isNotEmpty(request.getIntermediateList())) { Certificate oldCert = CertificateManagerImpl.getInstance().getCertificateBy(alias); List<ListCertificateType> oldChain = buildChain(oldCert, getTempKeystore()); for (ListCertificateType item : oldChain) { if (!alias.equalsIgnoreCase(item.getAlias())) { getTempKeystore().deleteEntry(item.getAlias()); } } Certificate caRoot = CertificateUtil.createCertificate(request.getRootCert()); getTempKeystore().setCertificateEntry(MessageFormat.format("{0}_root", alias), caRoot); int indexInt = 0; for (DataHandler cert : request.getIntermediateList()) { Certificate caIntermediate = CertificateUtil.createCertificate(cert); getTempKeystore().setCertificateEntry( MessageFormat.format("{0}_intermediate_{1}", alias, indexInt++), caIntermediate); } } getTempKeystore().setKeyEntry(alias, privateKey, getPasswordKeystore().toCharArray(), CoreHelpUtils.getCertificateChain(publicKey)); CoreHelpUtils.saveJksTo(getTempKeystore(), getPasswordKeystore(), FILE_JKS_GATEWAY_NEW); } catch (CertificateManagerException | UtilException | KeyStoreException | CertificateEncodingException e) { LOG.error("Error occurred while importing to KeyStore: {}", e.getLocalizedMessage(), e); return buildSimpleResponse(false, "Error occurred while importing to KeyStore."); } return buildSimpleResponse(true, "Import to KeyStore is successful."); }