midhttpssupportserver.java.svn-base

来自「cqME :java framework for TCK test.」· SVN-BASE 代码 · 共 246 行

SVN-BASE
246
字号
/* * $Id$ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.tck.midp.lib;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.net.InetAddress;import java.net.ServerSocket;import java.net.SocketException;import java.net.URL;import java.net.UnknownHostException;import java.security.KeyManagementException;import java.security.KeyStore;import java.security.KeyStoreException;import java.security.NoSuchAlgorithmException;import java.security.UnrecoverableKeyException;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import java.util.ResourceBundle;import javax.net.ServerSocketFactory;import javax.net.ssl.KeyManagerFactory;import javax.net.ssl.SSLContext;import javax.net.ssl.SSLServerSocket;import com.sun.cldc.communication.midp.BaseHttpServer;public class MIDHttpsSupportServer extends MIDHttpSupportServer {    private String protocolName = null;    private static X509Certificate cert = null;    private String alias = null;    private String storepass = null;    private String keypass = null;    private String certName = null;    public MIDHttpsSupportServer() {        done = true;        try {            host = InetAddress.getLocalHost().getHostName();        } catch (UnknownHostException uhe) {            host = "localhost";        }    }    public void init(String[] arg) {	// arg [protocolName, certName, alias, storepass, keypass]        String[] arg1;        protocolName = arg[arg.length-5];        certName = arg[arg.length-4];        alias = arg[arg.length-3];        storepass = arg[arg.length-2];        keypass = arg[arg.length-1];        arg1 =  new String[0];        if (arg.length > 5) {            arg1 = new String[arg.length-5];            System.arraycopy(arg, 0, arg1, 0, arg.length-5);        }        runners = new RequestHandler[handlerCount];        for (int i = 0; i < handlerCount; i++) {            runners[i] = new RequestHandler();            runners[i].setName("HttpsSupportRequestHandler_"+i);        }        super.init(arg1);        for (int i = 0; i < handlerCount; i++) {            ((BaseHttpServer)runners[i]).setParams(verbose, host, port, retry);        }    }    public void start() {        if (!done) throw new IllegalThreadStateException(i18n.getString("error.httpsState"));        try {            socket = getServerSocket();            socket.setSoTimeout(10000);        } catch (SocketException se) {            throw new RuntimeException(i18n.getString("error.httpsProtocol"));        }        done = false;        for (int i = 0; i < handlerCount; i++) {            ((BaseHttpServer)runners[i]).setSocket(socket);            runners[i].start();        }    }    private ServerSocket getServerSocket() {        char[] keyStorePW = storepass.toCharArray();        char[] keyPW = keypass.toCharArray();        SSLContext sslc = null;        KeyManagerFactory kmf = null;        KeyStore keyStore = null;        SSLServerSocket serverSocket = null;        try {            keyStore = KeyStore.getInstance("JKS");            InputStream certStream = null;            try {                certStream = new FileInputStream(new File(certName));                keyStore.load(certStream, keyStorePW);            } finally {                if (certStream != null) {                    try {                        certStream.close();                    } catch (IOException e) {                        assert false : "MIDHttpsSupportServer: "                            + "IOE during certStream.close(): " + e;                    }                }            }        } catch (KeyStoreException kse) {        } catch (IOException ioe) {            throw new RuntimeException(i18n.getString("error.httpsCertNotFound"));        } catch (NoSuchAlgorithmException nae) {            throw new RuntimeException(i18n.getString("error.httpsNoAlgorithm1"));        } catch (CertificateException ce) {            throw new RuntimeException(i18n.getString("error.httpsCertLoad") + certName);        }        String[] protocolsAllowed = null;        try {            kmf = KeyManagerFactory.getInstance("SunX509");            kmf.init(keyStore, keyPW);            cert = (X509Certificate)keyStore.getCertificate(alias);            if (cert == null) {                throw new RuntimeException(i18n.getString("error.httpsCertAlias") + alias);            }            if (protocolName.equals("SSL")) {                protocolsAllowed = new String[] {"SSLv3", "SSLv2Hello"};                sslc = SSLContext.getInstance("SSLv3");            } else if (protocolName.equals("TLS") || protocolName.equals("WTLS")) {                protocolsAllowed = new String [] {"TLSv1", "SSLv2Hello"};                sslc = SSLContext.getInstance("TLSv1");            } else {                throw new IllegalArgumentException(                        i18n.getString("error.invalidProtocol")                        + protocolName);            }        } catch (NoSuchAlgorithmException nae) {            throw new RuntimeException(i18n.getString("error.httpsNoAlgorithm2"));        } catch (UnrecoverableKeyException uke) {            throw new RuntimeException(i18n.getString("error.privateKey") + keypass + "'");        } catch (KeyStoreException kse) {            throw new RuntimeException(i18n.getString("error.keyFactory"));        }        try {            sslc.init(kmf.getKeyManagers(), null, null);            ServerSocketFactory ssf = sslc.getServerSocketFactory();            serverSocket = (SSLServerSocket) ssf.createServerSocket(port);            serverSocket.setEnabledProtocols(protocolsAllowed);        } catch (KeyManagementException kme) {            throw new RuntimeException(i18n.getString("error.sslContext"));        } catch (IOException ioe) {            throw new RuntimeException(i18n.getString("error.socket") + port);        }        return serverSocket;    }    class RequestHandler extends MIDHttpSupportServer.RequestHandler {        public RequestHandler() {            readMore = true;        }        void sendData(DataOutputStream out, String s) throws IOException {            out.writeBytes("HTTP/1.1 200 OK\r\n");            out.writeBytes("Content-Length: "+s.length()+"\r\n");            out.writeBytes("Content-Type: text/html\r\n\r\n");            out.writeUTF(s);            System.out.println("String = "+s);            out.flush();        }        protected void handleGet(URL request, DataInputStream in) throws IOException {            // reset readMore to true            readMore = true;            String s = null;            String path = request.getFile();            int ind = path.indexOf("Certificate");            if (ind != -1) {                setResponseProperty("getversion", Integer.toString(cert.getVersion()));                setResponseProperty("getissuer", cert.getIssuerDN().toString());                setResponseProperty("getnotafter", Long.toString(cert.getNotAfter().getTime()));                setResponseProperty("getnotbefore", Long.toString(cert.getNotBefore().getTime()));                setResponseProperty("getserialnumber", cert.getSerialNumber().toString(16).toUpperCase());                setResponseProperty("getsigalgname", cert.getSigAlgName());                setResponseProperty("getsubject", cert.getSubjectDN().toString());                setResponseProperty("Content-Type", "application/octet-stream");                setResponseProperty("Content-Length", "0");                sendDiagnostics(HTTP_OK, phrase(HTTP_OK));                    return;            }            super.handleGet(request, in);        }    }    private static ResourceBundle i18n =         ResourceBundle.getBundle("com.sun.tck.midp.lib.i18n");}            

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?