Java Server Socket Create createServerSocket(int port, int backlog, String host, SSLServerSocketFactory f)

Here you can find the source of createServerSocket(int port, int backlog, String host, SSLServerSocketFactory f)

Description

create Server Socket

License

Open Source License

Declaration

public static SSLServerSocket createServerSocket(int port, int backlog, String host, SSLServerSocketFactory f)
            throws IOException, UnknownHostException 

Method Source Code


//package com.java2s;
import java.io.*;
import java.net.*;

import java.util.*;
import java.util.regex.*;

public class Main {
    private static String[] enabledCipherSuites = null;

    public static SSLServerSocket createServerSocket(int port, int backlog, String host, SSLServerSocketFactory f)
            throws IOException, UnknownHostException {
        ServerSocket ss = null;//from  w  ww .  j av a  2  s .c  om

        if (f != null) {
            ss = f.createServerSocket(port, backlog, InetAddress.getByName(host));
        } else {
            return null;
        }

        String oldCipherOrder[] = ((SSLServerSocket) ss).getSupportedCipherSuites();
        //String oldCipherOrder[] = ((SSLServerSocket)ss).getEnabledCipherSuites();
        ((SSLServerSocket) ss).setEnabledCipherSuites(getEnabledCipherSuites(oldCipherOrder));

        return (SSLServerSocket) ss;
    }

    private static String[] getEnabledCipherSuites(String[] oldCipherOrder) {
        if (enabledCipherSuites == null) {
            enabledCipherSuites = getSupportedCiphers(oldCipherOrder);
        }

        return enabledCipherSuites;
    }

    private static String[] getSupportedCiphers(String ciphers[]) {
        Pattern pattern256AES = Pattern.compile("AES.256");
        Pattern pattern128AES = Pattern.compile("AES.128");
        Pattern pattern3DES = Pattern.compile("3DES");

        Matcher matcher;
        boolean matchFound;

        int len = ciphers.length;

        ArrayList list256AES = new ArrayList();
        ArrayList list128AES = new ArrayList();
        ArrayList list3DES = new ArrayList();
        ArrayList listOthers = new ArrayList();

        for (int i = 0; i < len; i++) {
            // we won't accept anything using md5 (FIPS compliancy)
            if (ciphers[i].toLowerCase().endsWith("md5"))
                continue;

            // determine if pattern exists in input
            matcher = pattern256AES.matcher(ciphers[i]);
            matchFound = matcher.find();

            if (matchFound) {
                list256AES.add(ciphers[i]);
            } else {
                matcher = pattern128AES.matcher(ciphers[i]);
                matchFound = matcher.find();

                if (matchFound) {
                    list128AES.add(ciphers[i]);
                } else {
                    matcher = pattern3DES.matcher(ciphers[i]);
                    matchFound = matcher.find();

                    if (matchFound) {
                        list3DES.add(ciphers[i]);
                    } else {
                        //listOthers.add( ciphers[i] );
                    }
                }
            }
        }

        String set[] = new String[list256AES.size() + list128AES.size() + list3DES.size() + listOthers.size()];

        int j = 0;

        for (int k = 0; k < list256AES.size(); k++) {
            set[j++] = (String) list256AES.get(k);
        }

        for (int l = 0; l < list128AES.size(); l++) {
            set[j++] = (String) list128AES.get(l);
        }

        for (int m = 0; m < list3DES.size(); m++) {
            set[j++] = (String) list3DES.get(m);
        }

        for (int n = 0; n < listOthers.size(); n++) {
            set[j++] = (String) listOthers.get(n);
        }

        return set;
    }
}

Related

  1. createServerSocket()
  2. createServerSocket(int port)
  3. createServerSocket(int start_port)