Example usage for org.apache.commons.httpclient HttpStatus SC_MULTI_STATUS

List of usage examples for org.apache.commons.httpclient HttpStatus SC_MULTI_STATUS

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpStatus SC_MULTI_STATUS.

Prototype

int SC_MULTI_STATUS

To view the source code for org.apache.commons.httpclient HttpStatus SC_MULTI_STATUS.

Click Source Link

Document

<tt>207 Multi-Status</tt> (WebDAV - RFC 2518) or <tt>207 Partial Update OK</tt> (HTTP/1.1 - draft-ietf-http-v11-spec-rev-01?)

Usage

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

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    try {//from  w w w.  j  ava 2 s  .  co m
        DavPropertyNameSet propSet = new DavPropertyNameSet();
        propSet.add(DavPropertyName.GETETAG);

        PropFindMethod propfind = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(path),
                propSet, 0);
        int status = client.executeMethod(propfind, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);

        if (status == HttpStatus.SC_MULTI_STATUS || status == HttpStatus.SC_OK) {
            MultiStatusResponse resp = propfind.getResponseBodyAsMultiStatus().getResponses()[0];

            String etag = WebdavUtils.parseEtag(
                    (String) resp.getProperties(HttpStatus.SC_OK).get(DavPropertyName.GETETAG).getValue());

            if (etag.equals(expectedEtag)) {
                return new RemoteOperationResult(RemoteOperationResult.ResultCode.ETAG_UNCHANGED);
            } else {
                RemoteOperationResult result = new RemoteOperationResult(
                        RemoteOperationResult.ResultCode.ETAG_CHANGED);

                ArrayList<Object> list = new ArrayList<>();
                list.add(etag);
                result.setData(list);

                return result;
            }
        }

        if (status == HttpStatus.SC_NOT_FOUND) {
            return new RemoteOperationResult(RemoteOperationResult.ResultCode.FILE_NOT_FOUND);
        }
    } catch (Exception e) {
        Log_OC.e(TAG, "Error while retrieving eTag");
    }

    return new RemoteOperationResult(RemoteOperationResult.ResultCode.ETAG_CHANGED);
}

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

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;
    PropPatchMethod propPatchMethod = null;

    DavPropertySet newProps = new DavPropertySet();
    DavPropertyNameSet removeProperties = new DavPropertyNameSet();

    if (makeItFavorited) {
        DavProperty favoriteProperty = new DefaultDavProperty("oc:favorite", "1",
                Namespace.getNamespace(WebdavEntry.NAMESPACE_OC));
        newProps.add(favoriteProperty);//  ww  w  .  j  a v  a 2  s.c o m
    } else {
        removeProperties.add("oc:favorite", Namespace.getNamespace(WebdavEntry.NAMESPACE_OC));
    }

    String webDavUrl = client.getNewWebdavUri().toString();
    String encodedPath = Uri.encode(userID + filePath).replace("%2F", "/");
    String fullFilePath = webDavUrl + "/files/" + encodedPath;

    try {
        propPatchMethod = new PropPatchMethod(fullFilePath, newProps, removeProperties);
        int status = client.executeMethod(propPatchMethod);

        boolean isSuccess = (status == HttpStatus.SC_MULTI_STATUS || status == HttpStatus.SC_OK);

        if (isSuccess) {
            result = new RemoteOperationResult(true, status, propPatchMethod.getResponseHeaders());
        } else {
            client.exhaustResponse(propPatchMethod.getResponseBodyAsStream());
            result = new RemoteOperationResult(false, status, propPatchMethod.getResponseHeaders());
        }
    } catch (IOException e) {
        result = new RemoteOperationResult(e);
    } finally {
        if (propPatchMethod != null) {
            propPatchMethod.releaseConnection(); // let the connection available for other methods
        }
    }

    return result;
}

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

/**
  * Performs the read operation.//from ww w . jav a  2  s . c o m
  * 
  * @param   client      Client object to communicate with the remote ownCloud server.
  */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;
    PropFindMethod query = null;

    try {
        // remote request
        query = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath),
                WebdavUtils.getAllPropSet(), // PropFind Properties
                DavConstants.DEPTH_1);
        int status = client.executeMethod(query);

        // check and process response
        boolean isSuccess = (status == HttpStatus.SC_MULTI_STATUS || status == HttpStatus.SC_OK);
        if (isSuccess) {
            // get data from remote folder 
            MultiStatus dataInServer = query.getResponseBodyAsMultiStatus();
            readData(dataInServer, client);

            // Result of the operation
            result = new RemoteOperationResult(true, status, query.getResponseHeaders());
            // Add data to the result
            if (result.isSuccess()) {
                result.setData(mFolderAndFiles);
            }
        } else {
            // synchronization failed
            client.exhaustResponse(query.getResponseBodyAsStream());
            result = new RemoteOperationResult(false, status, query.getResponseHeaders());
        }

    } catch (Exception e) {
        result = new RemoteOperationResult(e);

    } finally {
        if (query != null)
            query.releaseConnection(); // let the connection available for other methods
        if (result.isSuccess()) {
            Log_OC.i(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage());
        } else {
            if (result.isException()) {
                Log_OC.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage(),
                        result.getException());
            } else {
                Log_OC.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage());
            }
        }

    }
    return result;
}

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

/**
 * Performs the read operation.// w w w  .  ja v a  2  s  .  co  m
 * 
 * @param client      Client object to communicate with the remote ownCloud server.
 */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    PropFindMethod propfind = null;
    RemoteOperationResult result = null;

    /// take the duty of check the server for the current state of the file there
    try {
        // remote request
        propfind = new PropFindMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath),
                WebdavUtils.getFilePropSet(), // PropFind Properties
                DavConstants.DEPTH_0);
        int status;
        status = client.executeMethod(propfind, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);

        boolean isSuccess = (status == HttpStatus.SC_MULTI_STATUS || status == HttpStatus.SC_OK);
        if (isSuccess) {
            // Parse response
            MultiStatus resp = propfind.getResponseBodyAsMultiStatus();
            WebdavEntry we = new WebdavEntry(resp.getResponses()[0], client.getWebdavUri().getPath());
            RemoteFile remoteFile = new RemoteFile(we);
            ArrayList<Object> files = new ArrayList<Object>();
            files.add(remoteFile);

            // Result of the operation
            result = new RemoteOperationResult(true, status, propfind.getResponseHeaders());
            result.setData(files);

        } else {
            client.exhaustResponse(propfind.getResponseBodyAsStream());
            result = new RemoteOperationResult(false, status, propfind.getResponseHeaders());
        }

    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        e.printStackTrace();
        Log_OC.e(TAG, "Synchronizing  file " + mRemotePath + ": " + result.getLogMessage(),
                result.getException());
    } finally {
        if (propfind != null)
            propfind.releaseConnection();
    }
    return result;
}

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

/**
 * Performs the read operation.//from  w w  w.  j  a  v a2  s  . c  om
 *
 * @param client Client object to communicate with the remote ownCloud server.
 */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;
    PropFindMethod query = null;

    try {
        // remote request
        if (userId.isEmpty()) {
            throw new IllegalArgumentException("UserId may not be empty!");
        }

        String uri = client.getNewWebdavUri() + "/versions/" + userId + "/versions/" + fileId;
        DavPropertyNameSet propSet = WebdavUtils.getFileVersionPropSet();

        query = new PropFindMethod(uri, propSet, DavConstants.DEPTH_1);
        int status = client.executeMethod(query);

        // check and process response
        boolean isSuccess = (status == HttpStatus.SC_MULTI_STATUS || status == HttpStatus.SC_OK);

        if (isSuccess) {
            // get data from remote folder
            MultiStatus dataInServer = query.getResponseBodyAsMultiStatus();
            readData(dataInServer, client);

            // Result of the operation
            result = new RemoteOperationResult(true, query);
            // Add data to the result
            if (result.isSuccess()) {
                result.setData(versions);
            }
        } else {
            // synchronization failed
            client.exhaustResponse(query.getResponseBodyAsStream());
            result = new RemoteOperationResult(false, query);
        }
    } catch (Exception e) {
        result = new RemoteOperationResult(e);
    } finally {
        if (query != null)
            query.releaseConnection(); // let the connection available for other methods

        if (result == null) {
            result = new RemoteOperationResult(new Exception("unknown error"));
            Log_OC.e(TAG, "Synchronized file with id " + fileId + ": failed");
        } else {
            if (result.isSuccess()) {
                Log_OC.i(TAG, "Synchronized file with id " + fileId + ": " + result.getLogMessage());
            } else {
                if (result.isException()) {
                    Log_OC.e(TAG, "Synchronized with id " + fileId + ": " + result.getLogMessage(),
                            result.getException());
                } else {
                    Log_OC.w(TAG, "Synchronized with id " + fileId + ": " + result.getLogMessage());
                }
            }
        }
    }

    return result;
}

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

/**
 * Performs the read operation.// w ww .java  2 s.c  o  m
 *
 * @param client Client object to communicate with the remote ownCloud server.
 */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;
    PropFindMethod query = null;

    try {
        // remote request
        if (userId.isEmpty()) {
            throw new IllegalArgumentException("UserId may not be empty!");
        }

        String baseUri = client.getNewWebdavUri() + "/trashbin/" + userId + "/trash/";
        DavPropertyNameSet propSet = WebdavUtils.getTrashbinPropSet();

        query = new PropFindMethod(baseUri + WebdavUtils.encodePath(remotePath), propSet, DavConstants.DEPTH_1);
        int status = client.executeMethod(query);

        // check and process response
        boolean isSuccess = (status == HttpStatus.SC_MULTI_STATUS || status == HttpStatus.SC_OK);

        if (isSuccess) {
            // get data from remote folder
            MultiStatus dataInServer = query.getResponseBodyAsMultiStatus();
            readData(dataInServer, client);

            // Result of the operation
            result = new RemoteOperationResult(true, query);
            // Add data to the result
            if (result.isSuccess()) {
                result.setData(folderAndFiles);
            }
        } else {
            // synchronization failed
            client.exhaustResponse(query.getResponseBodyAsStream());
            result = new RemoteOperationResult(false, query);
        }
    } catch (Exception e) {
        result = new RemoteOperationResult(e);
    } finally {
        if (query != null)
            query.releaseConnection(); // let the connection available for other methods

        if (result == null) {
            result = new RemoteOperationResult(new Exception("unknown error"));
            Log_OC.e(TAG, "Synchronized " + remotePath + ": failed");
        } else {
            if (result.isSuccess()) {
                Log_OC.i(TAG, "Synchronized " + remotePath + ": " + result.getLogMessage());
            } else {
                if (result.isException()) {
                    Log_OC.e(TAG, "Synchronized " + remotePath + ": " + result.getLogMessage(),
                            result.getException());
                } else {
                    Log_OC.e(TAG, "Synchronized " + remotePath + ": " + result.getLogMessage());
                }
            }
        }
    }

    return result;
}

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

/**
 * Performs the rename operation./*from   w  ww .j  a v  a  2s .co m*/
 *
 * @param   client      Client object to communicate with the remote ownCloud server.
 */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {

    /// check parameters
    if (!FileUtils.isValidPath(mTargetRemotePath)) {
        return new RemoteOperationResult(ResultCode.INVALID_CHARACTER_IN_NAME);
    }

    if (mTargetRemotePath.equals(mSrcRemotePath)) {
        // nothing to do!
        return new RemoteOperationResult(ResultCode.OK);
    }

    if (mTargetRemotePath.startsWith(mSrcRemotePath)) {
        return new RemoteOperationResult(ResultCode.INVALID_MOVE_INTO_DESCENDANT);
    }

    /// perform remote operation
    //LocalMoveMethod move = null;
    MoveMethod move = null;
    RemoteOperationResult result = null;
    try {
        move = new MoveMethod(client.getWebdavUri() + WebdavUtils.encodePath(mSrcRemotePath),
                client.getWebdavUri() + WebdavUtils.encodePath(mTargetRemotePath), mOverwrite);
        int status = client.executeMethod(move, MOVE_READ_TIMEOUT, MOVE_CONNECTION_TIMEOUT);

        /// process response
        if (status == HttpStatus.SC_MULTI_STATUS) {
            result = processPartialError(move);

        } else if (status == HttpStatus.SC_PRECONDITION_FAILED && !mOverwrite) {

            result = new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
            client.exhaustResponse(move.getResponseBodyAsStream());

            /// for other errors that could be explicitly handled, check first:
            /// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.9.4

        } else {

            result = new RemoteOperationResult(isSuccess(status), // move.succeeded()? trustful?
                    status, move.getResponseHeaders());
            client.exhaustResponse(move.getResponseBodyAsStream());
        }

        Log.i(TAG, "Move " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage());

    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log.e(TAG, "Move " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage(), e);

    } finally {
        if (move != null)
            move.releaseConnection();
    }

    return result;
}

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

/**
 * Performs the rename operation./*from   w w w .ja v  a 2 s  . c o  m*/
 *
 * @param client Client object to communicate with the remote ownCloud server.
 */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {

    OwnCloudVersion version = client.getOwnCloudVersion();
    boolean versionWithForbiddenChars = (version != null && version.isVersionWithForbiddenCharacters());

    /// check parameters
    if (!FileUtils.isValidPath(mTargetRemotePath, versionWithForbiddenChars)) {
        return new RemoteOperationResult(ResultCode.INVALID_CHARACTER_IN_NAME);
    }

    if (mTargetRemotePath.equals(mSrcRemotePath)) {
        // nothing to do!
        return new RemoteOperationResult(ResultCode.OK);
    }

    if (mTargetRemotePath.startsWith(mSrcRemotePath)) {
        return new RemoteOperationResult(ResultCode.INVALID_COPY_INTO_DESCENDANT);
    }

    /// perform remote operation
    CopyMethod copyMethod = null;
    RemoteOperationResult result = null;
    try {
        copyMethod = new CopyMethod(client.getWebdavUri() + WebdavUtils.encodePath(mSrcRemotePath),
                client.getWebdavUri() + WebdavUtils.encodePath(mTargetRemotePath), mOverwrite);
        int status = client.executeMethod(copyMethod, COPY_READ_TIMEOUT, COPY_CONNECTION_TIMEOUT);

        /// process response
        if (status == HttpStatus.SC_MULTI_STATUS) {
            result = processPartialError(copyMethod);

        } else if (status == HttpStatus.SC_PRECONDITION_FAILED && !mOverwrite) {

            result = new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
            client.exhaustResponse(copyMethod.getResponseBodyAsStream());

            /// for other errors that could be explicitly handled, check first:
            /// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.9.4

        } else if (status == 400) {
            result = new RemoteOperationResult(copyMethod.succeeded(), copyMethod.getResponseBodyAsString(),
                    status);
        } else {
            result = new RemoteOperationResult(isSuccess(status), // copy.succeeded()? trustful?
                    status, copyMethod.getResponseHeaders());
            client.exhaustResponse(copyMethod.getResponseBodyAsStream());
        }

        Log.i(TAG, "Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage());

    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log.e(TAG, "Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage(), e);

    } finally {
        if (copyMethod != null)
            copyMethod.releaseConnection();
    }

    return result;
}

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

/**
 * Performs the rename operation./*from w  w w  . jav  a 2 s  .co  m*/
 * 
 * @param   client      Client object to communicate with the remote ownCloud server.
 */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {

    OwnCloudVersion version = client.getOwnCloudVersion();
    boolean versionWithForbiddenChars = (version != null && version.isVersionWithForbiddenCharacters());

    /// check parameters
    if (!FileUtils.isValidPath(mTargetRemotePath, versionWithForbiddenChars)) {
        return new RemoteOperationResult(ResultCode.INVALID_CHARACTER_IN_NAME);
    }

    if (mTargetRemotePath.equals(mSrcRemotePath)) {
        // nothing to do!
        return new RemoteOperationResult(ResultCode.OK);
    }

    if (mTargetRemotePath.startsWith(mSrcRemotePath)) {
        return new RemoteOperationResult(ResultCode.INVALID_MOVE_INTO_DESCENDANT);
    }

    /// perform remote operation
    //LocalMoveMethod move = null;
    MoveMethod move = null;
    RemoteOperationResult result = null;
    try {
        move = new MoveMethod(client.getWebdavUri() + WebdavUtils.encodePath(mSrcRemotePath),
                client.getWebdavUri() + WebdavUtils.encodePath(mTargetRemotePath), mOverwrite);
        int status = client.executeMethod(move, MOVE_READ_TIMEOUT, MOVE_CONNECTION_TIMEOUT);

        /// process response
        if (status == HttpStatus.SC_MULTI_STATUS) {
            result = processPartialError(move);

        } else if (status == HttpStatus.SC_PRECONDITION_FAILED && !mOverwrite) {

            result = new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
            client.exhaustResponse(move.getResponseBodyAsStream());

            /// for other errors that could be explicitly handled, check first:
            /// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.9.4

        } else if (status == 400) {
            result = new RemoteOperationResult(move.succeeded(), move.getResponseBodyAsString(), status);
        } else {
            result = new RemoteOperationResult(isSuccess(status), // move.succeeded()? trustful?
                    status, move.getResponseHeaders());
            client.exhaustResponse(move.getResponseBodyAsStream());
        }

        Log.i(TAG, "Move " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage());

    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log.e(TAG, "Move " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage(), e);

    } finally {
        if (move != null)
            move.releaseConnection();
    }

    return result;
}

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

public boolean isMultiStatus(int status) {
    return (status == HttpStatus.SC_MULTI_STATUS);
}