Exemple #1
0
  @Override
  public void authenticate(String username, String password)
      throws NntpClientAuthenticationException {
    NntpFuture<GenericResponse> future =
        this.sendCommand(Response.ResponseType.AUTHINFO, "USER", username);
    GenericResponse resp = Futures.getUnchecked(future);
    if (resp.getCode() == 281) {
      // Well ... that was easy.
      return;
    }

    if (resp.getCode() == 381) {
      future = this.sendCommand(Response.ResponseType.AUTHINFO, "PASS", password);
      resp = Futures.getUnchecked(future);
      if (resp.getCode() == 281) {
        return;
      }

      if (resp.getCode() == 481) {
        throw new NntpClientAuthenticationException(new NntpInvalidLoginException());
      }
    }

    throw new NntpClientAuthenticationException(new NntpClientException("Unknown login error."));
  }
Exemple #2
0
  @Override
  public void connect() throws NntpClientConnectionError {
    // We'll be waiting for the connection message.
    NntpFuture<GenericResponse> welcomeFuture = new NntpFuture<>(Response.ResponseType.WELCOME);
    this.pipeline.add(welcomeFuture);

    // Connect to the server now.
    ChannelFuture future = this.initializeChannel(new InetSocketAddress(this.host, this.port));
    if (!future.isSuccess()) {
      throw new NntpClientConnectionError(future.getCause());
    }
    this.channel = future.getChannel();

    if (this.ssl) {
      ChannelFuture handshakeFuture =
          this.channel.getPipeline().get(SslHandler.class).handshake().awaitUninterruptibly();
      if (!handshakeFuture.isSuccess()) {
        throw new NntpClientConnectionError(handshakeFuture.getCause());
      }
    }

    GenericResponse response = Futures.getUnchecked(welcomeFuture);
    boolean temporarilyUnavailable = false;
    switch (response.getCode()) {
      case 200:
        this.canPost = true;
      case 201:
        return;
      case 400:
        temporarilyUnavailable = true;
      case 502:
        throw new NntpClientConnectionError(
            new NntpServerUnavailableException(temporarilyUnavailable));
      default:
        // FIXME: typed exception here mebbe?
        throw new NntpClientConnectionError(
            new RuntimeException(
                "Unexpected status code "
                    + response.getCode()
                    + " returned on initial connection."));
    }
  }