public static boolean unregister(String username, String basicAuthenticationHeader) {

    try {
      BoundRequestBuilder reqBunregister =
          asynSenderclient
              .prepareDelete(
                  HttpResources.getBaseHost() + HttpResources.getRegisterResource(username))
              .addHeader("Authorization", basicAuthenticationHeader);
      // -- Sync response management, no other thing can be done before this step is completed
      Response response = reqBunregister.execute().get();
      if (!HttpErrorCodeEvaluator.isCorrectRegistrationOperation(response.getStatusCode())) {
        log.error("FAIL - Could not register " + response.getStatusCode());
        return false;
      } else {
        //				unregisterNotification = requestRegisterNotification(username,
        // basicAuthenticationHeader);
        if (ServiceExample
            .isUnregisterNotificationArrived()) { // You got the registration notification
          log.info("OK - Unregistered successfully: " + response.getStatusCode());
          return true;
        } else { // The only possibility is that it was caught on the very last notification request
          log.info("OK - Waiting for the unregister notification to come...");
          Thread.sleep(21000);
          return ServiceExample.isUnregisterNotificationArrived();
        }
      }
    } catch (Exception e) {
      log.error("FAIL - UNREGISTER ERROR: username="******", cause: " + e.getCause());
    }
    return false;
  }
  private BoundRequestBuilder getBuilderWithHeaderAndQuery(
      final String verb,
      final String url,
      @Nullable final String username,
      @Nullable final String password,
      final Multimap<String, String> options) {
    BoundRequestBuilder builder;

    if (verb.equals("GET")) {
      builder = httpClient.prepareGet(url);
    } else if (verb.equals("POST")) {
      builder = httpClient.preparePost(url);
    } else if (verb.equals("PUT")) {
      builder = httpClient.preparePut(url);
    } else if (verb.equals("DELETE")) {
      builder = httpClient.prepareDelete(url);
    } else if (verb.equals("HEAD")) {
      builder = httpClient.prepareHead(url);
    } else if (verb.equals("OPTIONS")) {
      builder = httpClient.prepareOptions(url);
    } else {
      throw new IllegalArgumentException("Unrecognized verb: " + verb);
    }

    if (username != null && password != null) {
      final Realm realm = new RealmBuilder().setPrincipal(username).setPassword(password).build();
      builder.setRealm(realm);
    }

    final Collection<String> acceptHeaders = options.removeAll(HTTP_HEADER_ACCEPT);
    final String acceptHeader;
    if (!acceptHeaders.isEmpty()) {
      acceptHeader = CSV_JOINER.join(acceptHeaders);
    } else {
      acceptHeader = ACCEPT_JSON;
    }
    builder.addHeader(HTTP_HEADER_ACCEPT, acceptHeader);

    String contentTypeHeader = getUniqueValue(options, HTTP_HEADER_CONTENT_TYPE);
    if (contentTypeHeader == null) {
      contentTypeHeader = CONTENT_TYPE_JSON;
    } else {
      options.removeAll(HTTP_HEADER_CONTENT_TYPE);
    }
    builder.addHeader(HTTP_HEADER_CONTENT_TYPE, contentTypeHeader);

    builder.setBodyEncoding("UTF-8");

    for (final String key : options.keySet()) {
      if (options.get(key) != null) {
        for (final String value : options.get(key)) {
          builder.addQueryParam(key, value);
        }
      }
    }

    return builder;
  }
    public Request apply(HttpRequest request) {

      for (HttpRequestFilter filter : request.getFilters()) {
        filter.filter(request);
      }

      AsyncHttpClient client = new AsyncHttpClient();
      AsyncHttpClient.BoundRequestBuilder nativeRequestBuilder;
      String endpoint = request.getEndpoint().toASCIIString();
      if (request.getMethod().equals(HttpMethod.HEAD)) {
        nativeRequestBuilder = client.prepareHead(endpoint);
      } else if (request.getMethod().equals(HttpMethod.GET)) {
        nativeRequestBuilder = client.prepareGet(endpoint);
      } else if (request.getMethod().equals(HttpMethod.DELETE)) {
        nativeRequestBuilder = client.prepareDelete(endpoint);
      } else if (request.getMethod().equals(HttpMethod.PUT)) {
        nativeRequestBuilder = client.preparePut(endpoint);
      } else if (request.getMethod().equals(HttpMethod.POST)) {
        nativeRequestBuilder = client.preparePost(endpoint);
      } else {
        throw new UnsupportedOperationException(request.getMethod());
      }
      Payload payload = request.getPayload();
      if (payload != null) {
        boolean chunked = "chunked".equals(request.getFirstHeaderOrNull("Transfer-Encoding"));

        if (request.getPayload().getContentMD5() != null)
          nativeRequestBuilder.addHeader(
              "Content-MD5", CryptoStreams.base64(request.getPayload().getContentMD5()));
        if (request.getPayload().getContentType() != null)
          nativeRequestBuilder.addHeader(
              HttpHeaders.CONTENT_TYPE, request.getPayload().getContentType());
        if (!chunked) {
          Long length =
              checkNotNull(request.getPayload().getContentLength(), "payload.getContentLength");
          nativeRequestBuilder.addHeader(HttpHeaders.CONTENT_LENGTH, length.toString());
        }
        setPayload(nativeRequestBuilder, payload);
      } else {
        nativeRequestBuilder.addHeader(HttpHeaders.CONTENT_LENGTH, "0");
      }

      nativeRequestBuilder.addHeader(HttpHeaders.USER_AGENT, USER_AGENT);
      for (String header : request.getHeaders().keySet()) {
        for (String value : request.getHeaders().get(header)) {
          nativeRequestBuilder.addHeader(header, value);
        }
      }

      return nativeRequestBuilder.build();
    }
Exemplo n.º 4
0
  @Override
  public void deleteItem(ProxyRepository repository, ResourceStoreRequest request)
      throws ItemNotFoundException, UnsupportedStorageOperationException, RemoteStorageException {
    final URL remoteURL = getAbsoluteUrlFromBase(repository, request);

    final String itemUrl = remoteURL.toString();

    final AsyncHttpClient client = getClient(repository);

    try {
      Response response = client.prepareDelete(itemUrl).execute().get();

      // expected: 200 OK, 202 ACCEPTED, 204 NO_CONTENT
      validateResponse(repository, request, "DELETE", itemUrl, response, 200, 202, 204);
    } catch (ItemNotFoundException e) {
      throw e;
    } catch (RemoteStorageException e) {
      throw e;
    } catch (Exception e) {
      throw new RemoteStorageException(e);
    }
  }
  private void restCall(Tuple tuple) throws ExecutionException, InterruptedException {
    Future<Response> fureRet = null;
    for (RestInfo restInfo : restInfos) {
      String url = restInfo.getUrl();
      String body = restInfo.getBody();
      for (BaseBindColumnDef bindColumn : restInfo.getColumns()) {
        url =
            url.replace(
                "[:" + bindColumn.getName() + "]", tuple.getStringByField(bindColumn.getName()));
        if (body != null) {
          body =
              body.replace(
                  "[:" + bindColumn.getName() + "]", tuple.getStringByField(bindColumn.getName()));
        }
      }

      AsyncHttpClient.BoundRequestBuilder boundRequestBuilder = null;
      if (RestCmd.PUT.equals(restInfo.getCmd())) {
        boundRequestBuilder = asyncHttpClient.preparePut(url);
      } else if (RestCmd.GET.equals(restInfo.getCmd())) {
        boundRequestBuilder = asyncHttpClient.prepareGet(url);
      } else if (RestCmd.DELETE.equals(restInfo.getCmd())) {
        boundRequestBuilder = asyncHttpClient.prepareDelete(url);
      } else if (RestCmd.POST.equals(restInfo.getCmd())) {
        boundRequestBuilder = asyncHttpClient.preparePost(url);
      } else {
        break;
      }

      if (body != null) {
        fureRet = boundRequestBuilder.setBody(body).execute();
      } else {
        fureRet = boundRequestBuilder.execute();
      }

      fureRet.get();
    }
  }
 public Future<Response> delete() throws IOException {
   return asyncHttpClient
       .prepareDelete(getCapsuleUrl() + "/api" + readContextPath() + "/" + id)
       .setRealm(getRealm())
       .execute(new ThrowOnHttpFailure());
 }
 private void doDELETE(final String resource) {
   callRecurlySafe(client.prepareDelete(baseUrl + resource), null);
 }