@Override public void sendStanza(Stanza packet) throws NotConnectedException { super.sendStanza(packet); if (packet instanceof IQ && !timeout) { timeout = false; // Set reply packet to match one being sent. We haven't started the // other thread yet so this is still safe. IQ replyPacket = replyQ.peek(); // If no reply has been set via addIQReply, then we create a simple reply if (replyPacket == null) { replyPacket = IQ.createResultIQ((IQ) packet); replyQ.add(replyPacket); } replyPacket.setStanzaId(packet.getStanzaId()); replyPacket.setFrom(packet.getTo()); replyPacket.setTo(packet.getFrom()); replyPacket.setType(Type.result); new ProcessQueue(replyQ).start(); } }
/** * Removes a roster entry from the roster. The roster entry will also be removed from the * unfiled entries or from any roster group where it could belong and will no longer be part * of the roster. Note that this is a synchronous call -- Smack must wait for the server * to send an updated subscription status. * * @param entry a roster entry. * @throws XMPPErrorException if an XMPP error occurs. * @throws NotLoggedInException if not logged in. * @throws NoResponseException SmackException if there was no response from the server. * @throws NotConnectedException * @throws IllegalStateException if connection is not logged in or logged in anonymously */ public void removeEntry(RosterEntry entry) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException { final XMPPConnection connection = connection(); if (!connection.isAuthenticated()) { throw new NotLoggedInException(); } if (connection.isAnonymous()) { throw new IllegalStateException("Anonymous users can't have a roster."); } // Only remove the entry if it's in the entry list. // The actual removal logic takes place in RosterPacketListenerprocess>>Packet(Packet) if (!entries.containsKey(entry.getUser())) { return; } RosterPacket packet = new RosterPacket(); packet.setType(IQ.Type.set); RosterPacket.Item item = RosterEntry.toRosterItem(entry); // Set the item type as REMOVE so that the server will delete the entry item.setItemType(RosterPacket.ItemType.remove); packet.addRosterItem(item); connection.createPacketCollectorAndSend(packet).nextResultOrThrow(); }
/** * Remove all roster entries by iterating trough {@link Roster#getEntries()} * and simulating receiving roster pushes from the server. * * @param connection the dummy connection of which the provided roster belongs to. * @param roster the roster (or buddy list) which should be initialized. */ public static void removeAllRosterEntries(DummyConnection connection, Roster roster) throws InterruptedException, XMPPException { for(RosterEntry entry : roster.getEntries()) { // prepare the roster push packet final RosterPacket rosterPush= new RosterPacket(); rosterPush.setType(Type.set); rosterPush.setTo(connection.getUser()); // prepare the buddy's item entry which should be removed final RosterPacket.Item item = new RosterPacket.Item(entry.getUser(), entry.getName()); item.setItemType(ItemType.remove); rosterPush.addRosterItem(item); // simulate receiving the roster push connection.processPacket(rosterPush); } }
/** * Creates a node with specified configuration. * * Note: This is the only way to create a collection node. * * @param name The name of the node, which must be unique within the * pubsub service * @param config The configuration for the node * @return The node that was created * @throws XMPPErrorException * @throws NoResponseException * @throws NotConnectedException */ public Node createNode(String name, Form config) throws NoResponseException, XMPPErrorException, NotConnectedException { PubSub request = PubSub.createPubsubPacket(to, Type.set, new NodeExtension(PubSubElementType.CREATE, name), null); boolean isLeafNode = true; if (config != null) { request.addExtension(new FormNode(FormNodeType.CONFIGURE, config)); FormField nodeTypeField = config.getField(ConfigureNodeFields.node_type.getFieldName()); if (nodeTypeField != null) isLeafNode = nodeTypeField.getValues().get(0).equals(NodeType.leaf.toString()); } // Errors will cause exceptions in getReply, so it only returns // on success. sendPubsubPacket(con, request); Node newNode = isLeafNode ? new LeafNode(con, name) : new CollectionNode(con, name); newNode.setTo(to); nodeMap.put(newNode.getId(), newNode); return newNode; }
private List<Subscription> getSubscriptions(List<ExtensionElement> additionalExtensions, Collection<ExtensionElement> returnedExtensions, PubSubNamespace pubSubNamespace) throws NoResponseException, XMPPErrorException, NotConnectedException { PubSub pubSub = createPubsubPacket(Type.get, new NodeExtension(PubSubElementType.SUBSCRIPTIONS, getId()), pubSubNamespace); if (additionalExtensions != null) { for (ExtensionElement pe : additionalExtensions) { pubSub.addExtension(pe); } } PubSub reply = sendPubsubPacket(pubSub); if (returnedExtensions != null) { returnedExtensions.addAll(reply.getExtensions()); } SubscriptionsExtension subElem = (SubscriptionsExtension) reply.getExtension(PubSubElementType.SUBSCRIPTIONS); return subElem.getSubscriptions(); }
/** * Get the affiliations of this node. * <p> * {@code additionalExtensions} can be used e.g. to add a "Result Set Management" extension. * {@code returnedExtensions} will be filled with the stanza(/packet) extensions found in the answer. * </p> * * @param additionalExtensions additional {@code PacketExtensions} add to the request * @param returnedExtensions a collection that will be filled with the returned packet * extensions * @return List of {@link Affiliation} * @throws NoResponseException * @throws XMPPErrorException * @throws NotConnectedException */ public List<Affiliation> getAffiliations(List<ExtensionElement> additionalExtensions, Collection<ExtensionElement> returnedExtensions) throws NoResponseException, XMPPErrorException, NotConnectedException { PubSub pubSub = createPubsubPacket(Type.get, new NodeExtension(PubSubElementType.AFFILIATIONS, getId())); if (additionalExtensions != null) { for (ExtensionElement pe : additionalExtensions) { pubSub.addExtension(pe); } } PubSub reply = sendPubsubPacket(pubSub); if (returnedExtensions != null) { returnedExtensions.addAll(reply.getExtensions()); } AffiliationsExtension affilElem = (AffiliationsExtension) reply.getExtension(PubSubElementType.AFFILIATIONS); return affilElem.getAffiliations(); }
private EntityTimeManager(XMPPConnection connection) { super(connection); if (autoEnable) enable(); connection.registerIQRequestHandler(new AbstractIqRequestHandler(Time.ELEMENT, Time.NAMESPACE, Type.get, Mode.async) { @Override public IQ handleIQRequest(IQ iqRequest) { if (enabled) { return Time.createResponse(iqRequest); } else { return IQ.createErrorResponse(iqRequest, new XMPPError(Condition.not_acceptable)); } } }); }
/** * Creates a node with specified configuration. * * Note: This is the only way to create a collection node. * * @param name The name of the node, which must be unique within the * pubsub service * @param config The configuration for the node * @return The node that was created * @exception XMPPException */ public Node createNode(String name, Form config) throws XMPPException { PubSub request = createPubsubPacket(to, Type.SET, new NodeExtension(PubSubElementType.CREATE, name)); boolean isLeafNode = true; if (config != null) { request.addExtension(new FormNode(FormNodeType.CONFIGURE, config)); FormField nodeTypeField = config.getField(ConfigureNodeFields.node_type.getFieldName()); if (nodeTypeField != null) isLeafNode = nodeTypeField.getValues().next().equals(NodeType.leaf.toString()); } // Errors will cause exceptions in getReply, so it only returns // on success. sendPubsubPacket(con, to, Type.SET, request); Node newNode = isLeafNode ? new LeafNode(con, name) : new CollectionNode(con, name); newNode.setTo(to); nodeMap.put(newNode.getId(), newNode); return newNode; }
/** * Get the items specified from the node. This would typically be * used when the server does not return the payload due to size * constraints. The user would be required to retrieve the payload * after the items have been retrieved via {@link #getItems()} or an * event, that did not include the payload. * * @param ids Item ids of the items to retrieve * * @return The list of {@link Item} with payload * * @throws XMPPException */ public <T extends Item> List<T> getItems(Collection<String> ids) throws XMPPException { List<Item> itemList = new ArrayList<Item>(ids.size()); for (String id : ids) { itemList.add(new Item(id)); } PubSub request = createPubsubPacket(Type.GET, new ItemsExtension(ItemsExtension.ItemsElementType.items, getId(), itemList)); PubSub result = (PubSub)SyncPacketSend.getReply(con, request); ItemsExtension itemsElem = (ItemsExtension)result.getExtension(PubSubElementType.ITEMS); return (List<T>)itemsElem.getItems(); }
public static void sendReadedInfo(String packetId, String to) { IQ result = new IQ() { @Override public String getChildElementXML() { return null; } }; result.setType(Type.RESULT); result.setPacketID(packetId); result.setTo(to); try { Constants.xmppManager.getConnection().sendPacket(result); } catch (Exception e) { e.printStackTrace(); } }
@Override public void setStatusFromConfig() {// 设置自己的当前状态,供外部服务调用 boolean messageCarbons = PreferenceUtils.getPrefBoolean(mService, PreferenceConstants.MESSAGE_CARBONS, true); String statusMode = PreferenceUtils.getPrefString(mService, PreferenceConstants.STATUS_MODE, PreferenceConstants.AVAILABLE); String statusMessage = PreferenceUtils.getPrefString(mService, PreferenceConstants.STATUS_MESSAGE, mService.getString(R.string.status_online)); int priority = PreferenceUtils.getPrefInt(mService, PreferenceConstants.PRIORITY, 0); if (messageCarbons) CarbonManager.getInstanceFor(mXMPPConnection).sendCarbonsEnabled( true); Presence presence = new Presence(Presence.Type.available); Mode mode = Mode.valueOf(statusMode); presence.setMode(mode); presence.setStatus(statusMessage); presence.setPriority(priority); mXMPPConnection.sendPacket(presence); }
@Override public void sendMessage(String toJID, String message) {// 发送消息 // TODO Auto-generated method stub final Message newMessage = new Message(toJID, Message.Type.chat); newMessage.setBody(message); newMessage.addExtension(new DeliveryReceiptRequest()); if (isAuthenticated()) { addChatMessageToDB(ChatConstants.OUTGOING, toJID, message, ChatConstants.DS_SENT_OR_READ, System.currentTimeMillis(), newMessage.getPacketID()); mXMPPConnection.sendPacket(newMessage); } else { // send offline -> store to DB addChatMessageToDB(ChatConstants.OUTGOING, toJID, message, ChatConstants.DS_NEW, System.currentTimeMillis(), newMessage.getPacketID()); } }
@Override public void sendServerPing() { if (mPingID != null) {// 此时说明上一次ping服务器还未回应,直接返回,直到连接超时 L.d("Ping: requested, but still waiting for " + mPingID); return; // a ping is still on its way } Ping ping = new Ping(); ping.setType(Type.GET); ping.setTo(PreferenceUtils.getPrefString(mService, PreferenceConstants.Server, PreferenceConstants.GMAIL_SERVER)); mPingID = ping.getPacketID();// 此id其实是随机生成,但是唯一的 mPingTimestamp = System.currentTimeMillis(); L.d("Ping: sending ping " + mPingID); mXMPPConnection.sendPacket(ping);// 发送ping消息 // register ping timeout handler: PACKET_TIMEOUT(30s) + 3s ((AlarmManager) mService.getSystemService(Context.ALARM_SERVICE)).set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + PACKET_TIMEOUT + 3000, mPongTimeoutAlarmPendIntent);// 此时需要启动超时判断的闹钟了,时间间隔为30+3秒 }
/** * Creates a node with specified configuration. * * Note: This is the only way to create a collection node. * * @param name * The name of the node, which must be unique within the pubsub * service * @param config * The configuration for the node * @return The node that was created * @exception XMPPException */ public Node createNode(String name, Form config) throws XMPPException { PubSub request = createPubsubPacket(to, Type.SET, new NodeExtension( PubSubElementType.CREATE, name)); boolean isLeafNode = true; if (config != null) { request.addExtension(new FormNode(FormNodeType.CONFIGURE, config)); FormField nodeTypeField = config .getField(ConfigureNodeFields.node_type.getFieldName()); if (nodeTypeField != null) isLeafNode = nodeTypeField.getValues().next() .equals(NodeType.leaf.toString()); } // Errors will cause exceptions in getReply, so it only returns // on success. sendPubsubPacket(con, to, Type.SET, request); Node newNode = isLeafNode ? new LeafNode(con, name) : new CollectionNode(con, name); newNode.setTo(to); nodeMap.put(newNode.getId(), newNode); return newNode; }
/** * Get the items specified from the node. This would typically be used when * the server does not return the payload due to size constraints. The user * would be required to retrieve the payload after the items have been * retrieved via {@link #getItems()} or an event, that did not include the * payload. * * @param ids * Item ids of the items to retrieve * * @return The list of {@link Item} with payload * * @throws XMPPException */ public <T extends Item> List<T> getItems(Collection<String> ids) throws XMPPException { List<Item> itemList = new ArrayList<Item>(ids.size()); for (String id : ids) { itemList.add(new Item(id)); } PubSub request = createPubsubPacket(Type.GET, new ItemsExtension( ItemsExtension.ItemsElementType.items, getId(), itemList)); PubSub result = (PubSub) SyncPacketSend.getReply(con, request); ItemsExtension itemsElem = (ItemsExtension) result .getExtension(PubSubElementType.ITEMS); return (List<T>) itemsElem.getItems(); }
@Override public void sendServerPing() { if (mPingID != null) {// 此时说明上一次ping服务器还未回应,直接返回,直到连接超时 AppLogger.d("Ping: requested, but still waiting for " + mPingID); return; // a ping is still on its way } Ping ping = new Ping(); ping.setType(Type.GET); ping.setTo(PreferenceUtils.getPrefString(mService, PreferenceConstants.Server, PreferenceConstants.GMAIL_SERVER)); mPingID = ping.getPacketID();// 此id其实是随机生成,但是唯一的 mPingTimestamp = System.currentTimeMillis(); AppLogger.d("Ping: sending ping " + mPingID); mXMPPConnection.sendPacket(ping);// 发送ping消息 // register ping timeout handler: PACKET_TIMEOUT(30s) + 3s ((AlarmManager) mService.getSystemService(Context.ALARM_SERVICE)).set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + PACKET_TIMEOUT + 3000, mPongTimeoutAlarmPendIntent);// 此时需要启动超时判断的闹钟了,时间间隔为30+3秒 }
public void unsupportedInfo( IQ iq ) { IQ resp = IQ.createResultIQ(iq); resp.setType(Type.ERROR); resp.addExtension(new PacketExtension() { @Override public String getElementName() { return "error"; } @Override public String getNamespace() { return null; } @Override public String toXML() { return "<error type='modify'><feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/><unsupported-info xmlns='urn:xmpp:jingle:errors:1'/></error>" ; } }); connection.sendPacket(resp); }
private void tryToRemoveRosterEntry(String user) throws YaximXMPPException { try { RosterEntry rosterEntry = mRoster.getEntry(user); if (rosterEntry != null) { // first, unsubscribe the user Presence unsub = new Presence(Presence.Type.unsubscribed); unsub.setTo(rosterEntry.getUser()); mXMPPConnection.sendPacket(unsub); // then, remove from roster mRoster.removeEntry(rosterEntry); } } catch (XMPPException e) { throw new YaximXMPPException(e.getLocalizedMessage()); } }
protected AbstractIqRequestHandler(String element, String namespace, Type type, Mode mode) { switch (type) { case set: case get: break; default: throw new IllegalArgumentException("Only get and set IQ type allowed"); } this.element = element; this.namespace = namespace; this.type = type; this.mode = mode; }
public void run() { try { while (true) { final Stanza packet = connection.getSentPacket(); if (packet instanceof RosterPacket && ((IQ) packet).getType() == Type.set) { final RosterPacket rosterRequest = (RosterPacket) packet; // Prepare and process the roster push final RosterPacket rosterPush = new RosterPacket(); final Item item = rosterRequest.getRosterItems().iterator().next(); if (item.getItemType() != ItemType.remove) { item.setItemType(ItemType.none); } rosterPush.setType(Type.set); rosterPush.setTo(connection.getUser()); rosterPush.addRosterItem(item); connection.processPacket(rosterPush); // Create and process the IQ response final IQ response = IQ.createResultIQ(rosterRequest); connection.processPacket(response); // Verify the roster update request assertSame("A roster set MUST contain one and only one <item/> element.", 1, rosterRequest.getRosterItemCount()); verifyUpdateRequest(rosterRequest); break; } } } catch (Throwable e) { exception = e; fail(e.getMessage()); } }
/** * Tests that a non-empty roster result empties the store. * @throws SmackException * @throws XMPPException */ @Test(timeout = 5000) public void testOtherVersionStored() throws InterruptedException, XMPPException, SmackException { Item vaglafItem = vaglafItem(); // We expect that the roster request is the only packet sent. This is not part of the specification, // but a shortcut in the test implementation. Stanza sentPacket = connection.getSentPacket(); if (sentPacket instanceof RosterPacket) { RosterPacket sentRP = (RosterPacket)sentPacket; RosterPacket answer = new RosterPacket(); answer.setStanzaId(sentRP.getStanzaId()); answer.setType(Type.result); answer.setTo(sentRP.getFrom()); answer.setVersion("newVersion"); answer.addRosterItem(vaglafItem); rosterListener.reset(); connection.processPacket(answer); rosterListener.waitUntilInvocationOrTimeout(); } else { assertTrue("Expected to get a RosterPacket ", false); } Roster roster = Roster.getInstanceFor(connection); assertEquals("Size of roster", 1, roster.getEntries().size()); RosterEntry entry = roster.getEntry(vaglafItem.getUser()); assertNotNull("Roster contains vaglaf entry", entry); assertEquals("vaglaf entry in roster equals the sent entry", vaglafItem, RosterEntry.toRosterItem(entry)); RosterStore store = roster.getRosterStore(); assertEquals("Size of store", 1, store.getEntries().size()); Item item = store.getEntry(vaglafItem.getUser()); assertNotNull("Store contains vaglaf entry"); assertEquals("vaglaf entry in store equals the sent entry", vaglafItem, item); }
/** * Creates an instant node, if supported. * * @return The node that was created * @throws XMPPErrorException * @throws NoResponseException * @throws NotConnectedException */ public LeafNode createNode() throws NoResponseException, XMPPErrorException, NotConnectedException { PubSub reply = sendPubsubPacket(Type.set, new NodeExtension(PubSubElementType.CREATE), null); NodeExtension elem = reply.getExtension("create", PubSubNamespace.BASIC.getXmlns()); LeafNode newNode = new LeafNode(con, elem.getNode()); newNode.setTo(to); nodeMap.put(newNode.getId(), newNode); return newNode; }
static PubSub sendPubsubPacket(XMPPConnection con, String to, Type type, List<ExtensionElement> extList, PubSubNamespace ns) throws NoResponseException, XMPPErrorException, NotConnectedException { PubSub pubSub = new PubSub(to, type, ns); for (ExtensionElement pe : extList) { pubSub.addExtension(pe); } return sendPubsubPacket(con ,pubSub); }
/** * Delete the items with the specified id's from the node. * * @param itemIds The list of id's of items to delete * @throws XMPPErrorException * @throws NoResponseException if there was no response from the server. * @throws NotConnectedException */ public void deleteItem(Collection<String> itemIds) throws NoResponseException, XMPPErrorException, NotConnectedException { List<Item> items = new ArrayList<Item>(itemIds.size()); for (String id : itemIds) { items.add(new Item(id)); } PubSub request = createPubsubPacket(Type.set, new ItemsExtension(ItemsExtension.ItemsElementType.retract, getId(), items)); con.createPacketCollectorAndSend(request).nextResultOrThrow(); }
/** * Publish an event. * * @param item the item to publish. * @throws NotConnectedException */ public void publish(PEPItem item) throws NotConnectedException { // Create a new message to publish the event. PEPPubSub pubSub = new PEPPubSub(item); pubSub.setType(Type.set); //pubSub.setFrom(connection.getUser()); // Send the message that contains the roster connection.sendStanza(pubSub); }
@Override protected boolean acceptSpecific(Privacy privacy) { if (privacy.getType() != Type.set) { return false; } return privacy.getActiveName() != null || privacy.isDeclineActiveList(); }
@Override protected boolean acceptSpecific(Privacy privacy) { if (privacy.getType() != Type.set) { return false; } return privacy.getDefaultName() != null || privacy.isDeclineDefaultList(); }
/** * Creates an instant node, if supported. * * @return The node that was created * @exception XMPPException */ public LeafNode createNode() throws XMPPException { PubSub reply = (PubSub)sendPubsubPacket(Type.SET, new NodeExtension(PubSubElementType.CREATE)); NodeExtension elem = (NodeExtension)reply.getExtension("create", PubSubNamespace.BASIC.getXmlns()); LeafNode newNode = new LeafNode(con, elem.getNode()); newNode.setTo(to); nodeMap.put(newNode.getId(), newNode); return newNode; }