下面列出了org.apache.http.HttpStatus#SC_PRECONDITION_FAILED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @inheritDoc
*/
@Override
public void moveFolder(String folderPath, String targetPath) throws IOException {
HttpMove httpMove = new HttpMove(URIUtil.encodePath(getFolderPath(folderPath)),
URIUtil.encodePath(getFolderPath(targetPath)), false);
try (CloseableHttpResponse response = httpClientAdapter.execute(httpMove)) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_PRECONDITION_FAILED) {
throw new HttpPreconditionFailedException(BundleMessage.format("EXCEPTION_UNABLE_TO_MOVE_FOLDER"));
} else if (statusCode != HttpStatus.SC_CREATED) {
throw HttpClientAdapter.buildHttpResponseException(httpMove, response);
} else if (folderPath.equalsIgnoreCase("/users/" + getEmail() + "/calendar")) {
// calendar renamed, need to reload well known folders
getWellKnownFolders();
}
}
}
protected void moveMessage(String sourceUrl, String targetFolder) throws IOException {
String targetPath = URIUtil.encodePath(getFolderPath(targetFolder)) + '/' + UUID.randomUUID().toString();
HttpMove method = new HttpMove(URIUtil.encodePath(sourceUrl), targetPath, false);
// allow rename if a message with the same name exists
method.setHeader("Allow-Rename", "t");
try (CloseableHttpResponse response = httpClientAdapter.execute(method)) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_PRECONDITION_FAILED ||
statusCode == HttpStatus.SC_CONFLICT) {
throw new DavMailException("EXCEPTION_UNABLE_TO_MOVE_MESSAGE");
} else if (statusCode != HttpStatus.SC_CREATED) {
throw HttpClientAdapter.buildHttpResponseException(method, response);
}
} finally {
method.releaseConnection();
}
}
/**
* Build Http Exception from method status
*
* @param method Http Method
* @return Http Exception
*/
public static HttpResponseException buildHttpResponseException(HttpRequestBase method, StatusLine statusLine) {
int status = statusLine.getStatusCode();
StringBuilder message = new StringBuilder();
message.append(status).append(' ').append(statusLine.getReasonPhrase());
message.append(" at ").append(method.getURI());
if (method instanceof HttpCopy || method instanceof HttpMove) {
message.append(" to ").append(method.getFirstHeader("Destination"));
}
// 440 means forbidden on Exchange
if (status == 440) {
return new LoginTimeoutException(message.toString());
} else if (status == HttpStatus.SC_FORBIDDEN) {
return new HttpForbiddenException(message.toString());
} else if (status == HttpStatus.SC_NOT_FOUND) {
return new HttpNotFoundException(message.toString());
} else if (status == HttpStatus.SC_PRECONDITION_FAILED) {
return new HttpPreconditionFailedException(message.toString());
} else if (status == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
return new HttpServerErrorException(message.toString());
} else {
return new HttpResponseException(status, message.toString());
}
}
protected void moveItem(HttpMove httpMove) throws IOException {
try (CloseableHttpResponse response = httpClientAdapter.execute(httpMove)) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_PRECONDITION_FAILED) {
throw new DavMailException("EXCEPTION_UNABLE_TO_MOVE_ITEM");
} else if (statusCode != HttpStatus.SC_CREATED && statusCode != HttpStatus.SC_OK) {
throw HttpClientAdapter.buildHttpResponseException(httpMove, response);
}
}
}
protected void copyMessage(String sourceUrl, String targetFolder) throws IOException {
String targetPath = URIUtil.encodePath(getFolderPath(targetFolder)) + '/' + UUID.randomUUID().toString();
HttpCopy httpCopy = new HttpCopy(URIUtil.encodePath(sourceUrl), targetPath, false, false);
// allow rename if a message with the same name exists
httpCopy.addHeader("Allow-Rename", "t");
try (CloseableHttpResponse response = httpClientAdapter.execute(httpCopy)) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_PRECONDITION_FAILED) {
throw new DavMailException("EXCEPTION_UNABLE_TO_COPY_MESSAGE");
} else if (statusCode != HttpStatus.SC_CREATED) {
throw HttpClientAdapter.buildHttpResponseException(httpCopy, response);
}
}
}
public void mintDOI(final MCRDigitalObjectIdentifier doi, URI url) throws MCRPersistentIdentifierException {
URI requestURI = getRequestURI("/doi");
HttpPost post = new HttpPost(requestURI);
try (CloseableHttpClient httpClient = getHttpClient()) {
post.setEntity(new StringEntity(
String.format(Locale.ENGLISH, DOI_REGISTER_REQUEST_TEMPLATE, doi.asString(), url.toString())));
CloseableHttpResponse response = httpClient.execute(post);
StatusLine statusLine = response.getStatusLine();
switch (statusLine.getStatusCode()) {
case HttpStatus.SC_CREATED:
return;
case HttpStatus.SC_BAD_REQUEST:
throw new MCRDatacenterException(
getStatusString(response)); // invalid PREFIX or wrong format, but format is hard defined!
case HttpStatus.SC_UNAUTHORIZED:
throw new MCRDatacenterAuthenticationException();
case HttpStatus.SC_PRECONDITION_FAILED:
throw new MCRDatacenterException(String.format(Locale.ENGLISH,
"Metadata must be uploaded first! (%s)", getStatusString(response)));
default:
throw new MCRDatacenterException(String.format(Locale.ENGLISH,
"Datacenter-Error while minting doi: \"%s\" : %s", doi.asString(), getStatusString(response)));
}
} catch (IOException e) {
throw new MCRDatacenterException("Unknown error while mint new doi", e);
}
}
private int computeStatus(int status) {
switch (status) {
case HttpStatus.SC_FORBIDDEN:
case HttpStatus.SC_METHOD_NOT_ALLOWED:
case HttpStatus.SC_BAD_REQUEST:
case HttpStatus.SC_UNAUTHORIZED:
case HttpStatus.SC_PAYMENT_REQUIRED:
case HttpStatus.SC_NOT_FOUND:
case HttpStatus.SC_NOT_ACCEPTABLE:
case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
case HttpStatus.SC_REQUEST_TIMEOUT:
case HttpStatus.SC_CONFLICT:
case HttpStatus.SC_GONE:
case HttpStatus.SC_LENGTH_REQUIRED:
case HttpStatus.SC_PRECONDITION_FAILED:
case HttpStatus.SC_REQUEST_TOO_LONG:
case HttpStatus.SC_REQUEST_URI_TOO_LONG:
case HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE:
case HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE:
case HttpStatus.SC_EXPECTATION_FAILED:
case HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE:
case HttpStatus.SC_METHOD_FAILURE:
case HttpStatus.SC_UNPROCESSABLE_ENTITY:
case HttpStatus.SC_LOCKED:
case HttpStatus.SC_FAILED_DEPENDENCY:
case HttpStatus.SC_INTERNAL_SERVER_ERROR:
case HttpStatus.SC_NOT_IMPLEMENTED:
case HttpStatus.SC_BAD_GATEWAY:
case HttpStatus.SC_SERVICE_UNAVAILABLE:
case HttpStatus.SC_GATEWAY_TIMEOUT:
case HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED:
case HttpStatus.SC_INSUFFICIENT_STORAGE:
return 0;
case HttpStatus.SC_CONTINUE:
case HttpStatus.SC_SWITCHING_PROTOCOLS:
case HttpStatus.SC_PROCESSING:
case HttpStatus.SC_OK:
case HttpStatus.SC_CREATED:
case HttpStatus.SC_ACCEPTED:
case HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION:
case HttpStatus.SC_NO_CONTENT:
case HttpStatus.SC_RESET_CONTENT:
case HttpStatus.SC_PARTIAL_CONTENT:
case HttpStatus.SC_MULTI_STATUS:
case HttpStatus.SC_MULTIPLE_CHOICES:
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_MOVED_TEMPORARILY:
case HttpStatus.SC_SEE_OTHER:
case HttpStatus.SC_NOT_MODIFIED:
case HttpStatus.SC_USE_PROXY:
case HttpStatus.SC_TEMPORARY_REDIRECT:
return 1;
default :
return 1;
}
}
@Override
public void stale(String message) {
this.message = message;
httpCode = HttpStatus.SC_PRECONDITION_FAILED;
}
/**
* HttpResponseException with 412 precondition failed status.
*
* @param message exception message
*/
public HttpPreconditionFailedException(String message) {
super(HttpStatus.SC_PRECONDITION_FAILED, message);
}