Search in sources :

Example 41 with Attribute

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

the class MailItemResource method patchProperties.

/* Modifies the set of dead properties saved for this resource.
     * Properties in the parameter 'set' are added to the existing properties.
     * Properties in 'remove' are removed.
     */
@Override
public void patchProperties(DavContext ctxt, java.util.Collection<Element> set, java.util.Collection<QName> remove) throws DavException, IOException {
    List<QName> reqProps = new ArrayList<QName>();
    for (QName n : remove) {
        mDeadProps.remove(n);
        reqProps.add(n);
    }
    for (Element e : set) {
        QName name = e.getQName();
        if (name.equals(DavElements.E_DISPLAYNAME) && (type == MailItem.Type.FOLDER || type == MailItem.Type.MOUNTPOINT)) {
            // rename folder
            try {
                String val = e.getText();
                String uri = getUri();
                Mailbox mbox = getMailbox(ctxt);
                mbox.rename(ctxt.getOperationContext(), mId, type, val, mFolderId);
                setProperty(DavElements.P_DISPLAYNAME, val);
                UrlNamespace.addToRenamedResource(getOwner(), uri, this);
                UrlNamespace.addToRenamedResource(getOwner(), uri.substring(0, uri.length() - 1), this);
            } catch (ServiceException se) {
                ctxt.getResponseProp().addPropError(DavElements.E_DISPLAYNAME, new DavException(se.getMessage(), DavProtocol.STATUS_FAILED_DEPENDENCY));
            }
            mDeadProps.remove(name);
            continue;
        } else if (name.equals(DavElements.E_CALENDAR_COLOR) && (type == MailItem.Type.FOLDER || type == MailItem.Type.MOUNTPOINT)) {
            // change color
            String colorStr = e.getText();
            Color color = new Color(colorStr.substring(0, 7));
            byte col = (byte) COLOR_LIST.indexOf(colorStr);
            if (col >= 0)
                color.setColor(col);
            try {
                Mailbox mbox = getMailbox(ctxt);
                mbox.setColor(ctxt.getOperationContext(), new int[] { mId }, type, color);
            } catch (ServiceException se) {
                ctxt.getResponseProp().addPropError(DavElements.E_CALENDAR_COLOR, new DavException(se.getMessage(), DavProtocol.STATUS_FAILED_DEPENDENCY));
            }
            mDeadProps.remove(name);
            continue;
        } else if (name.equals(DavElements.E_SUPPORTED_CALENDAR_COMPONENT_SET)) {
            // change default view
            @SuppressWarnings("unchecked") List<Element> elements = e.elements(DavElements.E_COMP);
            boolean isTodo = false;
            boolean isEvent = false;
            for (Element element : elements) {
                Attribute attr = element.attribute(DavElements.P_NAME);
                if (attr != null && CalComponent.VTODO.name().equals(attr.getValue())) {
                    isTodo = true;
                } else if (attr != null && CalComponent.VEVENT.name().equals(attr.getValue())) {
                    isEvent = true;
                }
            }
            if (isEvent ^ isTodo) {
                // we support a calendar collection of type event or todo, not both or none.
                Type type = (isEvent) ? Type.APPOINTMENT : Type.TASK;
                try {
                    Mailbox mbox = getMailbox(ctxt);
                    mbox.setFolderDefaultView(ctxt.getOperationContext(), mId, type);
                    // See UrlNamespace.addToRenamedResource()
                    if (this instanceof Collection) {
                        ((Collection) this).view = type;
                    }
                } catch (ServiceException se) {
                    ctxt.getResponseProp().addPropError(name, new DavException(se.getMessage(), DavProtocol.STATUS_FAILED_DEPENDENCY));
                }
            } else {
                ctxt.getResponseProp().addPropError(name, new DavException.CannotModifyProtectedProperty(name));
            }
            continue;
        }
        mDeadProps.put(name, e);
        reqProps.add(name);
    }
    String configVal = "";
    if (mDeadProps.size() > 0) {
        org.dom4j.Document doc = org.dom4j.DocumentHelper.createDocument();
        Element top = doc.addElement(CONFIG_KEY);
        for (Map.Entry<QName, Element> entry : mDeadProps.entrySet()) top.add(entry.getValue().detach());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OutputFormat format = OutputFormat.createCompactFormat();
        XMLWriter writer = new XMLWriter(out, format);
        writer.write(doc);
        configVal = new String(out.toByteArray(), "UTF-8");
        if (configVal.length() > PROP_LENGTH_LIMIT)
            for (Map.Entry<QName, Element> entry : mDeadProps.entrySet()) ctxt.getResponseProp().addPropError(entry.getKey(), new DavException("prop length exceeded", DavProtocol.STATUS_INSUFFICIENT_STORAGE));
    }
    Mailbox mbox = null;
    try {
        mbox = getMailbox(ctxt);
        mbox.lock.lock();
        Metadata data = mbox.getConfig(ctxt.getOperationContext(), CONFIG_KEY);
        if (data == null) {
            data = new Metadata();
        }
        data.put(Integer.toString(mId), configVal);
        mbox.setConfig(ctxt.getOperationContext(), CONFIG_KEY, data);
    } catch (ServiceException se) {
        for (QName qname : reqProps) ctxt.getResponseProp().addPropError(qname, new DavException(se.getMessage(), HttpServletResponse.SC_FORBIDDEN));
    } finally {
        if (mbox != null)
            mbox.lock.release();
    }
}
Also used : Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) Metadata(com.zimbra.cs.mailbox.Metadata) XMLWriter(org.dom4j.io.XMLWriter) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) DavException(com.zimbra.cs.dav.DavException) QName(org.dom4j.QName) Color(com.zimbra.common.mailbox.Color) OutputFormat(org.dom4j.io.OutputFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Type(com.zimbra.cs.mailbox.MailItem.Type) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 42 with Attribute

use of org.dom4j.Attribute in project Openfire by igniterealtime.

the class MUCRoomHistory method addOldMessage.

/**
 * Creates a new message and adds it to the history. The new message will be created based on
 * the provided information. This information will likely come from the database when loading
 * the room history from the database.
 *
 * @param senderJID the sender's JID of the message to add to the history.
 * @param nickname the sender's nickname of the message to add to the history.
 * @param sentDate the date when the message was sent to the room.
 * @param subject the subject included in the message.
 * @param body the body of the message.
 * @param stanza the stanza to add
 */
public void addOldMessage(String senderJID, String nickname, Date sentDate, String subject, String body, String stanza) {
    Message message = new Message();
    message.setType(Message.Type.groupchat);
    if (stanza != null) {
        // payload initialized as XML string from DB
        try {
            Element element = SAXReaderUtil.readRootElement(stanza);
            for (Element child : (List<Element>) element.elements()) {
                Namespace ns = child.getNamespace();
                if (ns == null || ns.getURI().equals("jabber:client") || ns.getURI().equals("jabber:server")) {
                    continue;
                }
                Element added = message.addChildElement(child.getName(), child.getNamespaceURI());
                if (!child.getText().isEmpty()) {
                    added.setText(child.getText());
                }
                for (Attribute attr : (List<Attribute>) child.attributes()) {
                    added.addAttribute(attr.getQName(), attr.getValue());
                }
                for (Element el : (List<Element>) child.elements()) {
                    added.add(el.createCopy());
                }
            }
            if (element.attribute("id") != null) {
                message.setID(element.attributeValue("id"));
            }
        } catch (Exception ex) {
            Log.error("Failed to parse payload XML", ex);
            if (ex instanceof InterruptedException) {
                Thread.currentThread().interrupt();
            }
        }
    }
    message.setSubject(subject);
    message.setBody(body);
    // Set the sender of the message
    if (nickname != null && nickname.trim().length() > 0) {
        JID roomJID = getRoom().getRole().getRoleAddress();
        // Recreate the sender address based on the nickname and room's JID
        message.setFrom(new JID(roomJID.getNode(), roomJID.getDomain(), nickname, true));
    } else {
        // Set the room as the sender of the message
        message.setFrom(getRoom().getRole().getRoleAddress());
    }
    // Add the delay information to the message
    Element delayInformation = message.addChildElement("delay", "urn:xmpp:delay");
    delayInformation.addAttribute("stamp", XMPPDateTimeFormat.format(sentDate));
    if (getRoom().canAnyoneDiscoverJID()) {
        // Set the Full JID as the "from" attribute
        delayInformation.addAttribute("from", senderJID);
    } else {
        // Set the Room JID as the "from" attribute
        delayInformation.addAttribute("from", getRoom().getRole().getRoleAddress().toString());
    }
    historyStrategy.addMessage(message);
}
Also used : Message(org.xmpp.packet.Message) JID(org.xmpp.packet.JID) Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) Namespace(org.dom4j.Namespace) IOException(java.io.IOException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 43 with Attribute

use of org.dom4j.Attribute in project Openfire by igniterealtime.

the class MediaProxyService method processIQ.

private void processIQ(IQ iq) {
    IQ reply = IQ.createResultIQ(iq);
    Element childElement = iq.getChildElement();
    String namespace = childElement.getNamespaceURI();
    Element childElementCopy = iq.getChildElement().createCopy();
    reply.setChildElement(childElementCopy);
    if ("http://jabber.org/protocol/disco#info".equals(namespace)) {
        reply = XMPPServer.getInstance().getIQDiscoInfoHandler().handleIQ(iq);
        router.route(reply);
        return;
    } else if ("http://jabber.org/protocol/disco#items".equals(namespace)) {
        // a component
        reply = XMPPServer.getInstance().getIQDiscoItemsHandler().handleIQ(iq);
        router.route(reply);
        return;
    } else if (NAMESPACE.equals(namespace) && enabled) {
        Element candidateElement = childElementCopy.element("candidate");
        String sid = childElementCopy.attribute("sid").getValue() + "-" + iq.getFrom();
        if (candidateElement != null) {
            childElementCopy.remove(candidateElement);
            Element candidate = childElementCopy.addElement("candidate ");
            ProxyCandidate proxyCandidate = mediaProxy.addRelayAgent(sid, iq.getFrom().toString());
            Log.debug("MediaProxyService: " + sid);
            proxyCandidate.start();
            candidate.addAttribute("name", "voicechannel");
            candidate.addAttribute("ip", mediaProxy.getPublicIP());
            candidate.addAttribute("porta", String.valueOf(proxyCandidate.getLocalPortA()));
            candidate.addAttribute("portb", String.valueOf(proxyCandidate.getLocalPortB()));
            candidate.addAttribute("pass", proxyCandidate.getPass());
        } else {
            candidateElement = childElementCopy.element("relay");
            if (candidateElement != null) {
                MediaProxySession session = mediaProxy.getSession(sid);
                Log.debug("MediaProxyService: " + sid);
                if (session != null) {
                    Attribute pass = candidateElement.attribute("pass");
                    if (pass != null && pass.getValue().trim().equals(session.getPass().trim())) {
                        Attribute portA = candidateElement.attribute("porta");
                        Attribute portB = candidateElement.attribute("portb");
                        Attribute hostA = candidateElement.attribute("hosta");
                        Attribute hostB = candidateElement.attribute("hostb");
                        try {
                            if (hostA != null && portA != null) {
                                for (int i = 0; i < 2; i++) {
                                    session.sendFromPortA(hostB.getValue(), Integer.parseInt(portB.getValue()));
                                }
                            }
                        } catch (Exception e) {
                            Log.error(e.getMessage(), e);
                        }
                    } else {
                        reply.setError(PacketError.Condition.forbidden);
                    }
                }
                childElementCopy.remove(candidateElement);
            } else {
                candidateElement = childElementCopy.element("publicip");
                if (candidateElement != null) {
                    childElementCopy.remove(candidateElement);
                    Element publicIp = childElementCopy.addElement("publicip");
                    try {
                        String ip = sessionManager.getSession(iq.getFrom()).getHostAddress();
                        if (ip != null) {
                            publicIp.addAttribute("ip", ip);
                        }
                    } catch (UnknownHostException e) {
                        Log.error(e.getMessage(), e);
                    }
                } else {
                    childElementCopy.remove(candidateElement);
                    reply.setError(PacketError.Condition.forbidden);
                }
            }
        }
    } else {
        // Answer an error since the server can't handle the requested namespace
        reply.setError(PacketError.Condition.service_unavailable);
    }
    try {
        if (Log.isDebugEnabled()) {
            Log.debug("MediaProxyService: RETURNED:" + reply.toXML());
        }
        router.route(reply);
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) PacketException(org.jivesoftware.openfire.PacketException) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) SocketException(java.net.SocketException) UnknownHostException(java.net.UnknownHostException)

Example 44 with Attribute

use of org.dom4j.Attribute in project gocd by gocd.

the class JUnitReportGenerator method setAttr.

private static void setAttr(int i, Element test, final String attr) {
    Attribute classname = test.attribute(attr);
    classname.setValue(classname.getValue() + i);
}
Also used : Attribute(org.dom4j.Attribute)

Example 45 with Attribute

use of org.dom4j.Attribute in project tinker by Tencent.

the class ManifestDecoder method copyAttributes.

private void copyAttributes(Element srcNode, Element destNode) {
    for (Object attrObj : srcNode.attributes()) {
        final Attribute attr = (Attribute) attrObj;
        destNode.addAttribute(attr.getQName(), attr.getValue());
    }
}
Also used : Attribute(org.dom4j.Attribute)

Aggregations

Attribute (org.dom4j.Attribute)114 Element (org.dom4j.Element)88 Document (org.dom4j.Document)30 ArrayList (java.util.ArrayList)28 List (java.util.List)26 Iterator (java.util.Iterator)23 Node (org.dom4j.Node)14 HashMap (java.util.HashMap)12 File (java.io.File)11 SAXReader (org.dom4j.io.SAXReader)11 IOException (java.io.IOException)10 Map (java.util.Map)6 VFSItem (org.olat.core.util.vfs.VFSItem)6 QTIObject (org.olat.ims.qti.editor.beecom.objects.QTIObject)6 Namespace (org.dom4j.Namespace)5 QName (org.dom4j.QName)5 PropertyString (org.pentaho.commons.util.repository.type.PropertyString)5 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)4 OutcomesProcessing (org.olat.ims.qti.editor.beecom.objects.OutcomesProcessing)4 AnnotatedElement (java.lang.reflect.AnnotatedElement)3