Пример #1
0
  protected void exceptionIfImageSizeGreaterThanAvailableCapacity(String url) {
    if (CoreGlobalProperty.UNIT_TEST_ON) {
      return;
    }

    url = url.trim();
    if (!url.startsWith("http") && !url.startsWith("https")) {
      return;
    }

    String len;
    try {
      HttpHeaders header = restf.getRESTTemplate().headForHeaders(url);
      len = header.getFirst("Content-Length");
    } catch (Exception e) {
      logger.warn(String.format("cannot get image.  The image url : %s.", url));
      return;
    }
    if (len == null) {
      return;
    }

    long size = Long.valueOf(len);
    if (size > self.getAvailableCapacity()) {
      throw new OperationFailureException(
          errf.stringToOperationError(
              String.format(
                  "the backup storage[uuid:%s, name:%s] has not enough capacity to download the image[%s]."
                      + "Required size:%s, available size:%s",
                  self.getUuid(), self.getName(), url, size, self.getAvailableCapacity())));
    }
  }
 public void reply(HttpEntity<String> entity, Object rsp) {
   String taskUuid = entity.getHeaders().getFirst(RESTConstant.TASK_UUID);
   String callbackUrl = entity.getHeaders().getFirst(RESTConstant.CALLBACK_URL);
   String rspBody = JSONObjectUtil.toJsonString(rsp);
   HttpHeaders headers = new HttpHeaders();
   headers.setContentType(MediaType.APPLICATION_JSON);
   headers.setContentLength(rspBody.length());
   headers.set(RESTConstant.TASK_UUID, taskUuid);
   HttpEntity<String> rreq = new HttpEntity<String>(rspBody, headers);
   restf.getRESTTemplate().exchange(callbackUrl, HttpMethod.POST, rreq, String.class);
 }