Java Code Examples for org.asynchttpclient.RequestBuilder

The following examples show how to use org.asynchttpclient.RequestBuilder. These examples are extracted from open source projects. 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 Project: schedge   Author: BUGS-NYU   File: GetRatings.java    License: 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 Project: schedge   Author: BUGS-NYU   File: GetRatings.java    License: 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 #3
Source Project: schedge   Author: BUGS-NYU   File: QuerySchool.java    License: 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 #4
Source Project: mutual-tls-ssl   Author: Hakky54   File: AsyncHttpClientServiceShould.java    License: 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 #5
Source Project: java-specialagent   Author: opentracing-contrib   File: AsyncHttpClientITest.java    License: 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 #6
Source Project: pulsar   Author: apache   File: FunctionsImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Void> uploadFunctionAsync(String sourceFile, String path) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        RequestBuilder builder = post(functions.path("upload").getUri().toASCIIString())
                .addBodyPart(new FilePart("data", new File(sourceFile), MediaType.APPLICATION_OCTET_STREAM))
                .addBodyPart(new StringPart("path", path, MediaType.TEXT_PLAIN));

        asyncHttpClient.executeRequest(addAuthHeaders(functions, builder).build()).toCompletableFuture()
                .thenAccept(response -> {
                    if (response.getStatusCode() < 200 || response.getStatusCode() >= 300) {
                        future.completeExceptionally(
                                getApiException(Response
                                        .status(response.getStatusCode())
                                        .entity(response.getResponseBody())
                                        .build()));
                    } else {
                        future.complete(null);
                    }
                });
    } catch (Exception e) {
        future.completeExceptionally(getApiException(e));
    }
    return future;
}
 
Example #7
@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 #8
@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 #9
@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 #10
@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 #11
Source Project: schedge   Author: BUGS-NYU   File: QueryCatalog.java    License: MIT License 5 votes vote down vote up
private static Future<HttpContext> getContextAsync() {
  logger.debug("Getting CSRF token...");
  Request request =
      new RequestBuilder().setUri(ROOT_URI).setMethod("GET").build();

  return GetClient.getClient()
      .executeRequest(request)
      .toCompletableFuture()
      .handleAsync((resp, throwable) -> {
        if (resp == null) {
          logger.error(throwable.getMessage());
          return null;
        }

        List<Cookie> cookies =
            resp.getHeaders()
                .getAll("Set-Cookie")
                .stream()
                .map(cookie -> ClientCookieDecoder.STRICT.decode(cookie))
                .collect(Collectors.toList());
        Cookie csrfCookie =
            cookies.stream()
                .filter(cookie -> cookie.name().equals("CSRFCookie"))
                .findAny()
                .orElse(null);
        if (csrfCookie == null) {
          logger.error("Couldn't find cookie with name=CSRFCookie");
          return null;
        }
        logger.debug("Retrieved CSRF token `{}`", csrfCookie.value());
        return new HttpContext(csrfCookie.value(), cookies);
      });
}
 
Example #12
Source Project: schedge   Author: BUGS-NYU   File: QuerySection.java    License: MIT License 5 votes vote down vote up
public static <T> Future<T> querySectionAsync(Term term,
                                              int registrationNumber,
                                              Function<String, T> transform) {

  logger.debug("Querying section in term=" + term +
               " with registrationNumber=" + registrationNumber + "...");
  if (registrationNumber < 0)
    throw new IllegalArgumentException(
        "Registration numbers aren't negative!");

  Request request = new RequestBuilder()
                        .setUri(Uri.create(DATA_URL_STRING + term.getId() +
                                           "/" + registrationNumber))
                        .setRequestTimeout(60000)
                        .setMethod("GET")
                        .build();

  return GetClient.getClient()
      .executeRequest(request)
      .toCompletableFuture()
      .handleAsync((resp, throwable) -> {
        if (resp == null) {
          logger.error("Error (registrationNumber={}): {}",
                       registrationNumber, throwable.getMessage());
          return null;
        }
        return transform.apply(resp.getResponseBody());
      });
}
 
Example #13
Source Project: schedge   Author: BUGS-NYU   File: Context.java    License: MIT License 5 votes vote down vote up
public static Future<HttpContext> getContextAsync(Term term) {
  Request request = new RequestBuilder()
                        .setUri(Uri.create(ROOT_URI + term.getId()))
                        .setMethod("GET")
                        .build();

  return GetClient.getClient()
      .executeRequest(request)
      .toCompletableFuture()
      .handleAsync((resp, throwable) -> {
        if (resp == null) {
          return null;
        }

        List<Cookie> cookies =
            resp.getHeaders()
                .getAll("Set-Cookie")
                .stream()
                .map(cookie -> ClientCookieDecoder.STRICT.decode(cookie))
                .collect(Collectors.toList());
        Cookie csrfCookie =
            cookies.stream()
                .filter(cookie -> cookie.name().equals("CSRFCookie"))
                .findAny()
                .orElse(null);
        if (csrfCookie == null) {
          return null;
        }
        return new HttpContext(csrfCookie.value(), cookies);
      });
}
 
Example #14
Source Project: schedge   Author: BUGS-NYU   File: EnrollCourses.java    License: MIT License 5 votes vote down vote up
/**
 * With class with waitlist, when enroll, user will be atomatically
 * set up for the class waitlist.
 * @param user
 * @param term
 * @param registrationNumbers
 * @param context
 * @return
 */
public static Future<String> enrollCourse(User user, Term term,
                                          List<Integer> registrationNumbers,
                                          Context.HttpContext context) {
  Context.HttpContext newContext = getLoginSession(user, context);
  String enrollForm =
      setUpForm(term, registrationNumbers, "&enroll=", newContext);
  Request request =
      new RequestBuilder()
          .setUri(Uri.create(DATA_URL_STRING + term.getId()))
          .setRequestTimeout(60000)
          .setHeader("Referer", ENROLL_ROOT_URL_STRING)
          .setHeader("Host", "m.albert.nyu.edu")
          .setHeader("Accept-Language", "en-US,en;q=0.5")
          .setHeader("Accept-Encoding", "gzip, deflate, br")
          .setHeader("Content-Type",
                     "application/x-www-form-urlencoded; charset=UTF-8")
          .setHeader("X-Requested-With", "XMLHttpRequest")
          .setHeader("Origin", "https://m.albert.nyu.edu")
          .setHeader("DNT", "1")
          .setHeader("Connection", "keep-alive")
          .setHeader("Cookie", newContext.cookies.stream()
                                   .map(it -> it.name() + '=' + it.value())
                                   .collect(Collectors.joining("; ")))
          .setMethod("POST")
          .setBody(enrollForm)
          .build();
  return GetClient.getClient()
      .executeRequest(request)
      .toCompletableFuture()
      .handleAsync(((resp, throwable) -> resp.getResponseBody()));
}
 
Example #15
Source Project: schedge   Author: BUGS-NYU   File: EnrollCourses.java    License: MIT License 5 votes vote down vote up
public static void removeFromCart(User user, Term term,
                                  List<Integer> registrationNumbers,
                                  Context.HttpContext context) {
  Context.HttpContext newContext = getLoginSession(user, context);
  /*
  "https://m.albert.nyu.edu/app/student/enrollmentcart/enroll/NYUNV/UGRD/1204"
  "Can change to more by adding more selected field
   */
  String deleteForm =
      setUpForm(term, registrationNumbers, "&delete=", newContext);
  Request request =
      new RequestBuilder()
          .setUri(Uri.create(DATA_URL_STRING + term.getId()))
          .setRequestTimeout(60000)
          .setHeader("Referer", DATA_URL_STRING)
          .setHeader("Host", "m.albert.nyu.edu")
          .setHeader("Accept-Language", "en-US,en;q=0.5")
          .setHeader("Accept-Encoding", "gzip, deflate, br")
          .setHeader("Content-Type",
                     "application/x-www-form-urlencoded; charset=UTF-8")
          .setHeader("X-Requested-With", "XMLHttpRequest")
          .setHeader("Origin", "https://m.albert.nyu.edu")
          .setHeader("DNT", "1")
          .setHeader("Connection", "keep-alive")
          .setHeader("Cookie", newContext.cookies.stream()
                                   .map(it -> it.name() + '=' + it.value())
                                   .collect(Collectors.joining("; ")))
          .setMethod("POST")
          .setBody(deleteForm)
          .build();
  GetClient.getClient()
      .executeRequest(request)
      .toCompletableFuture()
      .handleAsync(((resp, throwable) -> {
        System.out.println(resp.getHeaders());
        System.out.println(resp.getStatusCode());
        System.out.println(resp.getResponseBody());
        return null;
      }));
}
 
Example #16
Source Project: schedge   Author: BUGS-NYU   File: AddToCart.java    License: MIT License 5 votes vote down vote up
public static Future<Void> addRelated(Term term, String form,
                                      Integer registrationNumber,
                                      Context.HttpContext context) {

  /**
   * Make the request given session token and shopping cart
   * "https://m.albert.nyu.edu/app/student/enrollmentcart/addToCart/NYUNV/UGRD/1204/7669";
   */
  System.out.println(ADD_RELATED_ROOT_URL_STRING + term.getId() + "/" +
                     registrationNumber);
  Request sectionRequest =
      new RequestBuilder()
          .setUri(Uri.create(ADD_RELATED_DATA_URL_STRING))
          .setRequestTimeout(60000)
          .setHeader("Referer", ADD_RELATED_ROOT_URL_STRING + term.getId() +
                                    "/" + registrationNumber)
          .setHeader("Host", "m.albert.nyu.edu")
          .setHeader("Accept-Language", "en-US,en;q=0.5")
          .setHeader("Accept-Encoding", "gzip, deflate, br")
          .setHeader("Content-Type",
                     "application/x-www-form-urlencoded; charset=UTF-8")
          .setHeader("X-Requested-With", "XMLHttpRequest")
          .setHeader("Origin", "https://m.albert.nyu.edu")
          .setHeader("DNT", "1")
          .setHeader("Connection", "keep-alive")
          .setHeader("Cookie", context.cookies.stream()
                                   .map(it -> it.name() + '=' + it.value())
                                   .collect(Collectors.joining("; ")))
          .setMethod("POST")
          .setBody(form)
          .build();

  GetClient.getClient()
      .executeRequest(sectionRequest)
      .toCompletableFuture()
      .handleAsync(((resp, throwable) -> { return null; }));
  return null;
}
 
Example #17
Source Project: schedge   Author: BUGS-NYU   File: AddToCart.java    License: MIT License 5 votes vote down vote up
/**
 * Currently only work for Fall 2020. Fix later
 */
public static Future<String> addOptions(Term term, String form,
                                        Integer registrationNumber,
                                        Context.HttpContext context) {

  /**
   * Make the request given session token and shopping cart
   * "https://m.albert.nyu.edu/app/student/enrollmentcart/addToCart/NYUNV/UGRD/1204/7669";
   */
  Request optionRequest =
      new RequestBuilder()
          .setUri(ADD_OPTIONS_URI)
          .setRequestTimeout(60000)
          .setHeader("Referer", SELECT_OPTIONS_ROOT_URL_STRING +
                                    term.getId() + "/" + registrationNumber +
                                    "/add")
          .setHeader("Host", "m.albert.nyu.edu")
          .setHeader("Accept-Language", "en-US,en;q=0.9,vi;q=0.8")
          .setHeader("Accept-Encoding", "gzip, deflate, br")
          .setHeader("Content-Type", "application/x-www-form-urlencoded")
          .setHeader("cache-Control", "no-cache")
          .setHeader("Sec-Fetch-Dest", "document")
          .setHeader("Sec-Fetch-Mode", "navigate")
          .setHeader("Sec-Fetch-Size", "same-origin")
          .setHeader("Origin", "https://m.albert.nyu.edu")
          .setHeader("DNT", "1")
          .setHeader("Connection", "keep-alive")
          .setHeader("Cookie", context.cookies.stream()
                                   .map(it -> it.name() + '=' + it.value())
                                   .collect(Collectors.joining("; ")))
          .setMethod("POST")
          .setBody(form)
          .build();
  GetClient.getClient()
      .executeRequest(optionRequest)
      .toCompletableFuture()
      .handleAsync(((resp, throwable) -> { return null; }));
  return null;
}
 
Example #18
Source Project: mutual-tls-ssl   Author: Hakky54   File: AsyncHttpClientService.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ClientResponse executeRequest(String url) throws Exception {
    RequestBuilder requestBuilder = new RequestBuilder()
            .setUrl(url)
            .setHeader(HEADER_KEY_CLIENT_TYPE, getClientType().getValue());

    Response response = httpClient.executeRequest(requestBuilder)
            .toCompletableFuture()
            .get(TIMEOUT_AMOUNT_IN_SECONDS, TimeUnit.SECONDS);

    return new ClientResponse(response.getResponseBody(), response.getStatusCode());
}
 
Example #19
Source Project: java-specialagent   Author: opentracing-contrib   File: AsyncHttpClientTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testNoHandler(final MockTracer tracer) throws IOException {
  try (final AsyncHttpClient client = new DefaultAsyncHttpClient()) {
    final Request request = new RequestBuilder(HttpConstants.Methods.GET).setUrl("http://localhost:12345").build();
    try {
      client.executeRequest(request).get(10, TimeUnit.SECONDS);
    }
    catch (final Exception ignore) {
    }
  }

  await().atMost(15, TimeUnit.SECONDS).until(TestUtil.reportedSpansSize(tracer), equalTo(1));
  assertEquals(1, tracer.finishedSpans().size());
  assertNull(tracer.activeSpan());
}
 
Example #20
Source Project: java-specialagent   Author: opentracing-contrib   File: AsyncHttpClientTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testWithHandler(final MockTracer tracer) throws IOException {
  final AtomicInteger counter = new AtomicInteger();
  try (final AsyncHttpClient client = new DefaultAsyncHttpClient()) {
    final Request request = new RequestBuilder(HttpConstants.Methods.GET).setUrl("http://localhost:12345").build();
    try {
      client.executeRequest(request, new AsyncCompletionHandler<Object>() {
        @Override
        public Object onCompleted(final Response response) {
          assertNotNull(tracer.activeSpan());
          counter.incrementAndGet();
          return response;
        }

        @Override
        public void onThrowable(final Throwable t) {
          assertNotNull(tracer.activeSpan());
          counter.incrementAndGet();
        }
      }).get(10, TimeUnit.SECONDS);
    }
    catch (final Exception ignore) {
    }
  }

  await().atMost(15, TimeUnit.SECONDS).until(TestUtil.reportedSpansSize(tracer), equalTo(1));
  assertEquals(1, tracer.finishedSpans().size());
  assertEquals(1, counter.get());
  assertNull(tracer.activeSpan());
}
 
Example #21
Source Project: galeb   Author: galeb   File: StepDefs.java    License: Apache License 2.0 5 votes vote down vote up
private void executeRequest() throws InterruptedException, java.util.concurrent.ExecutionException {
    long start = System.currentTimeMillis();
    this.response = client.execute(new RequestBuilder(method,true)
            .setHeaders(headers).setAddress(address).setUri(uri));
    this.requestTime = System.currentTimeMillis() - start;
    logger.info("request time (ms): " + requestTime);
}
 
Example #22
Source Project: dremio-oss   Author: dremio   File: ADLSClient.java    License: Apache License 2.0 5 votes vote down vote up
public Request build() {
  Preconditions.checkNotNull(filePath, "File path must be specified.");
  Preconditions.checkNotNull(params.op, "Operation must be specified.");

  final StringBuilder pathBuilder = new StringBuilder(params.op.namespace);
  final String prefix = client.getFilePathPrefix();
  if (prefix != null) {
    pathBuilder.append(prefix);
  }

  if (filePath.charAt(0) != '/') {
    pathBuilder.append('/');
  }
  pathBuilder.append(filePath);

  final URIBuilder uriBuilder = new URIBuilder()
    .setScheme(client.getHttpPrefix())
    .setHost(client.getAccountName())
    .setPath(pathBuilder.toString())
    .setCustomQuery(params.serialize());

  try {
    final String jdkUri = uriBuilder.build().toASCIIString();
    logger.debug("ADLS request built: {}", jdkUri);

    return new RequestBuilder()
      .setUri(Uri.create(jdkUri))
      .setHeaders(headers)
      .build();
  } catch (URISyntaxException e) {
    throw new IllegalArgumentException(e);
  }
}
 
Example #23
Source Project: dremio-oss   Author: dremio   File: AzureAsyncContainerProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean doesContainerExists(final String containerName) {
  // API: https://docs.microsoft.com/en-gb/rest/api/storageservices/datalakestoragegen2/filesystem/getproperties
  logger.debug("Checking for missing azure container " + account + ":" + containerName);
  final Request req = new RequestBuilder(HttpConstants.Methods.HEAD)
    .addHeader("x-ms-date", toHttpDateFormat(System.currentTimeMillis()))
    .addHeader("x-ms-version", XMS_VERSION)
    .addHeader("Content-Length", 0)
    .addHeader("x-ms-client-request-id", UUID.randomUUID().toString())
    .setUrl(AzureAsyncHttpClientUtils.getBaseEndpointURL(account, true) + "/" + containerName)
    .addQueryParam("resource", "filesystem")
    .addQueryParam("timeout", String.valueOf(requestTimeoutSeconds)).build();

  req.getHeaders().add("Authorization", authProvider.getAuthzHeaderValue(req));
  final AtomicBoolean containerExists = new AtomicBoolean(false);
  retryer.call(() -> {
    int status = asyncHttpClient.executeRequest(req).get().getStatusCode();
    if (status != 200 && status != 404) {
      logger.error("Error while checking for azure container " + account + ":" + containerName + " status code " + status);
      throw new RuntimeException(String.format("Error response %d while checking for existence of container %s", status, containerName));
    }
    if (status == 200) {
      logger.debug("Azure container is found valid " + account + ":" + containerName);
      containerExists.set(true);
    }
    return true;
  });

  return containerExists.get();
}
 
Example #24
Source Project: dremio-oss   Author: dremio   File: AzureAsyncContainerProvider.java    License: Apache License 2.0 5 votes vote down vote up
private Request buildRequest() throws URISyntaxException {
  // API - https://docs.microsoft.com/en-gb/rest/api/storageservices/datalakestoragegen2/path/list
  URIBuilder uriBuilder = new URIBuilder(uri);
  uriBuilder.addParameter("resource", "account");
  uriBuilder.addParameter("continuation", continuation);
  uriBuilder.addParameter("maxResults", String.valueOf(PAGE_SIZE));

  RequestBuilder requestBuilder = AzureAsyncHttpClientUtils.newDefaultRequestBuilder()
    .addHeader("x-ms-date", toHttpDateFormat(System.currentTimeMillis()))
    .setUri(Uri.create(uriBuilder.build().toASCIIString()));
  return requestBuilder.build();
}
 
Example #25
Source Project: dremio-oss   Author: dremio   File: AzureAsyncHttpClientUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static RequestBuilder newDefaultRequestBuilder() {
  return new RequestBuilder(HttpConstants.Methods.GET)
    .addHeader("Date", toHttpDateFormat(System.currentTimeMillis()))
    .addHeader("Content-Length", 0)
    .addHeader("x-ms-version", XMS_VERSION)
    .addHeader("x-ms-client-request-id", UUID.randomUUID().toString())
    .addHeader("User-Agent", USER_AGENT_VAL);
}
 
Example #26
Source Project: dremio-oss   Author: dremio   File: TestAzureSharedKeyAuthTokenProvider.java    License: 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 #27
Source Project: pulsar   Author: apache   File: FunctionsImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> createFunctionAsync(FunctionConfig functionConfig, String fileName) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        RequestBuilder builder =
                post(functions.path(functionConfig.getTenant()).path(functionConfig.getNamespace())
                        .path(functionConfig.getName()).getUri().toASCIIString())
                .addBodyPart(new StringPart("functionConfig", ObjectMapperFactory.getThreadLocal()
                        .writeValueAsString(functionConfig), MediaType.APPLICATION_JSON));

        if (fileName != null && !fileName.startsWith("builtin://")) {
            // If the function code is built in, we don't need to submit here
            builder.addBodyPart(new FilePart("data", new File(fileName), MediaType.APPLICATION_OCTET_STREAM));
        }
        asyncHttpClient.executeRequest(addAuthHeaders(functions, builder).build())
                .toCompletableFuture()
                .thenAccept(response -> {
                    if (response.getStatusCode() < 200 || response.getStatusCode() >= 300) {
                        future.completeExceptionally(
                                getApiException(Response
                                        .status(response.getStatusCode())
                                        .entity(response.getResponseBody())
                                        .build()));
                    } else {
                        future.complete(null);
                    }
                });

    } catch (Exception e) {
        future.completeExceptionally(e);
    }
    return future;
}
 
Example #28
Source Project: pulsar   Author: apache   File: FunctionsImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> updateFunctionAsync(
        FunctionConfig functionConfig, String fileName, UpdateOptions updateOptions) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        RequestBuilder builder =
                put(functions.path(functionConfig.getTenant())
                        .path(functionConfig.getNamespace())
                        .path(functionConfig.getName()).getUri().toASCIIString())
                .addBodyPart(new StringPart("functionConfig", ObjectMapperFactory.getThreadLocal()
                        .writeValueAsString(functionConfig), MediaType.APPLICATION_JSON));

        if (updateOptions != null) {
            builder.addBodyPart(new StringPart("updateOptions", ObjectMapperFactory.getThreadLocal()
                    .writeValueAsString(updateOptions), MediaType.APPLICATION_JSON));
        }

        if (fileName != null && !fileName.startsWith("builtin://")) {
            // If the function code is built in, we don't need to submit here
            builder.addBodyPart(new FilePart("data", new File(fileName), MediaType.APPLICATION_OCTET_STREAM));
        }

        asyncHttpClient.executeRequest(addAuthHeaders(functions, builder).build())
                .toCompletableFuture()
                .thenAccept(response -> {
                    if (response.getStatusCode() < 200 || response.getStatusCode() >= 300) {
                        future.completeExceptionally(
                                getApiException(Response
                                        .status(response.getStatusCode())
                                        .entity(response.getResponseBody())
                                        .build()));
                    } else {
                        future.complete(null);
                    }
                });
    } catch (Exception e) {
        future.completeExceptionally(getApiException(e));
    }
    return future;
}
 
Example #29
Source Project: pulsar   Author: apache   File: FunctionsImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> putFunctionStateAsync(
        String tenant, String namespace, String function, FunctionState state) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        RequestBuilder builder =
                post(functions.path(tenant).path(namespace).path(function)
                        .path("state").path(state.getKey()).getUri().toASCIIString());
        builder.addBodyPart(new StringPart("state", ObjectMapperFactory.getThreadLocal()
                .writeValueAsString(state), MediaType.APPLICATION_JSON));
        asyncHttpClient.executeRequest(addAuthHeaders(functions, builder).build())
                .toCompletableFuture()
                .thenAccept(response -> {
                    if (response.getStatusCode() < 200 || response.getStatusCode() >= 300) {
                        future.completeExceptionally(getApiException(
                                Response.status(response.getStatusCode())
                                        .entity(response.getResponseBody()).build()));
                    } else {
                        future.complete(null);
                    }
                });

    } catch (Exception e) {
        future.completeExceptionally(e);
    }
    return future;
}
 
Example #30
Source Project: pulsar   Author: apache   File: FunctionsImpl.java    License: Apache License 2.0 5 votes vote down vote up
public CompletableFuture<Void> updateOnWorkerLeaderAsync(String tenant, String namespace,
                                                         String function, byte[] functionMetaData,
                                                         boolean delete) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        RequestBuilder builder =
                put(functions.path("leader").path(tenant).path(namespace)
                        .path(function).getUri().toASCIIString())
                        .addBodyPart(new ByteArrayPart("functionMetaData", functionMetaData))
                .addBodyPart(new StringPart("delete", Boolean.toString(delete)));

        asyncHttpClient.executeRequest(addAuthHeaders(functions, builder).build())
                .toCompletableFuture()
                .thenAccept(response -> {
                    if (response.getStatusCode() < 200 || response.getStatusCode() >= 300) {
                        future.completeExceptionally(
                                getApiException(Response
                                        .status(response.getStatusCode())
                                        .entity(response.getResponseBody())
                                        .build()));
                    } else {
                        future.complete(null);
                    }
                });

    } catch (Exception e) {
        future.completeExceptionally(e);
    }
    return future;
}