org.asynchttpclient.Request Java Examples

The following examples show how to use org.asynchttpclient.Request. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GetRatings.java    From schedge with MIT License 6 votes vote down vote up
/**
 * Given at instructor, will find the coresponding
 * rmp-id for the instructor.
 * @param instructor
 * @return
 */
private static Future<Instructor> getLinkAsync(Instructor instructor) {
  String param = parseInstructorName(instructor.name);
  Request request = new RequestBuilder()
                        .setUri(Uri.create(RMP_URL + param))
                        .setRequestTimeout(60000)
                        .setMethod("GET")
                        .build();
  return GetClient.getClient()
      .executeRequest(request)
      .toCompletableFuture()
      .handleAsync((resp, throwable) -> {
        if (resp == null) {
          logger.error(throwable.getMessage());
          return null;
        }
        String link = parseLink(resp.getResponseBody());
        if (link == null)
          logger.warn("Instructor query " + instructor.name +
                      " returned no results.");
        return new Instructor(instructor.id, link);
      });
}
 
Example #2
Source File: QuerySchool.java    From schedge with MIT License 6 votes vote down vote up
public static String querySchool(Term term) {
  logger.info("querying school for term={}", term);

  Request request =
      new RequestBuilder().setUri(ROOT_URI).setRequestTimeout(60000).build();

  try {
    return GetClient.getClient()
        .executeRequest(request)
        .get()
        .getResponseBody();
  } catch (InterruptedException | ExecutionException e) {
    logger.error("Error (term=" + term + "): " + e.getMessage());
    return null;
  }
}
 
Example #3
Source File: AsyncHttpClientEdgeGridSignatureCalculatorTest.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testNotDuplicatingQueryString() throws Exception {

    ClientCredential credential = ClientCredential.builder()
        .accessToken("akaa-dm5g2bfwoodqnc6k-ju7vlao2wz6oz2rp")
        .clientToken("akaa-k7glklzuxkkh2ycw-oadjphopvpn6yjoj")
        .clientSecret("SOMESECRET")
        .host("endpoint.net")
        .build();

    Request request = new RequestBuilder().setUrl("http://localhost/test").addQueryParam("x", "y").build();
    RequestBuilder requestToUpdate = new RequestBuilder(request);

    new AsyncHttpClientEdgeGridSignatureCalculator(credential).calculateAndAddSignature(
        request, requestToUpdate);
    Request updatedRequest = requestToUpdate.build();

    assertThat(updatedRequest.getUri().getQuery(), equalTo("x=y"));

}
 
Example #4
Source File: AsyncHttpClientEdgeGridSignatureCalculatorTest.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testPreservingQueryString() throws Exception {

    ClientCredential credential = ClientCredential.builder()
        .accessToken("akaa-dm5g2bfwoodqnc6k-ju7vlao2wz6oz2rp")
        .clientToken("akaa-k7glklzuxkkh2ycw-oadjphopvpn6yjoj")
        .clientSecret("SOMESECRET")
        .host("endpoint.net")
        .build();

    Request request = new RequestBuilder().setUrl("http://localhost/test?x=y").build();
    RequestBuilder requestToUpdate = new RequestBuilder(request);

    new AsyncHttpClientEdgeGridSignatureCalculator(credential).calculateAndAddSignature(
        request, requestToUpdate);
    Request updatedRequest = requestToUpdate.build();

    assertThat(updatedRequest.getUri().getQuery(), equalTo("x=y"));

}
 
Example #5
Source File: AsyncHttpClientEdgeGridSignatureCalculatorTest.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "requests")
public void testCalculateAndAddSignatureForGet(Request request) throws Exception {

    ClientCredential credential = ClientCredential.builder()
        .accessToken("akaa-dm5g2bfwoodqnc6k-ju7vlao2wz6oz2rp")
        .clientToken("akaa-k7glklzuxkkh2ycw-oadjphopvpn6yjoj")
        .clientSecret("SOMESECRET")
        .host("endpoint.net")
        .build();

    RequestBuilder requestToUpdate = new RequestBuilder(request);
    new AsyncHttpClientEdgeGridSignatureCalculator(credential).calculateAndAddSignature(
        request, requestToUpdate);
    Request updatedRequest = requestToUpdate.build();

    assertThat(updatedRequest.getHeaders().get("Authorization"), not(isEmptyOrNullString()));
    assertThat(updatedRequest.getHeaders().get("Host"), equalTo("endpoint.net"));
    assertThat(updatedRequest.getUri().getHost(), equalTo("endpoint.net"));

}
 
Example #6
Source File: AsyncHttpClientEdgeGridRequestSignerIntegrationTest.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 6 votes vote down vote up
@Test
public void signEachRequest() throws URISyntaxException, IOException, ExecutionException, InterruptedException {

    wireMockServer.stubFor(post(urlPathEqualTo("/papi/v0/properties"))
        .withHeader("Authorization", matching(".*"))
        .withHeader("Host", equalTo(SERVICE_MOCK))
        .willReturn(aResponse()
            .withStatus(201)
            .withHeader("Content-Type", "text/xml")
            .withBody("<response>Some content</response>")));

    Request request = new RequestBuilder("POST")
        .setUrl("http://" + credential.getHost() + "/papi/v0/properties")
        .addQueryParam("contractId","ctr_1-3CV382")
        .addQueryParam("groupId","grp_18385")
        .setBody("{ \"productId\": \"Site_Accel\", \"propertyName\": \"8LuWyUjwea\" }")
        .setHeader("Content-Type", "application/json")
        .setSignatureCalculator(new AsyncHttpClientEdgeGridSignatureCalculator(credential))
        .build();

    asyncHttpClient().executeRequest(request).get();

    assertThat(wireMockServer.findAllUnmatchedRequests().size(), CoreMatchers.equalTo(0));
}
 
Example #7
Source File: AbstractAsyncHttpClientInstrumentation.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodEnter(suppress = Throwable.class)
private static void onBeforeExecute(@Advice.Argument(value = 0) Request request,
                                    @Advice.Argument(value = 1) AsyncHandler<?> asyncHandler,
                                    @Advice.Local("span") Span span) {
    if (tracer == null || tracer.getActive() == null) {
        return;
    }
    ElasticApmAgent.ensureInstrumented(asyncHandler.getClass(), ASYNC_HANDLER_INSTRUMENTATIONS);

    final AbstractSpan<?> parent = tracer.getActive();
    Uri uri = request.getUri();
    span = HttpClientHelper.startHttpClientSpan(parent, request.getMethod(), uri.toUrl(), uri.getScheme(), uri.getHost(), uri.getPort());

    if (span != null) {
        span.activate();
        TextHeaderSetter<Request> headerSetter = null;
        if (headerSetterManager != null) {
            headerSetter = headerSetterManager.getForClassLoaderOfClass(Request.class);
        }
        if (headerSetter != null) {
            span.propagateTraceContext(request, headerSetter);
        }
        handlerSpanMap.put(asyncHandler, span);
    }
}
 
Example #8
Source File: AsyncHttpClientServiceShould.java    From mutual-tls-ssl with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void executeRequest() throws Exception {
    Response response = mock(Response.class);
    ListenableFuture<Response> listenableFuture = mock(ListenableFuture.class);

    when(httpClient.executeRequest(any(RequestBuilder.class))).thenReturn(listenableFuture);
    when(listenableFuture.toCompletableFuture()).thenReturn(CompletableFuture.completedFuture(response));

    when(response.getStatusCode()).thenReturn(200);
    when(response.getResponseBody()).thenReturn("Hello");

    ArgumentCaptor<RequestBuilder> requestBuilderArgumentCaptor = ArgumentCaptor.forClass(RequestBuilder.class);

    ClientResponse clientResponse = victim.executeRequest(HTTP_URL);

    assertThat(clientResponse.getStatusCode()).isEqualTo(200);
    assertThat(clientResponse.getResponseBody()).isEqualTo("Hello");

    verify(httpClient, times(1)).executeRequest(requestBuilderArgumentCaptor.capture());
    Request request = requestBuilderArgumentCaptor.getValue().build();
    assertThat(request.getUrl()).isEqualTo(HTTP_URL);
    assertThat(request.getMethod()).isEqualTo(GET_METHOD);
    assertThat(request.getHeaders().get(HEADER_KEY_CLIENT_TYPE)).isEqualTo(ASYNC_HTTP_CLIENT.getValue());
}
 
Example #9
Source File: NingCookieExtractorV2.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Override
public String getCookie(Request httpRequest) {
    List<Cookie> cookies = httpRequest.getCookies();
    if (cookies.isEmpty()) {
        return null;
    }

    final StringBuilder sb = new StringBuilder();
    Iterator<Cookie> iterator = cookies.iterator();
    while (iterator.hasNext()) {
        Cookie cookie = iterator.next();
        sb.append(cookie.getName());
        sb.append('=');
        sb.append(cookie.getValue());
        if (iterator.hasNext()) {
            sb.append(',');
        }
    }
    return sb.toString();
}
 
Example #10
Source File: AsyncHttpClientITest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
public static void main(final String[] args) throws Exception {
  try (final AsyncHttpClient client = new DefaultAsyncHttpClient()) {
    final Request request = new RequestBuilder(HttpConstants.Methods.GET).setUrl("http://www.google.com").build();
    final int statusCode = client.executeRequest(request, new AsyncCompletionHandler<Response>() {
      @Override
      public Response onCompleted(final Response response) {
        TestUtil.checkActiveSpan();
        return response;
      }
    }).get(10, TimeUnit.SECONDS).getStatusCode();

    if (200 != statusCode)
      throw new AssertionError("ERROR: response: " + statusCode);

    TestUtil.checkSpan(true, new ComponentSpanCount("java-asynchttpclient", 1), new ComponentSpanCount("netty", 1));
  }
}
 
Example #11
Source File: AsyncHttpClientAgentIntercept.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
public static Object enter(final Object request, final Object handler) {
  final Request req = (Request)request;
  final Tracer tracer = GlobalTracer.get();
  final Span span = tracer
    .buildSpan(req.getMethod())
    .withTag(Tags.COMPONENT.getKey(), COMPONENT_NAME)
    .withTag(Tags.HTTP_METHOD.getKey(), req.getMethod())
    .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
    .withTag(Tags.HTTP_URL.getKey(), req.getUrl()).start();

  tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, new TextMap() {
    @Override
    public Iterator<Entry<String,String>> iterator() {
      throw new UnsupportedOperationException("iterator not supported with Tracer.inject()");
    }

    @Override
    public void put(final String key, final String value) {
      req.getHeaders().add(key, value);
    }
  });

  return WrapperProxy.wrap(handler, new TracingAsyncHandler(tracer, (AsyncHandler<?>)handler, span));
}
 
Example #12
Source File: ExecuteInterceptor.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
public ExecuteInterceptor(TraceContext traceContext, MethodDescriptor descriptor) {
    this.traceContext = traceContext;
    this.descriptor = descriptor;
    final NingAsyncHttpClientPluginConfig config = new NingAsyncHttpClientPluginConfig(traceContext.getProfilerConfig());

    ClientRequestAdaptor<Request> clientRequestAdaptor = new NingAsyncHttpClientRequestAdaptorV2();
    this.clientRequestRecorder = new ClientRequestRecorder<Request>(config.isParam(), clientRequestAdaptor);

    CookieExtractor<Request> cookieExtractor = NingCookieExtractorV2.INSTANCE;
    this.cookieRecorder = CookieRecorderFactory.newCookieRecorder(config.getHttpDumpConfig(), cookieExtractor);

    EntityExtractor<Request> entityExtractor = NingEntityExtractorV2.INSTANCE;
    this.entityRecorder = EntityRecorderFactory.newEntityRecorder(config.getHttpDumpConfig(), entityExtractor);

    ClientHeaderAdaptor<Request> clientHeaderAdaptor = new RequestHeaderAdaptorV2();
    this.requestTraceWriter = new DefaultRequestTraceWriter<Request>(clientHeaderAdaptor, traceContext);
}
 
Example #13
Source File: AsyncHttpClientAspect2x.java    From glowroot with Apache License 2.0 6 votes vote down vote up
@OnBefore
public static @Nullable AsyncTraceEntry onBefore(ThreadContext context,
        @BindParameter @Nullable Request request) {
    // need to start trace entry @OnBefore in case it is executed in a "same thread
    // executor" in which case will be over in @OnReturn
    if (request == null) {
        return null;
    }
    String method = request.getMethod();
    if (method == null) {
        method = "";
    } else {
        method += " ";
    }
    String url = request.getUrl();
    if (url == null) {
        url = "";
    }
    return context.startAsyncServiceCallEntry("HTTP", method + Uris.stripQueryString(url),
            MessageSupplier.create("http client request: {}{}", method, url), timerName);
}
 
Example #14
Source File: BrokerServiceLookupTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private AsyncHttpClient getHttpClient(String version) {
    DefaultAsyncHttpClientConfig.Builder confBuilder = new DefaultAsyncHttpClientConfig.Builder();
    confBuilder.setFollowRedirect(true);
    confBuilder.setUserAgent(version);
    confBuilder.setKeepAliveStrategy(new DefaultKeepAliveStrategy() {
        @Override
        public boolean keepAlive(InetSocketAddress remoteAddress, Request ahcRequest,
                                 HttpRequest request, HttpResponse response) {
            // Close connection upon a server error or per HTTP spec
            return (response.status().code() / 100 != 5)
                   && super.keepAlive(remoteAddress, ahcRequest, request, response);
        }
    });
    AsyncHttpClientConfig config = confBuilder.build();
    return new DefaultAsyncHttpClient(config);
}
 
Example #15
Source File: ClickHouseClient.java    From clickhouse-client-java with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<String> sendRequest(Request request) {
	LOG.debug("Sending request {}", request.getUrl());
	return httpClient.executeRequest(request).toCompletableFuture()
	.handle((response, t) -> {
		if (t != null) {
			LOG.error("Error sending request to endpoint=" + endpoint, t);
			throw new RuntimeException("Error sending request to endpoint=" + endpoint);
			
		} else {
			final int statusCode = response.getStatusCode();
			final String body = response.getResponseBody();

			if (statusCode != 200) {
				final String decodedUrl = decodedUrl(request);	
				
				HttpRequestException e = new HttpRequestException(statusCode, body, decodedUrl);
				LOG.error("[{}] {} : {}", statusCode, decodedUrl, body);

				throw e;
			}

			return body;
		}
	});
}
 
Example #16
Source File: GetRatings.java    From schedge with MIT License 6 votes vote down vote up
/**
 * Given the rmp-id, we get the rating.
 * Rating can be either a float or N/A, in the case of N/A, we return 0.0
 * @param url
 * @param id
 * @return
 */
private static Future<Rating> queryRatingAsync(String url, int id) {
  Request request = new RequestBuilder()
                        .setUri(Uri.create(RMP_ROOT_URL + url))
                        .setRequestTimeout(60000)
                        .setMethod("GET")
                        .build();

  return GetClient.getClient()
      .executeRequest(request)
      .toCompletableFuture()
      .handleAsync((resp, throwable) -> {
        if (resp == null) {
          logger.error(throwable.getMessage());
          return null;
        }
        if (url == null) {
          logger.warn("URL is null for id=" + id);
          return new Rating(id, -1, -1.0f);
        }

        return new Rating(id, Integer.parseInt(url),
                          parseRating(resp.getResponseBody()));
      });
}
 
Example #17
Source File: ExecuteInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
    if (isDebug) {
        // Do not log result
        logger.afterInterceptor(target, args);
    }

    final Trace trace = traceContext.currentTraceObject();
    if (trace == null) {
        return;
    }

    if (!validate(args)) {
        return;
    }

    try {
        final SpanEventRecorder recorder = trace.currentSpanEventRecorder();
        final Request httpRequest = (Request) args[0];
        if (httpRequest != null) {
            // Accessing httpRequest here not BEFORE() because it can cause side effect.
            this.clientRequestRecorder.record(recorder, httpRequest, throwable);
            this.cookieRecorder.record(recorder, httpRequest, throwable);
            this.entityRecorder.record(recorder, httpRequest, throwable);
        }
        recorder.recordApi(descriptor);
        recorder.recordException(throwable);
    } finally {
        trace.traceBlockEnd();
    }
}
 
Example #18
Source File: Unauthorized401Interceptor.java    From ck with Apache License 2.0 5 votes vote down vote up
private void kerberosChallenge(Realm realm,
                               Request request,
                               HttpHeaders headers) throws SpnegoEngineException {

	Uri uri = request.getUri();
	String host = withDefault(request.getVirtualHost(), uri.getHost());
	String challengeHeader = SpnegoEngine.instance(realm.getPrincipal(),
			realm.getPassword(),
			realm.getServicePrincipalName(),
			realm.getRealmName(),
			realm.isUseCanonicalHostname(),
			realm.getCustomLoginConfig(),
			realm.getLoginContextName()).generateToken(host);
	headers.set(AUTHORIZATION, NEGOTIATE + " " + challengeHeader);
}
 
Example #19
Source File: AsyncHttpClientEdgeGridRequestSigner.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 5 votes vote down vote up
private byte[] serializeBody(Request request) {

        if (request.getByteData() != null) {
            return request.getByteData();
        } else if (request.getCompositeByteData() != null) {
            List<Byte> buff = new ArrayList<>();
            for (byte[] bytes : request.getCompositeByteData()) {
                buff.addAll(Bytes.asList(bytes)); // Without Guava that would be quite cumbersome
            }
            return Bytes.toArray(buff);
        } else if (request.getStringData() != null) {
            return request.getStringData().getBytes();
        } else if (request.getByteBufferData() != null) {
            throw new UnsupportedOperationException("Serializing ByteBufferData in request body is not supported");
        } else if (request.getStreamData() != null) {
            throw new UnsupportedOperationException("Serializing StreamData in request body is not supported");
        } else if (isNonEmpty(request.getFormParams())) {
            throw new UnsupportedOperationException("Serializing FormParams in request body is not supported");
        } else if (isNonEmpty(request.getBodyParts())) {
            throw new UnsupportedOperationException("Serializing BodyParts in request body is not supported");
        } else if (request.getFile() != null) {
            throw new UnsupportedOperationException("Serializing File in request body is not supported");
        } else if (request.getBodyGenerator() instanceof FileBodyGenerator) {
            throw new UnsupportedOperationException("Serializing FileBodyGenerator in request body is not supported");
        } else if (request.getBodyGenerator() instanceof InputStreamBodyGenerator) {
            throw new UnsupportedOperationException("Serializing InputStreamBodyGenerator in request body is not supported");
        } else if (request.getBodyGenerator() instanceof ReactiveStreamsBodyGenerator) {
            throw new UnsupportedOperationException("Serializing ReactiveStreamsBodyGenerator in request body is not supported");
        } else if (request.getBodyGenerator() != null) {
            throw new UnsupportedOperationException("Serializing generic BodyGenerator in request body is not supported");
        } else {
            return new byte[]{};
        }
    }
 
Example #20
Source File: AsyncHttpClientEdgeGridRequestSigner.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 5 votes vote down vote up
@Override
protected com.akamai.edgegrid.signer.Request map(Request request) {

    com.akamai.edgegrid.signer.Request.RequestBuilder builder = com.akamai.edgegrid.signer.Request.builder()
        .uri(request.getUrl())
        .method(request.getMethod())
        .body(serializeBody(request));

    for (Map.Entry<String, String> e : request.getHeaders().entries()) {
        builder.header(e.getKey(), e.getValue());
    }

    return builder.build();
}
 
Example #21
Source File: RequestHeaderAdaptorV2.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public void setHeader(Request request, String name, String value) {
    final HttpHeaders httpRequestHeaders = request.getHeaders();
    if (httpRequestHeaders != null) {
        httpRequestHeaders.set(name, value);
        if (isDebug) {
            logger.debug("Set header {}={}", name, value);
        }
    }
}
 
Example #22
Source File: ExecuteInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private boolean validate(final Object[] args) {
    if (args == null || args.length == 0 || !(args[0] instanceof Request)) {
        if (isDebug) {
            logger.debug("Invalid args[0] object. args={}.", args);
        }
        return false;
    }

    return true;
}
 
Example #23
Source File: Zendesk.java    From zendesk-java-client with Apache License 2.0 5 votes vote down vote up
public ArticleAttachments createUploadArticle(long articleId, File file, boolean inline) throws IOException {
RequestBuilder builder = reqBuilder("POST", tmpl("/help_center/articles/{id}/attachments.json").set("id", articleId).toString());
    builder.setHeader("Content-Type", "multipart/form-data");

    if (inline)
        builder.addBodyPart(new StringPart("inline", "true"));

  builder.addBodyPart(
        new FilePart("file", file, "application/octet-stream", StandardCharsets.UTF_8, file.getName()));
    final Request req = builder.build();
    return complete(submit(req, handle(ArticleAttachments.class, "article_attachment")));
}
 
Example #24
Source File: Zendesk.java    From zendesk-java-client with Apache License 2.0 5 votes vote down vote up
private <T> ListenableFuture<T> submit(Request request, ZendeskAsyncCompletionHandler<T> handler) {
    if (logger.isDebugEnabled()) {
        if (request.getStringData() != null) {
            logger.debug("Request {} {}\n{}", request.getMethod(), request.getUrl(), request.getStringData());
        } else if (request.getByteData() != null) {
            logger.debug("Request {} {} {} {} bytes", request.getMethod(), request.getUrl(),
                    request.getHeaders().get("Content-type"), request.getByteData().length);
        } else {
            logger.debug("Request {} {}", request.getMethod(), request.getUrl());
        }
    }
    return client.executeRequest(request, handler);
}
 
Example #25
Source File: AbstractWebRequest.java    From async-gamequery-lib with MIT License 5 votes vote down vote up
@Override
public Request getMessage() {
    build(requestBuilder);
    Request webRequest = requestBuilder.build();
    log.debug("Request URL: {}, PARAMS: {}", webRequest.getUrl(), webRequest.getQueryParams());
    return requestBuilder.build();
}
 
Example #26
Source File: AsyncHttpClientHelperTest.java    From riposte with Apache License 2.0 5 votes vote down vote up
private void verifyRequestBuilderWrapperGeneratedAsExpected(
    RequestBuilderWrapper rbw, String url, String method, Optional<CircuitBreaker<Response>> customCb,
    boolean disableCb
) {
    assertThat(rbw.url).isEqualTo(url);
    assertThat(rbw.httpMethod).isEqualTo(method);
    assertThat(rbw.customCircuitBreaker).isEqualTo(customCb);
    assertThat(rbw.disableCircuitBreaker).isEqualTo(disableCb);
    Request req = rbw.requestBuilder.build();
    assertThat(req.getMethod()).isEqualTo(method);
    assertThat(req.getUri()).isEqualTo(Uri.create(url));
    assertThat(req.getUrl()).isEqualTo(url);
    assertThat(req.getNameResolver()).isInstanceOf(RoundRobinInetAddressResolver.class);
}
 
Example #27
Source File: TestAzureAsyncReader.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private AzureAsyncReader prepareAsyncReader(final String responseBody, final int responseCode) {
  // Prepare response
  AsyncHttpClient client = mock(AsyncHttpClient.class);
  Response response = mock(Response.class);
  HttpResponseStatus status = mock(HttpResponseStatus.class);
  when(status.getStatusCode()).thenReturn(responseCode);
  when(response.getResponseBody()).thenReturn(responseBody);

  CompletableFuture<Response> future = new CompletableFuture<>(); //CompletableFuture.completedFuture(response);
  ListenableFuture<Response> resFuture = mock(ListenableFuture.class);
  when(resFuture.toCompletableFuture()).thenReturn(future);
  LocalDateTime versionDate = LocalDateTime.now(ZoneId.of("GMT")).minusDays(2);

  when(client.executeRequest(any(Request.class), any(AsyncCompletionHandler.class))).then(invocationOnMock -> {
    AsyncCompletionHandler<Response> responseHandler = invocationOnMock.getArgument(1, AsyncCompletionHandler.class);
    assertEquals(responseHandler.getClass(), BufferBasedCompletionHandler.class);
    responseHandler.onStatusReceived(status);
    try {
      responseHandler.onCompleted(response);
    } catch (Exception e) {
      future.completeExceptionally(e);
    }
    return resFuture;
  });

  AzureAsyncReader azureAsyncReader = spy(new AzureAsyncReader(
    "account", new Path("container/directory/file_00.parquet"),
    getMockAuthTokenProvider(), String.valueOf(versionDate.atZone(ZoneId.of("GMT")).toInstant().toEpochMilli()),
    true, client
  ));
  return azureAsyncReader;

}
 
Example #28
Source File: TestAzureSharedKeyAuthTokenProvider.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private Request prepareTestRequest() {
  return new RequestBuilder(HttpConstants.Methods.GET)
    .addHeader("Date", "Tue, 31 Dec 2019 07:18:50 GMT")
    .addHeader("Content-Length", 0)
    .addHeader("x-ms-version", "2019-02-02")
    .addHeader("x-ms-client-request-id", "b2a11e2a-65a7-48ed-a643-229255139452")
    .addHeader("User-Agent", "azsdk-java-azure-storage-blob/12.1.0 (1.8.0_231; Mac OS X 10.14.5)")
    .addHeader("x-ms-range", String.format("bytes=%d-%d", 25, 125))
    .addHeader("If-Unmodified-Since", "Tue, 15 Dec 2019 07:18:50 GMT")
    .setUrl("https://account.blob.core.windows.net/container/directory%2Ffile_00.parquet")
    .build();
}
 
Example #29
Source File: AsyncHttpClientEdgeGridRequestSigner.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 5 votes vote down vote up
@Override
protected URI requestUri(Request request) {
    try {
        return request.getUri().toJavaNetURI();
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}
 
Example #30
Source File: AzureSharedKeyAuthTokenProvider.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public String getAuthzHeaderValue(final Request req) {
  try {
    final URL url = new URL(req.getUrl());
    final Map<String, String> headersMap = new HashMap<>();
    req.getHeaders().forEach(header -> headersMap.put(header.getKey(), header.getValue()));
    return this.storageSharedKeyCredential.generateAuthorizationHeader(url, req.getMethod(), headersMap);
  } catch (MalformedURLException e) {
    throw new IllegalStateException("The request URL is invalid ", e);
  }
}