Beispiel #1
0
  public Either<IOException, HttpResponse> execUploadRequest(
      String path, List<FormBodyPart> parts, HTTPMethod method, String acceptType) {
    HttpEntityEnclosingRequestBase httpReq = null;
    String url = getUriForPath(path);
    if (method.equals(HTTPMethod.POST)) {
      httpReq = new HttpPost(url);
    } else if (method.equals(HTTPMethod.PUT)) {
      httpReq = new HttpPut(url);
    } else {
      throw new IllegalArgumentException("gotta be this or that: post or put");
    }
    MultipartEntity entity = new MultipartEntity();
    for (FormBodyPart part : parts) {
      entity.addPart(part);
    }
    httpReq.setEntity(entity);

    return execRequest(httpReq, acceptType);
  }
 /**
  * Sets a JSON String as a request entity.
  *
  * @param httpRequest The request to set entity.
  * @param json The JSON String to set.
  */
 protected void setEntity(HttpEntityEnclosingRequestBase httpRequest, String json) {
   try {
     StringEntity entity = new StringEntity(json, "UTF-8");
     entity.setContentType("application/json");
     httpRequest.setEntity(entity);
   } catch (UnsupportedEncodingException e) {
     log.error("Error setting request data. " + e.getMessage());
     throw new IllegalArgumentException(e);
   }
 }
Beispiel #3
0
  public Either<IOException, HttpResponse> execNormalRequest(
      String path, Map<String, String> params, HTTPMethod method, String acceptType) {
    String url = getUriForPath(path);
    if (params == null) {
      params = new HashMap<String, String>();
    }
    HttpUriRequest httpReq = null;
    if (method.equals(HTTPMethod.GET)) {
      HttpGet get = new HttpGet(url);
      for (Map.Entry<String, String> param : params.entrySet()) {
        get.getParams().setParameter(param.getKey(), param.getValue());
      }
      httpReq = get;
    } else {
      HttpEntityEnclosingRequestBase postOrPut = null;
      if (method.equals(HTTPMethod.PUT)) {
        postOrPut = new HttpPut(url);
      } else {
        postOrPut = new HttpPost(url);
      }
      MultipartEntity entity = new MultipartEntity();

      List<NameValuePair> args = new ArrayList<NameValuePair>();
      for (Map.Entry<String, String> param : params.entrySet()) {
        args.add(new BasicNameValuePair(param.getKey(), param.getValue()));
      }
      try {
        postOrPut.setEntity(new UrlEncodedFormEntity(args));
      } catch (UnsupportedEncodingException e) {
        return new Left<IOException, HttpResponse>(e);
      }
      httpReq = postOrPut;
    }

    return execRequest(httpReq, acceptType);
  }
  public void tempRedirectInvoke(
      @Nonnull String tempEndpoint,
      @Nonnull String method,
      @Nonnull String account,
      @Nonnull String resource,
      @Nonnull String body)
      throws CloudException, InternalException {
    if (logger.isTraceEnabled()) {
      logger.trace(
          "enter - " + AzureMethod.class.getName() + ".post(" + account + "," + resource + ")");
    }
    if (wire.isDebugEnabled()) {
      wire.debug(
          "POST --------------------------------------------------------> "
              + endpoint
              + account
              + resource);
      wire.debug("");
    }
    try {
      HttpClient client = getClient();
      String url = tempEndpoint + account + resource;

      HttpRequestBase httpMethod = getMethod(method, url);

      // If it is networking configuration services
      if (httpMethod instanceof HttpPut) {
        if (url.endsWith("/services/networking/media")) {
          httpMethod.addHeader("Content-Type", "text/plain");
        } else {
          httpMethod.addHeader("Content-Type", "application/xml;charset=UTF-8");
        }
      } else {
        httpMethod.addHeader("Content-Type", "application/xml;charset=UTF-8");
      }

      // dmayne version is older for anything to do with images and for disk deletion
      if (url.indexOf("/services/images") > -1
          || (httpMethod instanceof HttpDelete && url.indexOf("/services/disks") > -1)) {
        httpMethod.addHeader("x-ms-version", "2012-08-01");
      } else {
        httpMethod.addHeader("x-ms-version", "2012-03-01");
      }

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

      if (wire.isDebugEnabled()) {
        wire.debug(httpMethod.getRequestLine().toString());
        for (Header header : httpMethod.getAllHeaders()) {
          wire.debug(header.getName() + ": " + header.getValue());
        }
        wire.debug("");
        if (body != null) {
          wire.debug(body);
          wire.debug("");
        }
      }

      if (httpMethod instanceof HttpEntityEnclosingRequestBase) {

        HttpEntityEnclosingRequestBase entityEnclosingMethod =
            (HttpEntityEnclosingRequestBase) httpMethod;

        if (body != null) {
          try {
            entityEnclosingMethod.setEntity(new StringEntity(body, "application/xml", "utf-8"));
          } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        }
      }

      HttpResponse response;
      StatusLine status;

      try {
        response = client.execute(httpMethod);
        status = response.getStatusLine();
      } catch (IOException e) {
        logger.error(
            "post(): Failed to execute HTTP request due to a cloud I/O error: " + e.getMessage());
        if (logger.isTraceEnabled()) {
          e.printStackTrace();
        }
        throw new CloudException(e);
      }
      if (logger.isDebugEnabled()) {
        logger.debug("post(): HTTP Status " + status);
      }
      Header[] headers = response.getAllHeaders();

      if (wire.isDebugEnabled()) {
        wire.debug(status.toString());
        for (Header h : headers) {
          if (h.getValue() != null) {
            wire.debug(h.getName() + ": " + h.getValue().trim());
          } else {
            wire.debug(h.getName() + ":");
          }
        }
        wire.debug("");
      }
      if (status.getStatusCode() != HttpServletResponse.SC_OK
          && status.getStatusCode() != HttpServletResponse.SC_CREATED
          && status.getStatusCode() != HttpServletResponse.SC_ACCEPTED) {
        logger.error("post(): Expected OK for GET request, got " + status.getStatusCode());

        HttpEntity entity = response.getEntity();

        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());
        }
        if (wire.isDebugEnabled()) {
          wire.debug(body);
        }
        wire.debug("");
        AzureException.ExceptionItems items =
            AzureException.parseException(status.getStatusCode(), body);

        if (items == null) {
          throw new CloudException(
              CloudErrorType.GENERAL, status.getStatusCode(), "Unknown", "Unknown");
        }
        logger.error(
            "post(): [" + status.getStatusCode() + " : " + items.message + "] " + items.details);
        throw new AzureException(items);
      }
    } finally {
      if (logger.isTraceEnabled()) {
        logger.trace("exit - " + AzureMethod.class.getName() + ".post()");
      }
      if (wire.isDebugEnabled()) {
        wire.debug("");
        wire.debug(
            "POST --------------------------------------------------------> "
                + endpoint
                + account
                + resource);
      }
    }
  }