package cz.scholz.aliaskeymanager;

import javax.net.ssl.SSLEngine;
import javax.net.ssl.X509ExtendedKeyManager;
import java.net.Socket;
import java.security.Principal;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;

/**
 * Created by schojak on 30.5.16.
 */
public class AliasKeyManager extends X509ExtendedKeyManager {
    X509ExtendedKeyManager original;
    String alias;

    public AliasKeyManager(String alias, X509ExtendedKeyManager original)
    {
        if (alias == null) {
            throw new IllegalArgumentException("The alias cannot be null.");
        }

        if (original == null) {
            throw new IllegalArgumentException("The original key manager cannot be null.");
        }

        this.alias = alias;
        this.original = original;
    }

    public String[] getClientAliases(String s, Principal[] principals) {
        return new String[] { alias };
    }

    public String chooseClientAlias(String[] strings, Principal[] principals, Socket socket) {
        return alias;
    }

    public String[] getServerAliases(String s, Principal[] principals) {
        return original.getServerAliases(s, principals);
    }

    public String chooseServerAlias(String s, Principal[] principals, Socket socket) {
        return original.chooseServerAlias(s, principals, socket);
    }

    public X509Certificate[] getCertificateChain(String s) {
        return original.getCertificateChain(s);
    }

    public PrivateKey getPrivateKey(String s) {
        return original.getPrivateKey(s);
    }

    public String chooseEngineClientAlias(String[] keyType, Principal[] issuers, SSLEngine engine) {
        return alias;
    }

    public String chooseEngineServerAlias(String keyType, Principal[] issuers, SSLEngine engine) {
        return original.chooseEngineServerAlias(keyType, issuers, engine);
    }
}

@@ -250,37 +250,133 @@
 %0A * 
-Created by schojak on 30.5.16
+AliasKeyManager extends the X509ExtendedKeyManager with a wrapper which allows to always select predefined client certificate
 .%0A *
@@ -495,16 +495,398 @@
 alias;%0A%0A
+    /**%0A     * Creates an instance of AliasKeyManager based on instance of X509EntendedKeyManager.%0A     *%0A     * @param alias The key alias which should be used for cleint authentication%0A     * @param original The original KeyManager which should be wrapped. All functionality unrelated to the client key%0A     *                 selection will be taked from this KeyManager.%0A     */%0A
     publ
@@ -1286,383 +1286,968 @@
     
-public String%5B%5D getClientAliases(String s, Principal%5B%5D principals) %7B%0A        return new String%5B%5D %7B alias %7D;%0A    %7D%0A%0A    public String chooseClientAlias(String%5B%5D strings, Principal%5B%5D principals, Socket socket) %7B%0A        return alias;%0A    %7D%0A%0A    public String%5B%5D getServerAliases(String s, Principal%5B%5D principals) %7B%0A        return original.getServerAliases(s, principals);%0A    %7D%0A
+/**%0A     * Returns always the selected key alias.%0A     *%0A     * @param s%0A     * @param principals%0A     * @return Key alias as a array%0A     */%0A    public String%5B%5D getClientAliases(String s, Principal%5B%5D principals) %7B%0A        return new String%5B%5D %7B alias %7D;%0A    %7D%0A%0A    /**%0A     * Returns always the selected key alias.%0A     *%0A     * @param s%0A     * @param principals%0A     * @param socket%0A     * @return Key alias as a String%0A     */%0A    public String chooseClientAlias(String%5B%5D strings, Principal%5B%5D principals, Socket socket) %7B%0A        return alias;%0A    %7D%0A%0A    /**%0A     * Falls back to the original KeyManager.%0A     *%0A     * @param s%0A     * @param principals%0A     * @return%0A     */%0A    public String%5B%5D getServerAliases(String s, Principal%5B%5D principals) %7B%0A        return original.getServerAliases(s, principals);%0A    %7D%0A%0A    /**%0A     * Falls back to the original KeyManager.%0A     *%0A     * @param s%0A     * @param principals%0A     * @param socket%0A     * @return%0A     */
 %0A   
@@ -2395,32 +2395,132 @@
 socket);%0A    %7D%0A%0A
+    /**%0A     * Falls back to the original KeyManager.%0A     *%0A     * @param s%0A     * @return%0A     */%0A
     public X509C
@@ -2724,133 +2724,450 @@
     
-public String chooseEngineClientAlias(String%5B%5D keyType, Principal%5B%5D issuers, SSLEngine engine) %7B%0A        return alias;%0A    %7D%0A
+/**%0A     * Returns always the selected key alias.%0A     *%0A     * @param s%0A     * @param principals%0A     * @param socket%0A     * @return Key alias as a String%0A     */%0A    public String chooseEngineClientAlias(String%5B%5D keyType, Principal%5B%5D issuers, SSLEngine engine) %7B%0A        return alias;%0A    %7D%0A%0A    /**%0A     * Falls back to the original KeyManager.%0A     *%0A     * @param keyType%0A     * @param issuers%0A     * @param engine%0A     * @return%0A     */
 %0A   

Add Javadoc