package at.gv.egiz.moazs.msg; import at.gv.egiz.moazs.util.FileUtils; import at.gv.egiz.moazs.util.SSLContextCreator; import at.gv.zustellung.app2mzs.xsd.ClientType; import at.gv.zustellung.app2mzs.xsd.KeyStoreType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.net.ssl.SSLContext; import static at.gv.zustellung.app2mzs.xsd.KeyStoreType.keyStoreTypeBuilder; @Component public class MsgClientFactory { private final StoreSOAPBodyBinaryInRepositoryInterceptor storeResponseInterceptor; private final SSLContextCreator sslContextCreator; private final FileUtils fileUtils; @Autowired public MsgClientFactory(StoreSOAPBodyBinaryInRepositoryInterceptor storeResponseInterceptor, SSLContextCreator creator, FileUtils fileUtils) { this.storeResponseInterceptor = storeResponseInterceptor; this.sslContextCreator = creator; this.fileUtils = fileUtils; } /** * Creates a client that communicates with a msg service. * * @param params for the client, such as service url and ssl parameters. * @return the msg client */ //TODO evaluate and honor laxhostnameverification and trustall parameter! public MsgClient create(ClientType params) { SSLContext sslContext = null; if (params.getURL().startsWith("https")) { var keystore = resolveKeyStorePath(params.getSSL().getKeyStore()); var truststore = resolveKeyStorePath(params.getSSL().getTrustStore()); sslContext = sslContextCreator.createSSLContext(keystore, truststore); } return new MsgClient(storeResponseInterceptor, params.getURL(), sslContext); } private KeyStoreType resolveKeyStorePath(KeyStoreType store) { return store == null ? null : keyStoreTypeBuilder(store) .withFileName(fileUtils.determinePath(store.getFileName())) .build(); } }