Example usage for org.apache.commons.httpclient.methods HeadMethod getResponseBodyAsStream

List of usage examples for org.apache.commons.httpclient.methods HeadMethod getResponseBodyAsStream

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods HeadMethod getResponseBodyAsStream.

Prototype

@Override
public InputStream getResponseBodyAsStream() throws IOException 

Source Link

Document

Returns the response body of the HTTP method, if any, as an InputStream .

Usage

From source file:com.owncloud.android.operations.ExistenceCheckOperation.java

@Override
protected RemoteOperationResult run(WebdavClient client) {
    if (!isOnline()) {
        return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
    }//w ww. ja va 2s. c o m
    RemoteOperationResult result = null;
    HeadMethod head = null;
    try {
        head = new HeadMethod(client.getBaseUri() + WebdavUtils.encodePath(mPath));
        int status = client.executeMethod(head, TIMEOUT, TIMEOUT);
        client.exhaustResponse(head.getResponseBodyAsStream());
        boolean success = (status == HttpStatus.SC_OK && !mSuccessIfAbsent)
                || (status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent);
        result = new RemoteOperationResult(success, status, head.getResponseHeaders());
        Log_OC.d(TAG,
                "Existence check for " + client.getBaseUri() + WebdavUtils.encodePath(mPath) + " targeting for "
                        + (mSuccessIfAbsent ? " absence " : " existence ") + "finished with HTTP status "
                        + status + (!success ? "(FAIL)" : ""));

    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG,
                "Existence check for " + client.getBaseUri() + WebdavUtils.encodePath(mPath) + " targeting for "
                        + (mSuccessIfAbsent ? " absence " : " existence ") + ": " + result.getLogMessage(),
                result.getException());

    } finally {
        if (head != null)
            head.releaseConnection();
    }
    return result;
}

From source file:com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation.java

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    if (!isOnline()) {
        return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
    }/*ww w.j  a v a 2  s.  co m*/
    RemoteOperationResult result = null;
    HeadMethod head = null;
    try {
        head = new HeadMethod(client.getWebdavUri() + WebdavUtils.encodePath(mPath));
        int status = client.executeMethod(head, TIMEOUT, TIMEOUT);
        client.exhaustResponse(head.getResponseBodyAsStream());
        boolean success = (status == HttpStatus.SC_OK && !mSuccessIfAbsent)
                || (status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent);
        result = new RemoteOperationResult(success, status, head.getResponseHeaders());
        Log_OC.d(TAG,
                "Existence check for " + client.getWebdavUri() + WebdavUtils.encodePath(mPath)
                        + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ")
                        + "finished with HTTP status " + status + (!success ? "(FAIL)" : ""));

    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG,
                "Existence check for " + client.getWebdavUri() + WebdavUtils.encodePath(mPath)
                        + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + ": "
                        + result.getLogMessage(),
                result.getException());

    } finally {
        if (head != null)
            head.releaseConnection();
    }
    return result;
}

From source file:com.owncloud.android.oc_framework.network.webdav.WebdavClient.java

/**
 * Check if a file exists in the OC server
 * /*from   ww w.  jav  a  2  s .  c o  m*/
 * TODO replace with ExistenceOperation
 * 
 * @return              'true' if the file exists; 'false' it doesn't exist
 * @throws  Exception   When the existence could not be determined
 */
public boolean existsFile(String path) throws IOException, HttpException {
    HeadMethod head = new HeadMethod(mUri.toString() + WebdavUtils.encodePath(path));
    try {
        int status = executeMethod(head);
        Log.d(TAG, "HEAD to " + path + " finished with HTTP status " + status
                + ((status != HttpStatus.SC_OK) ? "(FAIL)" : ""));
        exhaustResponse(head.getResponseBodyAsStream());
        return (status == HttpStatus.SC_OK);

    } finally {
        head.releaseConnection(); // let the connection available for other methods
    }
}

From source file:eu.alefzero.webdav.WebdavClient.java

/**
 * Check if a file exists in the OC server
 * /*from   w  ww  .ja v  a  2s.  c om*/
 * TODO replace with ExistenceOperation
 * 
 * @return              'true' if the file exists; 'false' it doesn't exist
 * @throws  Exception   When the existence could not be determined
 */
public boolean existsFile(String path) throws IOException, HttpException {
    HeadMethod head = new HeadMethod(mUri.toString() + WebdavUtils.encodePath(path));
    try {
        int status = executeMethod(head);
        Log_OC.d(TAG, "HEAD to " + path + " finished with HTTP status " + status
                + ((status != HttpStatus.SC_OK) ? "(FAIL)" : ""));
        exhaustResponse(head.getResponseBodyAsStream());
        return (status == HttpStatus.SC_OK);

    } finally {
        head.releaseConnection(); // let the connection available for other methods
    }
}

From source file:com.cerema.cloud2.lib.resources.files.ExistenceCheckRemoteOperation.java

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;
    HeadMethod head = null;
    boolean previousFollowRedirects = client.getFollowRedirects();
    try {/* ww w  .j  a  v  a  2 s .  co m*/
        head = new HeadMethod(client.getWebdavUri() + WebdavUtils.encodePath(mPath));
        client.setFollowRedirects(false);
        int status = client.executeMethod(head, TIMEOUT, TIMEOUT);
        if (previousFollowRedirects) {
            mRedirectionPath = client.followRedirection(head);
            status = mRedirectionPath.getLastStatus();
        }
        client.exhaustResponse(head.getResponseBodyAsStream());
        boolean success = (status == HttpStatus.SC_OK && !mSuccessIfAbsent)
                || (status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent);
        result = new RemoteOperationResult(success, status, head.getResponseHeaders());
        Log_OC.d(TAG,
                "Existence check for " + client.getWebdavUri() + WebdavUtils.encodePath(mPath)
                        + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ")
                        + "finished with HTTP status " + status + (!success ? "(FAIL)" : ""));

    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG,
                "Existence check for " + client.getWebdavUri() + WebdavUtils.encodePath(mPath)
                        + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + ": "
                        + result.getLogMessage(),
                result.getException());

    } finally {
        if (head != null)
            head.releaseConnection();
        client.setFollowRedirects(previousFollowRedirects);
    }
    return result;
}

From source file:com.cerema.cloud2.lib.common.OwnCloudClient.java

/**
 * Check if a file exists in the OC server
 * //  w  w w  .j a v a 2s .  com
 * @deprecated   Use ExistenceCheckOperation instead
 * 
 * @return                 'true' if the file exists; 'false' it doesn't exist
 * @throws     Exception   When the existence could not be determined
 */
@Deprecated
public boolean existsFile(String path) throws IOException, HttpException {
    HeadMethod head = new HeadMethod(getWebdavUri() + WebdavUtils.encodePath(path));
    try {
        int status = executeMethod(head);
        Log_OC.d(TAG, "HEAD to " + path + " finished with HTTP status " + status
                + ((status != HttpStatus.SC_OK) ? "(FAIL)" : ""));
        exhaustResponse(head.getResponseBodyAsStream());
        return (status == HttpStatus.SC_OK);

    } finally {
        head.releaseConnection(); // let the connection available for other methods
    }
}