Search in sources :

Example 31 with ElementIterator

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

the class RepositoryServiceImpl method getRepositoryDescriptors.

@Override
public Map<String, QValue[]> getRepositoryDescriptors() throws RepositoryException {
    if (descriptors.isEmpty()) {
        ReportInfo info = new ReportInfo(JcrRemotingConstants.REPORT_REPOSITORY_DESCRIPTORS, ItemResourceConstants.NAMESPACE);
        HttpReport request = null;
        try {
            request = new HttpReport(uriResolver.getRepositoryUri(), info);
            HttpResponse response = executeRequest(null, request);
            int sc = response.getStatusLine().getStatusCode();
            if (sc == HttpStatus.SC_UNAUTHORIZED || sc == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
                // JCR-3076: Mandatory authentication prevents us from
                // accessing the descriptors on the server, so instead
                // of failing with an exception we simply return an empty
                // set of descriptors
                log.warn("Authentication required to access repository descriptors");
                return descriptors;
            }
            request.checkSuccess(response);
            Document doc = request.getResponseBodyAsDocument(response.getEntity());
            if (doc != null) {
                Element rootElement = doc.getDocumentElement();
                ElementIterator nsElems = DomUtil.getChildren(rootElement, JcrRemotingConstants.XML_DESCRIPTOR, ItemResourceConstants.NAMESPACE);
                while (nsElems.hasNext()) {
                    Element elem = nsElems.nextElement();
                    String key = DomUtil.getChildText(elem, JcrRemotingConstants.XML_DESCRIPTORKEY, ItemResourceConstants.NAMESPACE);
                    ElementIterator it = DomUtil.getChildren(elem, JcrRemotingConstants.XML_DESCRIPTORVALUE, ItemResourceConstants.NAMESPACE);
                    List<QValue> vs = new ArrayList<QValue>();
                    while (it.hasNext()) {
                        Element dv = it.nextElement();
                        String descriptor = DomUtil.getText(dv);
                        if (key != null && descriptor != null) {
                            String typeStr = (DomUtil.getAttribute(dv, JcrRemotingConstants.ATTR_VALUE_TYPE, null));
                            int type = (typeStr == null) ? PropertyType.STRING : PropertyType.valueFromName(typeStr);
                            vs.add(getQValueFactory().create(descriptor, type));
                        } else {
                            log.error("Invalid descriptor key / value pair: " + key + " -> " + descriptor);
                        }
                    }
                    descriptors.put(key, vs.toArray(new QValue[vs.size()]));
                }
            }
        } catch (IOException e) {
            throw new RepositoryException(e);
        } catch (DavException e) {
            throw ExceptionConverter.generate(e);
        } finally {
            if (request != null) {
                request.releaseConnection();
            }
        }
    }
    return descriptors;
}
Also used : HttpReport(org.apache.jackrabbit.webdav.client.methods.HttpReport) ElementIterator(org.apache.jackrabbit.webdav.xml.ElementIterator) QValue(org.apache.jackrabbit.spi.QValue) DavException(org.apache.jackrabbit.webdav.DavException) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ReportInfo(org.apache.jackrabbit.webdav.version.report.ReportInfo) HttpResponse(org.apache.http.HttpResponse) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) Document(org.w3c.dom.Document)

Example 32 with ElementIterator

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

the class SubscriptionDiscovery method createFromXml.

// -----------------------------------------------------< static Factory >---
public static SubscriptionDiscovery createFromXml(Element sDiscoveryElement) {
    if (!DomUtil.matches(sDiscoveryElement, ObservationConstants.SUBSCRIPTIONDISCOVERY.getName(), ObservationConstants.SUBSCRIPTIONDISCOVERY.getNamespace())) {
        throw new IllegalArgumentException("'subscriptiondiscovery' element expected.");
    }
    List<Subscription> subscriptions = new ArrayList<Subscription>();
    ElementIterator it = DomUtil.getChildren(sDiscoveryElement, ObservationConstants.XML_SUBSCRIPTION, ObservationConstants.NAMESPACE);
    while (it.hasNext()) {
        final Element sb = it.nextElement();
        // anonymous inner class: Subscription interface
        Subscription s = new Subscription() {

            /**
             * @see Subscription#getSubscriptionId()
             */
            public String getSubscriptionId() {
                Element ltEl = DomUtil.getChildElement(sb, ObservationConstants.XML_SUBSCRIPTIONID, ObservationConstants.NAMESPACE);
                if (ltEl != null) {
                    return DomUtil.getChildText(sb, DavConstants.XML_HREF, DavConstants.NAMESPACE);
                }
                return null;
            }

            public boolean eventsProvideNodeTypeInformation() {
                String t = DomUtil.getChildText(sb, ObservationConstants.XML_EVENTSWITHTYPES, ObservationConstants.NAMESPACE);
                return t == null ? false : Boolean.parseBoolean(t);
            }

            public boolean eventsProvideNoLocalFlag() {
                String t = DomUtil.getChildText(sb, ObservationConstants.XML_EVENTSWITHLOCALFLAG, ObservationConstants.NAMESPACE);
                return t == null ? false : Boolean.parseBoolean(t);
            }

            /**
             * @see XmlSerializable#toXml(Document)
             */
            public Element toXml(Document document) {
                return (Element) document.importNode(sb, true);
            }
        };
        subscriptions.add(s);
    }
    return new SubscriptionDiscovery(subscriptions.toArray(new Subscription[subscriptions.size()]));
}
Also used : ElementIterator(org.apache.jackrabbit.webdav.xml.ElementIterator) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document)

Example 33 with ElementIterator

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

the class DefaultEventType method createFromXml.

/**
 * Retrieves one or multiple <code>EventType</code>s from the 'eventtype'
 * Xml element. While a subscription may register multiple types (thus
 * the 'eventtype' contains multiple child elements), a single event may only
 * refer to one single type.
 *
 * @param eventType
 * @return
 */
public static EventType[] createFromXml(Element eventType) {
    if (!DomUtil.matches(eventType, ObservationConstants.XML_EVENTTYPE, ObservationConstants.NAMESPACE)) {
        throw new IllegalArgumentException("'eventtype' element expected which contains a least a single child element.");
    }
    List<EventType> etypes = new ArrayList<EventType>();
    ElementIterator it = DomUtil.getChildren(eventType);
    while (it.hasNext()) {
        Element el = it.nextElement();
        etypes.add(create(el.getLocalName(), DomUtil.getNamespace(el)));
    }
    return etypes.toArray(new EventType[etypes.size()]);
}
Also used : ElementIterator(org.apache.jackrabbit.webdav.xml.ElementIterator) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList)

Example 34 with ElementIterator

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

the class EventDiscovery method createFromXml.

/**
 * Build a <code>EventDiscovery</code> from the specified xml element.
 *
 * @param eventDiscoveryElement
 * @return new <code>EventDiscovery</code> instance.
 * @throws IllegalArgumentException if the given document is <code>null</code>
 * or does not provide the required element.
 */
public static EventDiscovery createFromXml(Element eventDiscoveryElement) {
    if (!DomUtil.matches(eventDiscoveryElement, XML_EVENTDISCOVERY, ObservationConstants.NAMESPACE)) {
        throw new IllegalArgumentException("{" + ObservationConstants.NAMESPACE + "}" + XML_EVENTDISCOVERY + " element expected, but got: {" + eventDiscoveryElement.getNamespaceURI() + "}" + eventDiscoveryElement.getLocalName());
    }
    EventDiscovery eventDiscovery = new EventDiscovery();
    ElementIterator it = DomUtil.getChildren(eventDiscoveryElement, XML_EVENTBUNDLE, ObservationConstants.NAMESPACE);
    while (it.hasNext()) {
        final Element ebElement = it.nextElement();
        EventBundle eb = new EventBundle() {

            public Element toXml(Document document) {
                return (Element) document.importNode(ebElement, true);
            }
        };
        eventDiscovery.addEventBundle(eb);
    }
    return eventDiscovery;
}
Also used : ElementIterator(org.apache.jackrabbit.webdav.xml.ElementIterator) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 35 with ElementIterator

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

the class Position method createFromXml.

// -----------------------------------------------------< static methods >---
/**
 * Create a new <code>Position</code> object from the specified position
 * element. The element must fulfill the following structure:<br>
 * <pre>
 * &lt;!ELEMENT position (first | last | before | after) &gt;
 * &lt;!ELEMENT segment (#PCDATA) &gt;
 * &lt;!ELEMENT first EMPTY &gt;
 * &lt;!ELEMENT last EMPTY &gt;
 * &lt;!ELEMENT before segment &gt;
 * &lt;!ELEMENT after segment &gt;
 * </pre>
 *
 * @param positionElement Xml element defining the position.
 * @throws IllegalArgumentException if the given Xml element is not valid.
 */
public static Position createFromXml(Element positionElement) {
    if (!DomUtil.matches(positionElement, XML_POSITION, NAMESPACE)) {
        throw new IllegalArgumentException("The 'DAV:position' element required.");
    }
    ElementIterator it = DomUtil.getChildren(positionElement);
    if (it.hasNext()) {
        Element el = it.nextElement();
        String type = el.getLocalName();
        // read the text of DAV:segment child element inside the type
        String segmentText = DomUtil.getChildText(el, XML_SEGMENT, NAMESPACE);
        // stop after the first iteration
        return new Position(type, segmentText);
    } else {
        throw new IllegalArgumentException("The 'DAV:position' element required with exact one child indicating the type.");
    }
}
Also used : ElementIterator(org.apache.jackrabbit.webdav.xml.ElementIterator) Element(org.w3c.dom.Element)

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