Example usage for org.dom4j Element getNamespaceURI

List of usage examples for org.dom4j Element getNamespaceURI

Introduction

In this page you can find the example usage for org.dom4j Element getNamespaceURI.

Prototype

String getNamespaceURI();

Source Link

Document

Returns the URI mapped to the namespace of this element if one exists otherwise an empty String is returned.

Usage

From source file:com.thinkberg.webdav.data.DavResource.java

License:Apache License

protected boolean setPropertyValue(Element root, Element propertyEl) {
    LogFactory.getLog(getClass()).debug(String.format("[%s].set('%s')", object.getName(), propertyEl.asXML()));

    if (!ALL_PROPERTIES.contains(propertyEl.getName())) {
        final String nameSpace = propertyEl.getNamespaceURI();
        final String attributeName = getFQName(nameSpace, propertyEl.getName());
        try {//  ww  w.java  2 s .co m
            FileContent objectContent = object.getContent();
            final String command = propertyEl.getParent().getParent().getName();
            if (TAG_PROP_SET.equals(command)) {
                StringWriter propertyValueWriter = new StringWriter();
                propertyEl.write(propertyValueWriter);
                propertyValueWriter.close();
                objectContent.setAttribute(attributeName, propertyValueWriter.getBuffer().toString());
            } else if (TAG_PROP_REMOVE.equals(command)) {
                objectContent.setAttribute(attributeName, null);
            }
            root.addElement(propertyEl.getQName());
            return true;
        } catch (IOException e) {
            LogFactory.getLog(getClass()).error(String.format("can't store attribute property '%s' = '%s'",
                    attributeName, propertyEl.asXML()), e);
        }
    }
    return false;
}

From source file:de.tu_berlin.cit.intercloud.xmpp.component.ResourceContainerSocketManager.java

License:Apache License

/**
 * This method handles the IQ stanzas of type <tt>result</tt>
 * that are received by the component./* w ww.jav a 2s .  c  om*/
 * 
 * @param iq
 *            The IQ stanza of type <tt>result</tt> that was received by
 *            this component.
 */
protected void handleIQResult(IQ iq) {
    logger.info("the following iq result stanza has been received:" + iq.toString());

    // IQ get (and set) stanza's MUST be replied to.
    final Element childElement = iq.getChildElement();
    String namespace = null;
    if (childElement != null) {
        namespace = childElement.getNamespaceURI();
    }
    if (namespace == null) {
        logger.info("(serving component '{}') Invalid XMPP " + "- no child element or namespace in IQ "
                + "request (packetId {})", this.component.getName(), iq.getID());
        // this isn't valid XMPP.
        return;
    }
    if (ResourceContainerComponent.NAMESPACE_DISCO_ITEMS.equals(namespace)) {
        logger.info("discovery item result.");
        @SuppressWarnings("rawtypes")
        Iterator iter = childElement.elementIterator();
        String rootJID = null;
        ArrayList<String> exchangeJIDs = new ArrayList<String>();
        ArrayList<String> gatewayJIDs = new ArrayList<String>();
        while (iter.hasNext()) {
            Element item = (Element) iter.next();
            // filter
            if (item.attributeValue("name").equals(ServiceNames.RootComponentName)) {
                rootJID = item.attributeValue("jid");
            } else if (item.attributeValue("name").equals(ServiceNames.ExchangeComponentName)) {
                exchangeJIDs.add(item.attributeValue("jid"));
            } else if (item.attributeValue("name").equals(ServiceNames.GatewayComponentName)) {
                gatewayJIDs.add(item.attributeValue("jid"));
            }
        }
        IntercloudDiscoItems discoItems = new IntercloudDiscoItems(rootJID, exchangeJIDs, gatewayJIDs);
        try {
            discoItems = discoItemExchanger.exchange(discoItems);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else if (ResourceContainerComponent.NAMESPACE_DISCO_INFO.equals(namespace)) {
        logger.info("discovery info result.");
        @SuppressWarnings("rawtypes")
        Iterator iter = childElement.elementIterator();
        ArrayList<String> features = new ArrayList<String>();
        while (iter.hasNext()) {
            Element feature = (Element) iter.next();
            if (feature.getName().equals("feature")) {
                features.add(feature.attributeValue("var"));
            }
        }
        IntercloudDiscoFeatures discoFeatures = new IntercloudDiscoFeatures(iq.getFrom().toBareJID(), features);
        try {
            discoFeatures = discoFeatureExchanger.exchange(discoFeatures);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else if (ResourceContainerComponent.NAMESPACE_REST_XWADL.equals(namespace)) {
        logger.info("received xwadl iq.");
        try {
            handleRestXWADL(iq.getID(), ResourceTypeDocument.Factory.parse(childElement.asXML()));
        } catch (XmlException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } else if (ResourceContainerComponent.NAMESPACE_REST_XML.equals(namespace)) {
        logger.info("received rest xml iq.");
        try {
            handleRestXML(iq.getID(), ResourceDocument.Factory.parse(childElement.asXML()));
        } catch (XmlException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:de.tu_berlin.cit.intercloud.xmpp.core.component.AbstractComponent.java

License:Open Source License

/**
 * Processes IQ request stanzas (IQ stanzas of type <tt>get</tt> or
 * <tt>set</tt>. This method will, in order:
 * /*from   ww  w  .  j a  v  a2 s . c o  m*/
 * <ol>
 * <li>check if the stanza is a valid request stanza. If not, an IQ stanza
 * of type <tt>error</tt>, condition 'bad-request' is returned;</li>
 * <li>process Service Discovery requests by calling
 * {@link #handleDiscoInfo(IQ)} and {@link #handleDiscoItems(IQ)} where
 * appropriate;</li>
 * <li>call the abstract methods {@link #handleIQGet()} or
 * {@link #handleIQSet()} if the above actions did not apply to the request.
 * </li>
 * </ol>
 * 
 * Note that if this method returns <tt>null</tt>, an IQ stanza of type
 * <tt>error</tt>, condition <tt>feature-not-implemented</tt> will be
 * returned to the sender of the original request. This behavior can be
 * disabled by setting the <tt>enforceIQResult</tt> argument in the
 * constructor to <tt>false</tt>.
 * 
 * Note that if this method throws an Exception, an IQ stanza of type
 * <tt>error</tt>, condition 'internal-server-error' will be returned to the
 * sender of the original request.
 * 
 * Note that if you want to add or adjust functionality, you should
 * <strong>not</strong> override this method. Instead, you probably want to
 * override any of these methods: {@link #handleIQGet()},
 * {@link #handleIQSet()}, {@link #handleDiscoInfo(IQ)} and/or
 * {@link #handleDiscoItems(IQ)}
 * 
 * @param iq
 *            The IQ request stanza.
 * @return Response to the request, or null to indicate a
 *         'feature-not-implemented' error.
 */
final private IQ processIQRequest(IQ iq) throws Exception {
    log.debug("(serving component '{}') Processing IQ " + "request (packetId {}).", getName(), iq.getID());

    // IQ get (and set) stanza's MUST be replied to.
    final Element childElement = iq.getChildElement();
    String namespace = null;
    if (childElement != null) {
        namespace = childElement.getNamespaceURI();
    }
    if (namespace == null) {
        log.debug("(serving component '{}') Invalid XMPP " + "- no child element or namespace in IQ "
                + "request (packetId {})", getName(), iq.getID());
        // this isn't valid XMPP.
        final IQ response = IQ.createResultIQ(iq);
        response.setError(Condition.bad_request);
        return response;
    }
    // check if this is a component for local users only.
    if (servesLocalUsersOnly() && !sentByLocalEntity(iq)) {
        log.info("(serving component '{}') Returning " + "'not-authorized' IQ error to a user from "
                + "another domain: {}", getName(), iq.getFrom());
        log.debug("(serving component '{}') Returning " + "'not-authorized' IQ error to a user from "
                + "another domain: {}", getName(), iq.toXML());
        final IQ error = IQ.createResultIQ(iq);
        error.setError(Condition.not_authorized);
        return error;
    }
    final Type type = iq.getType();
    if (type == Type.get) {
        if (NAMESPACE_DISCO_INFO.equals(namespace)) {
            log.trace("(serving component '{}') " + "Calling #handleDiscoInfo() (packetId {}).", getName(),
                    iq.getID());
            return handleDiscoInfo(iq);
        } else if (NAMESPACE_DISCO_ITEMS.equals(namespace)) {
            log.trace("(serving component '{}') " + "Calling #handleDiscoItems() (packetId {}).", getName(),
                    iq.getID());
            return handleDiscoItems(iq);
        } else if (NAMESPACE_XMPP_PING.equals(namespace)) {
            log.trace("(serving component '{}') " + "Calling #handlePing() (packetId {}).", getName(),
                    iq.getID());
            return handlePing(iq);
        } else if (NAMESPACE_LAST_ACTIVITY.equals(namespace)) {
            log.trace("(serving component '{}') " + "Calling #handleLastActivity() (packetId {}).", getName(),
                    iq.getID());
            return handleLastActivity(iq);
        } else if (NAMESPACE_ENTITY_TIME.equals(namespace)) {
            log.trace("(serving component '{}') " + "Calling #handleEntityTime() (packetId {}).", getName(),
                    iq.getID());
            return handleEntityTime(iq);
        } else {
            return handleIQGet(iq);
        }
    }
    if (type == Type.set) {
        return handleIQSet(iq);
    }
    // If by now we didn't do anything to the packet, we don't know what to
    // do with this. Return error (as it is a SET or GET stanza, which MUST
    // be replied to).
    return null;
}

From source file:de.tu_berlin.cit.intercloud.xmpp.core.packet.IQ.java

License:Open Source License

/**
 * Returns the child element of this IQ. IQ packets may have a single child
 * element in an extended namespace. This is a convenience method to
 * avoid manipulating the underlying packet's Element instance directly.<p>
 *
 * An IQ child element in extended namespaces is used to extend the features
 * of XMPP. Although any valid XML can be included in a child element
 * in an extended namespace, many common features have been standardized
 * as <a href="http://xmpp.org/extensions/">XMPP Extension Protocols</a>
 * (XEPs)./*from w ww  .  jav  a2 s  . com*/
 *
 * @return the child element.
 */
@SuppressWarnings("unchecked")
public Element getChildElement() {
    List<Element> elements = element.elements();
    if (elements.isEmpty()) {
        return null;
    } else {
        // Search for a child element that is in a different namespace.
        for (int i = 0; i < elements.size(); i++) {
            Element element = elements.get(i);
            String namespace = element.getNamespaceURI();
            if (!namespace.equals("") && !namespace.equals("jabber:client")
                    && !namespace.equals("jabber:server")) {
                return element;
            }
        }
        return null;
    }
}

From source file:de.tu_berlin.cit.intercloud.xmpp.core.packet.Message.java

License:Open Source License

/**
 * Returns the first child element of this packet that matches the
 * given name and namespace. If no matching element is found,
 * <tt>null</tt> will be returned. This is a convenience method to avoid
 * manipulating this underlying packet's Element instance directly.<p>
 *
 * Child elements in extended namespaces are used to extend the features
 * of XMPP. Examples include a "user is typing" indicator and invitations to
 * group chat rooms. Although any valid XML can be included in a child element
 * in an extended namespace, many common features have been standardized
 * as <a href="http://xmpp.org/extensions/">XMPP Extension Protocols</a>
 * (XEPs).//from   w w  w .  j a v a2  s.co m
 *
 * @param name the element name.
 * @param namespace the element namespace.
 * @return the first matching child element, or <tt>null</tt> if there
 *      is no matching child element.
 */
@SuppressWarnings("unchecked")
public Element getChildElement(String name, String namespace) {
    for (Iterator<Element> i = element.elementIterator(name); i.hasNext();) {
        Element element = i.next();
        if (element.getNamespaceURI().equals(namespace)) {
            return element;
        }
    }
    return null;
}

From source file:de.tu_berlin.cit.intercloud.xmpp.core.packet.Message.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Element> getChildElements(String name, String namespace) {
    ArrayList<Element> elements = new ArrayList<Element>();
    for (Iterator<Element> i = element.elementIterator(name); i.hasNext();) {
        Element element = i.next();
        if (element.getNamespaceURI().equals(namespace)) {
            elements.add(element);/* w  w w. j  av  a 2s .c om*/
        }
    }
    return elements;
}

From source file:de.tu_berlin.cit.intercloud.xmpp.core.packet.PacketError.java

License:Open Source License

/**
 * Returns the error condition.//ww  w. ja va 2  s  . co  m
 *
 * @return the error condition.
 * @see Condition
 */
@SuppressWarnings("unchecked")
public Condition getCondition() {
    for (Iterator<Element> i = element.elementIterator(); i.hasNext();) {
        Element el = i.next();
        if (el.getNamespaceURI().equals(ERROR_NAMESPACE) && !el.getName().equals("text")) {
            return Condition.fromXMPP(el.getName());
        }
    }
    // Looking for XMPP condition failed. See if a legacy error code exists,
    // which can be mapped into an XMPP error condition.
    String code = element.attributeValue("code");
    if (code != null) {
        try {
            return Condition.fromLegacyCode(Integer.parseInt(code));
        } catch (Exception e) {
            // Ignore -- unable to map legacy code into a valid condition
            // so return null.
        }
    }
    return null;
}

From source file:de.tu_berlin.cit.intercloud.xmpp.core.packet.PacketError.java

License:Open Source License

/**
 * Sets the error condition.//  www  . ja v a 2s.  c  o  m
 *
 * @param condition the error condition.
 * @see Condition
 */
@SuppressWarnings("unchecked")
public void setCondition(Condition condition) {
    if (condition == null) {
        throw new NullPointerException("Condition cannot be null");
    }
    // Set the error code for legacy support.
    element.addAttribute("code", Integer.toString(condition.getLegacyCode()));

    Element conditionElement = null;
    for (Iterator<Element> i = element.elementIterator(); i.hasNext();) {
        Element el = i.next();
        if (el.getNamespaceURI().equals(ERROR_NAMESPACE) && !el.getName().equals("text")) {
            conditionElement = el;
        }
    }
    if (conditionElement != null) {
        element.remove(conditionElement);
    }

    conditionElement = docFactory.createElement(condition.toXMPP(), ERROR_NAMESPACE);
    element.add(conditionElement);
}

From source file:de.tu_berlin.cit.intercloud.xmpp.core.packet.PacketError.java

License:Open Source License

/**
 * Sets an application-specific error condition. Optionally, a
 * application-specific namespace can be specified to define its
 * own application-specific error./*from   w w w.  j a  v a2s .co m*/
 * 
 * @param name the name of the application-specific error condition.
 * @param namespaceURI the namespace of the application.
 */
@SuppressWarnings("unchecked")
public void setApplicationCondition(String name, String namespaceURI) {
    if (ERROR_NAMESPACE.equals(namespaceURI)) {
        throw new IllegalArgumentException();
    }

    Element applicationError = null;
    for (Iterator<Element> i = element.elementIterator(); i.hasNext();) {

        Element el = i.next();
        if (!el.getNamespaceURI().equals(ERROR_NAMESPACE)) {
            applicationError = el;
        }
    }

    if (applicationError != null) {
        element.remove(applicationError);
    }

    // If name is null, clear the application condition.
    if (name == null) {
        return;
    }

    if (namespaceURI == null) {
        // Set fallback namespace (see XEP-0182)
        namespaceURI = "urn:xmpp:errors";
    }
    applicationError = docFactory.createElement(name, namespaceURI);
    element.add(applicationError);
}

From source file:de.tu_berlin.cit.intercloud.xmpp.core.packet.PacketError.java

License:Open Source License

/**
 * Returns the name of the application-specific error condition,
 * or <tt>null</tt> if there is no application-specific error.
 *
 * @return the name of the application-specific error condition, if it exists.
 *///from  ww w  .j a v  a 2  s. c o  m
@SuppressWarnings("unchecked")
public String getApplicationConditionName() {
    for (Iterator<Element> i = element.elementIterator(); i.hasNext();) {
        Element el = i.next();
        if (!el.getNamespaceURI().equals(ERROR_NAMESPACE)) {
            return el.getName();
        }
    }
    return null;
}