Exemple #1
0
    public ListenerThread(ApiServer requestHandler, int port) {
      try {
        _serverSocket = new ServerSocket(port);
      } catch (IOException ioex) {
        s_logger.error("error initializing api server", ioex);
        return;
      }

      _params = new BasicHttpParams();
      _params
          .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 30000)
          .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
          .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
          .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
          .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpComponents/1.1");

      // Set up the HTTP protocol processor
      BasicHttpProcessor httpproc = new BasicHttpProcessor();
      httpproc.addInterceptor(new ResponseDate());
      httpproc.addInterceptor(new ResponseServer());
      httpproc.addInterceptor(new ResponseContent());
      httpproc.addInterceptor(new ResponseConnControl());

      // Set up request handlers
      HttpRequestHandlerRegistry reqistry = new HttpRequestHandlerRegistry();
      reqistry.register("*", requestHandler);

      // Set up the HTTP service
      _httpService =
          new HttpService(
              httpproc, new NoConnectionReuseStrategy(), new DefaultHttpResponseFactory());
      _httpService.setParams(_params);
      _httpService.setHandlerResolver(reqistry);
    }
 @Override
 protected void doService(HttpRequest request, HttpResponse response, HttpContext context)
     throws HttpException, IOException {
   response.addHeader(HttpHeaders.CACHE_CONTROL, "must-revalidate"); // Disable caching
   // Normalize the URI
   super.doService(request, response, context);
 }
Exemple #3
0
 @Override
 protected void runInContext() {
   HttpContext context = new BasicHttpContext(null);
   try {
     while (!Thread.interrupted() && _conn.isOpen()) {
       _httpService.handleRequest(_conn, context);
       _conn.close();
     }
   } catch (ConnectionClosedException ex) {
     if (s_logger.isTraceEnabled()) {
       s_logger.trace("ApiServer:  Client closed connection");
     }
   } catch (IOException ex) {
     if (s_logger.isTraceEnabled()) {
       s_logger.trace("ApiServer:  IOException - " + ex);
     }
   } catch (HttpException ex) {
     s_logger.warn("ApiServer:  Unrecoverable HTTP protocol violation" + ex);
   } finally {
     try {
       _conn.shutdown();
     } catch (IOException ignore) {
     }
   }
 }
    protected void accept() throws IOException {
      // Set up HTTP connection
      Socket socket = servicedSocket.accept();
      acceptedConnections.incrementAndGet();
      DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
      conn.bind(socket, serverParams);

      // Set up the HTTP service
      HttpService httpService =
          new HttpService(httpProcessor, reuseStrategy, new DefaultHttpResponseFactory());
      httpService.setParams(serverParams);
      httpService.setHandlerResolver(handlerRegistry);

      // Start worker thread
      Thread t = new Thread(new Worker(httpService, conn));
      workerThreads.add(t);
      t.setDaemon(true);
      t.start();
    } // accept
 @Override
 public void run() {
   logger.fine("New Connection Thread");
   HttpContext context = new BasicHttpContext();
   try {
     while (!Thread.interrupted() && connection.isOpen()) {
       httpService.handleRequest(connection, context);
     }
   } catch (ConnectionClosedException e) {
     logger.warning("Connection closed: " + e.getMessage());
   } catch (IOException e) {
     logger.warning("I/O error: " + e.getMessage());
   } catch (HttpException e) {
     logger.warning("Unrecoverable HTTP protocol violation: " + e.getMessage());
   } finally {
     try {
       connection.shutdown();
     } catch (IOException e) {
       logger.warning("Failed to shut down connection: " + e.getMessage());
     }
   }
 }