Search in sources :

Example 6 with DeleteMethod

use of org.apache.jackrabbit.webdav.client.methods.DeleteMethod in project lobcder by skoulouzis.

the class WebDAVTest method testMove.

@Test
public void testMove() throws HttpException, IOException, DavException, URISyntaxException {
    System.out.println("testMove");
    String testcol = root + "testResourceFortMove/";
    String testuri = testcol + "movetest";
    String destinationuri = testuri + "2";
    String destinationpath = new URI(destinationuri).getRawPath();
    // make sure the scheme is removed
    assertFalse(destinationpath.contains(":"));
    int status;
    try {
        // Make sure the testcol is deleted
        DeleteMethod del = new DeleteMethod(testcol);
        status = client.executeMethod(del);
        assertTrue("status: " + status, status == HttpStatus.SC_OK || status == HttpStatus.SC_NO_CONTENT || status == HttpStatus.SC_NOT_FOUND);
        // We can't create a resource if its parent does not exist.
        // When the PUT operation creates a new non-collection resource all
        // ancestors MUST already exist. If all ancestors do not exist, the
        // method MUST fail with a 409 (Conflict) status code. For example,
        // if resource /a/b/c/d.html is to be created and /a/b/c/ does not
        // exist, then the request must fail.
        // http://www.webdav.org/specs/rfc2518.html#rfc.section.8.7.2
        // In our case (milton API) will create the
        PutMethod put = new PutMethod(testuri);
        status = client.executeMethod(put);
        // assertTrue("status: " + status, status == HttpStatus.SC_CONFLICT);
        assertTrue("status: " + status, status == HttpStatus.SC_CREATED);
        // The collection is created from last step, although it shouldn't
        // MkColMethod mkCol = new MkColMethod(testcol);
        // status = client.executeMethod(mkCol);
        // assertTrue("status: " + status, status == HttpStatus.SC_CREATED);
        put = new PutMethod(testuri);
        status = client.executeMethod(put);
        assertTrue("status: " + status, status == HttpStatus.SC_CREATED);
        MoveMethod moveNormal = new MoveMethod(testuri, destinationpath, true);
        status = client.executeMethod(moveNormal);
        assertTrue("status: " + status, status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT);
        HeadMethod head = new HeadMethod(destinationuri);
        status = client.executeMethod(head);
        // We get back HttpStatus.SC_NO_CONTENT
        assertTrue("status: " + status, status == HttpStatus.SC_OK || status == HttpStatus.SC_NO_CONTENT);
        head = new HeadMethod(testuri);
        status = client.executeMethod(head);
        assertTrue("status: " + status, status == HttpStatus.SC_NOT_FOUND);
    } finally {
        utils.deleteResource(testcol, false);
    }
}
Also used : DeleteMethod(org.apache.jackrabbit.webdav.client.methods.DeleteMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) URI(java.net.URI) Test(org.junit.Test)

Example 7 with DeleteMethod

use of org.apache.jackrabbit.webdav.client.methods.DeleteMethod in project lobcder by skoulouzis.

the class WebDAVTest method testGetSetDescription.

@Test
public void testGetSetDescription() throws UnsupportedEncodingException, IOException, DavException {
    System.out.println("testGetSetCustomComment");
    String testcol1 = root + "testResourceIdForGetSetDescription/";
    String testuri1 = testcol1 + "file1";
    try {
        DeleteMethod delete = new DeleteMethod(testcol1);
        int status = client.executeMethod(delete);
        MkColMethod mkcol = new MkColMethod(testcol1);
        status = client.executeMethod(mkcol);
        assertEquals(HttpStatus.SC_CREATED, status);
        PutMethod put = new PutMethod(testuri1);
        put.setRequestEntity(new StringRequestEntity("foo", "text/plain", "UTF-8"));
        status = client.executeMethod(put);
        assertEquals(HttpStatus.SC_CREATED, status);
        DavPropertyNameSet commentNameSet = new DavPropertyNameSet();
        DavPropertyName commentName = DavPropertyName.create("description", Namespace.getNamespace("custom:"));
        commentNameSet.add(commentName);
        PropFindMethod propFind = new PropFindMethod(testuri1, commentNameSet, DavConstants.DEPTH_INFINITY);
        status = client.executeMethod(propFind);
        assertEquals(HttpStatus.SC_MULTI_STATUS, status);
        MultiStatus multiStatus = propFind.getResponseBodyAsMultiStatus();
        MultiStatusResponse[] responses = multiStatus.getResponses();
        for (MultiStatusResponse r : responses) {
            // System.out.println("Response: " + r.getHref());
            DavPropertySet allProp = utils.getProperties(r);
            DavPropertyIterator iter = allProp.iterator();
            while (iter.hasNext()) {
                DavProperty<?> p = iter.nextProperty();
                // System.out.println(p.getName() + " : " + p.getValue());
                assertEquals(p.getName(), commentName);
            }
        }
        DavPropertySet descriptionSet = new DavPropertySet();
        String description = "this is a comment for a file, blah blah aaaaa";
        DavProperty<String> driProp = new DefaultDavProperty<String>(commentName, description);
        descriptionSet.add(driProp);
        PropPatchMethod proPatch = new PropPatchMethod(testuri1, descriptionSet, commentNameSet);
        status = client.executeMethod(proPatch);
        assertEquals(HttpStatus.SC_MULTI_STATUS, status);
        propFind = new PropFindMethod(testuri1, commentNameSet, DavConstants.DEPTH_INFINITY);
        status = client.executeMethod(propFind);
        assertEquals(HttpStatus.SC_MULTI_STATUS, status);
        multiStatus = propFind.getResponseBodyAsMultiStatus();
        responses = multiStatus.getResponses();
        for (MultiStatusResponse r : responses) {
            System.out.println("Response: " + r.getHref());
            DavPropertySet allProp = utils.getProperties(r);
            DavPropertyIterator iter = allProp.iterator();
            while (iter.hasNext()) {
                DavProperty<?> p = iter.nextProperty();
                assertEquals(p.getName(), commentName);
                System.out.println(p.getName() + " : " + p.getValue());
                assertNotNull(p.getValue());
                if (new URL(testcol1).getPath().equals(r.getHref())) {
                    String val = p.getValue().toString();
                    assertEquals(description, val);
                }
            }
        }
    } finally {
        utils.deleteResource(testcol1, true);
    }
}
Also used : DeleteMethod(org.apache.jackrabbit.webdav.client.methods.DeleteMethod) URL(java.net.URL) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Test(org.junit.Test)

Example 8 with DeleteMethod

use of org.apache.jackrabbit.webdav.client.methods.DeleteMethod in project android by nextcloud.

the class RemoveRemoteEncryptedFileOperation method run.

/**
 * Performs the remove operation.
 */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result;
    DeleteMethod delete = null;
    String token = null;
    DecryptedFolderMetadata metadata;
    String privateKey = arbitraryDataProvider.getValue(account.name, EncryptionUtils.PRIVATE_KEY);
    try {
        // Lock folder
        LockFileOperation lockFileOperation = new LockFileOperation(parentId);
        RemoteOperationResult lockFileOperationResult = lockFileOperation.execute(client, true);
        if (lockFileOperationResult.isSuccess()) {
            token = (String) lockFileOperationResult.getData().get(0);
        } else if (lockFileOperationResult.getHttpCode() == HttpStatus.SC_FORBIDDEN) {
            throw new RemoteOperationFailedException("Forbidden! Please try again later.)");
        } else {
            throw new RemoteOperationFailedException("Unknown error!");
        }
        // refresh metadata
        GetMetadataOperation getMetadataOperation = new GetMetadataOperation(parentId);
        RemoteOperationResult getMetadataOperationResult = getMetadataOperation.execute(client, true);
        if (getMetadataOperationResult.isSuccess()) {
            // decrypt metadata
            String serializedEncryptedMetadata = (String) getMetadataOperationResult.getData().get(0);
            EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.deserializeJSON(serializedEncryptedMetadata, new TypeToken<EncryptedFolderMetadata>() {
            });
            metadata = EncryptionUtils.decryptFolderMetaData(encryptedFolderMetadata, privateKey);
        } else {
            throw new RemoteOperationFailedException("No Metadata found!");
        }
        // delete file remote
        delete = new DeleteMethod(client.getWebdavUri() + WebdavUtils.encodePath(remotePath));
        int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT);
        // exhaust the response, although not interesting
        delete.getResponseBodyAsString();
        result = new RemoteOperationResult((delete.succeeded() || status == HttpStatus.SC_NOT_FOUND), delete);
        Log_OC.i(TAG, "Remove " + remotePath + ": " + result.getLogMessage());
        // remove file from metadata
        metadata.getFiles().remove(fileName);
        EncryptedFolderMetadata encryptedFolderMetadata = EncryptionUtils.encryptFolderMetadata(metadata, privateKey);
        String serializedFolderMetadata = EncryptionUtils.serializeJSON(encryptedFolderMetadata);
        // upload metadata
        UpdateMetadataOperation storeMetadataOperation = new UpdateMetadataOperation(parentId, serializedFolderMetadata, token);
        RemoteOperationResult uploadMetadataOperationResult = storeMetadataOperation.execute(client, true);
        if (!uploadMetadataOperationResult.isSuccess()) {
            throw new RemoteOperationFailedException("Metadata not uploaded!");
        }
        // return success
        return result;
    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG, "Remove " + remotePath + ": " + result.getLogMessage(), e);
    } finally {
        if (delete != null) {
            delete.releaseConnection();
        }
        // unlock file
        if (token != null) {
            UnlockFileOperation unlockFileOperation = new UnlockFileOperation(parentId, token);
            RemoteOperationResult unlockFileOperationResult = unlockFileOperation.execute(client, true);
            if (!unlockFileOperationResult.isSuccess()) {
                Log_OC.e(TAG, "Failed to unlock " + parentId);
            }
        }
    }
    return result;
}
Also used : DeleteMethod(org.apache.jackrabbit.webdav.client.methods.DeleteMethod) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UpdateMetadataOperation(com.owncloud.android.lib.resources.files.UpdateMetadataOperation) LockFileOperation(com.owncloud.android.lib.resources.files.LockFileOperation) GetMetadataOperation(com.owncloud.android.lib.resources.files.GetMetadataOperation) UnlockFileOperation(com.owncloud.android.lib.resources.files.UnlockFileOperation) EncryptedFolderMetadata(com.owncloud.android.datamodel.EncryptedFolderMetadata) DecryptedFolderMetadata(com.owncloud.android.datamodel.DecryptedFolderMetadata)

Example 9 with DeleteMethod

use of org.apache.jackrabbit.webdav.client.methods.DeleteMethod in project lobcder by skoulouzis.

the class TestWebWAVFS method testPROPFIND_PUT_PROPFIND_GET_PUT.

@Test
public void testPROPFIND_PUT_PROPFIND_GET_PUT() throws IOException, DavException {
    System.err.println("testPROPFIND_PUT_PROPFIND_GET_PUT");
    // Make sure it's deleted
    String testFileURI1 = uri.toASCIIString() + TestSettings.TEST_FILE_NAME1 + ".txt";
    DeleteMethod del = new DeleteMethod(testFileURI1);
    int status = client1.executeMethod(del);
    assertTrue(status == HttpStatus.SC_OK || status == HttpStatus.SC_NO_CONTENT || status == HttpStatus.SC_NOT_FOUND);
    try {
        // PROPFIND file is not there
        testFileURI1 = uri.toASCIIString() + TestSettings.TEST_FILE_NAME1 + ".txt";
        PropFindMethod propFind = new PropFindMethod(testFileURI1, DavConstants.PROPFIND_ALL_PROP_INCLUDE, DavConstants.DEPTH_0);
        status = client1.executeMethod(propFind);
        assertEquals(HttpStatus.SC_NOT_FOUND, status);
        // PUT create an empty file
        PutMethod put = new PutMethod(testFileURI1);
        put.setRequestEntity(new StringRequestEntity("\n", "text/plain", "UTF-8"));
        status = client1.executeMethod(put);
        assertEquals(HttpStatus.SC_CREATED, status);
        // PROPFIND get proerties
        propFind = new PropFindMethod(testFileURI1, DavConstants.PROPFIND_ALL_PROP_INCLUDE, DavConstants.DEPTH_0);
        status = client1.executeMethod(propFind);
        assertEquals(HttpStatus.SC_MULTI_STATUS, status);
        MultiStatus multiStatus = propFind.getResponseBodyAsMultiStatus();
        MultiStatusResponse[] responses = multiStatus.getResponses();
        DavPropertySet allProp = utils.getProperties(responses[0]);
        // DavPropertyIterator iter = allProp.iterator();
        // while (iter.hasNext()) {
        // DavProperty<?> p = iter.nextProperty();
        // System.out.println("P: " + p.getName() + " " + p.getValue());
        // }
        String isCollStr = (String) allProp.get(DavPropertyName.ISCOLLECTION).getValue();
        Boolean isCollection = Boolean.getBoolean(isCollStr);
        assertFalse(isCollection);
        String lenStr = (String) allProp.get(DavPropertyName.GETCONTENTLENGTH).getValue();
        assertEquals(Long.valueOf(lenStr), Long.valueOf("\n".length()));
        String contentType = (String) allProp.get(DavPropertyName.GETCONTENTTYPE).getValue();
        assertEquals("text/plain; charset=UTF-8", contentType);
        // GET the file
        GetMethod get = new GetMethod(testFileURI1);
        status = client1.executeMethod(get);
        assertEquals(HttpStatus.SC_OK, status);
        assertEquals("\n", get.getResponseBodyAsString());
        // PUT
        put = new PutMethod(testFileURI1);
        String content = get.getResponseBodyAsString() + TestSettings.TEST_DATA;
        put.setRequestEntity(new StringRequestEntity(content, "text/plain", "UTF-8"));
        status = client1.executeMethod(put);
        assertEquals(HttpStatus.SC_CREATED, status);
        get = new GetMethod(testFileURI1);
        status = client1.executeMethod(get);
        assertEquals(HttpStatus.SC_OK, status);
        assertEquals(content, get.getResponseBodyAsString());
        put = new PutMethod(testFileURI1);
        content = get.getResponseBodyAsString() + TestSettings.TEST_DATA;
        put.setRequestEntity(new StringRequestEntity(content, "text/plain", "UTF-8"));
        status = client1.executeMethod(put);
        assertEquals(HttpStatus.SC_CREATED, status);
        get = new GetMethod(testFileURI1);
        status = client1.executeMethod(get);
        assertEquals(HttpStatus.SC_OK, status);
        assertEquals(content, get.getResponseBodyAsString());
    } finally {
        utils.deleteResource(testFileURI1, false);
    }
}
Also used : DeleteMethod(org.apache.jackrabbit.webdav.client.methods.DeleteMethod) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) DavPropertySet(org.apache.jackrabbit.webdav.property.DavPropertySet) PropFindMethod(org.apache.jackrabbit.webdav.client.methods.PropFindMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.jackrabbit.webdav.client.methods.PutMethod) Test(org.junit.Test)

Example 10 with DeleteMethod

use of org.apache.jackrabbit.webdav.client.methods.DeleteMethod in project lobcder by skoulouzis.

the class TestWebWAVFS method testCreateAndDeleteFile.

@Test
public void testCreateAndDeleteFile() throws IOException, DavException {
    System.err.println("testCreateAndDeleteFile");
    // Make sure it's deleted
    String testFileURI1 = uri.toASCIIString() + TestSettings.TEST_FILE_NAME1;
    DeleteMethod del = new DeleteMethod(testFileURI1);
    int status = client1.executeMethod(del);
    testFileURI1 = uri.toASCIIString() + TestSettings.TEST_FILE_NAME1;
    PutMethod put = new PutMethod(testFileURI1);
    put.setRequestEntity(new StringRequestEntity(TestSettings.TEST_DATA, "text/plain", "UTF-8"));
    status = client1.executeMethod(put);
    assertEquals(HttpStatus.SC_CREATED, status);
    String testFileURI2 = uri.toASCIIString() + TestSettings.TEST_TXT_FILE_NAME;
    put = new PutMethod(testFileURI2);
    put.setRequestEntity(new StringRequestEntity(TestSettings.TEST_DATA, "text/plain", "UTF-8"));
    status = client1.executeMethod(put);
    assertEquals(HttpStatus.SC_CREATED, status);
    utils.deleteResource(testFileURI1, true);
    utils.deleteResource(testFileURI2, true);
}
Also used : DeleteMethod(org.apache.jackrabbit.webdav.client.methods.DeleteMethod) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PutMethod(org.apache.jackrabbit.webdav.client.methods.PutMethod) Test(org.junit.Test)

Aggregations

DeleteMethod (org.apache.jackrabbit.webdav.client.methods.DeleteMethod)12 Test (org.junit.Test)10 PutMethod (org.apache.commons.httpclient.methods.PutMethod)7 URL (java.net.URL)4 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)3 PutMethod (org.apache.jackrabbit.webdav.client.methods.PutMethod)3 URI (java.net.URI)2 DecryptedFolderMetadata (com.owncloud.android.datamodel.DecryptedFolderMetadata)1 EncryptedFolderMetadata (com.owncloud.android.datamodel.EncryptedFolderMetadata)1 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)1 GetMetadataOperation (com.owncloud.android.lib.resources.files.GetMetadataOperation)1 LockFileOperation (com.owncloud.android.lib.resources.files.LockFileOperation)1 UnlockFileOperation (com.owncloud.android.lib.resources.files.UnlockFileOperation)1 UpdateMetadataOperation (com.owncloud.android.lib.resources.files.UpdateMetadataOperation)1 Header (org.apache.commons.httpclient.Header)1 GetMethod (org.apache.commons.httpclient.methods.GetMethod)1 AclMethod (org.apache.jackrabbit.webdav.client.methods.AclMethod)1 PropFindMethod (org.apache.jackrabbit.webdav.client.methods.PropFindMethod)1 LockInfo (org.apache.jackrabbit.webdav.lock.LockInfo)1 DavPropertySet (org.apache.jackrabbit.webdav.property.DavPropertySet)1