Пример #1
0
 public void sendPacket(Packet packet) {
   if (!isConnected()) {
     throw new IllegalStateException("Not connected to server.");
   }
   if (packet == null) {
     throw new NullPointerException("Packet is null.");
   }
   packetWriter.sendPacket(packet);
 }
Пример #2
0
  /**
   * Closes the connection by setting presence to unavailable then closing the stream to the XMPP
   * server. The shutdown logic will be used during a planned disconnection or when dealing with an
   * unexpected disconnection. Unlike {@link #disconnect()} the connection's packet reader, packet
   * writer, and {@link Roster} will not be removed; thus connection's state is kept.
   *
   * @param unavailablePresence the presence packet to send during shutdown.
   */
  protected void shutdown(Presence unavailablePresence) {
    // Set presence to offline.
    PacketWriter packetWriter = this.packetWriter;
    if (packetWriter != null) {
      packetWriter.sendPacket(unavailablePresence);
    }

    this.setWasAuthenticated(authenticated);
    authenticated = false;
    connected = false;

    PacketReader packetReader = this.packetReader;
    if (packetReader != null) {
      packetReader.shutdown();
    }
    packetWriter = this.packetWriter;
    if (packetWriter != null) {
      packetWriter.shutdown();
    }
    // Wait 150 ms for processes to clean-up, then shutdown.
    try {
      Thread.sleep(150);
    } catch (Exception e) {
      // Ignore.
    }

    // Close down the readers and writers.
    Reader reader = this.reader;
    if (reader != null) {
      try {
        reader.close();
      } catch (Throwable ignore) {
        /* ignore */
      }
      this.reader = null;
    }
    Writer writer = this.writer;
    if (writer != null) {
      try {
        writer.close();
      } catch (Throwable ignore) {
        /* ignore */
      }
      this.writer = null;
    }

    try {
      socket.close();
    } catch (Exception e) {
      // Ignore.
    }

    saslAuthentication.init();
  }
Пример #3
0
  @Override
  public synchronized void loginAnonymously() throws XMPPException {
    if (!isConnected()) {
      throw new IllegalStateException("Not connected to server.");
    }
    if (authenticated) {
      throw new IllegalStateException("Already logged in to server.");
    }

    String response;
    if (config.isSASLAuthenticationEnabled() && saslAuthentication.hasAnonymousAuthentication()) {
      response = saslAuthentication.authenticateAnonymously();
    } else {
      // Authenticate using Non-SASL
      response = new NonSASLAuthentication(this).authenticateAnonymously();
    }

    // Set the user value.
    this.user = response;
    // Update the serviceName with the one returned by the server
    config.setServiceName(StringUtils.parseServer(response));

    // If compression is enabled then request the server to use stream compression
    if (config.isCompressionEnabled()) {
      useCompression();
    }

    // Set presence to online.
    packetWriter.sendPacket(new Presence(Presence.Type.available));

    // Indicate that we're now authenticated.
    authenticated = true;
    anonymous = true;

    // If debugging is enabled, change the the debug window title to include the
    // name we are now logged-in as.
    // If DEBUG_ENABLED was set to true AFTER the connection was created the debugger
    // will be null
    if (config.isDebuggerEnabled() && debugger != null) {
      debugger.userHasLogged(user);
    }
  }
Пример #4
0
  @Override
  public synchronized void login(String username, String password, String resource)
      throws XMPPException {
    if (!isConnected()) {
      throw new IllegalStateException("Not connected to server.");
    }
    if (authenticated) {
      throw new IllegalStateException("Already logged in to server.");
    }
    // Do partial version of nameprep on the username.
    username = username.toLowerCase().trim();

    // Pause keep alive process while authentication and compression is in
    // progress
    packetWriter.stopKeepAliveProcess();

    String response;
    if (config.isSASLAuthenticationEnabled()
        && saslAuthentication.hasNonAnonymousAuthentication()) {
      // Authenticate using SASL
      if (password != null) {
        response = saslAuthentication.authenticate(username, password, resource);
      } else {
        response = saslAuthentication.authenticate(username, resource, config.getCallbackHandler());
      }
    } else {
      // Authenticate using Non-SASL
      response = new NonSASLAuthentication(this).authenticate(username, password, resource);
    }

    // Set the user.
    if (response != null) {
      this.user = response;
      // Update the serviceName with the one returned by the server
      config.setServiceName(StringUtils.parseServer(response));
    } else {
      this.user = username + "@" + getServiceName();
      if (resource != null) {
        this.user += "/" + resource;
      }
    }

    // If compression is enabled then request the server to use stream compression
    if (config.isCompressionEnabled()) {
      useCompression();
    }

    // Resume keep alive process (after authentication and compression was
    // complited)
    packetWriter.resumeKeepAliveProcess();

    // Indicate that we're now authenticated.
    authenticated = true;
    anonymous = false;

    if (config.isRosterLoadedAtLogin()) {
      // Create the roster if it is not a reconnection or roster already
      // created by getRoster()
      if (this.roster == null) {
        if (rosterStorage == null) {
          this.roster = new Roster(this);
        } else {
          this.roster = new Roster(this, rosterStorage);
        }
      }
      this.roster.reload();
    }

    // Set presence to online.
    if (config.isSendPresence()) {
      packetWriter.sendPacket(new Presence(Presence.Type.available));
    }

    // Stores the authentication for future reconnection
    config.setLoginInfo(username, password, resource);

    // If debugging is enabled, change the the debug window title to include the
    // name we are now logged-in as.
    // If DEBUG_ENABLED was set to true AFTER the connection was created the debugger
    // will be null
    if (config.isDebuggerEnabled() && debugger != null) {
      debugger.userHasLogged(user);
    }
  }