Example usage for org.apache.http.impl DefaultHttpServerConnection DefaultHttpServerConnection

List of usage examples for org.apache.http.impl DefaultHttpServerConnection DefaultHttpServerConnection

Introduction

In this page you can find the example usage for org.apache.http.impl DefaultHttpServerConnection DefaultHttpServerConnection.

Prototype

DefaultHttpServerConnection

Source Link

Usage

From source file:ru.apertum.qsystem.reports.net.RunnableSocket.java

@Override
public void run() {

    final DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
    try {//from w w  w .  j av a 2  s  .c  o m
        conn.bind(socket, QSystemHtmlInstance.htmlInstance().getParams());
    } catch (IOException ex) {
        throw new ReportException("Not bind socket to connection." + ex);
    }

    final HttpContext context = new BasicHttpContext(null);
    try {
        while (!Thread.interrupted() && conn.isOpen()) {
            QSystemHtmlInstance.htmlInstance().getHttpService().handleRequest(conn, context);
        }
    } catch (ConnectionClosedException ex) {
        QLog.l().logRep().error("Client closed connection", ex);
    } catch (IOException ex) {
        QLog.l().logRep().error("I/O error: " + ex.getMessage(), ex);
    } catch (HttpException ex) {
        QLog.l().logRep().error("Unrecoverable HTTP protocol violation: " + ex.getMessage(), ex);
    } catch (Exception ex) {
        QLog.l().logRep().error("Something with HTTP server.", ex);
    } finally {
        try {
            conn.shutdown();
        } catch (IOException ex) {
            QLog.l().logRep().error("Default Http Server Connection have error then shutdown.", ex);
        } catch (Exception ex) {
            QLog.l().logRep().error("Something with runnableSocket.", ex);
        }
    }

}

From source file:com.wondershare.http.core.Dispatcher.java

@Override
public void run() {
    DefaultHttpServerConnection httpServerConnnection = null;
    try {// w w w .j  av a 2s .  c  om
        httpServerConnnection = new DefaultHttpServerConnection();
        httpServerConnnection.bind(socket, new BasicHttpParams());
        // ??
        TResponse response = TResponse.getInstance();
        response.setContext(mContext);
        httpService.setHandlerResolver(response.getHttpRequestHandler());

        // 
        httpService.handleRequest(httpServerConnnection, new BasicHttpContext());
    } catch (IOException e) {
        e.printStackTrace();
    } catch (HttpException e) {
        e.printStackTrace();
    } finally {
        try {
            if (httpServerConnnection != null) {
                httpServerConnnection.shutdown();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.personalserver.HttpThread.java

public void run() {
    DefaultHttpServerConnection httpServer = new DefaultHttpServerConnection();
    try {/*www  .j  a va 2  s.co  m*/
        httpServer.bind(mSocket, new BasicHttpParams());
        mHttpService.handleRequest(httpServer, mHttpContext);
    } catch (IOException e) {
        System.err.println("Exception in HttpThread.java:can't bind");
        e.printStackTrace();
    } catch (HttpException e) {
        System.err.println("Exception in HttpThread.java:handle request");
        e.printStackTrace();
    } catch (Exception exce) {
        System.err.println("debug : error again !");
        System.err.println(exce.getMessage());
        exce.printStackTrace();
    } finally {
        try {
            httpServer.close();
        } catch (IOException e) {
            System.err.println("Excetion in HttpThread.java:can't shutdown");
            e.printStackTrace();
        }
    }
}

From source file:com.wentam.defcol.connect_to_computer.WebServer.java

public void runServer() {
    try {//w  w w  .ja v a2 s. c om
        serverSocket = new ServerSocket(serverPort);

        serverSocket.setReuseAddress(true);

        while (running) {
            try {
                final Socket socket = serverSocket.accept();

                DefaultHttpServerConnection serverConnection = new DefaultHttpServerConnection();

                serverConnection.bind(socket, new BasicHttpParams());

                httpService.handleRequest(serverConnection, httpContext);

                serverConnection.shutdown();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (HttpException e) {
                e.printStackTrace();
            }
        }

        serverSocket.close();
    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    running = false;
}

From source file:com.devoteam.srit.xmlloader.http.bio.BIOSocketServerListener.java

public void run() {
    try {/*from   w  w  w.  j a v a2  s . c o m*/
        while (true) {
            //
            // Set up HTTP connection
            //
            GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.PROTOCOL,
                    "SocketServerListener secure=", secure, "waiting for a connection on socket");
            Socket socket = serverSocket.accept();
            GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.PROTOCOL,
                    "SocketServerListener secure=", secure, "got a connection");

            DefaultHttpServerConnection serverConnection = new DefaultHttpServerConnection();
            HttpParams params = new BasicHttpParams();
            serverConnection.bind(socket, params);

            InetSocketAddress remoteInetSocketAddress = (InetSocketAddress) socket.getRemoteSocketAddress();
            InetSocketAddress localInetSocketAddress = (InetSocketAddress) socket.getLocalSocketAddress();

            String connectionName = "HTTPServerConnection" + Stack.nextTransactionId();
            String remoteHost = remoteInetSocketAddress.getAddress().getHostAddress();
            String remotePort = Integer.toString(remoteInetSocketAddress.getPort());
            String localHost = localInetSocketAddress.getAddress().getHostAddress();
            String localPort = Integer.toString(localInetSocketAddress.getPort());

            BIOChannelHttp connHttp = new BIOChannelHttp(connectionName, localHost, localPort, remoteHost,
                    remotePort, StackFactory.PROTOCOL_HTTP, secure);

            //
            // Start Server thread
            //
            BIOSocketServerHttp socketServerHttp = new BIOSocketServerHttp(serverConnection, connHttp);

            connHttp.setSocketServerHttp(socketServerHttp);
            StackFactory.getStack(StackFactory.PROTOCOL_HTTP).openChannel(connHttp);
        }
    } catch (Exception e) {
        GlobalLogger.instance().getApplicationLogger().error(TextEvent.Topic.PROTOCOL, e,
                "Exception in SocketServerListener secure=" + secure);
    }

    GlobalLogger.instance().getApplicationLogger().warn(TextEvent.Topic.PROTOCOL,
            "SocketServerListener secure=", secure, "stopped");
}

From source file:org.devtcg.five.server.AbstractHttpServer.java

public void run() {
    if (mReqHandler == null)
        throw new IllegalStateException("Request handler not set.");

    while (Thread.interrupted() == false) {
        try {//from  ww  w.j  a v a2 s.  c  o  m
            Socket sock = getSocket().accept();
            DefaultHttpServerConnection conn = new DefaultHttpServerConnection();

            conn.bind(sock, mParams);

            BasicHttpProcessor proc = new BasicHttpProcessor();
            proc.addInterceptor(new ResponseContent());
            proc.addInterceptor(new ResponseConnControl());

            HttpRequestHandlerRegistry reg = new HttpRequestHandlerRegistry();
            reg.register("*", mReqHandler);

            HttpService svc = new HttpService(proc, new DefaultConnectionReuseStrategy(),
                    new DefaultHttpResponseFactory());

            svc.setParams(mParams);
            svc.setHandlerResolver(reg);

            WorkerThread t;

            synchronized (mWorkers) {
                t = new WorkerThread(svc, conn);
                mWorkers.add(t);
            }

            t.start();
        } catch (IOException e) {
            if (!hasCanceled()) {
                if (LOG.isErrorEnabled())
                    LOG.error("I/O error initializing connection thread", e);
            }
            break;
        }
    }
}

From source file:org.devtcg.five.util.streaming.LocalHttpServer.java

public void run() {
    if (mReqHandler == null)
        throw new IllegalStateException("Request handler not set.");

    if (mSocket == null)
        throw new IllegalStateException("Not bound.");

    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

    while (Thread.interrupted() == false) {
        try {//  w  w w. jav  a  2s .  co  m
            Socket sock = mSocket.accept();
            DefaultHttpServerConnection conn = new DefaultHttpServerConnection();

            conn.bind(sock, mParams);

            BasicHttpProcessor proc = new BasicHttpProcessor();
            proc.addInterceptor(new ResponseContent());
            proc.addInterceptor(new ResponseConnControl());

            HttpRequestHandlerRegistry reg = new HttpRequestHandlerRegistry();
            reg.register("*", mReqHandler);

            HttpService svc = new HttpService(proc, new DefaultConnectionReuseStrategy(),
                    new DefaultHttpResponseFactory());

            svc.setParams(mParams);
            svc.setHandlerResolver(reg);

            WorkerThread t;

            synchronized (mWorkers) {
                t = new WorkerThread(svc, conn);
                mWorkers.add(t);
            }

            t.setDaemon(true);
            t.start();
        } catch (IOException e) {
            Log.e(TAG, "I/O error initializing connection thread: " + e.getMessage());
            break;
        }
    }
}

From source file:org.fourthline.cling.transport.impl.apache.StreamServerImpl.java

public void run() {

    log.fine("Entering blocking receiving loop, listening for HTTP stream requests on: "
            + serverSocket.getLocalSocketAddress());
    while (!stopped) {

        try {//from   ww  w. ja va  2  s.co m

            // Block until we have a connection
            final Socket clientSocket = serverSocket.accept();

            // We have to force this fantastic library to accept HTTP methods which are not in the holy RFCs.
            final DefaultHttpServerConnection httpServerConnection = new DefaultHttpServerConnection() {
                @Override
                protected HttpRequestFactory createHttpRequestFactory() {
                    return new UpnpHttpRequestFactory();
                }
            };

            log.fine("Incoming connection from: " + clientSocket.getInetAddress());
            httpServerConnection.bind(clientSocket, globalParams);

            // Wrap the processing of the request in a UpnpStream
            UpnpStream connectionStream = new HttpServerConnectionUpnpStream(router.getProtocolFactory(),
                    httpServerConnection, globalParams) {
                @Override
                protected Connection createConnection() {
                    return new ApacheServerConnection(clientSocket, httpServerConnection);

                }
            };

            router.received(connectionStream);

        } catch (InterruptedIOException ex) {
            log.fine("I/O has been interrupted, stopping receiving loop, bytes transfered: "
                    + ex.bytesTransferred);
            break;
        } catch (SocketException ex) {
            if (!stopped) {
                // That's not good, could be anything
                log.fine("Exception using server socket: " + ex.getMessage());
            } else {
                // Well, it's just been stopped so that's totally fine and expected
            }
            break;
        } catch (IOException ex) {
            log.fine("Exception initializing receiving loop: " + ex.getMessage());
            break;
        }
    }

    try {
        log.fine("Receiving loop stopped");
        if (!serverSocket.isClosed()) {
            log.fine("Closing streaming server socket");
            serverSocket.close();
        }
    } catch (Exception ex) {
        log.info("Exception closing streaming server socket: " + ex.getMessage());
    }

}

From source file:org.muckebox.android.net.DownloadServer.java

@Override
public void run() {
    super.run();/*from  ww w  .  ja v a 2  s. c  o  m*/

    try {
        d("Starting server on " + mPort);

        mServerSocket = new ServerSocket(mPort, 0, InetAddress.getByName("localhost"));
        mServerSocket.setReuseAddress(true);

        try {
            mReady = true;

            while (!mStopped) {
                Socket socket = null;
                DefaultHttpServerConnection connection = new DefaultHttpServerConnection();

                try {
                    d("Waiting for new connection");

                    socket = mServerSocket.accept();

                    d("Got connection");

                    connection.bind(socket, new BasicHttpParams());
                    mHttpService.handleRequest(connection, mHttpContext);

                    d("Request handling finished");
                } catch (HttpException e) {
                    Log.e(LOG_TAG, "Got HTTP error: " + e);
                } finally {
                    connection.shutdown();

                    if (socket != null)
                        socket.close();

                    socket = null;
                }
            }
        } finally {
            if (mServerSocket != null)
                mServerSocket.close();
        }

        d("Server stopped");
    } catch (IOException e) {
        Log.i(LOG_TAG, "IOException, probably ok");
        e.printStackTrace();
    }
}

From source file:com.yanzhenjie.andserver.DefaultAndServer.java

@Override
public void run() {
    try {//from ww w . j ava2  s .  c  o m
        mServerSocket = new ServerSocket();
        mServerSocket.setReuseAddress(true);
        mServerSocket.bind(new InetSocketAddress(mPort));

        // HTTP??
        BasicHttpProcessor httpProcessor = new BasicHttpProcessor();
        httpProcessor.addInterceptor(new ResponseDate());
        httpProcessor.addInterceptor(new ResponseServer());
        httpProcessor.addInterceptor(new ResponseContent());
        httpProcessor.addInterceptor(new ResponseConnControl());

        // HTTP Attribute.
        HttpParams httpParams = new BasicHttpParams();
        httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout)
                .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
                .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
                .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
                .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "WebServer/1.1");

        // Http?
        HttpRequestHandlerRegistry handlerRegistry = new HttpRequestHandlerRegistry();
        for (Map.Entry<String, AndServerRequestHandler> handlerEntry : mRequestHandlerMap.entrySet()) {
            handlerRegistry.register("/" + handlerEntry.getKey(),
                    new DefaultHttpRequestHandler(handlerEntry.getValue()));
        }

        // HTTP?
        HttpService httpService = new HttpService(httpProcessor, new DefaultConnectionReuseStrategy(),
                new DefaultHttpResponseFactory());
        httpService.setParams(httpParams);
        httpService.setHandlerResolver(handlerRegistry);

        /**
         * ?
         */
        while (isLoop) {
            // 
            if (!mServerSocket.isClosed()) {
                Socket socket = mServerSocket.accept();
                DefaultHttpServerConnection serverConnection = new DefaultHttpServerConnection();
                serverConnection.bind(socket, httpParams);

                // Dispatch request handler.
                RequestHandleTask requestTask = new RequestHandleTask(this, httpService, serverConnection);
                requestTask.setDaemon(true);
                AndWebUtil.executeRunnable(requestTask);
            }
        }
    } catch (Exception e) {
    } finally {
        close();
    }
}