當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpResponseException.getContent方法代碼示例

本文整理匯總了Java中com.google.api.client.http.HttpResponseException.getContent方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpResponseException.getContent方法的具體用法?Java HttpResponseException.getContent怎麽用?Java HttpResponseException.getContent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.google.api.client.http.HttpResponseException的用法示例。


在下文中一共展示了HttpResponseException.getContent方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: handleHttpResponseException

import com.google.api.client.http.HttpResponseException; //導入方法依賴的package包/類
@Override
public BlobDescriptor handleHttpResponseException(HttpResponseException httpResponseException)
    throws RegistryErrorException, HttpResponseException {
  if (httpResponseException.getStatusCode() != HttpStatusCodes.STATUS_CODE_NOT_FOUND) {
    throw httpResponseException;
  }

  // Finds a BLOB_UNKNOWN error response code.
  String errorContent = httpResponseException.getContent();
  if (errorContent == null) {
    // TODO: The Google HTTP client gives null content for HEAD requests. Make the content never be null, even for HEAD requests.
    return null;
  } else {
    try {
      ErrorResponseTemplate errorResponse =
          JsonTemplateMapper.readJson(errorContent, ErrorResponseTemplate.class);
      List<ErrorEntryTemplate> errors = errorResponse.getErrors();
      if (errors.size() == 1) {
        ErrorCodes errorCode = ErrorCodes.valueOf(errors.get(0).getCode());
        if (errorCode.equals(ErrorCodes.BLOB_UNKNOWN)) {
          return null;
        }
      }

    } catch (IOException ex) {
      throw new RegistryErrorExceptionBuilder(getActionDescription(), ex)
          .addReason("Failed to parse registry error response body")
          .build();
    }
  }

  // BLOB_UNKNOWN was not found as a error response code.
  throw httpResponseException;
}
 
開發者ID:GoogleCloudPlatform,項目名稱:minikube-build-tools-for-java,代碼行數:35,代碼來源:BlobChecker.java

示例2: newApiException

import com.google.api.client.http.HttpResponseException; //導入方法依賴的package包/類
/**
 * @deprecated As of xero-java-sdk version 0.6.0, to be removed
 * in favour of {@link XeroExceptionHandler#newApiException(HttpResponseException)}
 */
protected XeroApiException newApiException(HttpResponseException googleException) {
    Matcher matcher = MESSAGE_PATTERN.matcher(googleException.getContent());
    StringBuilder messages = new StringBuilder();
    while (matcher.find()) {
        if (messages.length() > 0) {
            messages.append(", ");
        }
        messages.append(matcher.group(1));
    }

    if (messages.length() > 0) {
        throw new XeroApiException(googleException.getStatusCode(), messages.toString());
    }
    if (googleException.getContent().contains("=")) {
        try {
            String value = URLDecoder.decode(googleException.getContent(), "UTF-8");
            String[] keyValuePairs = value.split("&");

            Map<String, String> errorMap = new HashMap<>();
            for (String pair : keyValuePairs) {
                String[] entry = pair.split("=");
                errorMap.put(entry[0].trim(), entry[1].trim());
            }
            throw new XeroApiException(googleException.getStatusCode(), errorMap);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    throw new XeroApiException(googleException.getStatusCode(), googleException.getContent());
}
 
開發者ID:XeroAPI,項目名稱:Xero-Java,代碼行數:36,代碼來源:XeroClient.java

示例3: newApiException

import com.google.api.client.http.HttpResponseException; //導入方法依賴的package包/類
/**
 * For backwards comparability with xero-java-sdk version 0.6.0 keep the old way of handling exceptions
 *
 * @param httpResponseException exception to handle
 * @return XeroApiException
 */
public XeroApiException newApiException(HttpResponseException httpResponseException) {
    Matcher matcher = MESSAGE_PATTERN.matcher(httpResponseException.getContent());
    StringBuilder messages = new StringBuilder();
    while (matcher.find()) {
        if (messages.length() > 0) {
            messages.append(", ");
        }
        messages.append(matcher.group(1));
    }

    if (messages.length() > 0) {
        return new XeroApiException(httpResponseException.getStatusCode(), messages.toString());
    }
    if (httpResponseException.getContent().contains("=")) {
        try {
            String value = URLDecoder.decode(httpResponseException.getContent(), "UTF-8");
            String[] keyValuePairs = value.split("&");

            Map<String, String> errorMap = new HashMap<>();
            for (String pair : keyValuePairs) {
                String[] entry = pair.split("=");
                errorMap.put(entry[0].trim(), entry[1].trim());
            }
            return new XeroApiException(httpResponseException.getStatusCode(), errorMap);

        } catch (UnsupportedEncodingException e) {
            LOGGER.severe(e.getMessage());
            throw new XeroClientException(e.getMessage(), e);
        }
    }

    return new XeroApiException(httpResponseException.getStatusCode(), httpResponseException.getContent());
}
 
開發者ID:XeroAPI,項目名稱:Xero-Java,代碼行數:40,代碼來源:XeroExceptionHandler.java

示例4: execute

import com.google.api.client.http.HttpResponseException; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public static <T> T execute(Type responseType, HttpRequest httpRequest)
    throws IOException, GerritApiException {
  HttpResponse response;
  try {
    response = httpRequest.execute();
  } catch (HttpResponseException e) {
    throw new GerritApiException(e.getStatusCode(), e.getContent());
  }
  return (T) response.parseAs(responseType);
}
 
開發者ID:google,項目名稱:copybara,代碼行數:12,代碼來源:GerritApiTransportImpl.java

示例5: asGlycerinExcetpion

import com.google.api.client.http.HttpResponseException; //導入方法依賴的package包/類
private GlycerinException asGlycerinExcetpion(String url, HttpResponseException hre) {
    String content = hre.getContent();
    Pattern responsePattern = Pattern.compile("<h1>([^<]+)</h1>");
    Matcher matcher = responsePattern.matcher(content);
    if (matcher.matches()) {
        String msg = matcher.group(1);
        if (msg.equals(DEVELOPER_INACTIVE_MSG)) {
            return new GlycerinUnauthorizedException();
        }
        if (msg.equals(DEVELOPER_OVER_RATE_MSG)) {
            return new GlycerinOverRateException();
        }
    }
    return new GlycerinException(url, hre);
}
 
開發者ID:mbst,項目名稱:glycerin,代碼行數:16,代碼來源:GlycerinHttpClient.java

示例6: handleBadRequest

import com.google.api.client.http.HttpResponseException; //導入方法依賴的package包/類
/**
 * Handle a HTTP 400 Bad Request from Xero. This method will build a {@link XeroApiException}
 * containing {@link ApiException} element with a useful summary of the reason for the error.
 * Use {@link ApiException} to get a list of the errors pertaining to the call in question.
 * <p>
 * For example: if you use {@link com.xero.api.XeroClient#createPayments(List)} and this fails
 * with a XeroApiException, you can extract details from the exception with the following code example.
 * <pre>
 * List<{@link com.xero.model.Elements}> elements = xeroApiException.getApiException().getElements();
 * {@link com.xero.model.Elements} element = elements.get(0);
 * List<{@link Object}> dataContractBase = element.getDataContractBase();
 * for (Object dataContract : dataContractBase) {
 *      {@link com.xero.model.Payment} failedPayment = ({@link com.xero.model.Payment}) dataContract;
 *      {@link com.xero.model.ArrayOfValidationError} validationErrors = failedPayment.getValidationErrors();
 *      ...
 * }
 * </pre>
 * <p>
 * Or if you use {@link com.xero.api.XeroClient#createInvoices(List)} and this fails
 * with a XeroApiException, you can extract details from the exception with the following code example.
 * <pre>
 * List<{@link com.xero.model.Elements}> elements = xeroApiException.getApiException().getElements();
 * {@link com.xero.model.Elements} element = elements.get(0);
 * List<{@link Object}> dataContractBase = element.getDataContractBase();
 * for (Object dataContract : dataContractBase) {
 *      {@link com.xero.model.Invoice} failedInvoice = ({@link com.xero.model.Invoice}) dataContract;
 *      {@link com.xero.model.ArrayOfValidationError} validationErrors = failedInvoice.getValidationErrors();
 *      ...
 * }
 * </pre>
 *
 * @param httpResponseException the exception to handle
 * @return XeroApiException containing {@link ApiException} with a useful summary of the reason for the error.
 */
public XeroApiException handleBadRequest(HttpResponseException httpResponseException) {
    String content = httpResponseException.getContent();

    //TODO we could use the ApiException.xsd to validate that the content is an ApiException xml
    if (content.contains("ApiException")) {
        try {
            ApiException apiException = xeroJaxbMarshaller.unmarshall(content, ApiException.class);
            return new XeroApiException(httpResponseException.getStatusCode(), content, apiException);
        } catch (Exception e) {
            LOGGER.severe(e.getMessage());
            return convertException(httpResponseException);
        }
    } else {
        return convertException(httpResponseException);
    }
}
 
開發者ID:XeroAPI,項目名稱:Xero-Java,代碼行數:51,代碼來源:XeroExceptionHandler.java


注:本文中的com.google.api.client.http.HttpResponseException.getContent方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。