package de.factoryfx.jetty.ssl;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.KeyStore;

import org.eclipse.jetty.util.ssl.SslContextFactory;

import de.factoryfx.data.attribute.types.Base64Attribute;
import de.factoryfx.data.attribute.types.EnumAttribute;
import de.factoryfx.data.attribute.types.StringAttribute;
import de.factoryfx.factory.FactoryBase;
import de.factoryfx.factory.SimpleFactoryBase;

public class SslContextFactoryFactory<V,R extends FactoryBase<?,V,R>> extends SimpleFactoryBase<SslContextFactory,V, R> {
    public final Base64Attribute keyStore=new Base64Attribute().en("keyStore").de("keyStore");
    public final EnumAttribute<KeyStoreType> keyStoreType=new EnumAttribute<>(KeyStoreType.class);
    public final StringAttribute keyStorePassword = new StringAttribute().en("keyStorePassword").de("keyStorePassword");

    public final Base64Attribute trustStore = new Base64Attribute().en("trustStore").de("trustStore");
    public final EnumAttribute<KeyStoreType> trustStoreType=new EnumAttribute<>(KeyStoreType.class);
    public final StringAttribute trustStorePassword = new StringAttribute().en("trustStorePassword").de("trustStorePassword");

    public final StringAttribute certAlias = new StringAttribute().en("certAlias").de("certAlias").nullable();

    @Override
    public SslContextFactory createImpl() {
        SslContextFactory sslContextFactory = new SslContextFactory();

        sslContextFactory.setKeyStorePassword(keyStorePassword.get());
        try {
            KeyStore keyStore = KeyStore.getInstance(keyStoreType.get().value());
            try (InputStream inputStream = new ByteArrayInputStream(this.keyStore.getBytes())) {
                keyStore.load(inputStream, keyStorePassword.get().toCharArray());
                sslContextFactory.setKeyStore(keyStore);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        sslContextFactory.setKeyStorePassword(trustStorePassword.get());
        try {
            KeyStore keyStore = KeyStore.getInstance(trustStoreType.get().value());
            try (InputStream inputStream = new ByteArrayInputStream(this.trustStore.getBytes())) {
                keyStore.load(inputStream, trustStorePassword.get().toCharArray());
                sslContextFactory.setTrustStore(keyStore);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        sslContextFactory.setCertAlias(certAlias.get());

        return sslContextFactory;
    }

}

@@ -177,16 +177,79 @@
 ctory;%0A%0A
+import de.factoryfx.data.attribute.primitive.BooleanAttribute;%0A
 import d
@@ -290,32 +290,32 @@
 ase64Attribute;%0A
-
 import de.factor
@@ -541,16 +541,17 @@
 ctory%3CV,
+ 
 R extend
@@ -566,18 +566,20 @@
 yBase%3C?,
+ 
 V,
+ 
 R%3E%3E exte
@@ -618,16 +618,17 @@
 Factory,
+ 
 V, R%3E %7B%0A
@@ -668,17 +668,19 @@
 keyStore
-=
+ = 
 new Base
@@ -773,33 +773,35 @@
 pe%3E keyStoreType
-=
+ = 
 new EnumAttribut
@@ -937,32 +937,149 @@
 StorePassword%22);
+%0A    public final StringAttribute keyPassword = new StringAttribute().en(%22keyPassword%22).de(%22keyPassword%22).nullable();
 %0A%0A    public fin
@@ -1226,17 +1226,19 @@
 toreType
-=
+ = 
 new Enum
@@ -1506,24 +1506,299 @@
 ullable();%0A%0A
+    public final BooleanAttribute wantClientAuth = new BooleanAttribute().en(%22wantClientAuth%22).de(%22wantClientAuth%22).defaultValue(false);%0A    public final BooleanAttribute needClientAuth = new BooleanAttribute().en(%22needClientAuth%22).de(%22needClientAuth%22).defaultValue(false);%0A%0A
     @Overrid
@@ -1978,32 +1978,133 @@
 assword.get());%0A
+        if (!keyPassword.isEmpty()) %7B sslContextFactory.setKeyManagerPassword(keyPassword.get()); %7D%0A%0A
         try %7B%0A  
@@ -2518,16 +2518,149 @@
       %7D%0A
+        sslContextFactory.setNeedClientAuth(needClientAuth.get());%0A        sslContextFactory.setWantClientAuth(wantClientAuth.get());
 %0A       

add Attributes for SslContextFactory