@Override
  public <T> ListenableFuture<T> execute(Request request, AsyncHandler<T> handler)
      throws IOException {
    final Expectation expectation = expectations.get(new URL(request.getUrl()));
    if (expectation == null) {
      throw new RuntimeException("Unknown URL requested, failing test: " + request.getUrl());
    }
    fulfilledExpectations.add(expectation);
    T t = null;
    try {
      final URI uri = expectation.getUrl().toURI();
      handler.onStatusReceived(
          new HttpResponseStatus(uri, this) {
            @Override
            public int getStatusCode() {
              return expectation.getStatusCode();
            }

            @Override
            public String getStatusText() {
              return ""; // TODO
            }

            @Override
            public String getProtocolName() {
              return expectation.getUrl().getProtocol();
            }

            @Override
            public int getProtocolMajorVersion() {
              return 1;
            }

            @Override
            public int getProtocolMinorVersion() {
              return 1;
            }

            @Override
            public String getProtocolText() {
              return ""; // TODO
            }
          });
      handler.onHeadersReceived(
          new HttpResponseHeaders(uri, this) {
            @Override
            public FluentCaseInsensitiveStringsMap getHeaders() {
              return new FluentCaseInsensitiveStringsMap();
            }
          });
      handler.onBodyPartReceived(
          new HttpResponseBodyPart(uri, this) {
            @Override
            public byte[] getBodyPartBytes() {
              return expectation.getPayload().getBytes(Charset.forName("UTF-8"));
            }

            @Override
            public int writeTo(OutputStream outputStream) throws IOException {
              final byte[] bodyPartBytes = getBodyPartBytes();
              outputStream.write(bodyPartBytes);
              return bodyPartBytes.length;
            }

            @Override
            public ByteBuffer getBodyByteBuffer() {
              return ByteBuffer.wrap(getBodyPartBytes());
            }

            @Override
            public boolean isLast() {
              return true;
            }

            @Override
            public void markUnderlyingConnectionAsClosed() {}

            @Override
            public boolean closeUnderlyingConnection() {
              return true;
            }
          });
      t = handler.onCompleted();
    } catch (Exception e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    }
    final T finalT = t;
    final Future<T> futureT =
        Executors.newSingleThreadExecutor()
            .submit(
                new Callable<T>() {
                  @Override
                  public T call() throws Exception {
                    return finalT;
                  }
                });
    return new ImmediateFuture<>(futureT);
  }
Exemplo n.º 2
0
 /**
  * Checks whether proxy should be used according to nonProxyHosts settings of it, or we want to go
  * directly to target host. If <code>null</code> proxy is passed in, this method returns true --
  * since there is NO proxy, we should avoid to use it. Simple hostname pattern matching using "*"
  * are supported, but only as prefixes. See
  * http://download.oracle.com/javase/1.4.2/docs/guide/net/properties.html
  *
  * @param proxyServer
  * @param request
  * @return true if we have to avoid proxy use (obeying non-proxy hosts settings), false otherwise.
  */
 public static boolean avoidProxy(final ProxyServer proxyServer, final Request request) {
   return avoidProxy(proxyServer, AsyncHttpProviderUtils.getHost(URI.create(request.getUrl())));
 }