@Override public void processPacket(Packet packet) { if (packet instanceof Message) { Message message = (Message) packet; processMessage(message); } else if (packet instanceof Presence) { Presence presence = (Presence) packet; processPresence(presence); } else if (packet instanceof IQ) { IQ iq = (IQ) packet; if (IQ.Type.error == iq.getType() || IQ.Type.result == iq.getType()) { return; } processIQ(iq); } }
public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) throws PacketRejectedException { // We only want packets recieved by the server if (!processed && incoming && packet instanceof IQ) { IQ iq = (IQ) packet; Element childElement = iq.getChildElement(); if (childElement == null) { return; } String namespace = childElement.getNamespaceURI(); String profile = childElement.attributeValue("profile"); // Check that the SI is about file transfer and try creating a file transfer if (NAMESPACE_SI.equals(namespace) && NAMESPACE_SI_FILETRANSFER.equals(profile)) { // If this is a set, check the feature offer if (iq.getType().equals(IQ.Type.set)) { JID from = iq.getFrom(); JID to = iq.getTo(); FileTransfer transfer = createFileTransfer(from, to, childElement); try { if (transfer == null || !acceptIncomingFileTransferRequest(transfer)) { throw new PacketRejectedException(); } } catch (FileTransferRejectedException e) { throw new PacketRejectedException(e); } } } } }
public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) throws PacketRejectedException { if (!processed && packet instanceof IQ && !incoming) { // Check for the Bookmark Storage element and hand off to the Bookmark engine. IQ iq = (IQ) packet; Element childElement = iq.getChildElement(); if (childElement == null || iq.getType() != IQ.Type.result) { return; } String namespace = childElement.getNamespaceURI(); if ("jabber:iq:private".equals(namespace)) { // In private data, when a user is attempting to retrieve bookmark // information, there will be a storage:bookmarks namespace. Element storageElement = childElement.element("storage"); if (storageElement == null) { return; } namespace = storageElement.getNamespaceURI(); if ("storage:bookmarks".equals(namespace)) { // Append Server defined bookmarks for user. JID toJID = iq.getTo(); addBookmarks(toJID, storageElement); } } } }
@SuppressWarnings("unchecked") @Test @Ignore public void testCanControlGatheredEntriesUsingRsm() throws Exception { NodeItem item1 = new NodeItemImpl(TEST_NODE_1, "node1:1", new Date(0), "<entry>item1</entry>"); NodeItem item2 = new NodeItemImpl(TEST_NODE_2, "node2:1", new Date(10), "<entry>item2</entry>"); NodeItem item3 = new NodeItemImpl(TEST_NODE_1, "node1:2", new Date(20), "<entry>item3</entry>"); NodeItem item4 = new NodeItemImpl(TEST_NODE_1, "node1:3", new Date(30), "<entry>item4</entry>"); ArrayList<NodeItem> results = new ArrayList<NodeItem>(); results.add(item1); results.add(item2); results.add(item3); results.add(item4); Mockito.when( channelManager.getUserFeedItems( Mockito.any(JID.class), Mockito.any(Date.class), Mockito.anyInt(), Mockito.any(GlobalItemID.class), Mockito.anyBoolean())) .thenReturn(new ClosableIteratorImpl<NodeItem>(results.iterator())); Mockito.when( channelManager.getCountUserFeedItems( Mockito.any(JID.class), Mockito.any(Date.class), Mockito.anyBoolean())) .thenReturn(results.size()); Element rsm = request.getElement().addElement("rsm"); rsm.addNamespace("", PubSubElementProcessorAbstract.NS_RSM); rsm.addElement("max").addText("2"); rsm.addElement("after").addText("node1:1"); userItemsGet.process(element, jid, request, null); IQ response = (IQ) queue.poll(); Assert.assertEquals(IQ.Type.result, response.getType()); Element pubsub = response.getChildElement(); Assert.assertEquals("pubsub", pubsub.getName()); Assert.assertEquals(JabberPubsub.NAMESPACE_URI, pubsub.getNamespaceURI()); List<Element> items = pubsub.elements("items"); Assert.assertEquals(2, items.size()); Assert.assertEquals(TEST_NODE_2, items.get(0).attributeValue("node")); Assert.assertEquals(TEST_NODE_1, items.get(1).attributeValue("node")); Assert.assertEquals(1, items.get(0).elements("item").size()); Assert.assertEquals(1, items.get(1).elements("item").size()); Element rsmResult = pubsub.element("set"); Assert.assertEquals("2", rsmResult.element("count").getText()); Assert.assertEquals("node2:1", rsmResult.element("first").getText()); Assert.assertEquals("node1:2", rsmResult.element("last").getText()); }
public void process(Packet packet) { // Check that the requested packet can be processed if (canProcess(packet)) { // Perform the actual processing of the packet. This usually implies sending // the packet to the entity try { // Invoke the interceptors before we send the packet InterceptorManager.getInstance().invokeInterceptors(packet, this, false, false); deliver(packet); // Invoke the interceptors after we have sent the packet InterceptorManager.getInstance().invokeInterceptors(packet, this, false, true); } catch (PacketRejectedException e) { // An interceptor rejected the packet so do nothing } catch (Exception e) { Log.error(LocaleUtils.getLocalizedString("admin.error"), e); } } else { // http://xmpp.org/extensions/xep-0016.html#protocol-error if (packet instanceof Message) { // For message stanzas, the server SHOULD return an error, which SHOULD be // <service-unavailable/>. Message message = (Message) packet; Message result = message.createCopy(); result.setTo(message.getFrom()); result.setError(PacketError.Condition.service_unavailable); XMPPServer.getInstance().getRoutingTable().routePacket(message.getFrom(), result, true); } else if (packet instanceof IQ) { // For IQ stanzas of type "get" or "set", the server MUST return an error, which SHOULD be // <service-unavailable/>. // IQ stanzas of other types MUST be silently dropped by the server. IQ iq = (IQ) packet; if (iq.getType() == IQ.Type.get || iq.getType() == IQ.Type.set) { IQ result = IQ.createResultIQ(iq); result.setError(PacketError.Condition.service_unavailable); XMPPServer.getInstance().getRoutingTable().routePacket(iq.getFrom(), result, true); } } } }
public IQ handleIQ(IQ iq) { Type type = iq.getType(); String group_id = iq.getTo().getNode(); if (group_id == null) return null; // 申请入群 if (Type.get == type) return applyUserGroup(iq); else if (Type.set == type) { Element element = iq.getChildElement(); Element quit = element.element("quit"); if (quit != null) return quitGroup(iq); else return processGroupApply(iq); } return null; }
public void process(IQ packet) throws UnauthorizedException, PacketException { // sanitize the input if (packet == null) { throw new IllegalArgumentException("Argument 'packet' cannot be null."); } 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; } } }
@SuppressWarnings("unchecked") @Test public void testOutgoingStanzaFormattedAsExpected() throws Exception { NodeItem item1 = new NodeItemImpl(TEST_NODE_1, "1", new Date(), "<entry>item1</entry>"); NodeItem item2 = new NodeItemImpl(TEST_NODE_2, "1", new Date(), "<entry>item2</entry>"); NodeItem item3 = new NodeItemImpl(TEST_NODE_1, "2", new Date(), "<entry>item3</entry>"); NodeItem item4 = new NodeItemImpl(TEST_NODE_1, "3", new Date(), "<entry>item4</entry>"); ArrayList<NodeItem> results = new ArrayList<NodeItem>(); results.add(item1); results.add(item2); results.add(item3); results.add(item4); Mockito.when( channelManager.getUserFeedItems( Mockito.any(JID.class), Mockito.any(Date.class), Mockito.anyInt(), Mockito.any(GlobalItemID.class), Mockito.anyBoolean())) .thenReturn(new ClosableIteratorImpl<NodeItem>(results.iterator())); userItemsGet.process(element, jid, request, null); IQ response = (IQ) queue.poll(); Assert.assertEquals(IQ.Type.result, response.getType()); Element pubsub = response.getChildElement(); Assert.assertEquals("pubsub", pubsub.getName()); Assert.assertEquals(JabberPubsub.NAMESPACE_URI, pubsub.getNamespaceURI()); List<Element> items = pubsub.elements("items"); Assert.assertEquals(3, items.size()); Assert.assertEquals(TEST_NODE_1, items.get(0).attributeValue("node")); Assert.assertEquals(TEST_NODE_2, items.get(1).attributeValue("node")); Assert.assertEquals(TEST_NODE_1, items.get(2).attributeValue("node")); Assert.assertEquals(1, items.get(0).elements("item").size()); Assert.assertEquals(2, items.get(2).elements("item").size()); }
@Test public void testNoUserItemsReturnsEmptyStanza() throws Exception { Mockito.when( channelManager.getUserFeedItems( Mockito.any(JID.class), Mockito.any(Date.class), Mockito.anyInt(), Mockito.any(GlobalItemID.class), Mockito.anyBoolean())) .thenReturn(new ClosableIteratorImpl<NodeItem>(new ArrayList<NodeItem>().iterator())); userItemsGet.process(element, jid, request, null); IQ response = (IQ) queue.poll(); Assert.assertEquals(IQ.Type.result, response.getType()); Element pubsub = response.getChildElement(); Assert.assertEquals("pubsub", pubsub.getName()); Assert.assertEquals(JabberPubsub.NAMESPACE_URI, pubsub.getNamespaceURI()); }
@Override public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) throws PacketRejectedException { if (!processed && incoming) { if (packet instanceof IQ) { Log.debug( "Incomping unprocessed package i might be interested in. Package: \n" + packet.toString() + "\n"); IQ myPacket = (IQ) packet; if (myPacket.getFrom() == null || myPacket.getTo() == null) { /* * If getTo() == null this is maybe a roster update from the * Client to the Server, check if we should mirror this * package to external component */ if (myPacket.getFrom() != null && myPacket.getType().equals(IQ.Type.set) && myPacket.getTo() == null) { if (XpathHelper.findNodesInDocument( myPacket.getChildElement().getDocument(), "//roster:item") .size() > 0) { _packetProcessor.get("clientToComponentUpdate").process(myPacket); } } return; } @SuppressWarnings("unused") String to = myPacket.getTo().toString(); String from = myPacket.getFrom().toString(); if (myPacket.getType().equals(IQ.Type.get) && from.equals(_mySubdomain)) { if (XpathHelper.findNodesInDocument(myPacket.getElement().getDocument(), "//roster:*") .size() == 1) { // This Package is a roster request by remote component _packetProcessor.get("sendRoster").process(packet); } } else if (myPacket.getType().equals(IQ.Type.set) && from.equals(_mySubdomain)) { if (XpathHelper.findNodesInDocument(myPacket.getElement().getDocument(), "//roster:item") .size() >= 1) { // Component sends roster update _packetProcessor.get("receiveChanges").process(packet); } } else if (myPacket.getType().equals(IQ.Type.get) && myPacket.toString().contains("http://jabber.org/protocol/disco#info") && myPacket.getTo().toString().equals(_mySubdomain)) { /* * modify the disco#info for spark clients if enabled in * admin panel */ _packetProcessor.get("sparkIQRegistered").process(packet); } else if (myPacket.getType().equals(IQ.Type.set) && myPacket.getTo().toString().equals(_mySubdomain)) { System.out.println("war das ein remove an mich????"); _packetProcessor.get("handleCleanUp").process(packet); } } // else if (packet instanceof Presence) { // if (packet.getFrom().toString().equals(_mySubdomain) // && // !JiveGlobals.getBooleanProperty("plugin.remoteroster.persistent", // false)) { // System.out.println("MACH EIN CLEANUP!!!!!!"); // _packetProcessor.get("handleCleanUp").process(packet); // } // } } }
/** * Handles the received IQ packet. * * @param packet the packet * @return the response to send back * @throws UnauthorizedException if the user is not authorized */ public IQ handleIQ(IQ packet) throws UnauthorizedException { IQ reply = null; ClientSession session = sessionManager.getSession(packet.getFrom()); if (session == null) { log.error("Session not found for key " + packet.getFrom()); reply = IQ.createResultIQ(packet); reply.setChildElement(packet.getChildElement().createCopy()); reply.setError(PacketError.Condition.internal_server_error); return reply; } try { Element iq = packet.getElement(); Element query = iq.element("query"); Element queryResponse = probeResponse.createCopy(); if (IQ.Type.get == packet.getType()) { // get query String username = query.elementText("username"); if (username != null) { queryResponse.element("username").setText(username); } reply = IQ.createResultIQ(packet); reply.setChildElement(queryResponse); if (session.getStatus() != Session.STATUS_AUTHENTICATED) { reply.setTo((JID) null); } } else { // set query String resource = query.elementText("resource"); String username = query.elementText("username"); String password = query.elementText("password"); String digest = null; if (query.element("digest") != null) { digest = query.elementText("digest").toLowerCase(); } // Verify the resource if (resource != null) { try { resource = JID.resourceprep(resource); } catch (StringprepException e) { throw new UnauthorizedException("Invalid resource: " + resource, e); } } else { throw new IllegalArgumentException("Invalid resource (empty or null)."); } // Verify the username if (username == null || username.trim().length() == 0) { throw new UnauthorizedException("Invalid username (empty or null)."); } try { Stringprep.nodeprep(username); } catch (StringprepException e) { throw new UnauthorizedException("Invalid username: " + username, e); } username = username.toLowerCase(); // Verify that username and password are correct AuthToken token = null; if (password != null && AuthManager.isPlainSupported()) { token = AuthManager.authenticate(username, password); } else if (digest != null && AuthManager.isDigestSupported()) { token = AuthManager.authenticate(username, session.getStreamID().toString(), digest); } if (token == null) { throw new UnauthenticatedException(); } // Set the session authenticated successfully session.setAuthToken(token, resource); packet.setFrom(session.getAddress()); reply = IQ.createResultIQ(packet); } } catch (Exception ex) { log.error(ex); reply = IQ.createResultIQ(packet); reply.setChildElement(packet.getChildElement().createCopy()); if (ex instanceof IllegalArgumentException) { reply.setError(PacketError.Condition.not_acceptable); } else if (ex instanceof UnauthorizedException) { reply.setError(PacketError.Condition.not_authorized); } else if (ex instanceof UnauthenticatedException) { reply.setError(PacketError.Condition.not_authorized); } else { reply.setError(PacketError.Condition.internal_server_error); } } // Send the response directly to the session if (reply != null) { session.process(reply); } return null; }
@SuppressWarnings("deprecation") @Override public IQ handleIQ(IQ packet) throws UnauthorizedException { final JID sender = packet.getFrom(); final JID recipient = packet.getTo(); // Process the request inside a try/catch so that unhandled exceptions // (oufofbounds etc...) can trigger a server error and we can send a // error result packet try { // A valid request is an IQ of type set, if (!packet.getType().equals(IQ.Type.set)) { IQ result = IQ.createResultIQ(packet); result.setChildElement(packet.getChildElement().createCopy()); result.setError(PacketError.Condition.bad_request); return result; } // If a recipient is specified, it must be equal to the sender // bareJID if (recipient != null && !recipient.toString().equals(sender.toBareJID())) { IQ result = IQ.createResultIQ(packet); result.setChildElement(packet.getChildElement().createCopy()); result.setError(PacketError.Condition.not_authorized); return result; } // Only a local user can publish its profile if (!userManager.isRegisteredUser(sender.getNode())) { IQ result = IQ.createResultIQ(packet); result.setChildElement(packet.getChildElement().createCopy()); result.setError(PacketError.Condition.not_authorized); return result; } // A valid submit requets must contain a vcard4 entry Element request = packet.getChildElement(); Element e_profile = request.element(QName.get(VCard4.VCARD_ELEMENT, VCard4.NAMESPACE)); if (e_profile == null) { IQ result = IQ.createResultIQ(packet); result.setChildElement(packet.getChildElement().createCopy()); result.setError(PacketError.Condition.bad_request); return result; } // Parse the profile VCard4DomReader reader = new PersistentVCard4DomReader(); Profile profile = reader.readProfile(new ElementAdapter(e_profile)); // Commit the profile (this will also trigger the messages) try { ProfileManager.getInstance().publishProfile(sender.toBareJID(), profile); } catch (UserNotFoundException e) { // We know this cannot happen } // Send a success result // TODO should this contain more, like the ID of the new activities ? IQ result = IQ.createResultIQ(packet); return result; } catch (Exception e) { Log.error(LocaleUtils.getLocalizedString("admin.error"), e); IQ result = IQ.createResultIQ(packet); result.setChildElement(packet.getChildElement().createCopy()); result.setError(PacketError.Condition.internal_server_error); return result; } }