Example usage for org.apache.commons.net.io Util copyReader

List of usage examples for org.apache.commons.net.io Util copyReader

Introduction

In this page you can find the example usage for org.apache.commons.net.io Util copyReader.

Prototype

public static final long copyReader(Reader source, Writer dest) throws CopyStreamException 

Source Link

Document

Same as copyReader(source, dest, DEFAULT_COPY_BUFFER_SIZE);

Usage

From source file:examples.mail.java

public final static void main(String[] args) {
    String sender, recipient, subject, filename, server, cc;
    Vector ccList = new Vector();
    BufferedReader stdin;//from   w  ww .  j a v a 2s  .  c o m
    FileReader fileReader = null;
    Writer writer;
    SimpleSMTPHeader header;
    SMTPClient client;
    Enumeration en;

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

    server = args[0];

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

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

        sender = stdin.readLine();

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

        recipient = stdin.readLine();

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

        subject = stdin.readLine();

        header = new SimpleSMTPHeader(sender, recipient, subject);

        while (true) {
            System.out.print("CC <enter one address per line, hit enter to end>: ");
            System.out.flush();

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

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

            header.addCC(cc);
            ccList.addElement(cc);
        }

        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());
        }

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

        client.connect(server);

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

        client.login();

        client.setSender(sender);
        client.addRecipient(recipient);

        en = ccList.elements();

        while (en.hasMoreElements())
            client.addRecipient((String) en.nextElement());

        writer = client.sendMessageData();

        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:com.xiangzhurui.util.email.SMTPMail.java

public static void main(String[] args) {
    String sender, recipient, subject, filename, server, cc;
    List<String> ccList = new ArrayList<String>();
    BufferedReader stdin;//  www. j a va  2s  .c  o  m
    FileReader fileReader = null;
    Writer writer;
    SimpleSMTPHeader header;
    SMTPClient client;

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

    server = args[0];

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

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

        sender = stdin.readLine();

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

        recipient = stdin.readLine();

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

        subject = stdin.readLine();

        header = new SimpleSMTPHeader(sender, recipient, subject);

        while (true) {
            System.out.print("CC <enter one address per line, hit enter to end>: ");
            System.out.flush();

            cc = stdin.readLine();

            if (cc == null || cc.length() == 0) {
                break;
            }

            header.addCC(cc.trim());
            ccList.add(cc.trim());
        }

        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());
        }

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

        client.connect(server);

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

        client.login();

        client.setSender(sender);
        client.addRecipient(recipient);

        for (String recpt : ccList) {
            client.addRecipient(recpt);
        }

        writer = client.sendMessageData();

        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:examples.nntp.post.java

public final static void main(String[] args) {
    String from, subject, newsgroup, filename, server, organization;
    String references;/*from  www. j a va2 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 && 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  w w . j  ava2s .  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 && 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:autohit.call.modules.SimpleSmtpModule.java

/**
 * Send the message in a Universe Object. Returns the SMTP reply code.
 * //from   w w  w. ja va2  s  .  c o  m
 * @param hostname
 *           hostname to use instead of localhost.
 * @throws CallException
 */
private String senduni(String uniobject) throws CallException {

    String result = ERROR_STRING_FOR_FAILURE;

    // Is it started?
    if (client == null) {
        this.fault("Session not start()'ed.");
    }

    try {
        // get the hoses
        InputStream unio = visUniverse.getStream(uniobject);
        BufferedReader bin = new BufferedReader(new InputStreamReader(unio));
        Writer mwriter = client.sendMessageData();

        // and pipe them together
        if (mwriter != null) {
            Util.copyReader(bin, mwriter);
            mwriter.close();
            unio.close();
            client.completePendingCommand(); // don't care if it was ok.

            // what happened?
            int code = client.getReplyCode();
            if (code >= SMTP_ERROR_THRESHOLD) {
                this.error("Message send FAILED (from Universe).  code=" + code + " reply="
                        + client.getReplyString());
            } else if (this.isDebugging()) {
                this.debug("Message send complete (from Universe).  code=");
            }
            result = Integer.toString(code);

        } else {
            this.log("Message send FAILED (from Universe) because SMTP connection was completely ready.  reply="
                    + client.getReplyString());
        }

    } catch (UniverseException uex) {
        this.fault("Could not send universe object due to Universe problem.  code=" + uex.numeric + "  message="
                + uex.getMessage(), uex);
    } catch (SMTPConnectionClosedException ex) {
        this.done();
        this.fault("Send failed (from Universe).  Connection expired and closed itself.");
    } catch (Exception ex) {
        this.done();
        this.fault("Send failed (from Universe) due to exception.  message=" + ex.getMessage());
    }
    return result;
}

From source file:autohit.call.modules.TolerantSmtpModule.java

/**
 * Send the message in a Universe Object. Returns the SMTP reply code.
 * /*from w ww .j  a  va  2 s  .c om*/
 * @param hostname
 *            hostname to use instead of localhost.
 * @throws CallException
 */
private String senduni(String uniobject) throws CallException {

    String result = ERROR_STRING_FOR_FAILURE;

    // Is it started?
    if (client == null) {
        this.error("Session not start()'ed.");
    }

    try {
        // get the hoses
        InputStream unio = visUniverse.getStream(uniobject);
        BufferedReader bin = new BufferedReader(new InputStreamReader(unio));
        Writer mwriter = client.sendMessageData();

        // and pipe them together
        if (mwriter != null) {
            Util.copyReader(bin, mwriter);
            mwriter.close();
            unio.close();
            client.completePendingCommand(); // don't care if it was ok.

            // what happened?
            int code = client.getReplyCode();
            if (code >= SMTP_ERROR_THRESHOLD) {
                this.error("Message send FAILED (from Universe).  code=" + code + " reply="
                        + client.getReplyString());
            } else if (this.isDebugging()) {
                this.debug("Message send complete (from Universe).  code=");
            }
            result = Integer.toString(code);

        } else {
            this.log("Message send FAILED (from Universe) because SMTP connection was completely ready.  reply="
                    + client.getReplyString());
        }

    } catch (UniverseException uex) {
        this.fault("Could not send universe object due to Universe problem.  code=" + uex.numeric + "  message="
                + uex.getMessage(), uex);
    } catch (SMTPConnectionClosedException ex) {
        this.done();
        this.error("Send failed (from Universe).  Connection expired and closed itself.  Last reply="
                + client.getReplyString());
    } catch (Exception ex) {
        this.done();
        this.error("Send failed (from Universe) due to exception.  message=" + ex.getMessage());
    }
    return result;
}

From source file:autohit.call.modules.SimpleSmtpModule.java

/**
 * Send the message in a Universe Object. Returns the SMTP reply code. It
 * will run a variable replace on it before sending it.
 * //from  w  w w.  j  av  a2s  .  co  m
 * @param hostname
 *           hostname to use instead of localhost.
 * @throws CallException
 */
private String senduniscrub(String uniobject) throws CallException {

    String result = ERROR_STRING_FOR_FAILURE;
    String tempObj = null;

    // Is it started?
    if (client == null) {
        this.fault("Session not start()'ed.");
    }

    try {

        // Process it
        try {
            tempObj = visUniverse.reserveUnique(TEMPFILE);
            String thing = UniverseUtils.load2String(visUniverse.getStream(uniobject));
            String thang = StringProcessors.evalString2Core(thing, visCore);
            OutputStream os = visUniverse.putStream(tempObj);
            UniverseUtils.saveString(os, thang);

        } catch (UniverseException ue) {
            throw ue;
        } catch (Exception e) {
            this.fault(
                    "Send failed (from Universe).  Could not create scrubbed intermediary tempfile.  message="
                            + e.getMessage());
        }

        // get the hoses
        InputStream unio = visUniverse.getStream(tempObj);
        BufferedReader bin = new BufferedReader(new InputStreamReader(unio));
        Writer mwriter = client.sendMessageData();

        // and pipe them together
        if (mwriter != null) {
            Util.copyReader(bin, mwriter);
            mwriter.close();
            unio.close();
            client.completePendingCommand(); // don't care if it was ok.

            // what happened?
            int code = client.getReplyCode();
            if (code >= SMTP_ERROR_THRESHOLD) {
                this.error("Message send FAILED (from Universe).  code=" + code + " reply="
                        + client.getReplyString());
            } else if (this.isDebugging()) {
                this.debug("Message send complete (from Universe).  code=");
            }
            result = Integer.toString(code);

        } else {
            this.log("Message send FAILED (from Universe) because SMTP connection was completely ready.  reply="
                    + client.getReplyString());
        }

    } catch (CallException cex) {
        throw cex;
    } catch (UniverseException uex) {
        this.fault("Could not send universe object due to Universe problem.  code=" + uex.numeric + "  message="
                + uex.getMessage(), uex);
    } catch (SMTPConnectionClosedException ex) {
        this.done();
        this.fault("Send failed (from Universe).  Connection expired and closed itself.");
    } catch (Exception ex) {
        this.done();
        this.fault("Send failed (from Universe) due to exception.  message=" + ex.getMessage());
    } finally {
        if ((!this.isDebugging()) && (tempObj != null)) {
            try {
                visUniverse.remove(tempObj);
            } catch (Exception ee) {
                // Don't care
            }
        }
    }
    return result;
}

From source file:autohit.call.modules.TolerantSmtpModule.java

/**
 * Send the message in a Universe Object. Returns the SMTP reply code. It
 * will run a variable replace on it before sending it.
 * /* w w  w  .  j av  a2s . c  o m*/
 * @param hostname
 *            hostname to use instead of localhost.
 * @throws CallException
 */
private String senduniscrub(String uniobject) throws CallException {

    String result = ERROR_STRING_FOR_FAILURE;
    String tempObj = null;

    // Is it started?
    if (client == null) {
        this.error("Session not start()'ed.");
    }

    try {

        // Process it
        try {
            tempObj = visUniverse.reserveUnique(TEMPFILE);
            OutputStream os = visUniverse.putStream(tempObj);
            StringProcessors.evalStreams2Core(visUniverse.getStream(uniobject), os, visCore);
            os.close();

        } catch (UniverseException ue) {
            throw ue;
        } catch (Exception e) {
            this.fault(
                    "Send failed (from Universe).  Could not create scrubbed intermediary tempfile.  message="
                            + e.getMessage());
        }

        // get the hoses
        InputStream unio = visUniverse.getStream(tempObj);
        BufferedReader bin = new BufferedReader(new InputStreamReader(unio));
        Writer mwriter = client.sendMessageData();

        // and pipe them together
        if (mwriter != null) {
            Util.copyReader(bin, mwriter);
            mwriter.close();
            unio.close();
            client.completePendingCommand(); // don't care if it was ok.

            // what happened?
            int code = client.getReplyCode();
            if (code >= SMTP_ERROR_THRESHOLD) {
                this.error("Message send FAILED (from Universe).  code=" + code + " reply="
                        + client.getReplyString());
            } else if (this.isDebugging()) {
                this.debug("Message send complete (from Universe).  code=");
            }
            result = Integer.toString(code);

        } else {
            this.log("Message send FAILED (from Universe) because SMTP connection was completely ready.  reply="
                    + client.getReplyString());
        }

    } catch (CallException cex) {
        throw cex;
    } catch (UniverseException uex) {
        this.fault("Could not send universe object due to Universe problem.  code=" + uex.numeric + "  message="
                + uex.getMessage(), uex);
    } catch (SMTPConnectionClosedException ex) {
        this.done();
        this.error("Send failed (from Universe).  Connection expired and closed itself.  Last reply="
                + client.getReplyString());
    } catch (Exception ex) {
        this.done();
        this.error("Send failed (from Universe) due to exception.  message=" + ex.getMessage());
    } finally {
        if ((!this.isDebugging()) && (tempObj != null)) {
            try {
                visUniverse.remove(tempObj);
            } catch (Exception ee) {
                // Don't care
            }
        }
    }
    return result;
}

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

/***
 * List the command help from the server.
 * <p>//from   w  w w .jav a 2 s .  co m
 * @return The sever help information.
 * @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 String listHelp() throws IOException {
    if (!NNTPReply.isInformational(help())) {
        return null;
    }

    StringWriter help = new StringWriter();
    BufferedReader reader = new DotTerminatedMessageReader(_reader_);
    Util.copyReader(reader, help);
    reader.close();
    help.close();
    return help.toString();
}

From source file:org.apache.common.net.examples.mail.SMTPMail.java

public final static void main(String[] args) {
    String sender, recipient, subject, filename, server, cc;
    List<String> ccList = new ArrayList<String>();
    BufferedReader stdin;/* w ww . j a  v  a2s  . c  o m*/
    FileReader fileReader = null;
    Writer writer;
    SimpleSMTPHeader header;
    SMTPClient client;

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

    server = args[0];

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

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

        sender = stdin.readLine();

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

        recipient = stdin.readLine();

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

        subject = stdin.readLine();

        header = new SimpleSMTPHeader(sender, recipient, subject);

        while (true) {
            System.out.print("CC <enter one address per line, hit enter to end>: ");
            System.out.flush();

            cc = stdin.readLine();

            if (cc == null || cc.length() == 0) {
                break;
            }

            header.addCC(cc.trim());
            ccList.add(cc.trim());
        }

        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());
        }

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

        client.connect(server);

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

        client.login();

        client.setSender(sender);
        client.addRecipient(recipient);

        for (String recpt : ccList) {
            client.addRecipient(recpt);
        }

        writer = client.sendMessageData();

        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);
    }
}