Search in sources :

Example 1 with Sardine

use of com.github.sardine.Sardine in project openhab1-addons by openhab.

the class CalDavLoaderImpl method addEvent.

@Override
public void addEvent(CalDavEvent calDavEvent) {
    final CalendarRuntime calendarRuntime = EventStorage.getInstance().getEventCache().get(calDavEvent.getCalendarId());
    CalDavConfig config = calendarRuntime.getConfig();
    if (config == null) {
        log.error("cannot find config for calendar id: {}", calDavEvent.getCalendarId());
    }
    Sardine sardine = Util.getConnection(config);
    Calendar calendar = Util.createCalendar(calDavEvent, defaultTimeZone);
    try {
        final String fullIcsFile = config.getUrl() + "/" + calDavEvent.getFilename() + ".ics";
        if (calendarRuntime.getEventContainerByFilename(calDavEvent.getFilename()) != null) {
            log.debug("event will be updated: {}", fullIcsFile);
            try {
                sardine.delete(fullIcsFile);
            } catch (IOException e) {
                log.error("cannot remove old ics file: {}", fullIcsFile);
            }
        } else {
            log.debug("event is new: {}", fullIcsFile);
        }
        sardine.put(fullIcsFile, calendar.toString().getBytes("UTF-8"));
        EventContainer eventContainer = new EventContainer(calDavEvent.getCalendarId());
        eventContainer.setEventId(calDavEvent.getId());
        eventContainer.setFilename(Util.getFilename(calDavEvent.getFilename()));
        eventContainer.getEventList().add(calDavEvent);
        eventContainer.setLastChanged(calDavEvent.getLastChanged());
        this.addEventToMap(eventContainer, false);
    } catch (UnsupportedEncodingException e) {
        log.error("cannot write event", e);
    } catch (IOException e) {
        log.error("cannot write event", e);
    }
}
Also used : Sardine(com.github.sardine.Sardine) EventContainer(org.openhab.io.caldav.internal.EventStorage.EventContainer) Calendar(net.fortuna.ical4j.model.Calendar) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) CalendarRuntime(org.openhab.io.caldav.internal.EventStorage.CalendarRuntime)

Example 2 with Sardine

use of com.github.sardine.Sardine in project openhab1-addons by openhab.

the class EventReloaderJob method loadEvents.

/**
     * all events which are available must be removed from the oldEventIds list
     *
     * @param calendarRuntime
     * @param oldEventIds
     * @throws IOException
     * @throws ParserException
     */
public synchronized void loadEvents(final CalendarRuntime calendarRuntime, final List<String> oldEventIds) throws IOException, ParserException {
    CalDavConfig config = calendarRuntime.getConfig();
    Sardine sardine = Util.getConnection(config);
    List<DavResource> list = sardine.list(config.getUrl(), 1, false);
    log.trace("before load events : oldeventsid contains : {}", oldEventIds.toString());
    for (DavResource resource : list) {
        final String filename = Util.getFilename(resource.getName());
        try {
            if (resource.isDirectory()) {
                continue;
            }
            // an ics file can contain multiple events
            // ==> multiple eventcontainers could have the same filename (and different eventid),
            // ==>we must not have one of them remaining in oldEventIds var (bad chosen name, cause it's a list of
            // oldEventContainers's filename, so with doubles possible)
            // or the remaining jobs with this filename will get unscheduled on the "removeDeletedEvents(config,
            // oldEventIds)" call (line 136)
            oldEventIds.removeAll(Arrays.asList(filename));
            // must not be loaded
            EventContainer eventContainer = calendarRuntime.getEventContainerByFilename(filename);
            final org.joda.time.DateTime lastResourceChangeFS = new org.joda.time.DateTime(resource.getModified());
            log.trace("eventContainer found: {}", eventContainer != null);
            log.trace("last resource modification: {}", lastResourceChangeFS);
            log.trace("last change of already loaded event: {}", eventContainer != null ? eventContainer.getLastChanged() : null);
            if (config.isLastModifiedFileTimeStampValid()) {
                if (eventContainer != null && !lastResourceChangeFS.isAfter(eventContainer.getLastChanged())) {
                    // to be created
                    if (eventContainer.getCalculatedUntil() != null && eventContainer.getCalculatedUntil().isAfter(org.joda.time.DateTime.now().plusMinutes(config.getReloadMinutes()))) {
                        // the event is calculated as long as the next reload
                        // interval can handle this
                        log.trace("skipping resource {}, not changed (calculated until: {})", resource.getName(), eventContainer.getCalculatedUntil());
                        continue;
                    }
                    if (eventContainer.isHistoricEvent()) {
                        // no more upcoming events, do nothing
                        log.trace("skipping resource {}, not changed (historic)", resource.getName());
                        continue;
                    }
                    File icsFile = Util.getCacheFile(config.getKey(), filename);
                    if (icsFile != null && icsFile.exists()) {
                        FileInputStream fis = new FileInputStream(icsFile);
                        this.loadEvents(filename, lastResourceChangeFS, fis, config, oldEventIds, false);
                        fis.close();
                        continue;
                    }
                }
            }
            log.debug("loading resource: {} (FSchangedTS not valid)", resource);
            // prepare resource url
            URL url = new URL(config.getUrl());
            String resourcePath = resource.getPath();
            String escapedResource = resource.getName().replaceAll("/", "%2F");
            resourcePath = resourcePath.replace(resource.getName(), escapedResource);
            url = new URL(url.getProtocol(), url.getHost(), url.getPort(), resourcePath);
            InputStream inputStream = sardine.get(url.toString().replaceAll(" ", "%20"));
            this.loadEvents(filename, lastResourceChangeFS, inputStream, config, oldEventIds, false);
        } catch (ParserException e) {
            log.error("error parsing ics file: " + filename, e);
        } catch (SardineException e) {
            log.error("error reading ics file: " + filename, e);
        }
    }
    log.trace("after load events : oldeventsid contains : {}", oldEventIds.toString());
}
Also used : Sardine(com.github.sardine.Sardine) ParserException(net.fortuna.ical4j.data.ParserException) DavResource(com.github.sardine.DavResource) EventContainer(org.openhab.io.caldav.internal.EventStorage.EventContainer) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CalDavConfig(org.openhab.io.caldav.internal.CalDavConfig) DateTime(net.fortuna.ical4j.model.DateTime) LocalDateTime(org.joda.time.LocalDateTime) FileInputStream(java.io.FileInputStream) URL(java.net.URL) SardineException(com.github.sardine.impl.SardineException) CalendarRuntime(org.openhab.io.caldav.internal.EventStorage.CalendarRuntime) File(java.io.File)

Aggregations

Sardine (com.github.sardine.Sardine)2 CalendarRuntime (org.openhab.io.caldav.internal.EventStorage.CalendarRuntime)2 EventContainer (org.openhab.io.caldav.internal.EventStorage.EventContainer)2 DavResource (com.github.sardine.DavResource)1 SardineException (com.github.sardine.impl.SardineException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URL (java.net.URL)1 ParserException (net.fortuna.ical4j.data.ParserException)1 Calendar (net.fortuna.ical4j.model.Calendar)1 DateTime (net.fortuna.ical4j.model.DateTime)1 LocalDateTime (org.joda.time.LocalDateTime)1 CalDavConfig (org.openhab.io.caldav.internal.CalDavConfig)1