Java Code Examples for org.jivesoftware.smack.packet.IQ#getError()

The following examples show how to use org.jivesoftware.smack.packet.IQ#getError() . 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: Roster.java    From AndroidPNClient with Apache License 2.0 6 votes vote down vote up
/**
 * 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 an asynchronous call -- Smack must wait for the server
 * to send an updated subscription status.
 *
 * @param entry a roster entry.
 * @throws XMPPException if an XMPP error occurs.
 */
public void removeEntry(RosterEntry entry) throws XMPPException {
    // 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);
    PacketCollector collector = connection.createPacketCollector(
            new PacketIDFilter(packet.getPacketID()));
    connection.sendPacket(packet);
    IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    collector.cancel();
    if (response == null) {
        throw new XMPPException("No response from the server.");
    }
    // If the server replied with an error, throw an exception.
    else if (response.getType() == IQ.Type.ERROR) {
        throw new XMPPException(response.getError());
    }
}
 
Example 2
Source File: NonSASLAuthentication.java    From AndroidPNClient with Apache License 2.0 6 votes vote down vote up
public String authenticateAnonymously() throws XMPPException {
    // Create the authentication packet we'll send to the server.
    Authentication auth = new Authentication();

    PacketCollector collector =
        connection.createPacketCollector(new PacketIDFilter(auth.getPacketID()));
    // Send the packet.
    connection.sendPacket(auth);
    // Wait up to a certain number of seconds for a response from the server.
    IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    if (response == null) {
        throw new XMPPException("Anonymous login failed.");
    }
    else if (response.getType() == IQ.Type.ERROR) {
        throw new XMPPException(response.getError());
    }
    // We're done with the collector, so explicitly cancel it.
    collector.cancel();

    if (response.getTo() != null) {
        return response.getTo();
    }
    else {
        return connection.getServiceName() + "/" + ((Authentication) response).getResource();
    }
}
 
Example 3
Source File: AccountManager.java    From AndroidPNClient with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the account registration info from the server.
 *
 * @throws org.jivesoftware.smack.XMPPException if an error occurs.
 */
private synchronized void getRegistrationInfo() throws XMPPException {
    Registration reg = new Registration();
    reg.setTo(connection.getServiceName());
    PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()),
            new PacketTypeFilter(IQ.class));
    PacketCollector collector = connection.createPacketCollector(filter);
    connection.sendPacket(reg);
    IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    collector.cancel();
    if (result == null) {
        throw new XMPPException("No response from server.");
    }
    else if (result.getType() == IQ.Type.ERROR) {
        throw new XMPPException(result.getError());
    }
    else {
        info = (Registration)result;
    }
}
 
Example 4
Source File: AccountManager.java    From AndroidPNClient with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes the currently logged-in account from the server. This operation can only
 * be performed after a successful login operation has been completed. Not all servers
 * support deleting accounts; an XMPPException will be thrown when that is the case.
 *
 * @throws IllegalStateException if not currently logged-in to the server.
 * @throws org.jivesoftware.smack.XMPPException if an error occurs when deleting the account.
 */
public void deleteAccount() throws XMPPException {
    if (!connection.isAuthenticated()) {
        throw new IllegalStateException("Must be logged in to delete a account.");
    }
    Registration reg = new Registration();
    reg.setType(IQ.Type.SET);
    reg.setTo(connection.getServiceName());
    // To delete an account, we set remove to true
    reg.setRemove(true);
    PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()),
            new PacketTypeFilter(IQ.class));
    PacketCollector collector = connection.createPacketCollector(filter);
    connection.sendPacket(reg);
    IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    collector.cancel();
    if (result == null) {
        throw new XMPPException("No response from server.");
    }
    else if (result.getType() == IQ.Type.ERROR) {
        throw new XMPPException(result.getError());
    }
}
 
Example 5
Source File: AccountManager.java    From AndroidPNClient with Apache License 2.0 6 votes vote down vote up
/**
 * Changes the password of the currently logged-in account. This operation can only
 * be performed after a successful login operation has been completed. Not all servers
 * support changing passwords; an XMPPException will be thrown when that is the case.
 *
 * @throws IllegalStateException if not currently logged-in to the server.
 * @throws org.jivesoftware.smack.XMPPException if an error occurs when changing the password.
 */
public void changePassword(String newPassword) throws XMPPException {
    Registration reg = new Registration();
    reg.setType(IQ.Type.SET);
    reg.setTo(connection.getServiceName());
    reg.setUsername(StringUtils.parseName(connection.getUser()));
    reg.setPassword(newPassword);
    PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()),
            new PacketTypeFilter(IQ.class));
    PacketCollector collector = connection.createPacketCollector(filter);
    connection.sendPacket(reg);
    IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    collector.cancel();
    if (result == null) {
        throw new XMPPException("No response from server.");
    }
    else if (result.getType() == IQ.Type.ERROR) {
        throw new XMPPException(result.getError());
    }
}
 
Example 6
Source File: RosterGroup.java    From AndroidPNClient with Apache License 2.0 5 votes vote down vote up
/**
 * Removes a roster entry from this group. If the entry does not belong to any other group 
 * then it will be considered as unfiled, therefore it will be added to the list of unfiled 
 * entries.
 * Note that this is an asynchronous call -- Smack must wait for the server
 * to receive the updated roster.
 *
 * @param entry a roster entry.
 * @throws XMPPException if an error occured while trying to remove the entry from the group. 
 */
public void removeEntry(RosterEntry entry) throws XMPPException {
    PacketCollector collector = null;
    // Only remove the entry if it's in the entry list.
    // Remove the entry locally, if we wait for RosterPacketListenerprocess>>Packet(Packet)
    // to take place the entry will exist in the group until a packet is received from the 
    // server.
    synchronized (entries) {
        if (entries.contains(entry)) {
            RosterPacket packet = new RosterPacket();
            packet.setType(IQ.Type.SET);
            RosterPacket.Item item = RosterEntry.toRosterItem(entry);
            item.removeGroupName(this.getName());
            packet.addRosterItem(item);
            // Wait up to a certain number of seconds for a reply from the server.
            collector = connection
                    .createPacketCollector(new PacketIDFilter(packet.getPacketID()));
            connection.sendPacket(packet);
        }
    }
    if (collector != null) {
        IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
        collector.cancel();
        if (response == null) {
            throw new XMPPException("No response from the server.");
        }
        // If the server replied with an error, throw an exception.
        else if (response.getType() == IQ.Type.ERROR) {
            throw new XMPPException(response.getError());
        }
    }
}
 
Example 7
Source File: Roster.java    From saros with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 an asynchronous call -- Smack must wait for the server to send an
 * updated subscription status.
 *
 * @param entry a roster entry.
 * @throws XMPPException if an XMPP error occurs.
 * @throws IllegalStateException if connection is not logged in or logged in anonymously
 */
public void removeEntry(RosterEntry entry) throws XMPPException {
  if (!connection.isAuthenticated()) {
    throw new IllegalStateException("Not logged in to server.");
  }
  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);
  PacketCollector collector =
      connection.createPacketCollector(new PacketIDFilter(packet.getPacketID()));
  connection.sendPacket(packet);
  IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
  collector.cancel();
  if (response == null) {
    throw new XMPPException("No response from the server.");
  }
  // If the server replied with an error, throw an exception.
  else if (response.getType() == IQ.Type.ERROR) {
    throw new XMPPException(response.getError());
  }
}
 
Example 8
Source File: Roster.java    From saros with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new roster entry and presence subscription. The server will asynchronously update the
 * roster with the subscription status.
 *
 * @param user the user. (e.g. [email protected])
 * @param name the nickname of the user.
 * @param groups the list of group names the entry will belong to, or <tt>null</tt> if the the
 *     roster entry won't belong to a group.
 * @throws XMPPException if an XMPP exception occurs.
 * @throws IllegalStateException if connection is not logged in or logged in anonymously
 */
public void createEntry(String user, String name, String[] groups) throws XMPPException {
  if (!connection.isAuthenticated()) {
    throw new IllegalStateException("Not logged in to server.");
  }
  if (connection.isAnonymous()) {
    throw new IllegalStateException("Anonymous users can't have a roster.");
  }

  // Create and send roster entry creation packet.
  RosterPacket rosterPacket = new RosterPacket();
  rosterPacket.setType(IQ.Type.SET);
  RosterPacket.Item item = new RosterPacket.Item(user, name);
  if (groups != null) {
    for (String group : groups) {
      if (group != null && group.trim().length() > 0) {
        item.addGroupName(group);
      }
    }
  }
  rosterPacket.addRosterItem(item);
  // Wait up to a certain number of seconds for a reply from the server.
  PacketCollector collector =
      connection.createPacketCollector(new PacketIDFilter(rosterPacket.getPacketID()));
  connection.sendPacket(rosterPacket);
  IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
  collector.cancel();
  if (response == null) {
    throw new XMPPException("No response from the server.");
  }
  // If the server replied with an error, throw an exception.
  else if (response.getType() == IQ.Type.ERROR) {
    throw new XMPPException(response.getError());
  }

  // Create a presence subscription packet and send.
  Presence presencePacket = new Presence(Presence.Type.subscribe);
  presencePacket.setTo(user);
  connection.sendPacket(presencePacket);
}
 
Example 9
Source File: FileTransferNegotiator.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * Send a request to another user to send them a file. The other user has
 * the option of, accepting, rejecting, or not responding to a received file
 * transfer request.
 * <p>
 * If they accept, the stanza will contain the other user's chosen stream
 * type to send the file across. The two choices this implementation
 * provides to the other user for file transfer are <a
 * href="http://www.xmpp.org/extensions/jep-0065.html">SOCKS5 Bytestreams</a>,
 * which is the preferred method of transfer, and <a
 * href="http://www.xmpp.org/extensions/jep-0047.html">In-Band Bytestreams</a>,
 * which is the fallback mechanism.
 * </p>
 * <p>
 * The other user may choose to decline the file request if they do not
 * desire the file, their client does not support XEP-0096, or if there are
 * no acceptable means to transfer the file.
 * </p>
 * Finally, if the other user does not respond this method will return null
 * after the specified timeout.
 *
 * @param userID          The userID of the user to whom the file will be sent.
 * @param streamID        The unique identifier for this file transfer.
 * @param fileName        The name of this file. Preferably it should include an
 *                        extension as it is used to determine what type of file it is.
 * @param size            The size, in bytes, of the file.
 * @param desc            A description of the file.
 * @param responseTimeout The amount of time, in milliseconds, to wait for the remote
 *                        user to respond. If they do not respond in time, this
 * @return Returns the stream negotiator selected by the peer.
 * @throws XMPPErrorException Thrown if there is an error negotiating the file transfer.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws NoResponseException if there was no response from the remote entity.
 * @throws NoAcceptableTransferMechanisms if no acceptable transfer mechanisms are available
 * @throws InterruptedException if the calling thread was interrupted.
 */
public StreamNegotiator negotiateOutgoingTransfer(final Jid userID,
        final String streamID, final String fileName, final long size,
        final String desc, int responseTimeout) throws XMPPErrorException, NotConnectedException, NoResponseException, NoAcceptableTransferMechanisms, InterruptedException {
    StreamInitiation si = new StreamInitiation();
    si.setSessionID(streamID);
    si.setMimeType(URLConnection.guessContentTypeFromName(fileName));

    StreamInitiation.File siFile = new StreamInitiation.File(fileName, size);
    siFile.setDesc(desc);
    si.setFile(siFile);

    si.setFeatureNegotiationForm(createDefaultInitiationForm());

    si.setFrom(connection().getUser());
    si.setTo(userID);
    si.setType(IQ.Type.set);

    Stanza siResponse = connection().createStanzaCollectorAndSend(si).nextResultOrThrow(
                    responseTimeout);

    if (siResponse instanceof IQ) {
        IQ iqResponse = (IQ) siResponse;
        if (iqResponse.getType().equals(IQ.Type.result)) {
            StreamInitiation response = (StreamInitiation) siResponse;
            return getOutgoingNegotiator(getStreamMethodField(response
                    .getFeatureNegotiationForm()));

        }
        else {
            throw new XMPPErrorException(iqResponse, iqResponse.getError());
        }
    }
    else {
        return null;
    }
}
 
Example 10
Source File: FileTooLargeError.java    From Smack with Apache License 2.0 5 votes vote down vote up
public static FileTooLargeError from(IQ iq) {
    StanzaError error = iq.getError();
    if (error == null) {
        return null;
    }
    return error.getExtension(ELEMENT, NAMESPACE);
}
 
Example 11
Source File: AccountManager.java    From AndroidPNClient with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new account using the specified username, password and account attributes.
 * The attributes Map must contain only String name/value pairs and must also have values
 * for all required attributes.
 *
 * @param username the username.
 * @param password the password.
 * @param attributes the account attributes.
 * @throws org.jivesoftware.smack.XMPPException if an error occurs creating the account.
 * @see #getAccountAttributes()
 */
public void createAccount(String username, String password, Map<String, String> attributes)
        throws XMPPException
{
    if (!supportsAccountCreation()) {
        throw new XMPPException("Server does not support account creation.");
    }
    Registration reg = new Registration();
    reg.setType(IQ.Type.SET);
    reg.setTo(connection.getServiceName());
    reg.setUsername(username);
    reg.setPassword(password);
    for(String s : attributes.keySet()){
    	reg.addAttribute(s, attributes.get(s));
    }
    PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()),
            new PacketTypeFilter(IQ.class));
    PacketCollector collector = connection.createPacketCollector(filter);
    connection.sendPacket(reg);
    IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    collector.cancel();
    if (result == null) {
        throw new XMPPException("No response from server.");
    }
    else if (result.getType() == IQ.Type.ERROR) {
        throw new XMPPException(result.getError());
    }
}
 
Example 12
Source File: RemoteGroupCreationOverXMPP.java    From olat with Apache License 2.0 5 votes vote down vote up
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 13
Source File: RosterGroup.java    From AndroidPNClient with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a roster entry to this group. If the entry was unfiled then it will be removed from 
 * the unfiled list and will be added to this group.
 * Note that this is an asynchronous call -- Smack must wait for the server
 * to receive the updated roster.
 *
 * @param entry a roster entry.
 * @throws XMPPException if an error occured while trying to add the entry to the group.
 */
public void addEntry(RosterEntry entry) throws XMPPException {
    PacketCollector collector = null;
    // Only add the entry if it isn't already in the list.
    synchronized (entries) {
        if (!entries.contains(entry)) {
            RosterPacket packet = new RosterPacket();
            packet.setType(IQ.Type.SET);
            RosterPacket.Item item = RosterEntry.toRosterItem(entry);
            item.addGroupName(getName());
            packet.addRosterItem(item);
            // Wait up to a certain number of seconds for a reply from the server.
            collector = connection
                    .createPacketCollector(new PacketIDFilter(packet.getPacketID()));
            connection.sendPacket(packet);
        }
    }
    if (collector != null) {
        IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
        collector.cancel();
        if (response == null) {
            throw new XMPPException("No response from the server.");
        }
        // If the server replied with an error, throw an exception.
        else if (response.getType() == IQ.Type.ERROR) {
            throw new XMPPException(response.getError());
        }
    }
}
 
Example 14
Source File: Roster.java    From AndroidPNClient with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new roster entry and presence subscription. The server will asynchronously
 * update the roster with the subscription status.
 *
 * @param user   the user. (e.g. [email protected])
 * @param name   the nickname of the user.
 * @param groups the list of group names the entry will belong to, or <tt>null</tt> if the
 *               the roster entry won't belong to a group.
 * @throws XMPPException if an XMPP exception occurs.
 */
public void createEntry(String user, String name, String[] groups) throws XMPPException {
    // Create and send roster entry creation packet.
    RosterPacket rosterPacket = new RosterPacket();
    rosterPacket.setType(IQ.Type.SET);
    RosterPacket.Item item = new RosterPacket.Item(user, name);
    if (groups != null) {
        for (String group : groups) {
            if (group != null && group.trim().length() > 0) {
                item.addGroupName(group);
            }
        }
    }
    rosterPacket.addRosterItem(item);
    // Wait up to a certain number of seconds for a reply from the server.
    PacketCollector collector = connection.createPacketCollector(
            new PacketIDFilter(rosterPacket.getPacketID()));
    connection.sendPacket(rosterPacket);
    IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    collector.cancel();
    if (response == null) {
        throw new XMPPException("No response from the server.");
    }
    // If the server replied with an error, throw an exception.
    else if (response.getType() == IQ.Type.ERROR) {
        throw new XMPPException(response.getError());
    }

    // Create a presence subscription packet and send.
    Presence presencePacket = new Presence(Presence.Type.subscribe);
    presencePacket.setTo(user);
    connection.sendPacket(presencePacket);
}
 
Example 15
Source File: RemoteGroupCreationOverXMPP.java    From olat with Apache License 2.0 5 votes vote down vote up
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 16
Source File: SASLAuthentication.java    From AndroidPNClient with Apache License 2.0 4 votes vote down vote up
private String bindResourceAndEstablishSession(String resource) throws XMPPException {
    // Wait until server sends response containing the <bind> element
    synchronized (this) {
        long endTime = System.currentTimeMillis() + 30000;
        while (!resourceBinded && (System.currentTimeMillis() < endTime)) {
            try {
                wait(Math.abs(System.currentTimeMillis() - endTime));
            }
            catch (InterruptedException e) {
                // Ignore
            }
        }
    }

    if (!resourceBinded) {
        // Server never offered resource binding
        throw new XMPPException("Resource binding not offered by server");
    }

    Bind bindResource = new Bind();
    bindResource.setResource(resource);

    PacketCollector collector = connection
            .createPacketCollector(new PacketIDFilter(bindResource.getPacketID()));
    // Send the packet
    connection.sendPacket(bindResource);
    // Wait up to a certain number of seconds for a response from the server.
    Bind response = (Bind) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    collector.cancel();
    if (response == null) {
        throw new XMPPException("No response from the server.");
    }
    // If the server replied with an error, throw an exception.
    else if (response.getType() == IQ.Type.ERROR) {
        throw new XMPPException(response.getError());
    }
    String userJID = response.getJid();

    if (sessionSupported) {
        Session session = new Session();
        collector = connection.createPacketCollector(new PacketIDFilter(session.getPacketID()));
        // Send the packet
        connection.sendPacket(session);
        // Wait up to a certain number of seconds for a response from the server.
        IQ ack = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
        collector.cancel();
        if (ack == null) {
            throw new XMPPException("No response from the server.");
        }
        // If the server replied with an error, throw an exception.
        else if (ack.getType() == IQ.Type.ERROR) {
            throw new XMPPException(ack.getError());
        }
    }
    else {
        // Server never offered session establishment
        throw new XMPPException("Session establishment not offered by server");
    }
    return userJID;
}
 
Example 17
Source File: RemoteAccountCreationOverXMPP.java    From olat with Apache License 2.0 4 votes vote down vote up
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 18
Source File: RemoteAccountCreationOverXMPP.java    From olat with Apache License 2.0 4 votes vote down vote up
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;
    }
}