HandlerFactory { private static HandshakeMessageHandler getServerKeyExchangeHandler(TlsContext context) { CipherSuite cs = context.getChooser().getSelectedCipherSuite(); KeyExchangeAlgorithm algorithm = AlgorithmResolver.getKeyExchangeAlgorithm(cs); switch (algorithm) { case ECDHE_ECDSA: case ECDH_ECDSA: case ECDH_RSA: case ECDHE_RSA: case ECDH_ANON: return new ECDHEServerKeyExchangeHandler(context); case DHE_DSS: case DHE_RSA: case DH_ANON: case DH_DSS: case DH_RSA: return new DHEServerKeyExchangeHandler(context); case PSK: return new PskServerKeyExchangeHandler(context); case DHE_PSK: return new PskDheServerKeyExchangeHandler(context); case ECDHE_PSK: return new PskEcDheServerKeyExchangeHandler(context); case SRP_SHA_DSS: case SRP_SHA_RSA: case SRP_SHA: return new SrpServerKeyExchangeHandler(context); case ECCPWD: return new PWDServerKeyExchangeHandler(context); default: throw new UnsupportedOperationException("Algorithm " + algorithm + " NOT supported yet."); } } private  HandlerFactory(); static ProtocolMessageHandler getHandler(TlsContext context, ProtocolMessageType protocolType,
            HandshakeMessageType handshakeType); static HandshakeMessageHandler getHandshakeHandler(TlsContext context, HandshakeMessageType type); static ExtensionHandler getExtensionHandler(TlsContext context, ExtensionType type,
            HandshakeMessageType handshakeMessageType);  }
@Test public void getServerKeyExchangeHandler() { context.setSelectedCipherSuite(CipherSuite.TLS_ECDHE_RSA_WITH_NULL_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.SERVER_KEY_EXCHANGE) instanceof ECDHEServerKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_ECDH_RSA_WITH_NULL_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.SERVER_KEY_EXCHANGE) instanceof ECDHEServerKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_ECDH_ECDSA_WITH_NULL_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.SERVER_KEY_EXCHANGE) instanceof ECDHEServerKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_ECDHE_ECDSA_WITH_NULL_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.SERVER_KEY_EXCHANGE) instanceof ECDHEServerKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.SERVER_KEY_EXCHANGE) instanceof DHEServerKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.SERVER_KEY_EXCHANGE) instanceof DHEServerKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_DH_anon_WITH_AES_128_CBC_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.SERVER_KEY_EXCHANGE) instanceof DHEServerKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_DH_DSS_WITH_AES_128_CBC_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.SERVER_KEY_EXCHANGE) instanceof DHEServerKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_DH_RSA_WITH_AES_128_CBC_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.SERVER_KEY_EXCHANGE) instanceof DHEServerKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_DHE_PSK_WITH_NULL_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.SERVER_KEY_EXCHANGE) instanceof PskDheServerKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_ECDHE_PSK_WITH_NULL_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.SERVER_KEY_EXCHANGE) instanceof PskEcDheServerKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_PSK_WITH_NULL_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.SERVER_KEY_EXCHANGE) instanceof PskServerKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.SERVER_KEY_EXCHANGE) instanceof SrpServerKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.SERVER_KEY_EXCHANGE) instanceof SrpServerKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_SRP_SHA_WITH_AES_128_CBC_SHA); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.SERVER_KEY_EXCHANGE) instanceof SrpServerKeyExchangeHandler); context.setSelectedCipherSuite(CipherSuite.TLS_ECCPWD_WITH_AES_128_GCM_SHA256); assertTrue(HandlerFactory.getHandshakeHandler(context, HandshakeMessageType.SERVER_KEY_EXCHANGE) instanceof PWDServerKeyExchangeHandler); }