Java Code Examples for org.jivesoftware.smack.packet.IQ#setFrom()
The following examples show how to use
org.jivesoftware.smack.packet.IQ#setFrom() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RemoteSessionCountOverXMPP.java From olat with Apache License 2.0 | 5 votes |
/** */ @Override public int countSessions() { final XMPPConnection con = adminUser.getConnection(); if (con != null && con.isConnected()) { // TODO:gs may put in other thread??? SessionCount response; try { final IQ packet = new SessionCount(); packet.setFrom(con.getUser()); final PacketCollector collector = con.createPacketCollector(new PacketIDFilter(packet.getPacketID())); con.sendPacket(packet); response = (SessionCount) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); collector.cancel(); if (response == null) { log.warn("Error while trying to count sessions at IM server. Response was null!"); return sessionCount; } if (response.getError() != null) { log.warn("Error while trying to count sessions at IM server. " + response.getError().getMessage()); return sessionCount; } else if (response.getType() == IQ.Type.ERROR) { log.warn("Error while trying to count sessions at IM server"); return sessionCount; } sessionCount = response.getNumberOfSessions(); if (sessionCount > 0) { return sessionCount - 1; } return sessionCount; } catch (final Exception e) { log.warn("Error while trying to count sessions at IM server. Response was null!", e); return sessionCount; } } return sessionCount; }
Example 2
Source File: RemoteGroupCreationOverXMPP.java From olat with Apache License 2.0 | 5 votes |
private boolean sendPacket(final IQ packet) { final XMPPConnection con = adminUser.getConnection(); try { packet.setFrom(con.getUser()); final PacketCollector collector = con.createPacketCollector(new PacketIDFilter(packet.getPacketID())); con.sendPacket(packet); final IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); collector.cancel(); if (response == null) { log.error("Error while trying to create/delete group at IM server. Response was null! packet type: " + packet.getClass()); return false; } if (response.getError() != null) { if (response.getError().getCode() == 409) { // 409 -> conflict / group already exists return true; } else if (response.getError().getCode() == 404) { // 404 -> not found, does not matter when trying to delete return true; } log.error("Error while trying to create/delete group at IM server. " + response.getError().getMessage()); return false; } else if (response.getType() == IQ.Type.ERROR) { log.error("Error while trying to create/delete group at IM server"); return false; } return true; } catch (final RuntimeException e) { log.error("Error while trying to create/delete group at IM server"); return false; } }
Example 3
Source File: RemotePluginVersionOverXMPP.java From olat with Apache License 2.0 | 5 votes |
/** */ @Override public String getPluginVersion() { final XMPPConnection con = adminUser.getConnection(); if (con != null && con.isConnected()) { PluginVersion response; try { final IQ packet = new PluginVersion(); packet.setFrom(con.getUser()); final PacketCollector collector = con.createPacketCollector(new PacketIDFilter(packet.getPacketID())); // TODO:gs is sending packets over one connection thread save? con.sendPacket(packet); response = (PluginVersion) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); collector.cancel(); if (response == null) { log.error("Error while trying to get version at IM server. Response was null!"); return version; } if (response.getError() != null) { log.error("Error while trying to get version at IM server. " + response.getError().getMessage()); return version; } else if (response.getType() == IQ.Type.ERROR) { log.error("Error while trying to get version at IM server"); return version; } return response.getVersion(); } catch (final Exception e) { log.error("Error while trying to get version at IM server. Response was null!", e); return version; } } return version; }
Example 4
Source File: RemoteSessionCountOverXMPP.java From olat with Apache License 2.0 | 5 votes |
/** */ @Override public int countSessions() { final XMPPConnection con = adminUser.getConnection(); if (con != null && con.isConnected()) { // TODO:gs may put in other thread??? SessionCount response; try { final IQ packet = new SessionCount(); packet.setFrom(con.getUser()); final PacketCollector collector = con.createPacketCollector(new PacketIDFilter(packet.getPacketID())); con.sendPacket(packet); response = (SessionCount) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); collector.cancel(); if (response == null) { log.warn("Error while trying to count sessions at IM server. Response was null!"); return sessionCount; } if (response.getError() != null) { log.warn("Error while trying to count sessions at IM server. " + response.getError().getMessage()); return sessionCount; } else if (response.getType() == IQ.Type.ERROR) { log.warn("Error while trying to count sessions at IM server"); return sessionCount; } sessionCount = response.getNumberOfSessions(); if (sessionCount > 0) { return sessionCount - 1; } return sessionCount; } catch (final Exception e) { log.warn("Error while trying to count sessions at IM server. Response was null!", e); return sessionCount; } } return sessionCount; }
Example 5
Source File: RemoteGroupCreationOverXMPP.java From olat with Apache License 2.0 | 5 votes |
private boolean sendPacket(final IQ packet) { final XMPPConnection con = adminUser.getConnection(); try { packet.setFrom(con.getUser()); final PacketCollector collector = con.createPacketCollector(new PacketIDFilter(packet.getPacketID())); con.sendPacket(packet); final IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); collector.cancel(); if (response == null) { log.error("Error while trying to create/delete group at IM server. Response was null! packet type: " + packet.getClass()); return false; } if (response.getError() != null) { if (response.getError().getCode() == 409) { // 409 -> conflict / group already exists return true; } else if (response.getError().getCode() == 404) { // 404 -> not found, does not matter when trying to delete return true; } log.error("Error while trying to create/delete group at IM server. " + response.getError().getMessage()); return false; } else if (response.getType() == IQ.Type.ERROR) { log.error("Error while trying to create/delete group at IM server"); return false; } return true; } catch (final RuntimeException e) { log.error("Error while trying to create/delete group at IM server"); return false; } }
Example 6
Source File: RemotePluginVersionOverXMPP.java From olat with Apache License 2.0 | 5 votes |
/** */ @Override public String getPluginVersion() { final XMPPConnection con = adminUser.getConnection(); if (con != null && con.isConnected()) { PluginVersion response; try { final IQ packet = new PluginVersion(); packet.setFrom(con.getUser()); final PacketCollector collector = con.createPacketCollector(new PacketIDFilter(packet.getPacketID())); // TODO:gs is sending packets over one connection thread save? con.sendPacket(packet); response = (PluginVersion) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); collector.cancel(); if (response == null) { log.error("Error while trying to get version at IM server. Response was null!"); return version; } if (response.getError() != null) { log.error("Error while trying to get version at IM server. " + response.getError().getMessage()); return version; } else if (response.getType() == IQ.Type.ERROR) { log.error("Error while trying to get version at IM server"); return version; } return response.getVersion(); } catch (final Exception e) { log.error("Error while trying to get version at IM server. Response was null!", e); return version; } } return version; }
Example 7
Source File: IBBPacketUtils.java From Smack with Apache License 2.0 | 5 votes |
/** * Returns an error IQ. * * @param from the senders JID * @param to the recipients JID * @param condition the XMPP error condition * @return an error IQ */ public static IQ createErrorIQ(Jid from, Jid to, StanzaError.Condition condition) { StanzaError xmppError = StanzaError.getBuilder(condition).build(); IQ errorIQ = new ErrorIQ(xmppError); errorIQ.setType(IQ.Type.error); errorIQ.setFrom(from); errorIQ.setTo(to); return errorIQ; }
Example 8
Source File: IBBPacketUtils.java From Smack with Apache License 2.0 | 5 votes |
/** * Returns a result IQ. * * @param from the senders JID * @param to the recipients JID * @return a result IQ */ public static IQ createResultIQ(Jid from, Jid to) { IQ result = new EmptyResultIQ(); result.setType(IQ.Type.result); result.setFrom(from); result.setTo(to); return result; }
Example 9
Source File: Socks5ClientForInitiatorTest.java From Smack with Apache License 2.0 | 5 votes |
/** * If the initiator can connect to a SOCKS5 proxy but activating the stream fails an exception * should be thrown. * * @throws Exception should not happen */ @Test public void shouldFailIfActivateSocks5ProxyFails() throws Exception { Protocol protocol = new Protocol(); XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID); // build error response as reply to the stream activation IQ error = new ErrorIQ(StanzaError.getBuilder(StanzaError.Condition.internal_server_error).build()); error.setFrom(proxyJID); error.setTo(initiatorJID); protocol.addResponse(error, Verification.correspondingSenderReceiver, Verification.requestTypeSET); // start a local SOCKS5 proxy try (Socks5TestProxy socks5Proxy = new Socks5TestProxy()) { StreamHost streamHost = new StreamHost(proxyJID, loopbackAddress, socks5Proxy.getPort()); // create digest to get the socket opened by target String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID); Socks5ClientForInitiator socks5Client = new Socks5ClientForInitiator(streamHost, digest, connection, sessionID, targetJID); try { socks5Client.getSocket(GET_SOCKET_TIMEOUT); fail("exception should be thrown"); } catch (XMPPErrorException e) { assertTrue(StanzaError.Condition.internal_server_error.equals(e.getStanzaError().getCondition())); protocol.verifyAll(); } } }
Example 10
Source File: RemoteAccountCreationOverXMPP.java From olat with Apache License 2.0 | 4 votes |
private boolean sendPacket(final IQ packet) { final XMPPConnection con = adminUser.getConnection(); try { packet.setFrom(con.getUser()); final PacketCollector collector = con.createPacketCollector(new PacketIDFilter(packet.getPacketID())); con.sendPacket(packet); final IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); collector.cancel(); if (response == null) { // OLAT-5384: error happens frequently, lowering to WARN log.warn("Error while trying to create/delete user at IM server. Response was null!"); return false; } if (response.getError() != null) { if (response.getError().getCode() == 503) { // 503 code means service not available, IM server plugin may not installed log.error("Openfire and OLAT talk over an custom Openfire plugin. Please make sure you have it installed! " + "Download it under http://www.olat.org/downloads/stable/olatUserAndGroupService.jar"); } else if (response.getError().getCode() == 407 || response.getError().getCode() == 409) { // 407 or 409 -> conflict / user already exists return true; } else if (response.getError().getCode() == 404) { // 404 -> user not found, ok when trying to delete return true; } log.warn("Error while trying to create/delete user at IM server. Errorcode: " + response.getError().getCode()); return false; } else if (response.getType() == IQ.Type.ERROR) { log.error("Error while trying to create/delete user at IM server. Response type error"); return false; } if (response instanceof UserCheck) { final UserCheck check = (UserCheck) response; return check.hasAccount(); } return true; } catch (final Exception e) { log.error("Error while trying to create/delete user at IM server", e); return false; } }
Example 11
Source File: RemoteAccountCreationOverXMPP.java From olat with Apache License 2.0 | 4 votes |
private boolean sendPacket(final IQ packet) { final XMPPConnection con = adminUser.getConnection(); try { packet.setFrom(con.getUser()); final PacketCollector collector = con.createPacketCollector(new PacketIDFilter(packet.getPacketID())); con.sendPacket(packet); final IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); collector.cancel(); if (response == null) { // OLAT-5384: error happens frequently, lowering to WARN log.warn("Error while trying to create/delete user at IM server. Response was null!"); return false; } if (response.getError() != null) { if (response.getError().getCode() == 503) { // 503 code means service not available, IM server plugin may not installed log.error("Openfire and OLAT talk over an custom Openfire plugin. Please make sure you have it installed! " + "Download it under http://www.olat.org/downloads/stable/olatUserAndGroupService.jar"); } else if (response.getError().getCode() == 407 || response.getError().getCode() == 409) { // 407 or 409 -> conflict / user already exists return true; } else if (response.getError().getCode() == 404) { // 404 -> user not found, ok when trying to delete return true; } log.warn("Error while trying to create/delete user at IM server. Errorcode: " + response.getError().getCode()); return false; } else if (response.getType() == IQ.Type.ERROR) { log.error("Error while trying to create/delete user at IM server. Response type error"); return false; } if (response instanceof UserCheck) { final UserCheck check = (UserCheck) response; return check.hasAccount(); } return true; } catch (final Exception e) { log.error("Error while trying to create/delete user at IM server", e); return false; } }
Example 12
Source File: Socks5ByteStreamManagerTest.java From Smack with Apache License 2.0 | 4 votes |
/** * Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} should fail if the * target does not accept a SOCKS5 Bytestream. See <a * href="http://xmpp.org/extensions/xep-0065.html#usecase-alternate">XEP-0065 Section 5.2 A2</a> * @throws InterruptedException if the calling thread was interrupted. * @throws SmackException if Smack detected an exceptional situation. * @throws XMPPException if an XMPP protocol error was received. * @throws IOException if an I/O error occurred. */ @Test public void shouldFailIfTargetDoesNotAcceptSocks5Bytestream() throws SmackException, InterruptedException, IOException, XMPPException { final Protocol protocol = new Protocol(); final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID); final String sessionID = "session_id_shouldFailIfTargetDoesNotAcceptSocks5Bytestream"; // get Socks5ByteStreamManager for connection Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection); byteStreamManager.setAnnounceLocalStreamHost(false); /** * create responses in the order they should be queried specified by the XEP-0065 * specification */ // build discover info that supports the SOCKS5 feature DiscoverInfoBuilder discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID); discoverInfo.addFeature(Bytestream.NAMESPACE); // return that SOCKS5 is supported if target is queried protocol.addResponse(discoverInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET); // build discover items containing a proxy item DiscoverItems discoverItems = Socks5PacketUtils.createDiscoverItems(xmppServer, initiatorJID); Item item = new Item(proxyJID); discoverItems.addItem(item); // return the proxy item if XMPP server is queried protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET); // build discover info for proxy containing information about being a SOCKS5 proxy DiscoverInfoBuilder proxyInfo = Socks5PacketUtils.createDiscoverInfo(proxyJID, initiatorJID); Identity identity = new Identity("proxy", proxyJID.toString(), "bytestreams"); proxyInfo.addIdentity(identity); // return the socks5 bytestream proxy identity if proxy is queried protocol.addResponse(proxyInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET); // build a socks5 stream host info containing the address and the port of the // proxy Bytestream streamHostInfo = Socks5PacketUtils.createBytestreamResponse(proxyJID, initiatorJID); streamHostInfo.addStreamHost(proxyJID, proxyAddress, 7778); // return stream host info if it is queried protocol.addResponse(streamHostInfo, Verification.correspondingSenderReceiver, Verification.requestTypeGET); // build error packet to reject SOCKS5 Bytestream StanzaError stanzaError = StanzaError.getBuilder(StanzaError.Condition.not_acceptable).build(); IQ rejectPacket = new ErrorIQ(stanzaError); rejectPacket.setFrom(targetJID); rejectPacket.setTo(initiatorJID); // return error packet as response to the bytestream initiation protocol.addResponse(rejectPacket, Verification.correspondingSenderReceiver, Verification.requestTypeSET); XMPPErrorException e = assertThrows(XMPPErrorException.class, () -> { // start SOCKS5 Bytestream byteStreamManager.establishSession(targetJID, sessionID); }); protocol.verifyAll(); assertEquals(rejectPacket.getError(), e.getStanzaError()); }
Example 13
Source File: Socks5ClientForInitiatorTest.java From Smack with Apache License 2.0 | 4 votes |
/** * Target and initiator should successfully connect to a "remote" SOCKS5 proxy and the initiator * activates the bytestream. * * @throws Exception should not happen */ @Test public void shouldSuccessfullyEstablishConnectionAndActivateSocks5Proxy() throws Exception { Protocol protocol = new Protocol(); XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID); // build activation confirmation response IQ activationResponse = new EmptyResultIQ(); activationResponse.setFrom(proxyJID); activationResponse.setTo(initiatorJID); protocol.addResponse(activationResponse, Verification.correspondingSenderReceiver, Verification.requestTypeSET, new Verification<Bytestream, IQ>() { @Override public void verify(Bytestream request, IQ response) { // verify that the correct stream should be activated assertNotNull(request.getToActivate()); assertEquals(targetJID, request.getToActivate().getTarget()); } }); Socket initiatorSocket = null, targetSocket = null; // start a local SOCKS5 proxy try (Socks5TestProxy socks5Proxy = new Socks5TestProxy()) { StreamHost streamHost = new StreamHost(proxyJID, loopbackAddress, socks5Proxy.getPort()); // create digest to get the socket opened by target String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID); Socks5ClientForInitiator socks5Client = new Socks5ClientForInitiator(streamHost, digest, connection, sessionID, targetJID); initiatorSocket = socks5Client.getSocket(10000); InputStream in = initiatorSocket.getInputStream(); targetSocket = socks5Proxy.getSocket(digest); OutputStream out = targetSocket.getOutputStream(); // verify test data for (int i = 0; i < 10; i++) { out.write(i); assertEquals(i, in.read()); } protocol.verifyAll(); } finally { CloseableUtil.maybeClose(initiatorSocket); CloseableUtil.maybeClose(targetSocket); } }
Example 14
Source File: AgentSession.java From Smack with Apache License 2.0 | 3 votes |
/** * Invites a user or agent to an existing session support. The provided invitee's JID can be of * a user, an agent, a queue or a workgroup. In the case of a queue or a workgroup the workgroup service * will decide the best agent to receive the invitation.<p> * * This method will return either when the service returned an ACK of the request or if an error occurred * while requesting the invitation. After sending the ACK the service will send the invitation to the target * entity. When dealing with agents the common sequence of offer-response will be followed. However, when * sending an invitation to a user a standard MUC invitation will be sent.<p> * * The agent or user that accepted the offer <b>MUST</b> join the room. Failing to do so will make * the invitation to fail. The inviter will eventually receive a message error indicating that the invitee * accepted the offer but failed to join the room. * * Different situations may lead to a failed invitation. Possible cases are: 1) all agents rejected the * offer and there are no agents available, 2) the agent that accepted the offer failed to join the room or * 2) the user that received the MUC invitation never replied or joined the room. In any of these cases * (or other failing cases) the inviter will get an error message with the failed notification. * * @param type type of entity that will get the invitation. * @param invitee JID of entity that will get the invitation. * @param sessionID ID of the support session that the invitee is being invited. * @param reason the reason of the invitation. * @throws XMPPErrorException if the sender of the invitation is not an agent or the service failed to process * the request. * @throws NoResponseException if there was no response from the remote entity. * @throws NotConnectedException if the XMPP connection is not connected. * @throws InterruptedException if the calling thread was interrupted. */ public void sendRoomInvitation(RoomInvitation.Type type, Jid invitee, String sessionID, String reason) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { final RoomInvitation invitation = new RoomInvitation(type, invitee, sessionID, reason); IQ iq = new RoomInvitation.RoomInvitationIQ(invitation); iq.setType(IQ.Type.set); iq.setTo(workgroupJID); iq.setFrom(connection.getUser()); connection.createStanzaCollectorAndSend(iq).nextResultOrThrow(); }
Example 15
Source File: AgentSession.java From Smack with Apache License 2.0 | 3 votes |
/** * Transfer an existing session support to another user or agent. The provided invitee's JID can be of * a user, an agent, a queue or a workgroup. In the case of a queue or a workgroup the workgroup service * will decide the best agent to receive the invitation.<p> * * This method will return either when the service returned an ACK of the request or if an error occurred * while requesting the transfer. After sending the ACK the service will send the invitation to the target * entity. When dealing with agents the common sequence of offer-response will be followed. However, when * sending an invitation to a user a standard MUC invitation will be sent.<p> * * Once the invitee joins the support room the workgroup service will kick the inviter from the room.<p> * * Different situations may lead to a failed transfers. Possible cases are: 1) all agents rejected the * offer and there are no agents available, 2) the agent that accepted the offer failed to join the room * or 2) the user that received the MUC invitation never replied or joined the room. In any of these cases * (or other failing cases) the inviter will get an error message with the failed notification. * * @param type type of entity that will get the invitation. * @param invitee JID of entity that will get the invitation. * @param sessionID ID of the support session that the invitee is being invited. * @param reason the reason of the invitation. * @throws XMPPErrorException if the sender of the invitation is not an agent or the service failed to process * the request. * @throws NoResponseException if there was no response from the remote entity. * @throws NotConnectedException if the XMPP connection is not connected. * @throws InterruptedException if the calling thread was interrupted. */ public void sendRoomTransfer(RoomTransfer.Type type, String invitee, String sessionID, String reason) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { final RoomTransfer transfer = new RoomTransfer(type, invitee, sessionID, reason); IQ iq = new RoomTransfer.RoomTransferIQ(transfer); iq.setType(IQ.Type.set); iq.setTo(workgroupJID); iq.setFrom(connection.getUser()); connection.createStanzaCollectorAndSend(iq).nextResultOrThrow(); }
Example 16
Source File: Socks5PacketUtils.java From Smack with Apache License 2.0 | 3 votes |
/** * Returns a response IQ for a activation request to the proxy. * * @param from JID of the proxy * @param to JID of the client who wants to activate the SOCKS5 Bytestream * @return response IQ for a activation request to the proxy */ public static IQ createActivationConfirmation(Jid from, Jid to) { IQ response = new EmptyResultIQ(); response.setFrom(from); response.setTo(to); return response; }