Example usage for org.apache.commons.net.nntp NNTPReply isPositiveCompletion

List of usage examples for org.apache.commons.net.nntp NNTPReply isPositiveCompletion

Introduction

In this page you can find the example usage for org.apache.commons.net.nntp NNTPReply isPositiveCompletion.

Prototype

public static boolean isPositiveCompletion(int reply) 

Source Link

Document

Determine if a reply code is a positive completion response.

Usage

From source file:examples.nntp.post.java

public final static void main(String[] args) {
    String from, subject, newsgroup, filename, server, organization;
    String references;/*from  w ww .j  a v  a 2 s  .  c om*/
    BufferedReader stdin;
    FileReader fileReader = null;
    SimpleNNTPHeader header;
    NNTPClient client;

    if (args.length < 1) {
        System.err.println("Usage: post newsserver");
        System.exit(1);
    }

    server = args[0];

    stdin = new BufferedReader(new InputStreamReader(System.in));

    try {
        System.out.print("From: ");
        System.out.flush();

        from = stdin.readLine();

        System.out.print("Subject: ");
        System.out.flush();

        subject = stdin.readLine();

        header = new SimpleNNTPHeader(from, subject);

        System.out.print("Newsgroup: ");
        System.out.flush();

        newsgroup = stdin.readLine();
        header.addNewsgroup(newsgroup);

        while (true) {
            System.out.print("Additional Newsgroup <Hit enter to end>: ");
            System.out.flush();

            // Of course you don't want to do this because readLine() may be null
            newsgroup = stdin.readLine().trim();

            if (newsgroup.length() == 0)
                break;

            header.addNewsgroup(newsgroup);
        }

        System.out.print("Organization: ");
        System.out.flush();

        organization = stdin.readLine();

        System.out.print("References: ");
        System.out.flush();

        references = stdin.readLine();

        if (organization != null && organization.length() > 0)
            header.addHeaderField("Organization", organization);

        if (references != null && organization.length() > 0)
            header.addHeaderField("References", references);

        header.addHeaderField("X-Newsreader", "NetComponents");

        System.out.print("Filename: ");
        System.out.flush();

        filename = stdin.readLine();

        try {
            fileReader = new FileReader(filename);
        } catch (FileNotFoundException e) {
            System.err.println("File not found. " + e.getMessage());
            System.exit(1);
        }

        client = new NNTPClient();
        client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

        client.connect(server);

        if (!NNTPReply.isPositiveCompletion(client.getReplyCode())) {
            client.disconnect();
            System.err.println("NNTP server refused connection.");
            System.exit(1);
        }

        if (client.isAllowedToPost()) {
            Writer writer = client.postArticle();

            if (writer != null) {
                writer.write(header.toString());
                Util.copyReader(fileReader, writer);
                writer.close();
                client.completePendingCommand();
            }
        }

        fileReader.close();

        client.logout();

        client.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:examples.nntp.PostMessage.java

public static void main(String[] args) {
    String from, subject, newsgroup, filename, server, organization;
    String references;//from  w  ww.  j av  a 2 s  .c o  m
    BufferedReader stdin;
    FileReader fileReader = null;
    SimpleNNTPHeader header;
    NNTPClient client;

    if (args.length < 1) {
        System.err.println("Usage: post newsserver");
        System.exit(1);
    }

    server = args[0];

    stdin = new BufferedReader(new InputStreamReader(System.in));

    try {
        System.out.print("From: ");
        System.out.flush();

        from = stdin.readLine();

        System.out.print("Subject: ");
        System.out.flush();

        subject = stdin.readLine();

        header = new SimpleNNTPHeader(from, subject);

        System.out.print("Newsgroup: ");
        System.out.flush();

        newsgroup = stdin.readLine();
        header.addNewsgroup(newsgroup);

        while (true) {
            System.out.print("Additional Newsgroup <Hit enter to end>: ");
            System.out.flush();

            // Of course you don't want to do this because readLine() may be null
            newsgroup = stdin.readLine().trim();

            if (newsgroup.length() == 0) {
                break;
            }

            header.addNewsgroup(newsgroup);
        }

        System.out.print("Organization: ");
        System.out.flush();

        organization = stdin.readLine();

        System.out.print("References: ");
        System.out.flush();

        references = stdin.readLine();

        if (organization != null && organization.length() > 0) {
            header.addHeaderField("Organization", organization);
        }

        if (references != null && references.length() > 0) {
            header.addHeaderField("References", references);
        }

        header.addHeaderField("X-Newsreader", "NetComponents");

        System.out.print("Filename: ");
        System.out.flush();

        filename = stdin.readLine();

        try {
            fileReader = new FileReader(filename);
        } catch (FileNotFoundException e) {
            System.err.println("File not found. " + e.getMessage());
            System.exit(1);
        }

        client = new NNTPClient();
        client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));

        client.connect(server);

        if (!NNTPReply.isPositiveCompletion(client.getReplyCode())) {
            client.disconnect();
            System.err.println("NNTP server refused connection.");
            System.exit(1);
        }

        if (client.isAllowedToPost()) {
            Writer writer = client.postArticle();

            if (writer != null) {
                writer.write(header.toString());
                Util.copyReader(fileReader, writer);
                writer.close();
                client.completePendingCommand();
            }
        }

        if (fileReader != null) {
            fileReader.close();
        }

        client.logout();

        client.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:com.almarsoft.GroundhogReader.lib.NNTPExtended.java

public boolean selectArticle(long articleNumber, ArticlePointer pointer) throws IOException {
    if (!NNTPReply.isPositiveCompletion(stat(articleNumber)))
        return false;

    if (pointer != null)
        __parseArticlePointer(getReplyString(), pointer);

    return true;/*from ww  w.ja v  a 2  s  .c  o m*/
}

From source file:net.longfalcon.newsj.nntp.client.CustomNNTPClient.java

private BufferedReader __retrieve(int command, String articleId, ArticleInfo pointer) throws IOException {
    if (articleId != null) {
        if (!NNTPReply.isPositiveCompletion(sendCommand(command, articleId))) {
            return null;
        }/*  w  ww.  jav  a2s .  c  o  m*/
    } else {
        if (!NNTPReply.isPositiveCompletion(sendCommand(command))) {
            return null;
        }
    }

    if (pointer != null) {
        __parseArticlePointer(getReplyString(), pointer);
    }

    return new DotTerminatedMessageReader(_reader_);
}

From source file:net.longfalcon.newsj.nntp.client.CustomNNTPClient.java

private BufferedReader __retrieve(int command, long articleNumber, ArticleInfo pointer) throws IOException {
    if (!NNTPReply.isPositiveCompletion(sendCommand(command, Long.toString(articleNumber)))) {
        return null;
    }/*from w  w w. java  2 s  .c  o  m*/

    if (pointer != null) {
        __parseArticlePointer(getReplyString(), pointer);
    }

    return new DotTerminatedMessageReader(_reader_);
}

From source file:net.longfalcon.newsj.nntp.client.CustomNNTPClient.java

/***
 * Select the specified newsgroup to be the target of for future article
 * retrieval and posting operations.  Also return the newsgroup
 * information contained in the server reply through the info parameter.
 * <p>//ww  w  . j  a v a 2s  . c  om
 * @param newsgroup  The newsgroup to select.
 * @param info  A parameter through which the newsgroup information of
 *      the selected newsgroup contained in the server reply is returned.
 *      Set this to null if you do not desire this information.
 * @return True if the newsgroup exists and was selected, false otherwise.
 * @exception NNTPConnectionClosedException
 *      If the NNTP server prematurely closes the connection as a result
 *      of the client being idle or some other reason causing the server
 *      to send NNTP reply code 400.  This exception may be caught either
 *      as an IOException or independently as itself.
 * @exception IOException  If an I/O error occurs while either sending a
 *      command to the server or receiving a reply from the server.
 ***/
@Override
public boolean selectNewsgroup(String newsgroup, NewsgroupInfo info) throws IOException {
    if (!NNTPReply.isPositiveCompletion(group(newsgroup))) {
        return false;
    }

    if (info != null) {
        __parseGroupReply(getReplyString(), info);
    }

    return true;
}

From source file:net.longfalcon.newsj.nntp.client.CustomNNTPClient.java

/**
 * Send a "LIST OVERVIEW.FMT" command to the server.
 *
 * @return the contents of the Overview format, of {@code null} if the command failed
 * @throws IOException//from   w  w  w. j a va2  s.c o  m
 */
@Override
public String[] listOverviewFmt() throws IOException {
    if (!NNTPReply.isPositiveCompletion(sendCommand("LIST", "OVERVIEW.FMT"))) {
        return null;
    }

    BufferedReader reader = new DotTerminatedMessageReader(_reader_);
    String line;
    ArrayList<String> list = new ArrayList<String>();
    while ((line = reader.readLine()) != null) {
        list.add(line);
    }
    reader.close();
    return list.toArray(new String[list.size()]);
}

From source file:net.longfalcon.newsj.nntp.client.CustomNNTPClient.java

/***
 * Select an article by its unique identifier (including enclosing
 * &lt and &gt) and return its article number and id through the
 * pointer parameter.  This is achieved through the STAT command.
 * According to RFC 977, this will NOT set the current article pointer
 * on the server.  To do that, you must reference the article by its
 * number./*w  ww. ja  va  2  s  . c  o m*/
 * <p>
 * @param articleId  The unique article identifier of the article that
 *    is being selectedd.  If this parameter is null, the
 *    body of the current article is selected
 * @param pointer    A parameter through which to return the article's
 *   number and unique id.  The articleId field cannot always be trusted
 *   because of server deviations from RFC 977 reply formats.  You may
 *   set this parameter to null if you do not desire to retrieve the
 *   returned article information.
 * @return True if successful, false if not.
 * @exception NNTPConnectionClosedException
 *      If the NNTP server prematurely closes the connection as a result
 *      of the client being idle or some other reason causing the server
 *      to send NNTP reply code 400.  This exception may be caught either
 *      as an IOException or independently as itself.
 * @exception IOException  If an I/O error occurs while either sending a
 *      command to the server or receiving a reply from the server.
 ***/
@Override
public boolean selectArticle(String articleId, ArticleInfo pointer) throws IOException {
    if (articleId != null) {
        if (!NNTPReply.isPositiveCompletion(stat(articleId))) {
            return false;
        }
    } else {
        if (!NNTPReply.isPositiveCompletion(stat())) {
            return false;
        }
    }

    if (pointer != null) {
        __parseArticlePointer(getReplyString(), pointer);
    }

    return true;
}

From source file:net.longfalcon.newsj.nntp.client.CustomNNTPClient.java

/***
 * Select an article in the currently selected newsgroup by its number.
 * and return its article number and id through the
 * pointer parameter.  This is achieved through the STAT command.
 * According to RFC 977, this WILL set the current article pointer
 * on the server.  Use this command to select an article before retrieving
 * it, or to obtain an article's unique identifier given its number.
 * <p>//from  w ww . j  av a2s  . com
 * @param articleNumber The number of the article to select from the
 *       currently selected newsgroup.
 * @param pointer    A parameter through which to return the article's
 *   number and unique id.  Although the articleId field cannot always
 *   be trusted because of server deviations from RFC 977 reply formats,
 *   we haven't found a server that misformats this information in response
 *   to this particular command.  You may set this parameter to null if
 *   you do not desire to retrieve the returned article information.
 * @return True if successful, false if not.
 * @exception NNTPConnectionClosedException
 *      If the NNTP server prematurely closes the connection as a result
 *      of the client being idle or some other reason causing the server
 *      to send NNTP reply code 400.  This exception may be caught either
 *      as an IOException or independently as itself.
 * @exception IOException  If an I/O error occurs while either sending a
 *      command to the server or receiving a reply from the server.
 ***/
@Override
public boolean selectArticle(long articleNumber, ArticleInfo pointer) throws IOException {
    if (!NNTPReply.isPositiveCompletion(stat(articleNumber))) {
        return false;
    }

    if (pointer != null) {
        __parseArticlePointer(getReplyString(), pointer);
    }

    return true;
}

From source file:net.longfalcon.newsj.nntp.client.CustomNNTPClient.java

/***
 * Select the article preceeding the currently selected article in the
 * currently selected newsgroup and return its number and unique id
 * through the pointer parameter.  Because of deviating server
 * implementations, the articleId information cannot be trusted.  To
 * obtain the article identifier, issue a
 * <code> selectArticle(pointer.articleNumber, pointer) </code> immediately
 * afterward.//  w w w  .  j a v  a2 s  . co m
 * <p>
 * @param pointer    A parameter through which to return the article's
 *   number and unique id.  The articleId field cannot always be trusted
 *   because of server deviations from RFC 977 reply formats.  You may
 *   set this parameter to null if you do not desire to retrieve the
 *   returned article information.
 * @return True if successful, false if not (e.g., there is no previous
 *     article).
 * @exception NNTPConnectionClosedException
 *      If the NNTP server prematurely closes the connection as a result
 *      of the client being idle or some other reason causing the server
 *      to send NNTP reply code 400.  This exception may be caught either
 *      as an IOException or independently as itself.
 * @exception IOException  If an I/O error occurs while either sending a
 *      command to the server or receiving a reply from the server.
 ***/
@Override
public boolean selectPreviousArticle(ArticleInfo pointer) throws IOException {
    if (!NNTPReply.isPositiveCompletion(last())) {
        return false;
    }

    if (pointer != null) {
        __parseArticlePointer(getReplyString(), pointer);
    }

    return true;
}