Exemplo n.º 1
0
  public Either<IOException, HttpResponse> execRequest(HttpUriRequest httpReq, String acceptType) {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    httpReq.setHeader("accept", acceptType);
    for (Header header : getHeaders()) {
      httpReq.setHeader(header);
    }

    try {
      HttpResponse resp = httpClient.execute(httpReq);
      Log.i(
          TAG,
          String.format(
              "%s %s %s => %d",
              httpReq.getRequestLine().getMethod(),
              httpReq.getRequestLine().getUri(),
              httpReq.getParams(),
              resp.getStatusLine().getStatusCode()));
      return new Right<IOException, HttpResponse>(resp);
    } catch (IOException e) {
      return new Left<IOException, HttpResponse>(e);
    }
  }
  public @Nullable InputStream getAsStream(@Nonnull String account, @Nonnull URI uri)
      throws CloudException, InternalException {
    logger.trace("enter - " + AzureMethod.class.getName() + ".get(" + account + "," + uri + ")");
    wire.debug("--------------------------------------------------------> " + uri.toASCIIString());

    try {
      HttpClient client = getClient();
      HttpUriRequest get = new HttpGet(uri);

      if (uri.toString().indexOf("/services/images") > -1) {
        get.addHeader("x-ms-version", "2012-08-01");
      } else if (uri.toString().contains("/services/vmimages")) {
        get.addHeader("x-ms-version", "2014-05-01");
      } else {
        get.addHeader("x-ms-version", "2012-03-01");
      }

      if (strategy != null && strategy.getSendAsHeader()) {
        get.addHeader(strategy.getHeaderName(), strategy.getRequestId());
      }

      wire.debug(get.getRequestLine().toString());
      for (Header header : get.getAllHeaders()) {
        wire.debug(header.getName() + ": " + header.getValue());
      }

      HttpResponse response;
      StatusLine status;

      try {
        response = client.execute(get);
        status = response.getStatusLine();
      } catch (IOException e) {
        logger.error(
            "get(): Failed to execute HTTP request due to a cloud I/O error: " + e.getMessage());
        throw new CloudException(e);
      }

      logger.debug("get(): HTTP Status " + status);

      Header[] headers = response.getAllHeaders();

      wire.debug(status.toString());
      for (Header h : headers) {
        if (h.getValue() != null) {
          wire.debug(h.getName() + ": " + h.getValue().trim());
        } else {
          wire.debug(h.getName() + ":");
        }
      }

      if (status.getStatusCode() == HttpServletResponse.SC_NOT_FOUND) {
        return null;
      }
      if (status.getStatusCode() != HttpServletResponse.SC_OK
          && status.getStatusCode() != HttpServletResponse.SC_NON_AUTHORITATIVE_INFORMATION) {
        logger.error("get(): Expected OK for GET request, got " + status.getStatusCode());

        HttpEntity entity = response.getEntity();
        String body;

        if (entity == null) {
          throw new AzureException(
              CloudErrorType.GENERAL,
              status.getStatusCode(),
              status.getReasonPhrase(),
              "An error was returned without explanation");
        }
        try {
          body = EntityUtils.toString(entity);
        } catch (IOException e) {
          throw new AzureException(
              CloudErrorType.GENERAL,
              status.getStatusCode(),
              status.getReasonPhrase(),
              e.getMessage());
        }

        wire.debug(body);

        AzureException.ExceptionItems items =
            AzureException.parseException(status.getStatusCode(), body);

        if (items == null) {
          return null;
        }
        logger.error(
            "get(): [" + status.getStatusCode() + " : " + items.message + "] " + items.details);
        throw new AzureException(items);
      } else {
        HttpEntity entity = response.getEntity();

        if (entity == null) {
          return null;
        }
        InputStream input;

        try {
          input = entity.getContent();
        } catch (IOException e) {
          logger.error(
              "get(): Failed to read response error due to a cloud I/O error: " + e.getMessage());
          throw new CloudException(e);
        }

        wire.debug("---> Binary Data <---");
        return input;
      }
    } finally {
      logger.trace("exit - " + AzureMethod.class.getName() + ".getStream()");
      wire.debug(
          "--------------------------------------------------------> " + uri.toASCIIString());
    }
  }