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:de.tu_berlin.cit.intercloud.xmpp.core.packet.PacketError.java

License:Open Source License

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

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

License:Open Source License

/**
 * Returns the error condition.//from   w  w  w.  j a  v  a 2s .  c om
 *
 * @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());
        }
    }
    return null;
}

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

License:Open Source License

/**
 * Sets the error condition.//from   w  w w .  ja va  2  s .  co 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");
    }
    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.resultsetmanagement.ResultSet.java

License:Open Source License

/**
 * Checks if the Element that has been passed as an argument is a valid
 * Result Set Management element, in a request context.
 * //from   w  ww  . j a  v a 2  s  . co  m
 * @param rsmElement
 *            The Element to check.
 * @return ''true'' if this is a valid RSM query representation, ''false''
 *         otherwise.
 */
// Dom4J doesn't do generics, sadly.
@SuppressWarnings("unchecked")
public static boolean isValidRSMRequest(Element rsmElement) {
    if (rsmElement == null) {
        throw new IllegalArgumentException("The argument 'rsmElement' cannot be null.");
    }

    if (!rsmElement.getName().equals("set")) {
        // the name of the element must be "set".
        return false;
    }

    if (!rsmElement.getNamespaceURI().equals(NAMESPACE_RESULT_SET_MANAGEMENT)) {
        // incorrect namespace
        return false;
    }

    final Element maxElement = rsmElement.element("max");
    if (maxElement == null) {
        // The 'max' element in an RSM request must be available
        return false;
    }

    final String sMax = maxElement.getText();
    if (sMax == null || sMax.length() == 0) {
        // max element must contain a value.
        return false;
    }

    try {
        if (Integer.parseInt(sMax) < 0) {
            // must be a postive integer.
            return false;
        }
    } catch (NumberFormatException e) {
        // the value of 'max' must be an integer value.
        return false;
    }

    List<Element> allElements = rsmElement.elements();
    int optionalElements = 0;
    for (Element element : allElements) {
        final String name = element.getName();
        if (!validRequestFields.contains(name)) {
            // invalid element.
            return false;
        }

        if (!name.equals("max")) {
            optionalElements++;
        }

        if (optionalElements > 1) {
            // only one optional element is allowed.
            return false;
        }

        if (name.equals("index")) {
            final String value = element.getText();
            if (value == null || value.equals("")) {
                // index elements must have a numberic value.
                return false;
            }
            try {
                if (Integer.parseInt(value) < 0) {
                    // index values must be positive.
                    return false;
                }
            } catch (NumberFormatException e) {
                // index values must be numeric.
                return false;
            }
        }
    }

    return true;
}

From source file:de.tu_berlin.cit.rwx4j.component.ResourceContainerSocketManager.java

License:Apache License

/**
 * This method handles the IQ stanzas of type <tt>result</tt>
 * that are received by the component./*from w  w  w.  j  av a  2 s  .  com*/
 * 
 * @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();
        IntercloudDiscoItems discoItems = new IntercloudDiscoItems();
        //         String rootJID = null;
        //         ArrayList<String> exchangeJIDs = new ArrayList<String>();
        //         ArrayList<String> gatewayJIDs = new ArrayList<String>();
        while (iter.hasNext()) {
            Element item = (Element) iter.next();
            // filter
            discoItems.addItem(item.attributeValue("jid"), item.attributeValue("name"));
            //            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(), XwadlDocument.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(), RestDocument.Factory.parse(childElement.asXML()));
        } catch (XmlException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:de.tu_berlin.cit.rwx4j.xmpp.whack.SocketReadThread.java

License:Open Source License

private IQ getIQ(Element doc) {
    Element query = doc.element("query");
    if (query != null && "jabber:iq:roster".equals(query.getNamespaceURI())) {
        return new Roster(doc);
    } else {//  w  w w  .  j  a va 2s . c  o m
        return new IQ(doc);
    }
}

From source file:freemarker.ext.xml._Dom4jNavigator.java

License:Apache License

void getChildren(Object node, String localName, String namespaceUri, List result) {
    if (node instanceof Element) {
        Element e = (Element) node;
        if (localName == null) {
            result.addAll(e.elements());
        } else {/*ww  w .  ja  va 2 s.  c  o  m*/
            result.addAll(
                    e.elements(e.getQName().getDocumentFactory().createQName(localName, "", namespaceUri)));
        }
    } else if (node instanceof Document) {
        Element root = ((Document) node).getRootElement();
        if (localName == null
                || (equal(root.getName(), localName) && equal(root.getNamespaceURI(), namespaceUri))) {
            result.add(root);
        }
    }
}

From source file:net.emiva.crossfire.plugin.muc.spi.LocalMUCUser.java

License:Open Source License

public void process(IQ packet) {
    // Ignore IQs of type ERROR or RESULT sent to a room
    if (IQ.Type.error == packet.getType()) {
        return;/*from  w w w .  j a v a  2 s .c  om*/
    }
    lastPacketTime = System.currentTimeMillis();
    JID recipient = packet.getTo();
    String group = recipient.getNode();
    if (group == null) {
        // Ignore packets to the groupchat server
        // In the future, we'll need to support TYPE_IQ queries to the server for MUC
        Log.info(LocaleUtils.getLocalizedString("muc.error.not-supported") + " " + packet.toString());
    } else {
        MUCRole role = roles.get(group);
        if (role == null) {
            sendErrorPacket(packet, PacketError.Condition.not_authorized);
        } else if (IQ.Type.result == packet.getType()) {
            // Only process IQ result packet if it's a private packet sent to another
            // room occupant
            if (packet.getTo().getResource() != null) {
                try {
                    // User is sending an IQ result packet to another room occupant
                    role.getChatRoom().sendPrivatePacket(packet, role);
                } catch (NotFoundException e) {
                    // Do nothing. No error will be sent to the sender of the IQ result packet
                }
            }
        } else {
            // Check and reject conflicting packets with conflicting roles
            // In other words, another user already has this nickname
            if (!role.getUserAddress().equals(packet.getFrom())) {
                sendErrorPacket(packet, PacketError.Condition.conflict);
            } else {
                try {
                    Element query = packet.getElement().element("query");
                    if (query != null
                            && "http://jabber.org/protocol/muc#owner".equals(query.getNamespaceURI())) {
                        role.getChatRoom().getIQOwnerHandler().handleIQ(packet, role);
                    } else if (query != null
                            && "http://jabber.org/protocol/muc#admin".equals(query.getNamespaceURI())) {
                        role.getChatRoom().getIQAdminHandler().handleIQ(packet, role);
                    } else {
                        if (packet.getTo().getResource() != null) {
                            // User is sending an IQ packet to another room occupant
                            role.getChatRoom().sendPrivatePacket(packet, role);
                        } else {
                            sendErrorPacket(packet, PacketError.Condition.bad_request);
                        }
                    }
                } catch (ForbiddenException e) {
                    sendErrorPacket(packet, PacketError.Condition.forbidden);
                } catch (NotFoundException e) {
                    sendErrorPacket(packet, PacketError.Condition.recipient_unavailable);
                } catch (ConflictException e) {
                    sendErrorPacket(packet, PacketError.Condition.conflict);
                } catch (NotAllowedException e) {
                    sendErrorPacket(packet, PacketError.Condition.not_allowed);
                } catch (CannotBeInvitedException e) {
                    sendErrorPacket(packet, PacketError.Condition.not_acceptable);
                } catch (Exception e) {
                    sendErrorPacket(packet, PacketError.Condition.internal_server_error);
                    Log.error(e.getMessage(), e);
                }
            }
        }
    }
}

From source file:net.emiva.crossfire.plugin.muc.spi.MultiUserChatServiceImpl.java

License:Open Source License

/**
 * Returns true if the IQ packet was processed. This method should only process disco packets
 * as well as jabber:iq:register packets sent to the MUC service.
 *
 * @param iq the IQ packet to process./* w ww. j a v a 2 s.c  om*/
 * @return true if the IQ packet was processed.
 */
private boolean process(IQ iq) {
    Element childElement = iq.getChildElement();
    String namespace = null;
    // Ignore IQs of type ERROR
    if (IQ.Type.error == iq.getType()) {
        return false;
    }
    if (iq.getTo().getResource() != null) {
        // Ignore IQ packets sent to room occupants
        return false;
    }
    if (childElement != null) {
        namespace = childElement.getNamespaceURI();
    }
    if ("jabber:iq:register".equals(namespace)) {
        IQ reply = registerHandler.handleIQ(iq);
        router.route(reply);
    } else if ("jabber:iq:search".equals(namespace)) {
        IQ reply = searchHandler.handleIQ(iq);
        router.route(reply);
    } else if ("http://jabber.org/protocol/disco#info".equals(namespace)) {
        // TODO MUC should have an IQDiscoInfoHandler of its own when MUC becomes
        // a component
        IQ reply = XMPPServer.getInstance().getIQDiscoInfoHandler().handleIQ(iq);
        router.route(reply);
    } else if ("http://jabber.org/protocol/disco#items".equals(namespace)) {
        // TODO MUC should have an IQDiscoItemsHandler of its own when MUC becomes
        // a component
        IQ reply = XMPPServer.getInstance().getIQDiscoItemsHandler().handleIQ(iq);
        router.route(reply);
    } else {
        return false;
    }
    return true;
}

From source file:net.sf.kraken.BaseTransport.java

License:Open Source License

/**
 * Handles all incoming iq stanzas.//from ww w.  ja v  a  2  s. c  o m
 *
 * @param packet The iq packet to be processed.
 * @return list of packets that will be sent back to the IQ requester.
 */
private List<Packet> processPacket(IQ packet) {
    Log.debug("Received iq packet: " + packet.toXML());
    List<Packet> reply = new ArrayList<Packet>();

    if (packet.getType() == IQ.Type.error) {
        // Lets not start a loop.  Ignore.
        return reply;
    }

    String xmlns = null;
    Element child = (packet).getChildElement();
    if (child != null) {
        xmlns = child.getNamespaceURI();
    }

    if (xmlns == null) {
        // No namespace defined.
        Log.debug("No XMLNS:" + packet.toString());
        IQ error = IQ.createResultIQ(packet);
        error.setError(Condition.bad_request);
        reply.add(error);
        return reply;
    }

    if (xmlns.equals(NameSpace.DISCO_INFO)) {
        reply.addAll(handleDiscoInfo(packet));
    } else if (xmlns.equals(NameSpace.DISCO_ITEMS)) {
        reply.addAll(handleDiscoItems(packet));
    } else if (xmlns.equals(NameSpace.IQ_GATEWAY)) {
        reply.addAll(handleIQGateway(packet));
    } else if (xmlns.equals(NameSpace.IQ_REGISTER)) {
        // TODO if all handling is going to be offloaded to
        // ChannelHandler-like constructs, the exception handling
        // could/should be made more generic.
        try {
            // note that this handler does not make use of the reply-queue.
            // Instead, it sends packets directly.
            new RegistrationHandler(this).process(packet);
        } catch (UnauthorizedException ex) {
            final IQ result = IQ.createResultIQ(packet);
            result.setError(Condition.forbidden);
            reply.add(result);
            final Message em = new Message();
            em.setType(Message.Type.error);
            em.setTo(packet.getFrom());
            em.setFrom(packet.getTo());
            em.setBody(ex.getMessage());
            reply.add(em);
        }
    } else if (xmlns.equals(NameSpace.IQ_VERSION)) {
        reply.addAll(handleIQVersion(packet));
    } else if (xmlns.equals(NameSpace.VCARD_TEMP) && child.getName().equals("vCard")) {
        reply.addAll(handleVCardTemp(packet));
    } else if (xmlns.equals(NameSpace.IQ_ROSTER)) {
        // No reason to 'argue' about this one.  Return success.
        reply.add(IQ.createResultIQ(packet));
    } else if (xmlns.equals(NameSpace.IQ_LAST)) {
        reply.addAll(handleIQLast(packet));
    } else {
        Log.debug("Unable to handle iq request: " + xmlns);
        IQ error = IQ.createResultIQ(packet);
        error.setError(Condition.service_unavailable);
        reply.add(error);
    }

    return reply;
}