public void testListVirtualGuests() throws SecurityException, NoSuchMethodException, IOException {
    Method method = VirtualGuestAsyncClient.class.getMethod("listVirtualGuests");
    HttpRequest httpRequest = processor.createRequest(method);

    assertRequestLineEquals(
        httpRequest,
        "GET https://api.softlayer.com/rest/v3/SoftLayer_Account/VirtualGuests.json?objectMask=virtualGuests.powerState%3BvirtualGuests.networkVlans%3BvirtualGuests.operatingSystem.passwords%3BvirtualGuests.datacenter%3BvirtualGuests.billingItem HTTP/1.1");
    assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
    assertPayloadEquals(httpRequest, null, null, false);

    // now make sure request filters apply by replaying
    httpRequest = Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest);
    httpRequest = Iterables.getOnlyElement(httpRequest.getFilters()).filter(httpRequest);

    assertRequestLineEquals(
        httpRequest,
        "GET https://api.softlayer.com/rest/v3/SoftLayer_Account/VirtualGuests.json?objectMask=virtualGuests.powerState%3BvirtualGuests.networkVlans%3BvirtualGuests.operatingSystem.passwords%3BvirtualGuests.datacenter%3BvirtualGuests.billingItem HTTP/1.1");
    // for example, using basic authentication, we should get "only one"
    // header
    assertNonPayloadHeadersEqual(
        httpRequest,
        "Accept: application/json\nAuthorization: Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==\n");
    assertPayloadEquals(httpRequest, null, null, false);

    assertResponseParserClassEquals(method, httpRequest, ParseJson.class);
    assertSaxResponseParserClassEquals(method, null);
    assertExceptionParserClassEquals(method, ReturnEmptySetOnNotFoundOr404.class);

    checkFilters(httpRequest);
  }
    public HttpResponse call() throws Exception {

      HttpResponse response = null;
      for (; ; ) {
        HttpRequest request = command.getCurrentRequest();
        Q nativeRequest = null;
        try {
          for (HttpRequestFilter filter : request.getFilters()) {
            request = filter.filter(request);
          }
          checkRequestHasContentLengthOrChunkedEncoding(
              request,
              "After filtering, the request has neither chunked encoding nor content length: "
                  + request);
          logger.debug("Sending request %s: %s", request.hashCode(), request.getRequestLine());
          wirePayloadIfEnabled(wire, request);
          utils.logRequest(headerLog, request, ">>");
          nativeRequest = convert(request);
          response = invoke(nativeRequest);

          logger.debug("Receiving response %s: %s", request.hashCode(), response.getStatusLine());
          utils.logResponse(headerLog, response, "<<");
          if (response.getPayload() != null && wire.enabled()) wire.input(response);
          int statusCode = response.getStatusCode();
          if (statusCode >= 300) {
            if (shouldContinue(response)) continue;
            else break;
          } else {
            break;
          }
        } catch (Exception e) {
          IOException ioe = Throwables2.getFirstThrowableOfType(e, IOException.class);
          if (ioe != null) {
            if (ioe instanceof SSLException) {
              command.setException(
                  new AuthorizationException(
                      e.getMessage()
                          + " connecting to "
                          + command.getCurrentRequest().getRequestLine(),
                      e));
              break;
            } else if (ioRetryHandler.shouldRetryRequest(command, ioe)) {
              continue;
            }
          }
          command.setException(
              new HttpResponseException(
                  e.getMessage() + " connecting to " + command.getCurrentRequest().getRequestLine(),
                  command,
                  null,
                  e));
          break;
        } finally {
          cleanup(nativeRequest);
        }
      }
      if (command.getException() != null) throw command.getException();
      return response;
    }
Пример #3
0
 public static <T> HttpRequest cleanRequest(HttpRequest returnVal) {
   checkNotNull(returnVal, "http request");
   for (HttpRequestFilter filter : returnVal.getFilters()) returnVal = filter.filter(returnVal);
   return HttpRequest.builder()
       .method(returnVal.getMethod())
       .endpoint(returnVal.getEndpoint())
       .headers(returnVal.getHeaders())
       .payload(returnVal.getPayload())
       .build();
 }
  public void testSignRemoveBlob()
      throws ArrayIndexOutOfBoundsException, SecurityException, IllegalArgumentException,
          NoSuchMethodException, IOException {
    HttpRequest request = signer.signRemoveBlob("container", "name");

    assertRequestLineEquals(request, "DELETE http://storageUrl/container/name HTTP/1.1");
    assertNonPayloadHeadersEqual(request, "X-Auth-Token: testtoken\n");
    assertPayloadEquals(request, null, null, false);

    assertEquals(request.getFilters().size(), 0);
  }
    public Request apply(HttpRequest request) {

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

      RequestBuilder builder = new RequestBuilder(request.getMethod());
      builder.setUrl(request.getEndpoint().toASCIIString());
      Payload payload = request.getPayload();
      if (payload != null) {
        boolean chunked = "chunked".equals(request.getFirstHeaderOrNull("Transfer-Encoding"));

        if (request.getPayload().getContentMetadata().getContentMD5() != null)
          builder.addHeader(
              "Content-MD5",
              CryptoStreams.base64(request.getPayload().getContentMetadata().getContentMD5()));
        if (request.getPayload().getContentMetadata().getContentType() != null)
          builder.addHeader(
              HttpHeaders.CONTENT_TYPE, request.getPayload().getContentMetadata().getContentType());
        if (request.getPayload().getContentMetadata().getContentLanguage() != null)
          builder.addHeader(
              HttpHeaders.CONTENT_LANGUAGE,
              request.getPayload().getContentMetadata().getContentLanguage());
        if (request.getPayload().getContentMetadata().getContentEncoding() != null)
          builder.addHeader(
              HttpHeaders.CONTENT_ENCODING,
              request.getPayload().getContentMetadata().getContentEncoding());
        if (request.getPayload().getContentMetadata().getContentDisposition() != null)
          builder.addHeader(
              "Content-Disposition",
              request.getPayload().getContentMetadata().getContentDisposition());
        if (!chunked) {
          Long length =
              checkNotNull(
                  request.getPayload().getContentMetadata().getContentLength(),
                  "payload.getContentLength");
          builder.addHeader(HttpHeaders.CONTENT_LENGTH, length.toString());
        }
        setPayload(builder, payload);
      } else {
        builder.addHeader(HttpHeaders.CONTENT_LENGTH, "0");
      }

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

      return builder.build();
    }
    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();
    }
  public void testSignGetBlob()
      throws ArrayIndexOutOfBoundsException, SecurityException, IllegalArgumentException,
          NoSuchMethodException, IOException {
    HttpRequest request = signer.signGetBlob("container", "name");

    assertRequestLineEquals(
        request, "GET https://accesspoint.atmosonline.com/rest/namespace/container/name HTTP/1.1");
    assertNonPayloadHeadersEqual(
        request,
        "Accept: */*\nDate: Thu, 05 Jun 2008 16:38:19 GMT\nx-emc-signature: Mhe5tqaKv04BlMvEjreNKkHHxzk=\nx-emc-uid: identity\n");
    assertPayloadEquals(request, null, null, false);

    assertEquals(request.getFilters().size(), 0);
  }
  public void testSignPutBlob()
      throws ArrayIndexOutOfBoundsException, SecurityException, IllegalArgumentException,
          NoSuchMethodException, IOException {
    Blob blob = blobFactory.create(null);
    blob.getMetadata().setName("name");
    blob.setPayload("");
    blob.getPayload().getContentMetadata().setContentLength(2l);
    blob.getPayload().getContentMetadata().setContentMD5(new byte[] {0, 2, 4, 8});
    blob.getPayload().getContentMetadata().setContentType("text/plain");

    HttpRequest request = signer.signPutBlob("container", blob);

    assertRequestLineEquals(request, "PUT http://storageUrl/container/name HTTP/1.1");
    assertNonPayloadHeadersEqual(request, "X-Auth-Token: testtoken\n");
    assertContentHeadersEqual(
        request, "text/plain", null, null, null, (long) 2l, new byte[] {0, 2, 4, 8});

    assertEquals(request.getFilters().size(), 0);
  }
  public void testSignPutBlob()
      throws ArrayIndexOutOfBoundsException, SecurityException, IllegalArgumentException,
          NoSuchMethodException, IOException {
    Blob blob = blobFactory.create(null);
    blob.getMetadata().setName("name");
    blob.setPayload("");
    blob.getPayload().getContentMetadata().setContentLength(2l);
    blob.getPayload().getContentMetadata().setContentMD5(new byte[] {0, 2, 4, 8});
    blob.getPayload().getContentMetadata().setContentType("text/plain");

    HttpRequest request = signer.signPutBlob("container", blob);

    assertRequestLineEquals(
        request, "POST https://accesspoint.atmosonline.com/rest/namespace/container/name HTTP/1.1");
    assertNonPayloadHeadersEqual(
        request,
        "Accept: */*\nDate: Thu, 05 Jun 2008 16:38:19 GMT\nx-emc-signature: aLpB1oQaCA27AXT6Nzam7s0f0pI=\nx-emc-uid: identity\n");

    assertContentHeadersEqual(
        request, "text/plain", null, null, null, (long) 2l, new byte[] {0, 2, 4, 8});

    assertEquals(request.getFilters().size(), 0);
  }
 @Override
 protected void checkFilters(HttpRequest request) {
   assertEquals(request.getFilters().size(), 1);
   assertEquals(request.getFilters().get(0).getClass(), SharedKeyLiteAuthentication.class);
 }
 @Override
 protected void checkFilters(HttpRequest request) {
   assertEquals(request.getFilters().size(), 2);
   assertEquals(request.getFilters().get(0).getClass(), AuthenticateRequest.class);
   assertEquals(request.getFilters().get(1).getClass(), AddTimestampQuery.class);
 }
 @Override
 protected void checkFilters(HttpRequest request) {
   assertEquals(request.getFilters().size(), 1);
   assertEquals(request.getFilters().get(0).getClass(), SetVCloudTokenCookie.class);
 }
Пример #13
0
 @Override
 protected void checkFilters(HttpRequest request) {
   assertEquals(request.getFilters().size(), 1);
   assertEquals(request.getFilters().get(0).getClass(), SignRequest.class);
 }
Пример #14
0
 @Override
 protected void checkFilters(HttpRequest request) {
   assertEquals(request.getFilters().size(), 0);
 }
Пример #15
0
 public T fromHttpRequest(HttpRequest in) {
   return super.fromHttpMessage(in)
       .method(in.getMethod())
       .endpoint(in.getEndpoint())
       .filters(in.getFilters());
 }