Search in sources :

Example 26 with ElementIterator

use of org.apache.jackrabbit.webdav.xml.ElementIterator in project jackrabbit by apache.

the class MergeInfo method getSourceHrefs.

/**
 * Returns the URL specified with the DAV:source element or <code>null</code>
 * if no such child element is present in the DAV:merge element.
 *
 * @return href present in the DAV:source child element or <code>null</code>.
 */
public String[] getSourceHrefs() {
    List<String> sourceHrefs = new ArrayList<String>();
    Element srcElem = DomUtil.getChildElement(mergeElement, DavConstants.XML_SOURCE, DavConstants.NAMESPACE);
    if (srcElem != null) {
        ElementIterator it = DomUtil.getChildren(srcElem, DavConstants.XML_HREF, DavConstants.NAMESPACE);
        while (it.hasNext()) {
            String href = DomUtil.getTextTrim(it.nextElement());
            if (href != null) {
                sourceHrefs.add(href);
            }
        }
    }
    return sourceHrefs.toArray(new String[sourceHrefs.size()]);
}
Also used : ElementIterator(org.apache.jackrabbit.webdav.xml.ElementIterator) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList)

Example 27 with ElementIterator

use of org.apache.jackrabbit.webdav.xml.ElementIterator in project jackrabbit by apache.

the class ProtectedRemoveConfig method parse.

void parse(InputStream inputStream) throws IOException {
    Element config;
    ProtectedItemRemoveHandler instance = null;
    try {
        config = DomUtil.parseDocument(inputStream).getDocumentElement();
        if (config == null) {
            log.warn("Missing mandatory config element");
            return;
        }
        ElementIterator handlers = DomUtil.getChildren(config, ELEMENT_HANDLER, null);
        while (handlers.hasNext()) {
            Element handler = handlers.nextElement();
            instance = createHandler(handler);
            manager.addHandler(instance);
        }
    } catch (ParserConfigurationException e) {
        log.error(e.getMessage(), e);
    } catch (SAXException e) {
        log.error(e.getMessage(), e);
    }
}
Also used : ElementIterator(org.apache.jackrabbit.webdav.xml.ElementIterator) Element(org.w3c.dom.Element) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 28 with ElementIterator

use of org.apache.jackrabbit.webdav.xml.ElementIterator 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) KeyStoreException(java.security.KeyStoreException) ItemNotFoundException(javax.jcr.ItemNotFoundException) PathNotFoundException(javax.jcr.PathNotFoundException) 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) RepositoryException(javax.jcr.RepositoryException) UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AccessDeniedException(javax.jcr.AccessDeniedException) LockException(javax.jcr.lock.LockException) LoginException(javax.jcr.LoginException) SAXException(org.xml.sax.SAXException) NamespaceException(javax.jcr.NamespaceException) 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 29 with ElementIterator

use of org.apache.jackrabbit.webdav.xml.ElementIterator in project jackrabbit by apache.

the class RepositoryServiceImpl method buildEventList.

private List<Event> buildEventList(Element bundleElement, SessionInfoImpl sessionInfo, String baseUri) throws RepositoryException {
    List<Event> events = new ArrayList<Event>();
    ElementIterator eventElementIterator = DomUtil.getChildren(bundleElement, ObservationConstants.N_EVENT);
    String userId = null;
    // get user id from enclosing Atom entry element in case this was a feed
    if (DomUtil.matches(bundleElement, AtomFeedConstants.N_ENTRY)) {
        Element authorEl = DomUtil.getChildElement(bundleElement, AtomFeedConstants.N_AUTHOR);
        Element nameEl = authorEl != null ? DomUtil.getChildElement(authorEl, AtomFeedConstants.N_NAME) : null;
        if (nameEl != null) {
            userId = DomUtil.getTextTrim(nameEl);
        }
    }
    while (eventElementIterator.hasNext()) {
        Element evElem = eventElementIterator.nextElement();
        Element typeEl = DomUtil.getChildElement(evElem, ObservationConstants.N_EVENTTYPE);
        EventType[] et = DefaultEventType.createFromXml(typeEl);
        if (et.length == 0 || et.length > 1) {
            // should not occur.
            log.error("Ambiguous event type definition: expected one single event type.");
            continue;
        }
        String href = DomUtil.getChildTextTrim(evElem, XML_HREF, NAMESPACE);
        int type = EventUtil.getJcrEventType(et[0].getName());
        Path eventPath = null;
        ItemId eventId = null;
        NodeId parentId = null;
        if (href != null) {
            href = resolve(baseUri, href);
            try {
                eventPath = uriResolver.getQPath(href, sessionInfo);
            } catch (RepositoryException e) {
                // should not occur
                log.error("Internal error while building Event: ()", e.getMessage());
                continue;
            }
            boolean isForNode = (type == Event.NODE_ADDED || type == Event.NODE_REMOVED || type == Event.NODE_MOVED);
            try {
                if (isForNode) {
                    eventId = uriResolver.getNodeIdAfterEvent(href, sessionInfo, type == Event.NODE_REMOVED);
                } else {
                    eventId = uriResolver.getPropertyId(href, sessionInfo);
                }
            } catch (RepositoryException e) {
                if (isForNode) {
                    eventId = idFactory.createNodeId((String) null, eventPath);
                } else {
                    try {
                        eventId = idFactory.createPropertyId(idFactory.createNodeId((String) null, eventPath.getAncestor(1)), eventPath.getName());
                    } catch (RepositoryException e1) {
                        log.warn("Unable to build event itemId: {}", e.getMessage());
                    }
                }
            }
            String parentHref = Text.getRelativeParent(href, 1, true);
            try {
                parentId = uriResolver.getNodeId(parentHref, sessionInfo);
            } catch (RepositoryException e) {
                log.warn("Unable to build event parentId: {}", e.getMessage());
            }
        }
        if (userId == null) {
            // user id not retrieved from container
            userId = DomUtil.getChildTextTrim(evElem, ObservationConstants.N_EVENTUSERID);
        }
        events.add(new EventImpl(eventId, eventPath, parentId, type, userId, evElem, getNamePathResolver(sessionInfo), getQValueFactory()));
    }
    return events;
}
Also used : Path(org.apache.jackrabbit.spi.Path) ElementIterator(org.apache.jackrabbit.webdav.xml.ElementIterator) DefaultEventType(org.apache.jackrabbit.webdav.observation.DefaultEventType) EventType(org.apache.jackrabbit.webdav.observation.EventType) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) ItemId(org.apache.jackrabbit.spi.ItemId) NodeId(org.apache.jackrabbit.spi.NodeId) Event(org.apache.jackrabbit.spi.Event)

Example 30 with ElementIterator

use of org.apache.jackrabbit.webdav.xml.ElementIterator 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)

Aggregations

ElementIterator (org.apache.jackrabbit.webdav.xml.ElementIterator)36 Element (org.w3c.dom.Element)34 ArrayList (java.util.ArrayList)17 DavException (org.apache.jackrabbit.webdav.DavException)13 RepositoryException (javax.jcr.RepositoryException)9 Document (org.w3c.dom.Document)7 IOException (java.io.IOException)6 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)5 HttpResponse (org.apache.http.HttpResponse)4 SAXException (org.xml.sax.SAXException)4 Name (org.apache.jackrabbit.spi.Name)3 QValue (org.apache.jackrabbit.spi.QValue)3 NameException (org.apache.jackrabbit.spi.commons.conversion.NameException)3 HashMap (java.util.HashMap)2 Event (org.apache.jackrabbit.spi.Event)2 EventBundleImpl (org.apache.jackrabbit.spi.commons.EventBundleImpl)2 InputStream (java.io.InputStream)1 Method (java.lang.reflect.Method)1 URISyntaxException (java.net.URISyntaxException)1 KeyManagementException (java.security.KeyManagementException)1