コード例 #1
0
 /**
  * Returns the URL to the resource of this bean (mapped and with the appropriate extension).
  *
  * @see LinkUtil#getUrl(SlingHttpServletRequest, String)
  */
 public String getUrl() {
   if (url == null) {
     RequestHandle request = getRequest();
     url = LinkUtil.getUrl(request, getPath(), request.getSelectors(), null);
   }
   return url;
 }
コード例 #2
0
    public void handleResponse(final HttpResponse response, final HttpContext context) {
      HttpEntity entity = response.getEntity();
      try {
        String content = EntityUtils.toString(entity);

        System.out.println("--------------");
        System.out.println(response.getStatusLine());
        System.out.println("--------------");
        System.out.println("Document length: " + content.length());
        System.out.println("--------------");
      } catch (IOException ex) {
        System.err.println("I/O error: " + ex.getMessage());
      }
      RequestHandle handle = (RequestHandle) context.removeAttribute("request-handle");
      if (handle != null) {
        handle.completed();
      }
    }
コード例 #3
0
  @Override
  public void run() {
    try {

      ServerSocket serverSocket = new ServerSocket(listenPort);

      while (serverSocket != null) {
        Socket socket = null;
        System.out.println("Creando socket.");

        // Avisamos que estamos en el accept (sigue a partir del wait).
        synchronized (this) {
          this.notify();
        }
        socket = serverSocket.accept();
        System.out.println("Conexión de: " + socket.getRemoteSocketAddress().toString());

        String peticion = null;
        String respuesta = null;

        while (true) {
          byte[] request = new byte[TAMANIO_BUFFER];
          try {
            int tamanio = socket.getInputStream().read(request);
            peticion = new String(request, 0, tamanio);
            System.out.println("Reuqest: " + peticion);

            // Invocamos la interface mockeada.
            respuesta = (String) handle.handleRequest(peticion);

            System.out.println("Response: " + respuesta.toString());
            socket.getOutputStream().write(respuesta.getBytes());
          } catch (IOException e) {
            e.printStackTrace();
            // Cerramos el socket para retornar -1.
            socket.close();
          }
          // Volvemos al accept.
          break;
        }
      }
    } catch (IOException ex) {
      ex.printStackTrace();
      System.exit(-1);
    }
  }
コード例 #4
0
 public RequestHandle getRequest() {
   if (request == null) {
     request = RequestHandle.use(getSling().getRequest());
   }
   return request;
 }
コード例 #5
0
  public static void main(String[] args) throws Exception {
    HttpParams params = new BasicHttpParams();
    params
        .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
        .setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000)
        .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
        .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
        .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
        .setParameter(CoreProtocolPNames.USER_AGENT, "HttpComponents/1.1");

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

    // Set up protocol handler
    BufferingHttpClientHandler protocolHandler =
        new BufferingHttpClientHandler(
            httpproc,
            new MyHttpRequestExecutionHandler(),
            new DefaultConnectionReuseStrategy(),
            params);
    protocolHandler.setEventListener(new EventLogger());

    // Limit the total maximum of concurrent connections to 5
    int maxTotalConnections = 5;

    // Use the connection manager to maintain a pool of connections to localhost:8080
    final AsyncConnectionManager connMgr =
        new AsyncConnectionManager(
            new HttpHost("localhost", 8080), maxTotalConnections, protocolHandler, params);

    // Start the I/O reactor in a separate thread
    Thread t =
        new Thread(
            new Runnable() {

              public void run() {
                try {
                  connMgr.execute(); // The connection manager wraps the I/O reactor.
                } catch (InterruptedIOException ex) {
                  System.err.println("Interrupted");
                } catch (IOException e) {
                  System.err.println("I/O error: " + e.getMessage());
                }
                System.out.println("I/O reactor terminated");
              }
            });
    t.start();

    // Submit 50 requests using maximum 5 concurrent connections
    Queue<RequestHandle> queue = new LinkedList<RequestHandle>();
    for (int i = 0; i < 50; i++) {
      AsyncConnectionRequest connRequest =
          connMgr.requestConnection(); // Get a working connection from the connection manager.
      connRequest
          .waitFor(); // Wait for the connection request to be completed to the target authority.
      NHttpClientConnection conn =
          connRequest.getConnection(); // Get the real connection from the connection request.
      if (conn == null) {
        System.err.println("Failed to obtain connection");
        break;
      }

      HttpContext context = conn.getContext();
      BasicHttpRequest httpget = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
      RequestHandle handle = new RequestHandle(connMgr, conn);

      context.setAttribute("request", httpget);
      context.setAttribute("request-handle", handle);

      queue.add(handle);
      conn.requestOutput();
    }

    // Wait until all requests have been completed
    while (!queue.isEmpty()) {
      RequestHandle handle = queue.remove();
      handle.waitFor();
    }

    // Give the I/O reactor 10 sec to shut down
    connMgr.shutdown(10000);
    System.out.println("Done");
  }
コード例 #6
0
 public void finalizeContext(final HttpContext context) {
   RequestHandle handle = (RequestHandle) context.removeAttribute("request-handle");
   if (handle != null) {
     handle.cancel();
   }
 }