List of usage examples for org.dom4j Element getNamespaceURI
String getNamespaceURI();
String
is returned. From source file:net.sf.kraken.BaseTransport.java
License:Open Source License
/** * Intercepts disco items packets to filter out users who aren't allowed to register. * * @see org.jivesoftware.openfire.interceptor.PacketInterceptor#interceptPacket(org.xmpp.packet.Packet, org.jivesoftware.openfire.session.Session, boolean, boolean) *//*from ww w.j av a 2 s .c o m*/ @SuppressWarnings("unchecked") public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) { // If not IQ, return immediately. if (!(packet instanceof IQ)) { return; } // If it's a result IQ, process for possible filtering. if (((IQ) packet).getType().equals(IQ.Type.result)) { // If the packet is not outgoing back to the user or not processed yet, we don't care. if (processed || incoming) { return; } // If not query, return immediately. Element child = packet.getElement().element("query"); if (child == null) { return; } // If no namespace uri, return immediately. if (child.getNamespaceURI() == null) { return; } // If not disco#items, return immediately. if (!child.getNamespaceURI().equals(NameSpace.DISCO_ITEMS)) { return; } // If the node is null, we don't care, not directly related to a user. JID to = packet.getTo(); if (to.getNode() == null) { return; } JID from = packet.getFrom(); // If not from server itself, return immediately. if (!XMPPServer.getInstance().isLocal(from)) { return; } // If user registered, return immediately. if (RegistrationManager.getInstance().isRegistered(to, transportType)) { return; } // Check if allowed, if so return immediately. if (permissionManager.hasAccess(to)) { return; } // Filter out item associated with transport. Iterator iter = child.elementIterator(); while (iter.hasNext()) { Element elem = (Element) iter.next(); try { if (elem.attribute("jid").getText().equals(this.jid.toString())) { child.remove(elem); } } catch (Exception e) { // No worries. Wasn't what we were looking for. } } //TODO: should filter conference as well at some point. return; } // If it's a set IQ, process for possible roster activity. if (((IQ) packet).getType().equals(IQ.Type.set)) { // If the packet is not coming from the user, we don't care. if (!incoming) { return; } // If not query, return immediately. Element child = packet.getElement().element("query"); if (child == null) { return; } // If not jabber:iq:roster, return immediately. if (!child.getNamespaceURI().equals(NameSpace.IQ_ROSTER)) { return; } // Example items in roster modification. Iterator iter = child.elementIterator(); while (iter.hasNext()) { Element elem = (Element) iter.next(); if (!elem.getName().equals("item")) { continue; } String jidStr; String nickname = null; String sub = null; ArrayList<String> groups = new ArrayList<String>(); try { jidStr = elem.attributeValue("jid"); } catch (Exception e) { // No JID found, we don't want this then. continue; } JID jid = new JID(jidStr); if (!jid.getDomain().equals(this.getJID().toString())) { // Not for our domain, moving on. continue; } if (jid.getNode() == null) { // Gateway itself, don't care. return; } try { nickname = elem.attributeValue("name"); } catch (Exception e) { // No nickname, ok then. } try { sub = elem.attributeValue("subscription"); } catch (Exception e) { // No subscription, no worries. } Iterator groupIter = elem.elementIterator(); while (groupIter.hasNext()) { Element groupElem = (Element) groupIter.next(); if (!groupElem.getName().equals("group")) { continue; } groups.add(groupElem.getText()); } if (sub != null && sub.equals("remove")) { try { TransportSession trSession = sessionManager.getSession(session.getAddress().getNode()); if (!trSession.isRosterLocked(jid.toString())) { Log.debug(getType().toString() + ": contact delete " + session.getAddress().getNode() + ":" + jid); trSession.getBuddyManager().removeBuddy(convertJIDToID(jid)); } } catch (NotFoundException e) { // Well we just don't care then. } } else { try { TransportSession trSession = sessionManager.getSession(session.getAddress().getNode()); if (!trSession.isRosterLocked(jid.toString())) { try { TransportBuddy buddy = trSession.getBuddyManager().getBuddy(jid); Log.debug( getType().toString() + ": contact update " + session.getAddress().getNode() + ":" + jid + ":" + nickname + ":" + groups); buddy.setNicknameAndGroups(nickname, groups); } catch (NotFoundException e) { Log.debug(getType().toString() + ": contact add " + session.getAddress().getNode() + ":" + jid + ":" + nickname + ":" + groups); trSession.addContact(jid, nickname, groups); } } } catch (NotFoundException e) { // Well we just don't care then. } } } } }
From source file:net.sf.kraken.muc.BaseMUCTransport.java
License:Open Source License
/** * Retrieves a pending request or null if no such request is found. * * @param from JID that the request was originally sent from. * @param to JID that the request was originally sent to. * @param namespace IQ namespace to identify request. * @return Matching pending request or null. *//*from w w w.java 2 s.co m*/ public IQ getPendingRequest(JID from, JID to, String namespace) { for (IQ request : pendingIQRequests.keySet()) { if (request.getTo().equals(to) && request.getFrom().toBareJID().equals(from.toBareJID())) { Element child = request.getChildElement(); if (child != null) { String xmlns = child.getNamespaceURI(); if (xmlns.equals(namespace)) { pendingIQRequests.remove(request); return request; } } } } return null; }
From source file:net.sf.kraken.muc.BaseMUCTransport.java
License:Open Source License
/** * Retires a pending request.//from ww w.ja v a2 s . com * * @param from JID that the request was originally sent from. * @param to JID that the request was originally sent to. * @param namespace IQ namespace to identify request. */ public void cancelPendingRequest(JID from, JID to, String namespace) { for (IQ request : pendingIQRequests.keySet()) { if (request.getTo().equals(to) && request.getFrom().toBareJID().equals(from.toBareJID())) { Element child = request.getChildElement(); if (child != null) { String xmlns = child.getNamespaceURI(); if (xmlns.equals(namespace)) { IQ result = IQ.createResultIQ(request); result.setError(PacketError.Condition.item_not_found); getTransport().sendPacket(result); pendingIQRequests.remove(request); return; } } } } }
From source file:net.sf.kraken.muc.BaseMUCTransport.java
License:Open Source License
/** * Handles all incoming iq stanzas.//w ww. j a va 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(PacketError.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.MUC_ADMIN)) { reply.addAll(handleMUCAdmin(packet)); } else if (xmlns.equals(NameSpace.MUC_USER)) { reply.addAll(handleMUCUser(packet)); } else { Log.debug("Unable to handle iq request: " + xmlns); IQ error = IQ.createResultIQ(packet); error.setError(PacketError.Condition.service_unavailable); reply.add(error); } return reply; }
From source file:net.sf.kraken.registration.RegistrationHandler.java
License:Open Source License
public void process(IQ packet) throws UnauthorizedException, PacketException { // sanitize the input if (packet == null) { throw new IllegalArgumentException("Argument 'packet' cannot be null."); }//from w ww .ja va 2s . co m final String xmlns; final Element child = (packet).getChildElement(); if (child != null) { xmlns = child.getNamespaceURI(); } else { xmlns = null; } if (xmlns == null) { // No namespace defined. Log.debug("Cannot process this stanza, as it has no namespace:" + packet.toXML()); final IQ error = IQ.createResultIQ(packet); error.setError(Condition.bad_request); parent.sendPacket(error); return; } // done sanitizing, start processing. final Element remove = packet.getChildElement().element("remove"); if (remove != null) { // User wants to unregister. =( // this.convinceNotToLeave() ... kidding. handleDeregister(packet); } else { // handle the request switch (packet.getType()) { case get: // client requests registration form getRegistrationForm(packet); break; case set: // client is providing (filled out) registration form setRegistrationForm(packet); break; default: // ignore result and error stanzas. break; } } }
From source file:net.sf.kraken.registration.RegistrationHandler.java
License:Open Source License
/** * Handles a IQ-register 'set' request, which is to be interpreted as a * request to create a new registration. * * @param packet the IQ-register 'set' stanza. * @throws UnauthorizedException if the user isn't allowed to register. *//* w w w . j a v a 2 s .c om*/ private void setRegistrationForm(IQ packet) throws UnauthorizedException { final JID from = packet.getFrom(); final boolean registered; Collection<Registration> registrations = RegistrationManager.getInstance().getRegistrations(from, parent.transportType); if (registrations.iterator().hasNext()) { registered = true; } else { registered = false; } if (!registered && !parent.permissionManager.hasAccess(from)) { // User does not have permission to register with transport. // We want to allow them to change settings if they are already // registered. throw new UnauthorizedException( LocaleUtils.getLocalizedString("gateway.base.registrationdeniedbyacls", "kraken")); } // Parse the input variables String username = null; String password = null; String nickname = null; try { if (packet.getChildElement().element("x") != null) { final DataForm form = new DataForm(packet.getChildElement().element("x")); final List<FormField> fields = form.getFields(); for (final FormField field : fields) { final String var = field.getVariable(); if (var.equals("username")) { username = field.getValues().get(0); } else if (var.equals("password")) { password = field.getValues().get(0); } else if (var.equals("nick")) { nickname = field.getValues().get(0); } } } } // TODO: This shouldn't be done by catching an Exception - check for the // existence of elements instead. If we insist doing this with an // exception handler, prevent catching a generic Exception (catch more // specific subclasses instead). catch (Exception ex) { // No with data form apparently Log.info("Most likely, no dataform was present " + "in the IQ-register request.", ex); } // input variables could also exist in the non-extended elements final Element userEl = packet.getChildElement().element("username"); final Element passEl = packet.getChildElement().element("password"); final Element nickEl = packet.getChildElement().element("nick"); if (userEl != null) { username = userEl.getTextTrim(); } if (passEl != null) { password = passEl.getTextTrim(); } if (nickEl != null) { nickname = nickEl.getTextTrim(); } username = (username == null || username.equals("")) ? null : username; password = (password == null || password.equals("")) ? null : password; nickname = (nickname == null || nickname.equals("")) ? null : nickname; // verify that we've got wat we need. if (username == null || (parent.isPasswordRequired() && password == null) || (parent.isNicknameRequired() && nickname == null)) { // Invalid information from stanza, lets yell. Log.info("Cannot process IQ register request, as it " + "fails to provide all data that's required: " + packet.toXML()); final IQ result = IQ.createResultIQ(packet); result.setError(Condition.bad_request); parent.sendPacket(result); return; } // Check if the client supports our proprietary 'rosterless' mode. final boolean rosterlessMode; final Element x = packet.getChildElement().element("x"); if (x != null && x.getNamespaceURI() != null && x.getNamespaceURI().equals(NameSpace.IQ_GATEWAY_REGISTER)) { rosterlessMode = true; Log.info("Registering " + packet.getFrom() + " as " + username + " in rosterless mode."); } else { rosterlessMode = false; Log.info("Registering " + packet.getFrom() + " as " + username + " (without making use of rosterless mode)."); } // Here's where the true magic lies: create the registration! try { addNewRegistration(from, username, password, nickname, rosterlessMode); registrations = RegistrationManager.getInstance().getRegistrations(from, parent.transportType); Registration registration = registrations.iterator().next(); TransportSession session = parent.registrationLoggedIn(registration, from, PresenceType.available, "", -1); session.setRegistrationPacket(packet); session.detachSession(); parent.getSessionManager().storeSession(from, session); //final IQ result = IQ.createResultIQ(packet); // I believe this shouldn't be included. Leaving it around just in // case. // Element response = // DocumentHelper.createElement(QName.get("query", IQ_REGISTER)); // result.setChildElement(response); //parent.sendPacket(result); } catch (UserNotFoundException e) { Log.warn("Someone attempted to register with the gateway " + "who is not registered with the server: " + from); final IQ eresult = IQ.createResultIQ(packet); eresult.setError(Condition.forbidden); parent.sendPacket(eresult); final Message em = new Message(); em.setType(Message.Type.error); em.setTo(packet.getFrom()); em.setFrom(packet.getTo()); em.setBody(LocaleUtils.getLocalizedString("gateway.base.registrationdeniednoacct", "kraken")); parent.sendPacket(em); } catch (IllegalAccessException e) { Log.warn("Someone who is not a user of this server " + "tried to register with the transport: " + from); final IQ eresult = IQ.createResultIQ(packet); eresult.setError(Condition.forbidden); parent.sendPacket(eresult); final Message em = new Message(); em.setType(Message.Type.error); em.setTo(packet.getFrom()); em.setFrom(packet.getTo()); em.setBody(LocaleUtils.getLocalizedString("gateway.base.registrationdeniedbyhost", "kraken")); parent.sendPacket(em); } catch (IllegalArgumentException e) { Log.warn("Someone attempted to register with the " + "gateway with an invalid username: " + from); final IQ eresult = IQ.createResultIQ(packet); eresult.setError(Condition.bad_request); parent.sendPacket(eresult); final Message em = new Message(); em.setType(Message.Type.error); em.setTo(packet.getFrom()); em.setFrom(packet.getTo()); em.setBody(LocaleUtils.getLocalizedString("gateway.base.registrationdeniedbadusername", "kraken")); parent.sendPacket(em); } }
From source file:nl.tue.gale.ae.processor.XMLProcessor.java
License:Open Source License
public void traverse(Element element, Resource resource) throws ProcessorException { if (element == null) return;// ww w. j av a 2 s . c o m Namespace ns = element.getNamespace(); String tag = null; if (ns != Namespace.NO_NAMESPACE) { tag = "{" + ns.getURI() + "}" + element.getName(); if (!moduleTable.containsKey(tag)) tag = null; } if (tag == null) tag = element.getName(); if (moduleTable.containsKey(tag)) { Module mod = moduleTable.get(tag); if (mod.getMimeToHandle().contains(GaleContext.mime(resource))) { try { element = moduleTable.get(tag).traverse(element, resource); } catch (Exception e) { e.printStackTrace(); element = (Element) GaleUtil.replaceNode(element, GaleUtil.createErrorElement("[" + e.getMessage() + "]")); } } } traverseChildren(element, resource); if ("text/xhtml".equals(GaleContext.mime(resource))) if (element != null) if ("http://www.w3.org/1999/xhtml".equals(element.getNamespaceURI())) element.setQName(DocumentFactory.getInstance().createQName(element.getName(), "", "http://www.w3.org/1999/xhtml")); }
From source file:org.androidpn.server.xmpp.router.MessageRouter.java
License:Open Source License
/** * handle send message//from w w w . j ava 2s . com * @param packet */ private void handle(Message packet) { Element element = packet.getElement(); String namespace = null; if (element != null) { namespace = element.getNamespaceURI(); } if (namespace == null) { Message.Type type = packet.getType(); if (type == null || Message.Type.error == type || packet.getTo() != null) { log.warn("Unknown Message type"); } } else { MessageHandler handler = getHandler(namespace); if (handler == null) { sendErrorPacket(packet, PacketError.Condition.service_unavailable); } else { handler.process(packet); } } }
From source file:org.b5chat.crossfire.core.net.StanzaHandler.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 {// www .ja v a2 s.com return new IQ(doc, !validateJIDs()); } }
From source file:org.b5chat.crossfire.xmpp.route.IQRouter.java
License:Open Source License
private void handle(IQ packet) { JID recipientJID = packet.getTo();//from w ww. j av a 2 s.c o m // Check if the packet was sent to the server hostname if (recipientJID != null && recipientJID.getNode() == null && recipientJID.getResource() == null && serverName.equals(recipientJID.getDomain())) { Element childElement = packet.getChildElement(); if (childElement != null && childElement.element("addresses") != null) { // Packet includes multicast processing instructions. Ask the multicastRouter // to route this packet multicastRouter.route(packet); return; } } if (packet.getID() != null && (IQ.Type.result == packet.getType() || IQ.Type.error == packet.getType())) { // The server got an answer to an IQ packet that was sent from the server IQResultListener iqResultListener = resultListeners.remove(packet.getID()); if (iqResultListener != null) { resultTimeout.remove(packet.getID()); if (iqResultListener != null) { try { iqResultListener.receivedAnswer(packet); } catch (Exception e) { Log.error("Error processing answer of remote entity", e); } return; } } } try { // Check for registered components, services or remote servers if (isLocalServer(recipientJID)) { // Let the server handle the Packet Element childElement = packet.getChildElement(); String namespace = null; if (childElement != null) { namespace = childElement.getNamespaceURI(); } if (namespace == null) { if (packet.getType() != IQ.Type.result && packet.getType() != IQ.Type.error) { // Do nothing. We can't handle queries outside of a valid namespace Log.warn("Unknown packet " + packet.toXML()); } } else { // Check if communication to local users is allowed if (recipientJID != null && userManager.isRegisteredUser(recipientJID.getNode())) { PrivacyList list = PrivacyListManager.getInstance() .getDefaultPrivacyList(recipientJID.getNode()); if (list != null && list.shouldBlockPacket(packet)) { // Communication is blocked if (IQ.Type.set == packet.getType() || IQ.Type.get == packet.getType()) { // Answer that the service is unavailable sendErrorPacket(packet, PacketError.Condition.service_unavailable); } return; } } IQHandler handler = getHandler(namespace); if (handler == null) { if (recipientJID == null) { // Answer an error since the server can't handle the requested namespace sendErrorPacket(packet, PacketError.Condition.service_unavailable); } else if (recipientJID.getNode() == null || "".equals(recipientJID.getNode())) { // Answer an error if JID is of the form <domain> sendErrorPacket(packet, PacketError.Condition.feature_not_implemented); } else { // JID is of the form <node@domain> // Answer an error since the server can't handle packets sent to a node sendErrorPacket(packet, PacketError.Condition.service_unavailable); } } else { handler.process(packet); } } } else { // JID is of the form <node@domain/resource> or belongs to a remote server // or to an uninstalled component routingTable.routePacket(recipientJID, packet, false); } } catch (Exception e) { Log.error(LocaleUtils.getLocalizedString("admin.error.routing"), e); ISession session = sessionManager.getSession(packet.getFrom()); if (session != null) { IQ reply = IQ.createResultIQ(packet); reply.setError(PacketError.Condition.internal_server_error); session.process(reply); } } }