Search in sources :

Example 91 with Document

use of org.dom4j.Document in project zm-mailbox by Zimbra.

the class WebDavClient method sendMultiResponseRequest.

public Collection<DavObject> sendMultiResponseRequest(DavRequest req) throws IOException, DavException {
    ArrayList<DavObject> ret = new ArrayList<DavObject>();
    HttpMethod m = null;
    try {
        m = executeFollowRedirect(req);
        int status = m.getStatusCode();
        if (status >= 400) {
            throw new DavException("DAV server returned an error: " + status, status);
        }
        Document doc = W3cDomUtil.parseXMLToDom4jDocUsingSecureProcessing(m.getResponseBodyAsStream());
        Element top = doc.getRootElement();
        for (Object obj : top.elements(DavElements.E_RESPONSE)) {
            if (obj instanceof Element) {
                ret.add(new DavObject((Element) obj));
            }
        }
    } catch (XmlParseException e) {
        throw new DavException("can't parse response", e);
    } finally {
        if (m != null) {
            m.releaseConnection();
        }
    }
    return ret;
}
Also used : DavException(com.zimbra.cs.dav.DavException) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) XmlParseException(com.zimbra.common.soap.XmlParseException) Document(org.dom4j.Document) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 92 with Document

use of org.dom4j.Document in project zm-mailbox by Zimbra.

the class Acl method handle.

public void handle(DavContext ctxt) throws DavException, IOException, ServiceException {
    DavResource rs = ctxt.getRequestedResource();
    if (!rs.isCollection() || !(rs instanceof MailItemResource))
        throw new DavException("acl not implemented for non-collection resource", HttpServletResponse.SC_NOT_IMPLEMENTED);
    if (!ctxt.hasRequestMessage())
        throw new DavException("empty request", HttpServletResponse.SC_BAD_REQUEST);
    Document reqMsg = ctxt.getRequestMessage();
    Element acl = reqMsg.getRootElement();
    if (!acl.getQName().equals(DavElements.E_ACL))
        throw new DavException("request does not start with acl element", HttpServletResponse.SC_BAD_REQUEST);
    List<Element> aceElements = acl.elements(DavElements.E_ACE);
    ArrayList<Ace> aceList = new ArrayList<Ace>();
    for (Element ace : aceElements) aceList.add(new Ace(ace));
    MailItemResource mir = (MailItemResource) rs;
    mir.setAce(ctxt, aceList);
}
Also used : Ace(com.zimbra.cs.dav.property.Acl.Ace) DavResource(com.zimbra.cs.dav.resource.DavResource) MailItemResource(com.zimbra.cs.dav.resource.MailItemResource) DavException(com.zimbra.cs.dav.DavException) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) Document(org.dom4j.Document)

Example 93 with Document

use of org.dom4j.Document in project zm-mailbox by Zimbra.

the class PropFind method handle.

@Override
public void handle(DavContext ctxt) throws DavException, IOException, ServiceException {
    if (ctxt.hasRequestMessage()) {
        Document req = ctxt.getRequestMessage();
        Element top = req.getRootElement();
        if (!top.getName().equals(DavElements.P_PROPFIND)) {
            throw new DavException("msg " + top.getName() + " not allowed in PROPFIND", HttpServletResponse.SC_BAD_REQUEST, null);
        }
    }
    RequestProp reqProp = ctxt.getRequestProp();
    DavResponse resp = ctxt.getDavResponse();
    if (ctxt.getDepth() == Depth.one) {
        resp.addResources(ctxt, ctxt.getAllRequestedResources(), reqProp);
    } else {
        DavResource resource = ctxt.getRequestedResource();
        resp.addResource(ctxt, resource, reqProp, false);
    }
    sendResponse(ctxt);
}
Also used : DavResource(com.zimbra.cs.dav.resource.DavResource) DavException(com.zimbra.cs.dav.DavException) DavResponse(com.zimbra.cs.dav.service.DavResponse) RequestProp(com.zimbra.cs.dav.DavContext.RequestProp) Element(org.dom4j.Element) Document(org.dom4j.Document)

Example 94 with Document

use of org.dom4j.Document in project zm-mailbox by Zimbra.

the class Lock method handle.

@Override
public void handle(DavContext ctxt) throws DavException, IOException, ServiceException {
    LockMgr lockmgr = LockMgr.getInstance();
    LockMgr.Lock lock = null;
    if (ctxt.hasRequestMessage()) {
        DavContext.Depth depth = ctxt.getDepth();
        if (depth == Depth.one)
            throw new DavException("invalid depth", HttpServletResponse.SC_BAD_REQUEST, null);
        String d = (depth == Depth.zero) ? "0" : depth.toString();
        LockMgr.LockScope scope = LockScope.shared;
        LockMgr.LockType type = LockType.write;
        Document req = ctxt.getRequestMessage();
        Element top = req.getRootElement();
        if (!top.getName().equals(DavElements.P_LOCKINFO))
            throw new DavException("msg " + top.getName() + " not allowed in LOCK", HttpServletResponse.SC_BAD_REQUEST, null);
        Element e = top.element(DavElements.E_LOCKSCOPE);
        @SuppressWarnings("unchecked") List<Element> ls = e.elements();
        for (Element v : ls) {
            if (v.getQName().equals(DavElements.E_EXCLUSIVE))
                scope = LockScope.exclusive;
            else if (v.getQName().equals(DavElements.E_SHARED))
                scope = LockScope.shared;
            else
                throw new DavException("unrecognized scope element " + v.toString(), HttpServletResponse.SC_BAD_REQUEST, null);
        }
        e = top.element(DavElements.E_LOCKTYPE);
        @SuppressWarnings("unchecked") List<Element> lt = e.elements();
        for (Element v : lt) {
            if (v.getQName().equals(DavElements.E_WRITE))
                type = LockType.write;
            else
                throw new DavException("unrecognized type element " + v.toString(), HttpServletResponse.SC_BAD_REQUEST, null);
        }
        String owner;
        e = top.element(DavElements.E_OWNER);
        if (e != null && e.elementIterator(DavElements.E_HREF).hasNext()) {
            Element ownerElem = (Element) e.elementIterator(DavElements.E_HREF).next();
            owner = ownerElem.getText();
        } else {
            owner = ctxt.getAuthAccount().getName();
        }
        lock = lockmgr.createLock(ctxt, owner, ctxt.getUri(), type, scope, d);
        ctxt.getResponse().addHeader(DavProtocol.HEADER_LOCK_TOKEN, lock.toLockTokenHeader());
    } else {
        // refresh lock
        String token = ctxt.getRequest().getHeader(DavProtocol.HEADER_IF);
        if (token == null) {
            throw new DavException("no request body", HttpServletResponse.SC_BAD_REQUEST, null);
        }
        token = token.trim();
        int len = token.length();
        if (token.charAt(0) == '(' && token.charAt(len - 1) == ')') {
            token = token.substring(1, len - 1);
        }
        List<LockMgr.Lock> locks = lockmgr.getLocks(ctxt.getUri());
        for (LockMgr.Lock l : locks) {
            if (l.token.equals(LockMgr.Lock.parseLockTokenHeader(token))) {
                l.extendExpiration();
                lock = l;
                break;
            }
        }
        if (lock == null) {
            throw new DavException("Lock does not exist", HttpServletResponse.SC_PRECONDITION_FAILED, null);
        }
    }
    ctxt.getDavResponse().addProperty(ctxt, new LockDiscovery(lock));
    ctxt.setStatus(HttpServletResponse.SC_OK);
    sendResponse(ctxt);
}
Also used : LockMgr(com.zimbra.cs.dav.LockMgr) DavException(com.zimbra.cs.dav.DavException) Element(org.dom4j.Element) Depth(com.zimbra.cs.dav.DavContext.Depth) Document(org.dom4j.Document) LockDiscovery(com.zimbra.cs.dav.property.LockDiscovery) LockScope(com.zimbra.cs.dav.LockMgr.LockScope) LockType(com.zimbra.cs.dav.LockMgr.LockType) DavContext(com.zimbra.cs.dav.DavContext)

Example 95 with Document

use of org.dom4j.Document in project zm-mailbox by Zimbra.

the class XMLChartConfig method load.

/**
     * Loads chart settings from the specified XML file.
     *
     * @throws IOException
     *             if there was an error reading the file
     * @throws DocumentException
     *             if there was an error parsing the file
     * @throws XmlParseException
     * @throws IllegalArgumentException
     *             if any attribute has invalid value
     */
public static List<ChartSettings> load(File xmlFile) throws IOException, DocumentException, XmlParseException {
    List<ChartSettings> charts = new ArrayList<ChartSettings>();
    Document document;
    try (FileInputStream fis = new FileInputStream(xmlFile)) {
        document = W3cDomUtil.parseXMLToDom4jDocUsingSecureProcessing(fis);
    }
    Element chartsElem = document.getRootElement();
    if (!chartsElem.getName().equals(E_CHARTS)) {
        throw new DocumentException("Missing <" + E_CHARTS + "> root element");
    }
    for (Iterator iter = chartsElem.elementIterator(E_CHART); iter.hasNext(); ) {
        Element chartElem = (Element) iter.next();
        String chartTitle = getAttr(chartElem, A_CHART_TITLE);
        String category = getAttr(chartElem, A_CHART_CATEGORY, "unknown");
        String outfile = getAttr(chartElem, A_CHART_OUTFILE);
        // inheritable attributes
        String xAxis = getInheritedAttr(chartElem, A_CHART_XAXIS, ChartSettings.DEFAULT_CHART_XAXIS);
        String yAxis = getInheritedAttr(chartElem, A_CHART_YAXIS, "");
        boolean allowLogScale = getInheritedAttrBoolean(chartElem, A_CHART_ALLOW_LOG_SCALE, ChartSettings.DEFAULT_CHART_ALLOW_LOG_SCALE);
        boolean plotZero = getInheritedAttrBoolean(chartElem, A_CHART_PLOT_ZERO, ChartSettings.DEFAULT_CHART_PLOT_ZERO);
        if (!allowLogScale)
            plotZero = true;
        int width = getInheritedAttrInt(chartElem, A_CHART_WIDTH, ChartSettings.DEFAULT_CHART_WIDTH);
        int height = getInheritedAttrInt(chartElem, A_CHART_HEIGHT, ChartSettings.DEFAULT_CHART_HEIGHT);
        String outDoc = getAttr(chartElem, A_CHART_DOCUMENT, null);
        String topPlotStr = getAttr(chartElem, A_CHART_TOP_PLOTS, null);
        int topPlots = -1;
        if (topPlotStr != null)
            topPlots = Integer.parseInt(topPlotStr);
        topPlotStr = getAttr(chartElem, A_CHART_TOP_PLOTS_TYPE, "max");
        ChartSettings.TopPlotsType topPlotsType = ChartSettings.TopPlotsType.valueOf(topPlotStr.toUpperCase());
        ChartSettings chart = new ChartSettings(chartTitle, category, outfile, xAxis, yAxis, allowLogScale, plotZero, width, height, outDoc, topPlots, topPlotsType);
        for (Iterator plotIter = chartElem.elementIterator(E_PLOT); plotIter.hasNext(); ) {
            Element plotElem = (Element) plotIter.next();
            String dataCol = getAttr(plotElem, A_PLOT_DATA_COLUMN, null);
            // inheritable attributes
            String legend = getInheritedAttr(plotElem, A_PLOT_LEGEND, null);
            String infile = getInheritedAttr(plotElem, A_PLOT_INFILE, null);
            boolean showRaw = getInheritedAttrBoolean(plotElem, A_PLOT_SHOW_RAW, PlotSettings.DEFAULT_PLOT_SHOW_RAW);
            boolean showMovingAvg = getInheritedAttrBoolean(plotElem, A_PLOT_SHOW_MOVING_AVG, PlotSettings.DEFAULT_PLOT_SHOW_MOVING_AVG);
            int movingAvgPoints = getInheritedAttrInt(plotElem, A_PLOT_MOVING_AVG_POINTS, PlotSettings.DEFAULT_PLOT_MOVING_AVG_POINTS);
            double multiplier = getInheritedAttrDouble(plotElem, A_PLOT_MULTIPLIER, PlotSettings.DEFAULT_PLOT_MULTIPLIER);
            double divisor = getInheritedAttrDouble(plotElem, A_PLOT_DIVISOR, PlotSettings.DEFAULT_PLOT_DIVISOR);
            boolean nonNegative = getInheritedAttrBoolean(plotElem, A_PLOT_NON_NEGATIVE, PlotSettings.DEFAULT_PLOT_NON_NEGATIVE);
            boolean percentTime = getInheritedAttrBoolean(plotElem, A_PLOT_PERCENT_TIME, PlotSettings.DEFAULT_PLOT_PERCENT_TIME);
            String dataFunction = getAttr(plotElem, A_PLOT_DATA_FUNCTION, PlotSettings.DEFAULT_PLOT_DATA_FUNCTION);
            String aggFunction = getAttr(plotElem, A_PLOT_AGGREGATE_FUNCTION, PlotSettings.DEFAULT_PLOT_AGGREGATE_FUNCTION);
            String ratioTop = getAttr(plotElem, A_PLOT_RATIO_TOP, null);
            String ratioBottom = getAttr(plotElem, A_PLOT_RATIO_BOTTOM, null);
            if ((ratioTop == null && ratioBottom != null) || (ratioTop != null && ratioBottom == null)) {
                throw new DocumentException("Both ratioTop/ratioBottom need to be specified");
            }
            if ((ratioTop == null && dataCol == null) || (ratioTop != null && dataCol != null)) {
                throw new DocumentException("Specify either ratio or data");
            }
            boolean optional = getInheritedAttrBoolean(plotElem, A_PLOT_OPTIONAL, PlotSettings.DEFAULT_PLOT_OPTIONAL);
            PlotSettings plot = new PlotSettings(legend, infile, dataCol, showRaw, showMovingAvg, movingAvgPoints, multiplier, divisor, nonNegative, percentTime, dataFunction, aggFunction, optional, ratioTop, ratioBottom);
            chart.addPlot(plot);
        }
        for (Iterator plotIter = chartElem.elementIterator(E_GROUP_PLOT); plotIter.hasNext(); ) {
            Element plotElem = (Element) plotIter.next();
            String dataCol = getAttr(plotElem, A_PLOT_DATA_COLUMN);
            // inheritable attributes
            String infile = getInheritedAttr(plotElem, A_PLOT_INFILE, null);
            boolean showRaw = getInheritedAttrBoolean(plotElem, A_PLOT_SHOW_RAW, PlotSettings.DEFAULT_PLOT_SHOW_RAW);
            boolean showMovingAvg = getInheritedAttrBoolean(plotElem, A_PLOT_SHOW_MOVING_AVG, PlotSettings.DEFAULT_PLOT_SHOW_MOVING_AVG);
            int movingAvgPoints = getInheritedAttrInt(plotElem, A_PLOT_MOVING_AVG_POINTS, PlotSettings.DEFAULT_PLOT_MOVING_AVG_POINTS);
            double multiplier = getInheritedAttrDouble(plotElem, A_PLOT_MULTIPLIER, PlotSettings.DEFAULT_PLOT_MULTIPLIER);
            double divisor = getInheritedAttrDouble(plotElem, A_PLOT_DIVISOR, PlotSettings.DEFAULT_PLOT_DIVISOR);
            boolean nonNegative = getInheritedAttrBoolean(plotElem, A_PLOT_NON_NEGATIVE, PlotSettings.DEFAULT_PLOT_NON_NEGATIVE);
            boolean percentTime = getInheritedAttrBoolean(plotElem, A_PLOT_PERCENT_TIME, PlotSettings.DEFAULT_PLOT_PERCENT_TIME);
            String dataFunction = getAttr(plotElem, A_PLOT_DATA_FUNCTION, PlotSettings.DEFAULT_PLOT_DATA_FUNCTION);
            String aggFunction = getAttr(plotElem, A_PLOT_AGGREGATE_FUNCTION, PlotSettings.DEFAULT_PLOT_AGGREGATE_FUNCTION);
            String groupBy = getAttr(plotElem, A_PLOT_GROUP_BY);
            String ignore = getAttr(plotElem, A_PLOT_IGNORE, null);
            boolean optional = getInheritedAttrBoolean(plotElem, A_PLOT_OPTIONAL, PlotSettings.DEFAULT_PLOT_OPTIONAL);
            GroupPlotSettings plot = new GroupPlotSettings(groupBy, ignore, infile, dataCol, showRaw, showMovingAvg, movingAvgPoints, multiplier, divisor, nonNegative, percentTime, dataFunction, aggFunction, optional);
            chart.addPlot(plot);
        }
        charts.add(chart);
    }
    return charts;
}
Also used : Element(org.dom4j.Element) ArrayList(java.util.ArrayList) Document(org.dom4j.Document) FileInputStream(java.io.FileInputStream) DocumentException(org.dom4j.DocumentException) Iterator(java.util.Iterator)

Aggregations

Document (org.dom4j.Document)288 Element (org.dom4j.Element)192 SAXReader (org.dom4j.io.SAXReader)143 Test (org.junit.Test)102 StringReader (java.io.StringReader)88 File (java.io.File)57 ArrayList (java.util.ArrayList)47 List (java.util.List)40 IOException (java.io.IOException)27 DocumentException (org.dom4j.DocumentException)25 XMLWriter (org.dom4j.io.XMLWriter)22 HashMap (java.util.HashMap)19 Map (java.util.Map)18 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)17 LinkedList (java.util.LinkedList)16 Attribute (org.dom4j.Attribute)15 Node (org.dom4j.Node)14 FileOutputStream (java.io.FileOutputStream)11 DavException (com.zimbra.cs.dav.DavException)10 OutputFormat (org.dom4j.io.OutputFormat)9