Search in sources :

Example 1 with PutMethod

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

the class TestWebWAVFS method testUploadFileOnRootWithoutAdminRole.

@Test
public void testUploadFileOnRootWithoutAdminRole() throws IOException, DavException {
    System.err.println("testUploadFileOnRootWithoutAdminRole");
    String uname = prop.getProperty(("webdav.test.non.admin.username1"), "nonAdmin");
    assertNotNull(uname);
    String pass = prop.getProperty(("webdav.test.non.admin.password1"), "secret");
    assertNotNull(pass);
    HttpClient client = new HttpClient();
    assertNotNull(uri.getHost());
    assertNotNull(uri.getPort());
    assertNotNull(client);
    client.getState().setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(uname, pass));
    String testFileURI1 = this.uri.toASCIIString() + TestSettings.TEST_FILE_NAME1 + ".txt";
    PutMethod put = new PutMethod(testFileURI1);
    put.setRequestEntity(new StringRequestEntity(TestSettings.TEST_DATA, "text/plain", "UTF-8"));
    int status = client.executeMethod(put);
    assertEquals(HttpStatus.SC_UNAUTHORIZED, status);
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.commons.httpclient.HttpClient) AuthScope(org.apache.commons.httpclient.auth.AuthScope) PutMethod(org.apache.jackrabbit.webdav.client.methods.PutMethod) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Test(org.junit.Test)

Example 2 with PutMethod

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

the class WebDAVSecurityTest method testAclMethod.

// @Test
// public void testGetCurrentUserPrivilegeSet() throws UnsupportedEncodingException, IOException, DavException {
// String testFileURI1 = uri.toASCIIString() + TestSettings.TEST_FILE_NAME1 + ".txt";
// try {
// 
// DeleteMethod delete = new DeleteMethod(testFileURI1);
// int status = client1.executeMethod(delete);
// 
// 
// 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);
// 
// DavPropertyNameSet d = new DavPropertyNameSet();
// DavPropertyName userPriv = DavPropertyName.create("current-user-privilege-set");
// d.add(userPriv);
// 
// PropFindMethod propFind = new PropFindMethod(testFileURI1, d, DavConstants.DEPTH_INFINITY);
// status = client1.executeMethod(propFind);
// assertEquals(HttpStatus.SC_MULTI_STATUS, status);
// 
// 
// MultiStatus multiStatus = propFind.getResponseBodyAsMultiStatus();
// MultiStatusResponse[] responses = multiStatus.getResponses();
// 
// for (MultiStatusResponse r : responses) {
// System.out.println("Responce: " + r.getHref());
// DavPropertySet allProp = getProperties(r);
// 
// DavPropertyIterator iter = allProp.iterator();
// while (iter.hasNext()) {
// DavProperty<?> p = iter.nextProperty();
// System.out.println("\tName: " + p.getName() + " Values " + p.getValue());
// }
// }
// 
// assertEquals(HttpStatus.SC_OK, responses[0].getStatus()[0].getStatusCode());
// DavPropertySet allProp = getProperties(responses[0]);
// DavProperty<?> prop = allProp.get(userPriv);
// assertEquals(userPriv, prop.getName());
// 
// //             ArrayList<org.apache.xerces.dom.DeferredElementNSImpl> value =  (ArrayList<org.apache.xerces.dom.DeferredElementNSImpl>) prop.getValue();
// //             for(org.apache.xerces.dom.DeferredElementNSImpl el : value){
// //                 System.out.println("DeferredElementNSImpl: "+el.getBaseURI());
// //                 System.out.println("getLocalName: "+el.getLocalName());
// //                 System.out.println("getNodeValue: "+el.getNodeValue());
// //             }
// //            String value = (String) prop.getValue();
// //            assertTrue(value.contains("READ") || value.contains("ALL"));
// System.out.println("Name: " + prop.getName() + " Values " + prop.getValue() + " class: " + prop.getValue().getClass().getName() + " " + prop.getValue().getClass().getName());
// } finally {
// DeleteMethod delete = new DeleteMethod(testFileURI1);
// int status = client1.executeMethod(delete);
// assertTrue("DeleteMethod status: " + status, status == HttpStatus.SC_OK || status == HttpStatus.SC_NO_CONTENT);
// }
// }
// 
// @Test
// public void testUnothorizedCreateFile() {
// try {
// String testFileURI1 = uri.toASCIIString() + TestSettings.TEST_FILE_NAME1 + ".txt";
// PutMethod put = new PutMethod(testFileURI1);
// put.setRequestEntity(new StringRequestEntity("foo", "text/plain", "UTF-8"));
// 
// client2.getState().setCredentials(
// new AuthScope(uri.getHost(), uri.getPort()),
// new UsernamePasswordCredentials(username2, "WRONG_PASSWORD"));
// 
// int status = client2.executeMethod(put);
// assertEquals(HttpStatus.SC_UNAUTHORIZED, status);
// 
// } catch (IOException ex) {
// Logger.getLogger(WebDAVSecurityTest.class.getName()).log(Level.SEVERE, null, ex);
// }
// }
// 
// @Test
// public void testGetSetACL() throws IOException, DavException {
// String testFileURI1 = uri.toASCIIString() + TestSettings.TEST_FILE_NAME1;
// DeleteMethod del = new DeleteMethod(testFileURI1);
// client1.executeMethod(del);
// 
// PutMethod put = new PutMethod(testFileURI1);
// put.setRequestEntity(new StringRequestEntity(TestSettings.TEST_DATA, "text/plain", "UTF-8"));
// int status = client1.executeMethod(put);
// assertEquals(HttpStatus.SC_CREATED, status);
// 
// DavPropertyNameSet d = new DavPropertyNameSet();
// d.add(SecurityConstants.ACL);
// PropFindMethod propFind = new PropFindMethod(testFileURI1, d, DavConstants.DEPTH_INFINITY);
// 
// status = client1.executeMethod(propFind);
// assertEquals(HttpStatus.SC_MULTI_STATUS, status);
// 
// 
// MultiStatus multiStatus = propFind.getResponseBodyAsMultiStatus();
// MultiStatusResponse[] responses = multiStatus.getResponses();
// 
// for (MultiStatusResponse r : responses) {
// System.out.println("Responce: " + r.getHref());
// DavPropertySet allProp = getProperties(r);
// 
// DavPropertyIterator iter = allProp.iterator();
// while (iter.hasNext()) {
// DavProperty<?> p = iter.nextProperty();
// //                assertNotNull(p.getValue());
// System.out.println("\tName: " + p.getName() + " Values " + p.getValue());
// }
// 
// }
// }
@Test
public void testAclMethod() throws IOException, DavException {
    String testFileURI1 = uri.toASCIIString() + TestSettings.TEST_FILE_NAME1;
    DeleteMethod del = new DeleteMethod(testFileURI1);
    client1.executeMethod(del);
    PutMethod put = new PutMethod(testFileURI1);
    put.setRequestEntity(new StringRequestEntity(TestSettings.TEST_DATA, "text/plain", "UTF-8"));
    int status = client1.executeMethod(put);
    assertEquals(HttpStatus.SC_CREATED, status);
    Privilege[] privs = new Privilege[] { Privilege.PRIVILEGE_WRITE, // /Privilege.getPrivilege("schedule-query-freebusy", SecurityConstants.NAMESPACE)
    Privilege.PRIVILEGE_READ };
    Principal principal = Principal.getHrefPrincipal(username1);
    Ace ace1 = AclProperty.createGrantAce(principal, privs, false, false, null);
    Principal principal2 = Principal.getHrefPrincipal("someUser");
    Ace ace2 = AclProperty.createDenyAce(principal2, privs, true, true, null);
    AclMethod acl = new AclMethod(testFileURI1, new AclProperty(new Ace[] { ace1, ace2 }));
    Header[] headers = acl.getRequestHeaders();
    for (Header h : headers) {
        System.out.println("getRequestHeaders: " + h.getName() + " : " + h.getValue());
    }
    status = client1.executeMethod(acl);
// assertEquals(HttpStatus.SC_OK, status);
// DavPropertyNameSet d = new DavPropertyNameSet();
// d.add(SecurityConstants.ACL);
// //        d.add(SecurityConstants.ACL_RESTRICTIONS);
// //        d.add(SecurityConstants.ALTERNATE_URI_SET);
// //        d.add(SecurityConstants.CURRENT_USER_PRIVILEGE_SET);
// //        d.add(SecurityConstants.GROUP);
// //        d.add(SecurityConstants.GROUP_MEMBERSHIP);
// //        d.add(SecurityConstants.GROUP_MEMBER_SET);
// //        d.add(SecurityConstants.INHERITED_ACL_SET);
// //        d.add(SecurityConstants.OWNER);
// //        d.add(SecurityConstants.PRINCIPAL_COLLECTION_SET);
// //        d.add(SecurityConstants.PRINCIPAL_URL);
// //        d.add(SecurityConstants.SUPPORTED_PRIVILEGE_SET);
// PropFindMethod propFind = new PropFindMethod(testFileURI1, d, DavConstants.DEPTH_INFINITY);
// 
// status = client1.executeMethod(propFind);
// assertEquals(HttpStatus.SC_MULTI_STATUS, status);
// 
// 
// MultiStatus multiStatus = propFind.getResponseBodyAsMultiStatus();
// MultiStatusResponse[] responses = multiStatus.getResponses();
// 
// for (MultiStatusResponse r : responses) {
// System.out.println("Responce: " + r.getHref());
// DavPropertySet allProp = getProperties(r);
// 
// DavPropertyIterator iter = allProp.iterator();
// while (iter.hasNext()) {
// DavProperty<?> p = iter.nextProperty();
// assertNotNull(p.getValue());
// System.out.println("\tName: " + p.getName() + " Values " + p.getValue());
// }
// }
}
Also used : DeleteMethod(org.apache.jackrabbit.webdav.client.methods.DeleteMethod) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) Ace(org.apache.jackrabbit.webdav.security.AclProperty.Ace) AclMethod(org.apache.jackrabbit.webdav.client.methods.AclMethod) Header(org.apache.commons.httpclient.Header) PutMethod(org.apache.jackrabbit.webdav.client.methods.PutMethod) Test(org.junit.Test)

Example 3 with PutMethod

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

the class TestWebWAVFS method testCopy.

@Test
public void testCopy() throws VlException, IOException {
    System.err.println("testCopy");
    String testFileURI1 = this.uri.toASCIIString() + TestSettings.TEST_FILE_NAME1 + ".txt";
    String testcol = root + "testResourceId/";
    try {
        PutMethod put = new PutMethod(testFileURI1);
        put.setRequestEntity(new StringRequestEntity(TestSettings.TEST_DATA, "text/plain", "UTF-8"));
        int status = client1.executeMethod(put);
        assertTrue(status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED);
        GetMethod get = new GetMethod(testFileURI1);
        status = client1.executeMethod(get);
        assertEquals(HttpStatus.SC_OK, status);
        String cont = get.getResponseBodyAsString();
        assertEquals(TestSettings.TEST_DATA, cont);
        MkColMethod mkcol = new MkColMethod(testcol);
        status = client1.executeMethod(mkcol);
        assertEquals(HttpStatus.SC_CREATED, status);
        String targetURL = testcol + TestSettings.TEST_FILE_NAME1 + ".txt";
        CopyMethod cp = new CopyMethod(testFileURI1, targetURL, true);
        status = client1.executeMethod(cp);
        assertTrue(status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED);
        get = new GetMethod(targetURL);
        status = client1.executeMethod(get);
        assertEquals(HttpStatus.SC_OK, status);
        cont = get.getResponseBodyAsString();
        System.out.println(cont);
        assertEquals(TestSettings.TEST_DATA, cont);
    } finally {
        utils.deleteResource(testcol, false);
        utils.deleteResource(testFileURI1, false);
    }
}
Also used : MkColMethod(org.apache.jackrabbit.webdav.client.methods.MkColMethod) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) CopyMethod(org.apache.jackrabbit.webdav.client.methods.CopyMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.jackrabbit.webdav.client.methods.PutMethod) Test(org.junit.Test)

Example 4 with PutMethod

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

the class TestWebWAVFS method testInconsistency.

@Test
public void testInconsistency() throws VlException, IOException {
    System.err.println("testInconsistency");
    String testFileURI1 = uri.toASCIIString() + TestSettings.TEST_FILE_NAME1 + ".txt";
    try {
        PutMethod put = new PutMethod(testFileURI1);
        put.setRequestEntity(new StringRequestEntity(TestSettings.TEST_DATA, "text/plain", "UTF-8"));
        int status = client1.executeMethod(put);
        assertEquals(HttpStatus.SC_CREATED, status);
        Set<PDRIDesc> pdris = null;
        // Wait for replication
        utils.waitForReplication(testFileURI1);
        pdris = getPdris(TestSettings.TEST_FILE_NAME1 + ".txt");
        // Delete the physical data
        deletePhysicalData(pdris);
        GetMethod get = new GetMethod(testFileURI1);
        status = client1.executeMethod(get);
        String cont = get.getResponseBodyAsString();
        // There is no offical status to get from this but  confilct seems apropriate:
        // Status code (409) indicating that the request could not be
        // completed due to a conflict with the current state of the resource.
        // Meaning at this time we have no physical data
        assertEquals(HttpStatus.SC_CONFLICT, status);
    } catch (Exception ex) {
        Logger.getLogger(TestWebWAVFS.class.getName()).log(Level.SEVERE, null, ex);
        fail(ex.getMessage());
    } finally {
        utils.deleteResource(testFileURI1, false);
    }
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PDRIDesc(nl.uva.cs.lobcder.tests.TestREST.PDRIDesc) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.jackrabbit.webdav.client.methods.PutMethod) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) VlException(nl.uva.vlet.exception.VlException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with PutMethod

use of org.apache.jackrabbit.webdav.client.methods.PutMethod 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)

Aggregations

StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)8 PutMethod (org.apache.jackrabbit.webdav.client.methods.PutMethod)8 Test (org.junit.Test)8 GetMethod (org.apache.commons.httpclient.methods.GetMethod)4 DeleteMethod (org.apache.jackrabbit.webdav.client.methods.DeleteMethod)3 PropFindMethod (org.apache.jackrabbit.webdav.client.methods.PropFindMethod)2 DavPropertySet (org.apache.jackrabbit.webdav.property.DavPropertySet)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 PDRIDesc (nl.uva.cs.lobcder.tests.TestREST.PDRIDesc)1 VlException (nl.uva.vlet.exception.VlException)1 Header (org.apache.commons.httpclient.Header)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)1 AuthScope (org.apache.commons.httpclient.auth.AuthScope)1 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)1 AclMethod (org.apache.jackrabbit.webdav.client.methods.AclMethod)1 CopyMethod (org.apache.jackrabbit.webdav.client.methods.CopyMethod)1 MkColMethod (org.apache.jackrabbit.webdav.client.methods.MkColMethod)1