コード例 #1
0
ファイル: HTTPUtil.java プロジェクト: saman-aghazadeh/panoply
 public synchronized List<Header> getHeaders(String key) {
   List<Header> keyh = new ArrayList<Header>();
   for (Header h : this.headers) {
     if (h.getName().equalsIgnoreCase(key.trim())) keyh.add(h);
   }
   return keyh;
 }
コード例 #2
0
ファイル: DConnect2.java プロジェクト: asascience/THREDDS
  private void checkHeaders(HTTPMethod method) {
    if (debugHeaders) {
      System.out.println("\nOpenConnection Headers for " + method.getPath());
      System.out.println("Status Line: " + method.getStatusLine());
    }

    Header[] responseHeaders = method.getResponseHeaders();
    for (int i1 = 0; i1 < responseHeaders.length; i1++) {
      Header responseHeader = responseHeaders[i1];
      if (debugHeaders) System.out.print("  " + responseHeader);
      String key = responseHeader.getName();
      String value = responseHeader.getValue();

      if (key.equals("Last-Modified")) {
        lastModified = value;
        if (debugHeaders) System.out.println(" **found lastModified = " + lastModified);

      } else if (key.equals("X-Last-Extended")) {
        lastExtended = value;
        if (debugHeaders) System.out.println(" **found lastExtended = " + lastExtended);

      } else if (key.equals("X-Last-Modified-Invalid")) {
        lastModifiedInvalid = value;
        if (debugHeaders)
          System.out.println(" **found lastModifiedInvalid = " + lastModifiedInvalid);
      }
    }

    if (debugHeaders) System.out.println("OpenConnection Headers for " + method.getPath());

    Cookie[] cookies = _session.getCookies();

    if (cookies.length > 0) {
      if (debugHeaders) System.out.println("Cookies= ");

      for (int i = 0; i < cookies.length; i++) {
        Cookie cooky = cookies[i];
        if (debugHeaders) System.out.println("  " + cooky);
        if (cooky.getName().equalsIgnoreCase("jsessionid")) hasSession = true;
      }
    }
  }
コード例 #3
0
  public String post(@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("");
    }
    String requestId = null;
    try {
      HttpClient client = getClient();
      String url = endpoint + account + resource;

      HttpPost post = new HttpPost(url);

      if (url.toLowerCase().contains("operations")) {
        post.addHeader("x-ms-version", "2014-02-01");
      } else if (url.toLowerCase().contains("/deployments")) {
        post.addHeader("x-ms-version", "2014-05-01");
      } else {
        post.addHeader("x-ms-version", "2012-03-01");
      }

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

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

      if (wire.isDebugEnabled()) {
        wire.debug(post.getRequestLine().toString());
        for (Header header : post.getAllHeaders()) {
          wire.debug(header.getName() + ": " + header.getValue());
        }
        wire.debug("");
        if (body != null) {
          wire.debug(body);
          wire.debug("");
        }
      }
      if (body != null) {
        try {
          if (url.endsWith("/services/networking/media")) {
            post.setEntity(new StringEntity(body, "text/plain", "utf-8"));
          } else {
            post.setEntity(new StringEntity(body, "application/xml", "utf-8"));
          }

        } catch (UnsupportedEncodingException e) {
          throw new InternalException(e);
        }
      }
      HttpResponse response;
      StatusLine status;

      try {
        response = client.execute(post);
        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) {
          if (wire.isDebugEnabled()) {
            wire.debug(h.getName() + ": " + h.getValue().trim());
          }
          if (h.getName().equalsIgnoreCase("x-ms-request-id")) {
            requestId = h.getValue().trim();
          }
        } else {
          if (wire.isDebugEnabled()) {
            wire.debug(h.getName() + ":");
          }
        }
      }
      if (wire.isDebugEnabled()) {
        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);
      }
    }
    return requestId;
  }
コード例 #4
0
  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);
      }
    }
  }
コード例 #5
0
  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());
    }
  }