本文整理汇总了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;
}
示例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());
}
示例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());
}
示例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);
}
示例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);
}
示例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);
}
}