Java Code Examples for org.apache.http.HttpStatus#SC_PRECONDITION_FAILED

The following examples show how to use org.apache.http.HttpStatus#SC_PRECONDITION_FAILED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HC4DavExchangeSession.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @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();
        }
    }
}
 
Example 2
Source File: HC4DavExchangeSession.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
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();
    }
}
 
Example 3
Source File: HttpClientAdapter.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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());
    }
}
 
Example 4
Source File: HC4DavExchangeSession.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example 5
Source File: HC4DavExchangeSession.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example 6
Source File: MCRDataciteClient.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example 7
Source File: HttpRequestHandler.java    From Asqatasun with GNU Affero General Public License v3.0 4 votes vote down vote up
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;
    }
}
 
Example 8
Source File: HttpLocalizedOperationResult.java    From gocd with Apache License 2.0 4 votes vote down vote up
@Override
public void stale(String message) {
    this.message = message;
    httpCode = HttpStatus.SC_PRECONDITION_FAILED;
}
 
Example 9
Source File: HttpPreconditionFailedException.java    From davmail with GNU General Public License v2.0 2 votes vote down vote up
/**
 * HttpResponseException with 412 precondition failed status.
 *
 * @param message exception message
 */
public HttpPreconditionFailedException(String message) {
    super(HttpStatus.SC_PRECONDITION_FAILED, message);
}