org.asynchttpclient.uri.Uri Java Examples

The following examples show how to use org.asynchttpclient.uri.Uri. 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 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 #2
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 #3
Source File: AsyncHttpClientHelper.java    From riposte with Apache License 2.0 6 votes vote down vote up
protected Optional<CircuitBreaker<Response>> getCircuitBreaker(
    RequestBuilderWrapper requestBuilderWrapper) {
  if (requestBuilderWrapper.disableCircuitBreaker)
    return Optional.empty();

  // Circuit breaking is enabled for this call. So we return the custom one specified or use the default one if a
  //      custom one is not specified.
  if (requestBuilderWrapper.customCircuitBreaker.isPresent())
    return requestBuilderWrapper.customCircuitBreaker;

  // No custom circuit breaker. Use the default for the given request's host.
  Uri uri = Uri.create(requestBuilderWrapper.url);
  String host = uri.getHost();
  EventLoop nettyEventLoop = requestBuilderWrapper.getCtx() == null
      ? null
      : requestBuilderWrapper.getCtx().channel().eventLoop();
  CircuitBreaker<Integer> defaultStatusCodeCircuitBreaker = getDefaultHttpStatusCodeCircuitBreakerForKey(
      host, Optional.ofNullable(nettyEventLoop), Optional.ofNullable(nettyEventLoop)
  );
  return Optional.of(
      new CircuitBreakerDelegate<>(
          defaultStatusCodeCircuitBreaker,
          response -> (response == null ? null : response.getStatusCode())
      )
  );
}
 
Example #4
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 #5
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 #6
Source File: AzureAsyncContainerProvider.java    From dremio-oss with 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 #7
Source File: ADLSClient.java    From dremio-oss with 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 #8
Source File: MockResponse.java    From flummi with Apache License 2.0 5 votes vote down vote up
public MockResponse(int statusCode, String statusText, String responseBody, Uri uri, String contentType) {
    this.statusCode = statusCode;
    this.statusText = statusText;
    this.responseBody = responseBody;
    this.uri = uri;
    this.contentType = contentType;
}
 
Example #9
Source File: AsyncHttpCaller.java    From pampas with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Response> asyncCall(AsyncHttpRequest req, ServerInstance serverInstance) {
    final FullHttpRequest httpRequest = req.getFullHttpRequest();
    try {
        final AsyncHttpClient httpClient = this.client;
        BoundRequestBuilder requestBuilder = new BoundRequestBuilder(httpClient,
                httpRequest.method().name(), true);

        requestBuilder.setUri(Uri.create(serverInstance.toUri() + req.getRequestPath()));
        for (Map.Entry<String, String> headerEntry : httpRequest.headers()) {
            if (StringUtils.isNotEmpty(headerEntry.getKey())
                    && !"Host".equals(headerEntry.getKey())) {
                requestBuilder.addHeader(headerEntry.getKey(), headerEntry.getValue());
            }

        }
        requestBuilder.addHeader(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);

        if (httpRequest.content() != null && httpRequest.content().isReadable()) {
            //请求body转换为ByteBuffer,并且设置为只读,ByteBuf复用 堆内存中的数据 zero copy
            ByteBuffer readOnlyBuffer = httpRequest.content().nioBuffer().asReadOnlyBuffer();
            requestBuilder.setBody(readOnlyBuffer);
        }
        ListenableFuture<Response> listenableFuture = requestBuilder.execute();
        return listenableFuture.toCompletableFuture();

    } catch (Exception ex) {
        log.warn("执行调用报错:{}", ex);
        CompletableFuture<Response> exceptionFuture = CompletableFuture.completedFuture(null);
        exceptionFuture.completeExceptionally(ex);
        return exceptionFuture;
    }
}
 
Example #10
Source File: AddToCart.java    From schedge with 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 #11
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 #12
Source File: EnrollCourses.java    From schedge with 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 #13
Source File: EnrollCourses.java    From schedge with 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 #14
Source File: Context.java    From schedge with 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 #15
Source File: QuerySection.java    From schedge with 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 #16
Source File: AsyncHttpRequest.java    From junit-servers with MIT License 5 votes vote down vote up
@Override
protected HttpResponse doExecute() throws Exception {
	final HttpUrl endpoint = getEndpoint();
	final String scheme = endpoint.getScheme();
	final String userInfo = null;
	final String host = endpoint.getHost();
	final int port = endpoint.getPort();
	final String path = Utf8UrlEncoder.encodePath(endpoint.getPath());
	final String query = null;
	final String fragment = null;
	final Uri uri = new Uri(scheme, userInfo, host, port, path, query, fragment);

	final String method = getMethod().getVerb();
	final RequestBuilder builder = new RequestBuilder(method, true).setUri(uri);

	handleQueryParameters(builder);
	handleBody(builder);
	handleHeaders(builder);
	handleCookies(builder);

	final Request request = builder.build();
	final ListenableFuture<Response> future = client.executeRequest(request);

	final long start = nanoTime();
	final Response response = future.get();
	final long duration = nanoTime() - start;

	return AsyncHttpResponseFactory.of(response, duration);
}
 
Example #17
Source File: MockResponse.java    From flummi with Apache License 2.0 4 votes vote down vote up
@Override
public Uri getUri() {
    return uri;
}
 
Example #18
Source File: MockResponse.java    From flummi with Apache License 2.0 4 votes vote down vote up
public MockResponse(int statusCode, String statusText, String responseBody) {
    this(statusCode, statusText, responseBody, new Uri("http", null, "somehost", 80, "/some/uri", null), "text/html");
}
 
Example #19
Source File: AsyncHttpClientEdgeGridRequestSigner.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 4 votes vote down vote up
private static Uri toAsyncHttpClientUri(URI uri) {
    return Uri.create(uri.toString());
}
 
Example #20
Source File: StepDefs.java    From galeb with Apache License 2.0 4 votes vote down vote up
@Do("^Do (.+) (.+)$")
public void sendMethodPath(String method, String path) throws Throwable {
    this.uri = Uri.create("http://127.0.0.1:" + SystemEnv.ROUTER_PORT.getValue() + path);
    this.method = method;
}
 
Example #21
Source File: HealthCheckerService.java    From galeb with Apache License 2.0 4 votes vote down vote up
@Override
public List<Cookie> get(Uri uri) {
    return Collections.emptyList();
}
 
Example #22
Source File: HealthCheckerService.java    From galeb with Apache License 2.0 4 votes vote down vote up
@Override
public void add(Uri uri, Cookie cookie) { }
 
Example #23
Source File: AddToCart.java    From schedge with MIT License 4 votes vote down vote up
public static Future<String> addToCart(User user, Term term,
                                       RegistrationCourse course,
                                       Context.HttpContext context) {
  // @TODO: Turn this into static if possible
  Context.HttpContext newContext = GetLogin.getLoginSession(user, context);
  String sectionsForm = getSectionsForm(term, course, newContext);

  /**
   * Make the request given session token and shopping cart
   * "https://m.albert.nyu.edu/app/student/enrollmentcart/addToCart/NYUNV/UGRD/1204/7669";
   */
  Request request =
      new RequestBuilder()
          .setUri(Uri.create(SHOPPING_CART_ADD_DATA_URL_STRING +
                             term.getId() + "/" + course.registrationNumber))
          .setRequestTimeout(60000)
          .setHeader("Referer", 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("GET")
          .build();
  //    GetClient.getClient()
  //            .executeRequest(request)
  //            .toCompletableFuture()
  //            .handleAsync(((resp, throwable) -> {
  //              //if status code is 200 then there is an error
  //              return null;
  //            }));

  if (sectionsForm != null) {
    System.out.println(sectionsForm);
    Request sectionRequest =
        new RequestBuilder()
            .setUri(Uri.create(ADD_RELATED_DATA_URL_STRING))
            .setRequestTimeout(60000)
            .setHeader("Referer", ADD_RELATED_ROOT_URL_STRING + term.getId() +
                                      "/" + course.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", newContext.cookies.stream()
                                     .map(it -> it.name() + '=' + it.value())
                                     .collect(Collectors.joining("; ")))
            .setMethod("POST")
            .setBody(sectionsForm)
            .build();

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

  GetClient.getClient()
      .executeRequest(request)
      .toCompletableFuture()
      .handleAsync(((resp, throwable) -> {
        // if status code is 200 then there is an error
        return null;
      }));
  return null;
}
 
Example #24
Source File: BlackholeCookieStore.java    From schedge with MIT License 4 votes vote down vote up
@Override
public List<Cookie> get(Uri uri) {
  return Collections.emptyList();
}
 
Example #25
Source File: BlackholeCookieStore.java    From schedge with MIT License 4 votes vote down vote up
@Override
public void add(Uri uri, Cookie cookie) {}