org.jivesoftware.smackx.iqregister.packet.Registration Java Examples

The following examples show how to use org.jivesoftware.smackx.iqregister.packet.Registration. 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: UserRegService.java    From xyTalk-pc with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void registerUser(XMPPConnection con, String gatewayDomain, String username, String password, String nickname, StanzaListener callback) throws SmackException.NotConnectedException
  {
      Map<String, String> attributes = new HashMap<>();
      if (username != null) {
          attributes.put("username", username);
      }
      if (password != null) {
          attributes.put("password", password);
      }
      if (nickname != null) {
          attributes.put("nick", nickname);
      }
      Registration registration = new Registration( attributes );
      registration.setType(IQ.Type.set);
      registration.setTo(gatewayDomain);
      registration.addExtension(new GatewayRegisterExtension());

      try {
	con.sendStanzaWithResponseCallback( registration, new IQReplyFilter( registration, con ), callback);
} catch (InterruptedException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
  }
 
Example #2
Source File: UserRegService.java    From xyTalk-pc with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void unregister(XMPPConnection con, String gatewayDomain) throws SmackException.NotConnectedException
  {
      Map<String,String> map = new HashMap<>();
      map.put("remove", "");
      Registration registration = new Registration( map );
      registration.setType(IQ.Type.set);
      registration.setTo(gatewayDomain);

      try {
	con.sendStanzaWithResponseCallback( registration, new IQReplyFilter( registration, con ), stanza -> {
	    IQ response = (IQ) stanza;
	    if (response.getType() == IQ.Type.error ) {
	        //Log.warning( "Unable to unregister from gateway: " + stanza );
	    }
	} );
} catch (InterruptedException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
  }
 
Example #3
Source File: AccountManager.java    From Smack with Apache License 2.0 6 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 XMPPErrorException if an error occurs creating the account.
 * @throws NoResponseException if there was no response from the server.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 * @see #getAccountAttributes()
 */
public void createAccount(Localpart username, String password, Map<String, String> attributes)
                throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    if (!connection().isSecureConnection() && !allowSensitiveOperationOverInsecureConnection) {
        throw new IllegalStateException("Creating account over insecure connection");
    }
    if (username == null) {
        throw new IllegalArgumentException("Username must not be null");
    }
    if (StringUtils.isNullOrEmpty(password)) {
        throw new IllegalArgumentException("Password must not be null");
    }

    attributes.put("username", username.toString());
    attributes.put("password", password);
    Registration reg = new Registration(attributes);
    reg.setType(IQ.Type.set);
    reg.setTo(connection().getXMPPServiceDomain());
    createStanzaCollectorAndSend(reg).nextResultOrThrow();
}
 
Example #4
Source File: AccountManager.java    From Smack with Apache License 2.0 6 votes vote down vote up
public boolean isSupported()
                throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    XMPPConnection connection = connection();

    ExtensionElement extensionElement = connection.getFeature(Registration.Feature.ELEMENT,
                    Registration.Feature.NAMESPACE);
    if (extensionElement != null) {
        return true;
    }

    // Fallback to disco#info only if this connection is authenticated, as otherwise we won't have an full JID and
    // won't be able to do IQs.
    if (connection.isAuthenticated()) {
        return ServiceDiscoveryManager.getInstanceFor(connection).serverSupportsFeature(Registration.NAMESPACE);
    }

    return false;
}
 
Example #5
Source File: TransportUtils.java    From Spark with Apache License 2.0 6 votes vote down vote up
/**
 * Registers a user with a gateway.
 *
 * @param con           the XMPPConnection.
 * @param gatewayDomain the domain of the gateway (service name)
 * @param username      the username.
 * @param password      the password.
 * @param nickname      the nickname.
 * @throws InterruptedException 
 * @throws XMPPException thrown if there was an issue registering with the gateway.
 */
public static void registerUser(XMPPConnection con, DomainBareJid gatewayDomain, String username, String password, String nickname, StanzaListener callback) throws SmackException.NotConnectedException, InterruptedException
{
    Map<String, String> attributes = new HashMap<>();
    if (username != null) {
        attributes.put("username", username);
    }
    if (password != null) {
        attributes.put("password", password);
    }
    if (nickname != null) {
        attributes.put("nick", nickname);
    }
    Registration registration = new Registration( attributes );
    registration.setType(IQ.Type.set);
    registration.setTo(gatewayDomain);
    registration.addExtension(new GatewayRegisterExtension());

    con.sendStanzaWithResponseCallback( registration, new IQReplyFilter( registration, con ), callback);
}
 
Example #6
Source File: TransportUtils.java    From Spark with Apache License 2.0 6 votes vote down vote up
/**
 * @param con           the XMPPConnection.
 * @param gatewayDomain the domain of the gateway (service name)
 * @throws InterruptedException 
 * @throws XMPPException thrown if there was an issue unregistering with the gateway.
 */
public static void unregister(XMPPConnection con, DomainBareJid gatewayDomain) throws SmackException.NotConnectedException, InterruptedException
{
    Map<String,String> map = new HashMap<>();
    map.put("remove", "");
    Registration registration = new Registration( map );
    registration.setType(IQ.Type.set);
    registration.setTo(gatewayDomain);

    con.sendStanzaWithResponseCallback( registration, new IQReplyFilter( registration, con ), stanza -> {
        IQ response = (IQ) stanza;
        if (response.getType() == IQ.Type.error ) {
            Log.warning( "Unable to unregister from gateway: " + stanza );
        }
    } );
}
 
Example #7
Source File: PrivateKeyReceiver.java    From desktopclient-java with GNU General Public License v3.0 5 votes vote down vote up
private void sendRequestAsync(String registrationToken) {
    // connect
    try {
        mConn.connect();
    } catch (XMPPException | SmackException | IOException | InterruptedException ex) {
        LOGGER.log(Level.WARNING, "can't connect to "+mConn.getServer(), ex);
        mHandler.handle(new Callback<>(ex));
        return;
    }

    Registration iq = new Registration();
    iq.setType(IQ.Type.set);
    iq.setTo(mConn.getXMPPServiceDomain());
    Form form = new Form(DataForm.Type.submit);

    // form type field
    FormField type = new FormField(FormField.FORM_TYPE);
    type.setType(FormField.Type.hidden);
    type.addValue(FORM_TYPE_VALUE);
    form.addField(type);

    // token field
    FormField fieldKey = new FormField(FORM_TOKEN_VAR);
    fieldKey.setLabel("Registration token");
    fieldKey.setType(FormField.Type.text_single);
    fieldKey.addValue(registrationToken);
    form.addField(fieldKey);

    iq.addExtension(form.getDataFormToSend());

    mConn.sendIqRequestAsync(iq)
         .onSuccess(this)
         .onError(exception -> mHandler.handle(new Callback<>(exception)));
}
 
Example #8
Source File: RegistrationStreamFeatureProvider.java    From Smack with Apache License 2.0 4 votes vote down vote up
@Override
public Registration.Feature parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) {
    return Registration.Feature.INSTANCE;
}
 
Example #9
Source File: RegistrationProvider.java    From Smack with Apache License 2.0 4 votes vote down vote up
@Override
public Registration parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    String instruction = null;
    Map<String, String> fields = new HashMap<>();
    List<ExtensionElement> packetExtensions = new LinkedList<>();
    outerloop:
    while (true) {
        XmlPullParser.Event eventType = parser.next();
        if (eventType == XmlPullParser.Event.START_ELEMENT) {
            // Any element that's in the jabber:iq:register namespace,
            // attempt to parse it if it's in the form <name>value</name>.
            if (parser.getNamespace().equals(Registration.NAMESPACE)) {
                String name = parser.getName();
                String value = "";

                if (parser.next() == XmlPullParser.Event.TEXT_CHARACTERS) {
                    value = parser.getText();
                }
                // Ignore instructions, but anything else should be added to the map.
                if (!name.equals("instructions")) {
                    fields.put(name, value);
                }
                else {
                    instruction = value;
                }
            }
            // Otherwise, it must be a packet extension.
            else {
                PacketParserUtils.addExtensionElement(packetExtensions, parser, xmlEnvironment);
            }
        }
        else if (eventType == XmlPullParser.Event.END_ELEMENT) {
            if (parser.getName().equals(IQ.QUERY_ELEMENT)) {
                break outerloop;
            }
        }
    }
    Registration registration = new Registration(instruction, fields);
    registration.addExtensions(packetExtensions);
    return registration;
}
 
Example #10
Source File: MultiUserChat.java    From Smack with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the room's registration form that an unaffiliated user, can use to become a member
 * of the room or <code>null</code> if no registration is possible. Some rooms may restrict the
 * privilege to register members and allow only room admins to add new members.<p>
 *
 * If the user requesting registration requirements is not allowed to register with the room
 * (e.g. because that privilege has been restricted), the room will return a "Not Allowed"
 * error to the user (error code 405).
 *
 * @return the registration Form that contains the fields to complete together with the
 * instrucions or <code>null</code> if no registration is possible.
 * @throws XMPPErrorException if an error occurs asking the registration form for the room or a
 * 405 error if the user is not allowed to register with the room.
 * @throws NoResponseException if there was no response from the server.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public Form getRegistrationForm() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    Registration reg = new Registration();
    reg.setType(IQ.Type.get);
    reg.setTo(room);

    IQ result = connection.createStanzaCollectorAndSend(reg).nextResultOrThrow();
    DataForm dataForm = DataForm.from(result);
    return new Form(dataForm);
}
 
Example #11
Source File: MultiUserChat.java    From Smack with Apache License 2.0 3 votes vote down vote up
/**
 * Sends the completed registration form to the server. After the user successfully submits
 * the form, the room may queue the request for review by the room admins or may immediately
 * add the user to the member list by changing the user's affiliation from "none" to "member.<p>
 *
 * If the desired room nickname is already reserved for that room, the room will return a
 * "Conflict" error to the user (error code 409). If the room does not support registration,
 * it will return a "Service Unavailable" error to the user (error code 503).
 *
 * @param form the completed registration form.
 * @throws XMPPErrorException if an error occurs submitting the registration form. In particular, a
 *      409 error can occur if the desired room nickname is already reserved for that room;
 *      or a 503 error can occur if the room does not support registration.
 * @throws NoResponseException if there was no response from the server.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public void sendRegistrationForm(FillableForm form) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    Registration reg = new Registration();
    reg.setType(IQ.Type.set);
    reg.setTo(room);
    reg.addExtension(form.getDataFormToSubmit());

    connection.createStanzaCollectorAndSend(reg).nextResultOrThrow();
}
 
Example #12
Source File: AccountManager.java    From Smack with Apache License 2.0 3 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.
 *
 * @param newPassword new password.
 *
 * @throws IllegalStateException if not currently logged-in to the server.
 * @throws XMPPErrorException if an error occurs when changing the password.
 * @throws NoResponseException if there was no response from the server.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public void changePassword(String newPassword) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    if (!connection().isSecureConnection() && !allowSensitiveOperationOverInsecureConnection) {
        throw new IllegalStateException("Changing password over insecure connection.");
    }
    Map<String, String> map = new HashMap<>();
    map.put("username",  connection().getUser().getLocalpart().toString());
    map.put("password", newPassword);
    Registration reg = new Registration(map);
    reg.setType(IQ.Type.set);
    reg.setTo(connection().getXMPPServiceDomain());
    createStanzaCollectorAndSend(reg).nextResultOrThrow();
}
 
Example #13
Source File: AccountManager.java    From Smack with Apache License 2.0 3 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 XMPPErrorException if an error occurs when deleting the account.
 * @throws NoResponseException if there was no response from the server.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public void deleteAccount() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    Map<String, String> attributes = new HashMap<>();
    // To delete an account, we add a single attribute, "remove", that is blank.
    attributes.put("remove", "");
    Registration reg = new Registration(attributes);
    reg.setType(IQ.Type.set);
    reg.setTo(connection().getXMPPServiceDomain());
    createStanzaCollectorAndSend(reg).nextResultOrThrow();
}
 
Example #14
Source File: AccountManager.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the account registration info from the server.
 * @throws XMPPErrorException if there was an XMPP error returned.
 * @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.
 *
 * @throws XMPPException if an error occurs.
 * @throws SmackException if there was no response from the server.
 */
private synchronized void getRegistrationInfo() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    Registration reg = new Registration();
    reg.setTo(connection().getXMPPServiceDomain());
    info = createStanzaCollectorAndSend(reg).nextResultOrThrow();
}