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);
}
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();
}
}
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();
}
}
}
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();
}
}
}
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;
}
Aggregations