Example usage for org.apache.http.nio.protocol EventListener EventListener

List of usage examples for org.apache.http.nio.protocol EventListener EventListener

Introduction

In this page you can find the example usage for org.apache.http.nio.protocol EventListener EventListener.

Prototype

EventListener

Source Link

Usage

From source file:org.frameworkset.spi.remote.http.HttpServer.java

private void startHttp() throws IOException {

    try {//w w w  .  ja v  a2s.  co  m
        this.ioReactor = new DefaultListeningIOReactor(workerCount, serverParams);

        EventListener serverEventListener = new EventListener() {

            // @Override
            public void connectionClosed(NHttpConnection conn) {
                // closedServerConns.decrement();
                // super.connectionClosed(conn);
                //               System.out.println("connectionClosed:" + conn);
            }

            public void connectionOpen(NHttpConnection conn) {
                //               System.out.println("connectionOpen:" + conn);

            }

            public void connectionTimeout(NHttpConnection conn) {
                System.out.println("connectionTimeout:" + conn);

            }

            public void fatalIOException(IOException ex, NHttpConnection conn) {
                //               System.out.println("fatalIOException:" + conn);
                ex.printStackTrace();
            }

            public void fatalProtocolException(HttpException ex, NHttpConnection conn) {
                //               System.out.println("fatalProtocolException:" + conn);
                ex.printStackTrace();

            }

        };

        final NHttpServiceHandler serviceHandler = createHttpServiceHandler(HttpUtil.getHttpBaseRPCIOHandler(),
                null, serverEventListener);
        this.endpoint = this.ioReactor.listen(new InetSocketAddress(ip, port));
        IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(serviceHandler, serverParams);
        //         this.execute(serviceHandler, ioEventDispatch);
        this.thread = new IOReactorThread(ioEventDispatch);
        this.thread.start();
        try {
            thread.join(1000);
            this.started = true;
        } catch (InterruptedException e) {

            e.printStackTrace();
        }

    } catch (IOReactorException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.frameworkset.spi.remote.http.HttpServer.java

private void startHttps() throws Exception {

    try {/*  w  w  w  .j  a va2  s .c  o m*/
        this.ioReactor = new DefaultListeningIOReactor(workerCount, serverParams);

        EventListener serverEventListener = new EventListener() {

            // @Override
            public void connectionClosed(NHttpConnection conn) {
                // closedServerConns.decrement();
                // super.connectionClosed(conn);
                //               System.out.println("connectionClosed:" + conn);
            }

            public void connectionOpen(NHttpConnection conn) {
                //               System.out.println("connectionOpen:" + conn);

            }

            public void connectionTimeout(NHttpConnection conn) {
                System.out.println("connectionTimeout:" + conn);

            }

            public void fatalIOException(IOException ex, NHttpConnection conn) {
                //               System.out.println("fatalIOException:" + conn);
                ex.printStackTrace();
            }

            public void fatalProtocolException(HttpException ex, NHttpConnection conn) {
                //               System.out.println("fatalProtocolException:" + conn);
                ex.printStackTrace();

            }

        };

        final NHttpServiceHandler serviceHandler = createHttpServiceHandler(HttpUtil.getHttpBaseRPCIOHandler(),
                null, serverEventListener);
        this.endpoint = this.ioReactor.listen(new InetSocketAddress(ip, port));
        ProMap ssls = ApplicationContext.getApplicationContext().getMapProperty("rpc.protocol.http.ssl.server");
        if (ssls == null) {
            throw new Exception(
                    "?ssl? rpc.protocol.http.ssl.server ?org/frameworkset/spi/manager-rpc-http.xml??");
        }
        String keyStore = ssls.getString("keyStore");
        String keyStorePassword = ssls.getString("keyStorePassword");
        String trustStore = ssls.getString("trustStore");
        String trustStorePassword = ssls.getString("trustStorePassword");
        SSLContext sslcontext = SSLHelper.createSSLContext(keyStore, keyStorePassword, trustStore,
                trustStorePassword);

        IOEventDispatch ioEventDispatch = new SSLServerIOEventDispatch(serviceHandler, sslcontext,
                serverParams);
        //         IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(
        //               serviceHandler, serverParams);
        //         this.execute(serviceHandler, ioEventDispatch);
        this.thread = new IOReactorThread(ioEventDispatch);
        this.thread.start();
        try {
            thread.join(1000);
            this.started = true;
        } catch (InterruptedException e) {

            e.printStackTrace();
        }

        //         ClassLoader cl = this.getClass().getClassLoader();
        //           URL url = cl.getResource("test.keystore");
        //           KeyStore keystore  = KeyStore.getInstance("jks");
        //           keystore.load(url.openStream(), "nopassword".toCharArray());
        //           KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(
        //                   KeyManagerFactory.getDefaultAlgorithm());
        //           kmfactory.init(keystore, "nopassword".toCharArray());
        //           KeyManager[] keymanagers = kmfactory.getKeyManagers(); 
        //           SSLContext sslcontext = SSLContext.getInstance("TLS");
        //           sslcontext.init(keymanagers, null, null);
        //           

        // Set up request handlers
        //           HttpRequestHandlerRegistry reqistry = new HttpRequestHandlerRegistry();
        //           reqistry.register("*", new HttpFileHandler(args[0]));
        //           
        //           handler.setHandlerResolver(reqistry);
        //           
        //           // Provide an event logger
        //           handler.setEventListener(new EventLogger());
        //           
        //          
        //           
        //           ListeningIOReactor ioReactor = new DefaultListeningIOReactor(2, params);
        //           try {
        //               ioReactor.listen(new InetSocketAddress(8080));
        //               ioReactor.execute(ioEventDispatch);
        //           } catch (InterruptedIOException ex) {
        //               System.err.println("Interrupted");
        //           } catch (IOException e) {
        //               System.err.println("I/O error: " + e.getMessage());
        //           }
        //           System.out.println("Shutdown");

    } catch (IOReactorException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.google.acre.script.NHttpClient.java

public NHttpClient(int max_connections) {
    _max_connections = max_connections;/*  ww  w.j a va  2s  .c o  m*/
    _costCollector = CostCollector.getInstance();

    BasicHttpProcessor httpproc = new BasicHttpProcessor();
    httpproc.addInterceptor(new RequestContent());
    httpproc.addInterceptor(new RequestTargetHost());
    httpproc.addInterceptor(new RequestConnControl());
    httpproc.addInterceptor(new RequestUserAgent());
    httpproc.addInterceptor(new RequestExpectContinue());

    BufferingHttpClientHandler handler = new BufferingHttpClientHandler(httpproc,
            new NHttpRequestExecutionHandler(), new DefaultConnectionReuseStrategy(), DEFAULT_HTTP_PARAMS);

    handler.setEventListener(new EventListener() {
        private final static String REQUEST_CLOSURE = "request-closure";

        public void connectionClosed(NHttpConnection conn) {
            // pass (should we be logging this?)
        }

        public void connectionOpen(NHttpConnection conn) {
            // pass (should we be logging this?)
        }

        public void connectionTimeout(NHttpConnection conn) {
            noteException(null, conn);
        }

        void noteException(Exception e, NHttpConnection conn) {
            HttpContext context = conn.getContext();
            NHttpClientClosure closure = (NHttpClientClosure) context.getAttribute(REQUEST_CLOSURE);
            if (closure != null)
                closure.exceptions().add(e);
        }

        public void fatalIOException(IOException e, NHttpConnection conn) {
            noteException(e, conn);
        }

        public void fatalProtocolException(HttpException e, NHttpConnection conn) {
            noteException(e, conn);
        }
    });

    try {
        SSLContext sctx = SSLContext.getInstance("SSL");
        sctx.init(null, null, null);
        _dispatch = new NHttpAdaptableSensibleAndLogicalIOEventDispatch(handler, sctx, DEFAULT_HTTP_PARAMS);
    } catch (java.security.KeyManagementException e) {
        throw new RuntimeException(e);
    } catch (java.security.NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }

    _requests = new ArrayList<NHttpClientClosure>();
    _connection_lock = new Semaphore(_max_connections);

}