Search in sources :

Example 1 with EventBundleImpl

use of org.apache.jackrabbit.spi.commons.EventBundleImpl in project jackrabbit by apache.

the class RepositoryServiceImpl method getEvents.

/**
     * {@inheritDoc}
     */
public EventBundle getEvents(SessionInfo sessionInfo, EventFilter filter, long after) throws RepositoryException, UnsupportedRepositoryOperationException {
    SessionInfoImpl sInfo = getSessionInfoImpl(sessionInfo);
    EventJournal journal = sInfo.getSession().getWorkspace().getObservationManager().getEventJournal();
    if (journal == null) {
        throw new UnsupportedRepositoryOperationException();
    }
    EventFactory factory = new EventFactory(sInfo.getSession(), sInfo.getNamePathResolver(), idFactory, qValueFactory);
    journal.skipTo(after);
    List<Event> events = new ArrayList<Event>();
    int batchSize = 1024;
    boolean distinctDates = true;
    long lastDate = Long.MIN_VALUE;
    while (journal.hasNext() && (batchSize > 0 || !distinctDates)) {
        Event e = factory.fromJCREvent(journal.nextEvent());
        if (filter.accept(e, false)) {
            distinctDates = lastDate != e.getDate();
            lastDate = e.getDate();
            events.add(e);
            batchSize--;
        }
    }
    return new EventBundleImpl(events, false);
}
Also used : UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) EventBundleImpl(org.apache.jackrabbit.spi.commons.EventBundleImpl) ArrayList(java.util.ArrayList) Event(org.apache.jackrabbit.spi.Event) EventJournal(javax.jcr.observation.EventJournal)

Example 2 with EventBundleImpl

use of org.apache.jackrabbit.spi.commons.EventBundleImpl in project jackrabbit by apache.

the class EventSubscription method createEventBundle.

//--------------------------------< internal >------------------------------
private void createEventBundle(javax.jcr.observation.EventIterator events, boolean isLocal) {
    // do not create events when disposed
    if (disposed) {
        return;
    }
    List<Event> spiEvents = new ArrayList<Event>();
    while (events.hasNext()) {
        try {
            Event spiEvent = eventFactory.fromJCREvent(events.nextEvent());
            spiEvents.add(spiEvent);
        } catch (Exception ex) {
            log.warn("Unable to create SPI Event: " + ex);
        }
    }
    EventBundle bundle = new EventBundleImpl(spiEvents, isLocal);
    synchronized (eventBundles) {
        eventBundles.add(bundle);
        eventBundles.notify();
    }
}
Also used : EventBundleImpl(org.apache.jackrabbit.spi.commons.EventBundleImpl) ArrayList(java.util.ArrayList) Event(org.apache.jackrabbit.spi.Event) EventBundle(org.apache.jackrabbit.spi.EventBundle) RepositoryException(javax.jcr.RepositoryException)

Example 3 with EventBundleImpl

use of org.apache.jackrabbit.spi.commons.EventBundleImpl in project jackrabbit by apache.

the class RepositoryServiceImpl method poll.

private EventBundle[] poll(String uri, String subscriptionId, long timeout, SessionInfoImpl sessionInfo) throws RepositoryException {
    HttpPoll request = null;
    try {
        request = new HttpPoll(uri, subscriptionId, timeout);
        HttpResponse response = executeRequest(sessionInfo, request);
        request.checkSuccess(response);
        EventDiscovery disc = request.getResponseBodyAsEventDiscovery(response);
        EventBundle[] events;
        if (disc.isEmpty()) {
            events = new EventBundle[0];
        } else {
            Element discEl = disc.toXml(DomUtil.createDocument());
            ElementIterator it = DomUtil.getChildren(discEl, ObservationConstants.N_EVENTBUNDLE);
            List<EventBundle> bundles = new ArrayList<EventBundle>();
            while (it.hasNext()) {
                Element bundleElement = it.nextElement();
                String value = DomUtil.getAttribute(bundleElement, ObservationConstants.XML_EVENT_LOCAL, null);
                // check if it matches a batch id recently submitted
                boolean isLocal = false;
                if (value != null) {
                    isLocal = Boolean.parseBoolean(value);
                }
                bundles.add(new EventBundleImpl(buildEventList(bundleElement, sessionInfo, uri), isLocal));
            }
            events = bundles.toArray(new EventBundle[bundles.size()]);
        }
        return events;
    } catch (IOException e) {
        throw new RepositoryException(e);
    } catch (ParserConfigurationException e) {
        throw new RepositoryException(e);
    } catch (DavException e) {
        throw ExceptionConverter.generate(e);
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
}
Also used : ElementIterator(org.apache.jackrabbit.webdav.xml.ElementIterator) DavException(org.apache.jackrabbit.webdav.DavException) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) EventBundle(org.apache.jackrabbit.spi.EventBundle) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) HttpPoll(org.apache.jackrabbit.webdav.client.methods.HttpPoll) EventDiscovery(org.apache.jackrabbit.webdav.observation.EventDiscovery) EventBundleImpl(org.apache.jackrabbit.spi.commons.EventBundleImpl) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 4 with EventBundleImpl

use of org.apache.jackrabbit.spi.commons.EventBundleImpl in project jackrabbit by apache.

the class RepositoryServiceImpl method getEvents.

@Override
public EventBundle getEvents(SessionInfo sessionInfo, EventFilter filter, long after) throws RepositoryException {
    // TODO: use filters remotely (JCR-3179)
    HttpGet request = null;
    String rootUri = uriResolver.getWorkspaceUri(sessionInfo.getWorkspaceName());
    // TODO should have a way to discover URI template
    rootUri += "?type=journal";
    try {
        request = new HttpGet(rootUri);
        // TODO
        request.addHeader("If-None-Match", "\"" + Long.toHexString(after) + "\"");
        initMethod(request, sessionInfo);
        HttpResponse response = executeRequest(sessionInfo, request);
        int status = response.getStatusLine().getStatusCode();
        if (status != 200) {
            throw new RepositoryException("getEvents to " + rootUri + " failed with " + response.getStatusLine());
        }
        HttpEntity entity = response.getEntity();
        InputStream in = entity.getContent();
        Document doc = null;
        if (in != null) {
            // read response and try to build a xml document
            try {
                doc = DomUtil.parseDocument(in);
            } catch (ParserConfigurationException e) {
                throw new IOException("XML parser configuration error", e);
            } catch (SAXException e) {
                throw new IOException("XML parsing error", e);
            } finally {
                in.close();
            }
        }
        List<Event> events = new ArrayList<Event>();
        ElementIterator entries = DomUtil.getChildren(doc.getDocumentElement(), AtomFeedConstants.N_ENTRY);
        while (entries.hasNext()) {
            Element entryElem = entries.next();
            Element contentElem = DomUtil.getChildElement(entryElem, AtomFeedConstants.N_CONTENT);
            if (contentElem != null && "application/vnd.apache.jackrabbit.event+xml".equals(contentElem.getAttribute("type"))) {
                List<Event> el = buildEventList(contentElem, (SessionInfoImpl) sessionInfo, rootUri);
                for (Event e : el) {
                    if (e.getDate() > after && (filter == null || filter.accept(e, false))) {
                        events.add(e);
                    }
                }
            }
        }
        return new EventBundleImpl(events, false);
    } catch (Exception ex) {
        log.error("extracting events from journal feed", ex);
        throw new RepositoryException("extracting events from journal feed: " + ex.getMessage(), ex);
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
}
Also used : ElementIterator(org.apache.jackrabbit.webdav.xml.ElementIterator) HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) Document(org.w3c.dom.Document) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) ItemNotFoundException(javax.jcr.ItemNotFoundException) AccessDeniedException(javax.jcr.AccessDeniedException) PathNotFoundException(javax.jcr.PathNotFoundException) LockException(javax.jcr.lock.LockException) IOException(java.io.IOException) DavException(org.apache.jackrabbit.webdav.DavException) URISyntaxException(java.net.URISyntaxException) InvalidItemStateException(javax.jcr.InvalidItemStateException) IllegalNameException(org.apache.jackrabbit.spi.commons.conversion.IllegalNameException) LoginException(javax.jcr.LoginException) SAXException(org.xml.sax.SAXException) RepositoryException(javax.jcr.RepositoryException) NamespaceException(javax.jcr.NamespaceException) UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) MalformedPathException(org.apache.jackrabbit.spi.commons.conversion.MalformedPathException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) EventBundleImpl(org.apache.jackrabbit.spi.commons.EventBundleImpl) Event(org.apache.jackrabbit.spi.Event) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 5 with EventBundleImpl

use of org.apache.jackrabbit.spi.commons.EventBundleImpl in project jackrabbit by apache.

the class EventSubscription method getEventBundles.

/**
     * @return all the pending event bundles.
     */
EventBundle[] getEventBundles(long timeout) {
    EventBundle[] bundles;
    synchronized (eventBundles) {
        if (eventBundles.isEmpty()) {
            try {
                eventBundles.wait(timeout);
            } catch (InterruptedException e) {
            // continue
            }
        }
        bundles = eventBundles.toArray(new EventBundle[eventBundles.size()]);
        eventBundles.clear();
    }
    EventFilter[] eventFilters = filters.toArray(new EventFilter[filters.size()]);
    // apply filters to bundles
    for (int i = 0; i < bundles.length; i++) {
        List<Event> filteredEvents = new ArrayList<Event>();
        for (Iterator<Event> it = bundles[i].getEvents(); it.hasNext(); ) {
            Event e = it.next();
            // TODO: this is actually not correct. if filters are empty no event should go out
            if (eventFilters == null || eventFilters.length == 0) {
                filteredEvents.add(e);
            } else {
                for (EventFilter eventFilter : eventFilters) {
                    if (eventFilter.accept(e, bundles[i].isLocal())) {
                        filteredEvents.add(e);
                        break;
                    }
                }
            }
        }
        bundles[i] = new EventBundleImpl(filteredEvents, bundles[i].isLocal());
    }
    return bundles;
}
Also used : EventBundleImpl(org.apache.jackrabbit.spi.commons.EventBundleImpl) ArrayList(java.util.ArrayList) Event(org.apache.jackrabbit.spi.Event) EventBundle(org.apache.jackrabbit.spi.EventBundle) EventFilter(org.apache.jackrabbit.spi.EventFilter)

Aggregations

ArrayList (java.util.ArrayList)5 EventBundleImpl (org.apache.jackrabbit.spi.commons.EventBundleImpl)5 Event (org.apache.jackrabbit.spi.Event)4 RepositoryException (javax.jcr.RepositoryException)3 EventBundle (org.apache.jackrabbit.spi.EventBundle)3 IOException (java.io.IOException)2 UnsupportedRepositoryOperationException (javax.jcr.UnsupportedRepositoryOperationException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 HttpResponse (org.apache.http.HttpResponse)2 DavException (org.apache.jackrabbit.webdav.DavException)2 ElementIterator (org.apache.jackrabbit.webdav.xml.ElementIterator)2 Element (org.w3c.dom.Element)2 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 AccessDeniedException (javax.jcr.AccessDeniedException)1 InvalidItemStateException (javax.jcr.InvalidItemStateException)1 ItemNotFoundException (javax.jcr.ItemNotFoundException)1 LoginException (javax.jcr.LoginException)1 NamespaceException (javax.jcr.NamespaceException)1 PathNotFoundException (javax.jcr.PathNotFoundException)1