Search in sources :

Example 1 with PropFindMethod

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

the class Utils method getAvailableStorageSites.

public String[] getAvailableStorageSites(String testuri1) throws IOException, DavException {
    DavPropertyNameSet availableStorageSitesNameSet = new DavPropertyNameSet();
    DavPropertyName availableStorageSitesName = DavPropertyName.create("avail-storage-sites", Namespace.getNamespace("custom:"));
    availableStorageSitesNameSet.add(availableStorageSitesName);
    PropFindMethod propFind = new PropFindMethod(testuri1, availableStorageSitesNameSet, DavConstants.DEPTH_INFINITY);
    int status = client.executeMethod(propFind);
    assertEquals(HttpStatus.SC_MULTI_STATUS, status);
    MultiStatus multiStatus = propFind.getResponseBodyAsMultiStatus();
    MultiStatusResponse[] responses = multiStatus.getResponses();
    String value = null;
    for (MultiStatusResponse r : responses) {
        DavPropertySet allProp = getProperties(r);
        DavPropertyIterator iter = allProp.iterator();
        while (iter.hasNext()) {
            DavProperty<?> p = iter.nextProperty();
            assertEquals(p.getName(), availableStorageSitesName);
            assertNotNull(p.getValue());
            value = (String) p.getValue();
        }
    }
    String sites = value.replaceAll("[\\[\\]]", "");
    return sites.split(",");
}
Also used : DavPropertySet(org.apache.jackrabbit.webdav.property.DavPropertySet) PropFindMethod(org.apache.jackrabbit.webdav.client.methods.PropFindMethod) DavPropertyNameSet(org.apache.jackrabbit.webdav.property.DavPropertyNameSet) MultiStatusResponse(org.apache.jackrabbit.webdav.MultiStatusResponse) MultiStatus(org.apache.jackrabbit.webdav.MultiStatus) DavPropertyIterator(org.apache.jackrabbit.webdav.property.DavPropertyIterator) DavPropertyName(org.apache.jackrabbit.webdav.property.DavPropertyName)

Example 2 with PropFindMethod

use of org.apache.jackrabbit.webdav.client.methods.PropFindMethod in project openmeetings by apache.

the class AppointmentManager method discoverCalendars.

/**
 * Function which finds all the calendars of the Principal URL of the calendar
 *
 * @param client - {@link HttpClient} to discover calendar
 * @param calendar - calendar to get principal URL from
 * @return - <code>true</code> in case calendar was discovered successfully
 */
private boolean discoverCalendars(HttpClient client, OmCalendar calendar) {
    cleanupIdleConnections();
    if (calendar.getSyncType() == SyncType.NONE) {
        PropFindMethod propFindMethod = null;
        String userPath = null, homepath = null;
        DavPropertyName curUserPrincipal = DavPropertyName.create("current-user-principal"), calHomeSet = DavPropertyName.create("calendar-home-set", CalDAVConstants.NAMESPACE_CALDAV), suppCalCompSet = DavPropertyName.create("supported-calendar-component-set", CalDAVConstants.NAMESPACE_CALDAV);
        // Find out whether it's a calendar or if we can find the calendar-home or current-user url
        try {
            String path = getPathfromCalendar(client, calendar);
            DavPropertyNameSet properties = new DavPropertyNameSet();
            properties.add(curUserPrincipal);
            properties.add(calHomeSet);
            properties.add(DavPropertyName.RESOURCETYPE);
            propFindMethod = new PropFindMethod(path, properties, CalDAVConstants.DEPTH_0);
            client.executeMethod(propFindMethod);
            if (propFindMethod.succeeded()) {
                for (MultiStatusResponse response : propFindMethod.getResponseBodyAsMultiStatus().getResponses()) {
                    DavPropertySet set = response.getProperties(SC_OK);
                    DavProperty<?> calhome = set.get(calHomeSet), curPrinci = set.get(curUserPrincipal), resourcetype = set.get(DavPropertyName.RESOURCETYPE);
                    if (checkCalendarResourceType(resourcetype)) {
                        // This is a calendar and thus initialize and return
                        return initCalendar(client, calendar);
                    }
                    // Else find all the calendars on the Principal and return.
                    if (calhome != null) {
                        // Calendar Home Path
                        homepath = getTextValuefromProperty(calhome);
                        break;
                    } else if (curPrinci != null) {
                        // Current User Principal Path
                        userPath = getTextValuefromProperty(curPrinci);
                        break;
                    }
                }
            } else {
                return false;
            }
            if (homepath == null && userPath != null) {
                // If calendar home path wasn't set, then we get it
                DavPropertyNameSet props = new DavPropertyNameSet();
                props.add(calHomeSet);
                propFindMethod = new PropFindMethod(userPath, props, DavConstants.DEPTH_0);
                client.executeMethod(propFindMethod);
                if (propFindMethod.succeeded()) {
                    for (MultiStatusResponse response : propFindMethod.getResponseBodyAsMultiStatus().getResponses()) {
                        DavPropertySet set = response.getProperties(SC_OK);
                        DavProperty<?> calhome = set.get(calHomeSet);
                        if (calhome != null) {
                            homepath = getTextValuefromProperty(calhome);
                            break;
                        }
                    }
                } else {
                    return false;
                }
            }
            if (homepath != null) {
                DavPropertyNameSet props = new DavPropertyNameSet();
                props.add(DavPropertyName.RESOURCETYPE);
                props.add(suppCalCompSet);
                props.add(DavPropertyName.DISPLAYNAME);
                propFindMethod = new PropFindMethod(homepath, props, DavConstants.DEPTH_1);
                client.executeMethod(propFindMethod);
                if (propFindMethod.succeeded()) {
                    boolean success = false;
                    for (MultiStatusResponse response : propFindMethod.getResponseBodyAsMultiStatus().getResponses()) {
                        boolean isVevent = false, isCalendar;
                        DavPropertySet set = response.getProperties(SC_OK);
                        DavProperty<?> p = set.get(suppCalCompSet), resourcetype = set.get(DavPropertyName.RESOURCETYPE), displayname = set.get(DavPropertyName.DISPLAYNAME);
                        isCalendar = checkCalendarResourceType(resourcetype);
                        if (p != null) {
                            for (Object o : (Collection<?>) p.getValue()) {
                                if (o instanceof Element) {
                                    Element e = (Element) o;
                                    String name = DomUtil.getAttribute(e, "name", null);
                                    if ("VEVENT".equals(name)) {
                                        isVevent = true;
                                    }
                                }
                            }
                        }
                        if (isCalendar && isVevent) {
                            success = true;
                            // Get New Calendar
                            OmCalendar tempCalendar = new OmCalendar();
                            if (displayname != null) {
                                tempCalendar.setTitle(displayname.getValue().toString());
                            }
                            tempCalendar.setHref(client.getHostConfiguration().getHostURL() + response.getHref());
                            tempCalendar.setDeleted(false);
                            tempCalendar.setOwner(calendar.getOwner());
                            calendarDao.update(tempCalendar);
                            initCalendar(client, tempCalendar);
                        }
                    }
                    return success;
                }
            }
        } catch (IOException e) {
            log.error("Error executing PROPFIND Method, during testConnection.", e);
        } catch (Exception e) {
            log.error("Severe Error in executing PROPFIND Method, during testConnection.", e);
        } finally {
            if (propFindMethod != null) {
                propFindMethod.releaseConnection();
            }
        }
    }
    return false;
}
Also used : PropFindMethod(org.apache.jackrabbit.webdav.client.methods.PropFindMethod) Element(org.w3c.dom.Element) MultiStatusResponse(org.apache.jackrabbit.webdav.MultiStatusResponse) IOException(java.io.IOException) DavPropertyName(org.apache.jackrabbit.webdav.property.DavPropertyName) IOException(java.io.IOException) DavPropertySet(org.apache.jackrabbit.webdav.property.DavPropertySet) DavPropertyNameSet(org.apache.jackrabbit.webdav.property.DavPropertyNameSet) Collection(java.util.Collection) OmCalendar(org.apache.openmeetings.db.entity.calendar.OmCalendar)

Example 3 with PropFindMethod

use of org.apache.jackrabbit.webdav.client.methods.PropFindMethod in project openmeetings by apache.

the class AppointmentManager method initCalendar.

/**
 * Function to initialize the Calendar on the type of syncing and whether it can be used or not.
 *
 * @param client - {@link HttpClient} to discover calendar
 * @param calendar - calendar to be inited
 * @return <code>true</code> in case calendar was inited
 */
private boolean initCalendar(HttpClient client, OmCalendar calendar) {
    if (calendar.getToken() == null || calendar.getSyncType() == SyncType.NONE) {
        calendarDao.update(calendar);
        PropFindMethod propFindMethod = null;
        try {
            String path = getPathfromCalendar(client, calendar);
            DavPropertyNameSet properties = new DavPropertyNameSet();
            properties.add(DavPropertyName.RESOURCETYPE);
            properties.add(DavPropertyName.DISPLAYNAME);
            properties.add(CtagHandler.DNAME_GETCTAG);
            properties.add(WebDAVSyncHandler.DNAME_SYNCTOKEN);
            propFindMethod = new PropFindMethod(path, properties, CalDAVConstants.DEPTH_0);
            client.executeMethod(propFindMethod);
            if (propFindMethod.succeeded()) {
                for (MultiStatusResponse response : propFindMethod.getResponseBodyAsMultiStatus().getResponses()) {
                    DavPropertySet set = response.getProperties(SC_OK);
                    if (calendar.getTitle() == null) {
                        DavProperty<?> property = set.get(DavPropertyName.DISPLAYNAME);
                        calendar.setTitle(property == null ? null : property.getValue().toString());
                    }
                    DavProperty<?> ctag = set.get(CtagHandler.DNAME_GETCTAG), syncToken = set.get(WebDAVSyncHandler.DNAME_SYNCTOKEN);
                    if (syncToken != null) {
                        calendar.setSyncType(SyncType.WEBDAV_SYNC);
                    } else if (ctag != null) {
                        calendar.setSyncType(SyncType.CTAG);
                    } else {
                        calendar.setSyncType(SyncType.ETAG);
                    }
                }
                syncItem(client, calendar);
                return true;
            } else {
                log.error("Error executing PROPFIND Method, with status Code: {}", propFindMethod.getStatusCode());
                calendar.setSyncType(SyncType.NONE);
            }
        } catch (IOException e) {
            log.error("Error executing OptionsMethod during testConnection.", e);
        } catch (Exception e) {
            log.error("Severe Error in executing OptionsMethod during testConnection.", e);
        } finally {
            if (propFindMethod != null) {
                propFindMethod.releaseConnection();
            }
        }
    }
    return false;
}
Also used : DavPropertySet(org.apache.jackrabbit.webdav.property.DavPropertySet) PropFindMethod(org.apache.jackrabbit.webdav.client.methods.PropFindMethod) DavPropertyNameSet(org.apache.jackrabbit.webdav.property.DavPropertyNameSet) MultiStatusResponse(org.apache.jackrabbit.webdav.MultiStatusResponse) IOException(java.io.IOException) IOException(java.io.IOException)

Example 4 with PropFindMethod

use of org.apache.jackrabbit.webdav.client.methods.PropFindMethod 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 5 with PropFindMethod

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

the class TestWebWAVFS method testSetGetPropertySet.

@Test
public void testSetGetPropertySet() throws IOException, DavException {
    System.err.println("testSetGetPropertySet");
    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 = client1.executeMethod(put);
    assertEquals(HttpStatus.SC_CREATED, status);
    PropFindMethod 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();
    assertEquals(HttpStatus.SC_OK, responses[0].getStatus()[0].getStatusCode());
    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(TestSettings.TEST_DATA.length()));
    String contentType = (String) allProp.get(DavPropertyName.GETCONTENTTYPE).getValue();
    assertEquals("text/plain; charset=UTF-8", contentType);
    utils.deleteResource(testFileURI1, true);
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) DavPropertySet(org.apache.jackrabbit.webdav.property.DavPropertySet) PropFindMethod(org.apache.jackrabbit.webdav.client.methods.PropFindMethod) PutMethod(org.apache.jackrabbit.webdav.client.methods.PutMethod) Test(org.junit.Test)

Aggregations

PropFindMethod (org.apache.jackrabbit.webdav.client.methods.PropFindMethod)6 DavPropertySet (org.apache.jackrabbit.webdav.property.DavPropertySet)5 DavPropertyNameSet (org.apache.jackrabbit.webdav.property.DavPropertyNameSet)4 MultiStatusResponse (org.apache.jackrabbit.webdav.MultiStatusResponse)3 IOException (java.io.IOException)2 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)2 PutMethod (org.apache.jackrabbit.webdav.client.methods.PutMethod)2 DavPropertyName (org.apache.jackrabbit.webdav.property.DavPropertyName)2 Test (org.junit.Test)2 Collection (java.util.Collection)1 GetMethod (org.apache.commons.httpclient.methods.GetMethod)1 MultiStatus (org.apache.jackrabbit.webdav.MultiStatus)1 DeleteMethod (org.apache.jackrabbit.webdav.client.methods.DeleteMethod)1 DavPropertyIterator (org.apache.jackrabbit.webdav.property.DavPropertyIterator)1 OmCalendar (org.apache.openmeetings.db.entity.calendar.OmCalendar)1 Element (org.w3c.dom.Element)1