Java Code Examples for org.apache.jackrabbit.webdav.xml.DomUtil#getChildElement()

The following examples show how to use org.apache.jackrabbit.webdav.xml.DomUtil#getChildElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CaldavMultiStatusReport.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * Parses an output filter out of the given report info.
 */
protected static OutputFilter findOutputFilter(ReportInfo info)
    throws CosmoDavException {
    Element propdata =
        DomUtil.getChildElement(getReportElementFrom(info),
                                XML_PROP, NAMESPACE);
    if (propdata == null) {
        return null;
    }

    Element cdata =
        DomUtil.getChildElement(propdata, ELEMENT_CALDAV_CALENDAR_DATA,
                                NAMESPACE_CALDAV);
    if (cdata == null) {
        return null;
    }

    return CaldavOutputFilter.createFromXml(cdata);
}
 
Example 2
Source File: QueryReport.java    From cosmo with Apache License 2.0 6 votes vote down vote up
private static VTimeZone findTimeZone(ReportInfo info) throws CosmoDavException {
    Element propdata =
        DomUtil.getChildElement(getReportElementFrom(info),
                                XML_PROP, NAMESPACE);
    if (propdata == null) {
        return null;
    }

    Element tzdata =
        DomUtil.getChildElement(propdata, ELEMENT_CALDAV_TIMEZONE,
                                NAMESPACE_CALDAV);
    if (tzdata == null) {
        return null;
    }

    String icaltz = DomUtil.getTextTrim(tzdata);
    if (icaltz == null) {
        throw new UnprocessableEntityException("Expected text content for " + QN_CALDAV_TIMEZONE);
    }

    return TimeZoneExtractor.extract(icaltz);
}
 
Example 3
Source File: PrincipalPropertySearchReport.java    From cosmo with Apache License 2.0 5 votes vote down vote up
public static SearchSpec createFromXml(Element root)
    throws CosmoDavException {
    if (! DomUtil.matches(root, "property-search", NAMESPACE)) {
        throw new IllegalArgumentException("Expected root element DAV:property-search");
    }

    Element p = DomUtil.getChildElement(root, "prop", NAMESPACE);
    if (p == null) {
        throw new BadRequestException("Expected DAV:prop child of DAV:property-search");
    }
    ElementIterator pi = DomUtil.getChildren(p);
    if (! pi.hasNext()) {
        throw new BadRequestException("Expected at least one child of DAV:prop");
    }

    HashSet<DavPropertyName> properties =
        new HashSet<DavPropertyName>();
    while (pi.hasNext()) {
        DavPropertyName name =
            DavPropertyName.createFromXml(pi.nextElement());
        properties.add(name);
    }

    Element m = DomUtil.getChildElement(root, "match", NAMESPACE);
    if (m == null) {
        throw new BadRequestException("Expected DAV:match child of DAV:property-search");
    }
    String match = DomUtil.getText(m);
    
    return new SearchSpec(properties, match);
}
 
Example 4
Source File: StandardDavRequest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @return DavPropertySet
 * @throws CosmoDavException 
 */
private DavPropertySet parseMkCalendarRequest() throws CosmoDavException {
    DavPropertySet propertySet = new DavPropertySet();

    Document requestDocument = getSafeRequestDocument(false);
    if (requestDocument == null) {
        return propertySet;
    }

    Element root = requestDocument.getDocumentElement();
    if (!DomUtil.matches(root, ELEMENT_CALDAV_MKCALENDAR, NAMESPACE_CALDAV)) {
        throw new BadRequestException("Expected " + QN_MKCALENDAR
                + " root element");
    }
    Element set = DomUtil.getChildElement(root, XML_SET, NAMESPACE);
    if (set == null) {
        throw new BadRequestException("Expected " + QN_SET + " child of "
                + QN_MKCALENDAR);
    }
    Element prop = DomUtil.getChildElement(set, XML_PROP, NAMESPACE);
    if (prop == null) {
        throw new BadRequestException("Expected " + QN_PROP + " child of "
                + QN_SET);
    }
    ElementIterator i = DomUtil.getChildren(prop);
    while (i.hasNext()){
        propertySet.add(StandardDavProperty.createFromXml(i.nextElement()));
    }

    return propertySet;
}
 
Example 5
Source File: MultiStatus.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Creates from xml.
 * @param e The element.
 * @return The multi status response.
 */
public static MultiStatusResponse createFromXml(Element e) {
    if (e == null) {
        throw new IllegalArgumentException("null DAV:response element");
    }

    MultiStatusResponse msr = new MultiStatusResponse();

    Element he = DomUtil.getChildElement(e, "href", NS);
    if (he == null) {
        throw new IllegalArgumentException("expected DAV:href child for DAV:response element");
    }
    msr.setHref(DomUtil.getTextTrim(he));

    String statusLine = DomUtil.getChildTextTrim(e, "status", NS);
    if (statusLine != null) {
        msr.setStatus(Status.createFromStatusLine(statusLine));
    }

    ElementIterator i = DomUtil.getChildren(e, "propstat", NS);
    while (i.hasNext()) {
        msr.getPropStats().add(PropStat.createFromXml(i.nextElement()));
    }

    String msrrd =
        DomUtil.getChildTextTrim(e, "responsedescription", NS);
    msr.setResponseDescription(msrrd);

    return msr;
}
 
Example 6
Source File: MultiStatus.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Creates from xml.
 * @param e The element.
 * @return Propstat.
 */
public static PropStat createFromXml(Element e) {
    if (e == null) {
        throw new IllegalArgumentException("null DAV:propstat element");
    }

    PropStat ps = new PropStat();

    Element pe = DomUtil.getChildElement(e, "prop", NS);
    if (pe == null) {
        throw new IllegalArgumentException("expected DAV:prop child for DAV:propstat element");
    }

    ElementIterator i = DomUtil.getChildren(pe);
    while (i.hasNext()) {
        ps.getProps().add(i.nextElement());
    }

    String statusLine = DomUtil.getChildTextTrim(e, "status", NS);
    if (statusLine != null) {
        ps.setStatus(Status.createFromStatusLine(statusLine));
    }

    String psrd =
        DomUtil.getChildTextTrim(e, "responsedescription", NS);
    ps.setResponseDescription(psrd);

    return ps;
}
 
Example 7
Source File: TicketContent.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a <code>TicketContent> populated with information from
 * the given XML fragment which is assumed to be response content
 * (containing an owner href rather than a <code>User</code>).
 *
 * The root element of the fragment must be a
 * <code>ticket:ticketinfo</code> element.
 * @param root The element.
 * @return The ticket content.
 * @throws Exception - if something is wrong this exception is thrown.
 */
public static TicketContent createFromXml(Element root) throws Exception {
    if (! DomUtil.matches(root, ELEMENT_TICKET_TICKETINFO,
                          NAMESPACE_TICKET)) {
        throw new Exception("root element not ticketinfo");
    }

    Ticket ticket = new HibTicket();

    String id = DomUtil.getChildTextTrim(root, ELEMENT_TICKET_ID,
                                         NAMESPACE_TICKET);
    ticket.setKey(id);

    String timeout =
        DomUtil.getChildTextTrim(root, ELEMENT_TICKET_TIMEOUT,
                                 NAMESPACE_TICKET);
    ticket.setTimeout(timeout);

    Element pe = DomUtil.getChildElement(root, XML_PRIVILEGE, NAMESPACE);
    DavPrivilegeSet privileges = DavPrivilegeSet.createFromXml(pe);
    privileges.setTicketPrivileges(ticket);

    Element owner = DomUtil.getChildElement(root, XML_OWNER, NAMESPACE);
    String ownerHref =
        DomUtil.getChildTextTrim(owner, XML_HREF, NAMESPACE);

    return new TicketContent(ticket, ownerHref);
}
 
Example 8
Source File: ExceptionConverter.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
public static FileSystemException generate(final DavException davExc, final DavMethod method)
        throws FileSystemException {
    String msg = davExc.getMessage();
    if (davExc.hasErrorCondition()) {
        try {
            final Element error = davExc.toXml(DomUtil.BUILDER_FACTORY.newDocumentBuilder().newDocument());
            if (DomUtil.matches(error, DavException.XML_ERROR, DavConstants.NAMESPACE)) {
                if (DomUtil.hasChildElement(error, "exception", null)) {
                    final Element exc = DomUtil.getChildElement(error, "exception", null);
                    if (DomUtil.hasChildElement(exc, "message", null)) {
                        msg = DomUtil.getChildText(exc, "message", null);
                    }
                    if (DomUtil.hasChildElement(exc, "class", null)) {
                        final Class<?> cl = Class.forName(DomUtil.getChildText(exc, "class", null));
                        final Constructor<?> excConstr = cl.getConstructor(new Class[] { String.class });
                        if (excConstr != null) {
                            final Object o = excConstr.newInstance(new Object[] { msg });
                            if (o instanceof FileSystemException) {
                                return (FileSystemException) o;
                            } else if (o instanceof Exception) {
                                return new FileSystemException(msg, (Exception) o);
                            }
                        }
                    }
                }
            }
        } catch (final Exception e) {
            throw new FileSystemException(e);
        }
    }

    return new FileSystemException(msg);
}
 
Example 9
Source File: ExceptionConverter.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
public static FileSystemException generate(final DavException davExc) throws FileSystemException {
    String msg = davExc.getMessage();
    if (davExc.hasErrorCondition()) {
        try {
            final Element error = davExc.toXml(DomUtil.createDocument());
            if (DomUtil.matches(error, DavException.XML_ERROR, DavConstants.NAMESPACE)) {
                if (DomUtil.hasChildElement(error, "exception", null)) {
                    final Element exc = DomUtil.getChildElement(error, "exception", null);
                    if (DomUtil.hasChildElement(exc, "message", null)) {
                        msg = DomUtil.getChildText(exc, "message", null);
                    }
                    if (DomUtil.hasChildElement(exc, "class", null)) {
                        final Class<?> cl = Class.forName(DomUtil.getChildText(exc, "class", null));
                        final Constructor<?> excConstr = cl.getConstructor(new Class[] { String.class });
                        if (excConstr != null) {
                            final Object o = excConstr.newInstance(new Object[] { msg });
                            if (o instanceof FileSystemException) {
                                return (FileSystemException) o;
                            } else if (o instanceof Exception) {
                                return new FileSystemException(msg, (Exception) o);
                            }
                        }
                    }
                }
            }
        } catch (final Exception e) {
            throw new FileSystemException(e);
        }
    }

    return new FileSystemException(msg);
}
 
Example 10
Source File: StandardDavRequest.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * 
 * @throws CosmoDavException 
 */
private void parsePropPatchRequest() throws CosmoDavException {
    Document requestDocument = getSafeRequestDocument();
    if (requestDocument == null) {
        throw new BadRequestException("PROPPATCH requires entity body");
    }

    Element root = requestDocument.getDocumentElement();
    if (!DomUtil.matches(root, XML_PROPERTYUPDATE, NAMESPACE)) {
        throw new BadRequestException("Expected " + QN_PROPERTYUPDATE
                + " root element");
    }

    ElementIterator sets = DomUtil.getChildren(root, XML_SET, NAMESPACE);
    ElementIterator removes = DomUtil.getChildren(root, XML_REMOVE,
            NAMESPACE);
    if (!(sets.hasNext() || removes.hasNext())) {
        throw new BadRequestException("Expected at least one of "
                + QN_REMOVE + " and " + QN_SET + " as a child of "
                + QN_PROPERTYUPDATE);
    }

    Element prop = null;
    ElementIterator i = null;

    proppatchSet = new DavPropertySet();
    while (sets.hasNext()) {
        Element set = sets.nextElement();
        prop = DomUtil.getChildElement(set, XML_PROP, NAMESPACE);
        if (prop == null) {
            throw new BadRequestException("Expected " + QN_PROP
                    + " child of " + QN_SET);
        }
        i = DomUtil.getChildren(prop);
        while (i.hasNext()) {
            StandardDavProperty p = StandardDavProperty.createFromXml(i
                    .nextElement());
            proppatchSet.add(p);
        }
    }

    proppatchRemove = new DavPropertyNameSet();
    while (removes.hasNext()) {
        Element remove = removes.nextElement();
        prop = DomUtil.getChildElement(remove, XML_PROP, NAMESPACE);
        if (prop == null) {
            throw new BadRequestException("Expected " + QN_PROP
                    + " child of " + QN_REMOVE);
        }
        i = DomUtil.getChildren(prop);
        while (i.hasNext()){
            proppatchRemove.add(DavPropertyName.createFromXml(i
                    .nextElement()));
        }
    }
}
 
Example 11
Source File: StandardDavRequest.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * 
 * @return Ticket 
 * @throws CosmoDavException  
 */
private Ticket parseTicketRequest() throws CosmoDavException {
    Document requestDocument = getSafeRequestDocument();
    if (requestDocument == null) {
        throw new BadRequestException("MKTICKET requires entity body");
    }

    Element root = requestDocument.getDocumentElement();
    if (!DomUtil.matches(root, ELEMENT_TICKET_TICKETINFO, NAMESPACE_TICKET)) {
        throw new BadRequestException("Expected " + QN_TICKET_TICKETINFO
                + " root element");
    }
    if (DomUtil.hasChildElement(root, ELEMENT_TICKET_ID, NAMESPACE_TICKET)) {
        throw new BadRequestException(QN_TICKET_TICKETINFO
                + " may not contain child " + QN_TICKET_ID);
    }
    if (DomUtil.hasChildElement(root, XML_OWNER, NAMESPACE)) {
        throw new BadRequestException(QN_TICKET_TICKETINFO
                + " may not contain child " + QN_OWNER);
    }

    String timeout = DomUtil.getChildTextTrim(root, ELEMENT_TICKET_TIMEOUT,
            NAMESPACE_TICKET);
    if (timeout != null && !timeout.equals(TIMEOUT_INFINITE)) {
        try {
            int seconds = Integer.parseInt(timeout.substring(7));
            if (LOG.isTraceEnabled()) {
                LOG.trace("Timeout seconds: " + seconds);
            }
        } catch (NumberFormatException e) {
            throw new BadRequestException("Malformed " + QN_TICKET_TIMEOUT
                    + " value " + timeout);
        }
    } else {
        timeout = TIMEOUT_INFINITE;
    }

    // visit limits are not supported

    Element pe = DomUtil.getChildElement(root, XML_PRIVILEGE, NAMESPACE);
    if (pe == null) {
        throw new BadRequestException("Expected " + QN_PRIVILEGE
                + " child of " + QN_TICKET_TICKETINFO);
    }

    DavPrivilegeSet privileges = DavPrivilegeSet.createFromXml(pe);
    if (!privileges.containsAny(DavPrivilege.READ, DavPrivilege.WRITE,
            DavPrivilege.READ_FREE_BUSY)) {
        throw new BadRequestException("Empty or invalid " + QN_PRIVILEGE);
    }

    Ticket ticket = entityFactory.creatTicket();
    ticket.setTimeout(timeout);
    privileges.setTicketPrivileges(ticket);

    return ticket;
}