diff options
Diffstat (limited to 'pki/base/util/src/netscape/net')
4 files changed, 252 insertions, 256 deletions
diff --git a/pki/base/util/src/netscape/net/NetworkClient.java b/pki/base/util/src/netscape/net/NetworkClient.java index 8275f0680..c9d004d96 100644 --- a/pki/base/util/src/netscape/net/NetworkClient.java +++ b/pki/base/util/src/netscape/net/NetworkClient.java @@ -27,61 +27,60 @@ import java.net.UnknownHostException; /** * This is the base class for network clients. - * - * @version 1.21, 08/07/97 - * @author Jonathan Payne + * + * @version 1.21, 08/07/97 + * @author Jonathan Payne */ public class NetworkClient { /** Socket for communicating with server. */ - protected Socket serverSocket = null; + protected Socket serverSocket = null; /** Stream for printing to the server. */ - public PrintStream serverOutput; + public PrintStream serverOutput; /** Buffered stream for reading replies from server. */ - public InputStream serverInput; + public InputStream serverInput; /** Open a connection to the server. */ - public void openServer(String server, int port) throws IOException, - UnknownHostException { - if (serverSocket != null) - closeServer(); - serverSocket = doConnect(server, port); - serverOutput = new PrintStream(new BufferedOutputStream( - serverSocket.getOutputStream()), true); - serverInput = new BufferedInputStream(serverSocket.getInputStream()); + public void openServer(String server, int port) + throws IOException, UnknownHostException { + if (serverSocket != null) + closeServer(); + serverSocket = doConnect (server, port); + serverOutput = new PrintStream(new BufferedOutputStream(serverSocket.getOutputStream()), + true); + serverInput = new BufferedInputStream(serverSocket.getInputStream()); } /** - * Return a socket connected to the server, with any appropriate options - * pre-established + * Return a socket connected to the server, with any + * appropriate options pre-established */ - protected Socket doConnect(String server, int port) throws IOException, - UnknownHostException { - return new Socket(server, port); + protected Socket doConnect (String server, int port) + throws IOException, UnknownHostException { + return new Socket (server, port); } /** Close an open connection to the server. */ public void closeServer() throws IOException { - if (!serverIsOpen()) { - return; - } - serverSocket.close(); - serverSocket = null; - serverInput = null; - serverOutput = null; + if (! serverIsOpen()) { + return; + } + serverSocket.close(); + serverSocket = null; + serverInput = null; + serverOutput = null; } /** Return server connection status */ public boolean serverIsOpen() { - return serverSocket != null; + return serverSocket != null; } /** Create connection with host <i>host</i> on port <i>port</i> */ public NetworkClient(String host, int port) throws IOException { - openServer(host, port); + openServer(host, port); } - public NetworkClient() { - } + public NetworkClient() {} } diff --git a/pki/base/util/src/netscape/net/TransferProtocolClient.java b/pki/base/util/src/netscape/net/TransferProtocolClient.java index 0de145789..23f133178 100644 --- a/pki/base/util/src/netscape/net/TransferProtocolClient.java +++ b/pki/base/util/src/netscape/net/TransferProtocolClient.java @@ -21,108 +21,105 @@ import java.io.IOException; import java.util.Vector; /** - * This class implements that basic intefaces of transfer protocols. It is used - * by subclasses implementing specific protocols. - * - * @version 1.25, 08/07/97 - * @author Jonathan Payne + * This class implements that basic intefaces of transfer protocols. + * It is used by subclasses implementing specific protocols. + * + * @version 1.25, 08/07/97 + * @author Jonathan Payne */ public class TransferProtocolClient extends NetworkClient { static final boolean debug = false; - /** - * Array of strings (usually 1 entry) for the last reply from the server. - */ - protected Vector serverResponse = new Vector(1); + /** Array of strings (usually 1 entry) for the last reply + from the server. */ + protected Vector serverResponse = new Vector(1); /** code for last reply */ - protected int lastReplyCode; + protected int lastReplyCode; + /** - * Pulls the response from the server and returns the code as a number. - * Returns -1 on failure. + * Pulls the response from the server and returns the code as a + * number. Returns -1 on failure. */ public int readServerResponse() throws IOException { - StringBuffer replyBuf = new StringBuffer(32); - int c; - int continuingCode = -1; - int code; - String response; + StringBuffer replyBuf = new StringBuffer(32); + int c; + int continuingCode = -1; + int code; + String response; - serverResponse.setSize(0); - while (true) { - while ((c = serverInput.read()) != -1) { - if (c == '\r') { - if ((c = serverInput.read()) != '\n') - replyBuf.append('\r'); - } - replyBuf.append((char) c); - if (c == '\n') - break; - } - response = replyBuf.toString(); - replyBuf.setLength(0); - if (debug) { - System.out.print(response); - } - try { - code = Integer.parseInt(response.substring(0, 3)); - } catch (NumberFormatException e) { - code = -1; - } catch (StringIndexOutOfBoundsException e) { - /* - * this line doesn't contain a response code, so we just - * completely ignore it - */ - continue; - } - serverResponse.addElement(response); - if (continuingCode != -1) { - /* we've seen a XXX- sequence */ - if (code != continuingCode - || (response.length() >= 4 && response.charAt(3) == '-')) { - continue; - } else { - /* seen the end of code sequence */ - continuingCode = -1; - break; - } - } else if (response.length() >= 4 && response.charAt(3) == '-') { - continuingCode = code; - continue; - } else { - break; - } - } + serverResponse.setSize(0); + while (true) { + while ((c = serverInput.read()) != -1) { + if (c == '\r') { + if ((c = serverInput.read()) != '\n') + replyBuf.append('\r'); + } + replyBuf.append((char)c); + if (c == '\n') + break; + } + response = replyBuf.toString(); + replyBuf.setLength(0); + if (debug) { + System.out.print(response); + } + try { + code = Integer.parseInt(response.substring(0, 3)); + } catch (NumberFormatException e) { + code = -1; + } catch (StringIndexOutOfBoundsException e) { + /* this line doesn't contain a response code, so + we just completely ignore it */ + continue; + } + serverResponse.addElement(response); + if (continuingCode != -1) { + /* we've seen a XXX- sequence */ + if (code != continuingCode || + (response.length() >= 4 && response.charAt(3) == '-')) { + continue; + } else { + /* seen the end of code sequence */ + continuingCode = -1; + break; + } + } else if (response.length() >= 4 && response.charAt(3) == '-') { + continuingCode = code; + continue; + } else { + break; + } + } - return lastReplyCode = code; + return lastReplyCode = code; } /** Sends command <i>cmd</i> to the server. */ public void sendServer(String cmd) { - serverOutput.print(cmd); - if (debug) { - System.out.print("Sending: " + cmd); - } + serverOutput.print(cmd); + if (debug) { + System.out.print("Sending: " + cmd); + } } /** converts the server response into a string. */ public String getResponseString() { - return (String) serverResponse.elementAt(0); + return (String) serverResponse.elementAt(0); } /** Returns all server response strings. */ public Vector getResponseStrings() { - return serverResponse; + return serverResponse; } /** standard constructor to host <i>host</i>, port <i>port</i>. */ public TransferProtocolClient(String host, int port) throws IOException { - super(host, port); + super(host, port); } /** creates an uninitialized instance of this class. */ - public TransferProtocolClient() { - } + public TransferProtocolClient() {} } diff --git a/pki/base/util/src/netscape/net/smtp/SmtpClient.java b/pki/base/util/src/netscape/net/smtp/SmtpClient.java index 3903e17ed..83ff6c095 100644 --- a/pki/base/util/src/netscape/net/smtp/SmtpClient.java +++ b/pki/base/util/src/netscape/net/smtp/SmtpClient.java @@ -25,14 +25,15 @@ import java.net.InetAddress; import netscape.net.TransferProtocolClient; /** - * This class implements the SMTP client. You can send a piece of mail by - * creating a new SmtpClient, calling the "to" method to add destinations, - * calling "from" to name the sender, calling startMessage to return a stream to - * which you write the message (with RFC733 headers) and then you finally close - * the Smtp Client. - * - * @version 1.17, 12 Dec 1994 - * @author James Gosling + * This class implements the SMTP client. + * You can send a piece of mail by creating a new SmtpClient, calling + * the "to" method to add destinations, calling "from" to name the + * sender, calling startMessage to return a stream to which you write + * the message (with RFC733 headers) and then you finally close the Smtp + * Client. + * + * @version 1.17, 12 Dec 1994 + * @author James Gosling */ public class SmtpClient extends TransferProtocolClient { @@ -42,123 +43,122 @@ public class SmtpClient extends TransferProtocolClient { * issue the QUIT command to the SMTP server and close the connection. */ public void closeServer() throws IOException { - if (serverIsOpen()) { - closeMessage(); - issueCommand("QUIT\r\n", 221); - super.closeServer(); - } + if (serverIsOpen()) { + closeMessage(); + issueCommand("QUIT\r\n", 221); + super.closeServer(); + } } void issueCommand(String cmd, int expect) throws IOException { - sendServer(cmd); - int reply; - while ((reply = readServerResponse()) != expect) - if (reply != 220) { - throw new SmtpProtocolException(getResponseString()); - } + sendServer(cmd); + int reply; + while ((reply = readServerResponse()) != expect) + if (reply != 220) { + throw new SmtpProtocolException(getResponseString()); + } } private void toCanonical(String s) throws IOException { - issueCommand("rcpt to: " + s + "\r\n", 250); + issueCommand("rcpt to: " + s + "\r\n", 250); } public void to(String s) throws IOException { - int st = 0; - int limit = s.length(); - int pos = 0; - int lastnonsp = 0; - int parendepth = 0; - boolean ignore = false; - while (pos < limit) { - int c = s.charAt(pos); - if (parendepth > 0) { - if (c == '(') - parendepth++; - else if (c == ')') - parendepth--; - if (parendepth == 0) - if (lastnonsp > st) - ignore = true; - else - st = pos + 1; - } else if (c == '(') - parendepth++; - else if (c == '<') - st = lastnonsp = pos + 1; - else if (c == '>') - ignore = true; - else if (c == ',') { - if (lastnonsp > st) - toCanonical(s.substring(st, lastnonsp)); - st = pos + 1; - ignore = false; - } else { - if (c > ' ' && !ignore) - lastnonsp = pos + 1; - else if (st == pos) - st++; - } - pos++; - } - if (lastnonsp > st) - toCanonical(s.substring(st, lastnonsp)); + int st = 0; + int limit = s.length(); + int pos = 0; + int lastnonsp = 0; + int parendepth = 0; + boolean ignore = false; + while (pos < limit) { + int c = s.charAt(pos); + if (parendepth > 0) { + if (c == '(') + parendepth++; + else if (c == ')') + parendepth--; + if (parendepth == 0) + if (lastnonsp > st) + ignore = true; + else + st = pos + 1; + } else if (c == '(') + parendepth++; + else if (c == '<') + st = lastnonsp = pos + 1; + else if (c == '>') + ignore = true; + else if (c == ',') { + if (lastnonsp > st) + toCanonical(s.substring(st, lastnonsp)); + st = pos + 1; + ignore = false; + } else { + if (c > ' ' && !ignore) + lastnonsp = pos + 1; + else if (st == pos) + st++; + } + pos++; + } + if (lastnonsp > st) + toCanonical(s.substring(st, lastnonsp)); } public void from(String s) throws IOException { - issueCommand("mail from: " + s + "\r\n", 250); + issueCommand("mail from: " + s + "\r\n", 250); } /** open a SMTP connection to host <i>host</i>. */ private void openServer(String host) throws IOException { - openServer(host, 25); - issueCommand("helo " + InetAddress.getLocalHost().getHostName() - + "\r\n", 250); + openServer(host, 25); + issueCommand("helo "+InetAddress.getLocalHost().getHostName()+"\r\n", 250); } public PrintStream startMessage() throws IOException { - issueCommand("data\r\n", 354); - return message = new SmtpPrintStream(serverOutput, this); + issueCommand("data\r\n", 354); + return message = new SmtpPrintStream(serverOutput, this); } void closeMessage() throws IOException { - if (message != null) - message.close(); + if (message != null) + message.close(); } /** New SMTP client connected to host <i>host</i>. */ - public SmtpClient(String host) throws IOException { - super(); - if (host != null) { - try { - openServer(host); - return; - } catch (Exception e) { - } - } - try { - String s; - try { - // java.security.AccessController.beginPrivileged(); - s = System.getProperty("mail.host"); - } finally { - // java.security.AccessController.endPrivileged(); - } - if (s != null) { - openServer(s); - return; - } - } catch (Exception e) { - } - try { - openServer("localhost"); - } catch (Exception e) { - openServer("mailhost"); - } + public SmtpClient (String host) throws IOException { + super(); + if (host != null) { + try { + openServer(host); + return; + } catch(Exception e) { + } + } + try { + String s; + try { +// java.security.AccessController.beginPrivileged(); + s = System.getProperty("mail.host"); + } finally { +// java.security.AccessController.endPrivileged(); + } + if (s != null) { + openServer(s); + return; + } + } catch(Exception e) { + } + try { + openServer("localhost"); + } catch(Exception e) { + openServer("mailhost"); + } } /** Create an uninitialized SMTP client. */ - public SmtpClient() throws IOException { - this(null); + public SmtpClient () throws IOException { + this(null); } } @@ -166,70 +166,69 @@ class SmtpPrintStream extends java.io.PrintStream { private SmtpClient target; private int lastc = '\n'; - SmtpPrintStream(OutputStream fos, SmtpClient cl) { - super(fos); - target = cl; + SmtpPrintStream (OutputStream fos, SmtpClient cl) { + super(fos); + target = cl; } public void close() { - if (target == null) - return; - if (lastc != '\n') { - write('\r'); - write('\n'); - } - try { - target.issueCommand(".\r\n", 250); - target.message = null; - out = null; - target = null; - } catch (IOException e) { - } + if (target == null) + return; + if (lastc != '\n') { + write('\r'); + write('\n'); + } + try { + target.issueCommand(".\r\n", 250); + target.message = null; + out = null; + target = null; + } catch (IOException e) { + } } public void write(int b) { - try { - // quote a dot at the beginning of a line - if (lastc == '\n' && b == '.') { - out.write('.'); - } - - // translate NL to CRLF - if (b == '\n') { - out.write('\r'); - } - out.write(b); - lastc = b; - } catch (IOException e) { - } + try { + // quote a dot at the beginning of a line + if (lastc == '\n' && b == '.') { + out.write('.'); + } + + // translate NL to CRLF + if (b == '\n') { + out.write('\r'); + } + out.write(b); + lastc = b; + } catch (IOException e) { + } } public void write(byte b[], int off, int len) { - try { - int lc = lastc; - while (--len >= 0) { - int c = b[off++]; - - // quote a dot at the beginning of a line - if (lc == '\n' && c == '.') - out.write('.'); - - // translate NL to CRLF - if (c == '\n') { - out.write('\r'); - } - out.write(c); - lc = c; - } - lastc = lc; - } catch (IOException e) { - } + try { + int lc = lastc; + while (--len >= 0) { + int c = b[off++]; + + // quote a dot at the beginning of a line + if (lc == '\n' && c == '.') + out.write('.'); + + // translate NL to CRLF + if (c == '\n') { + out.write('\r'); + } + out.write(c); + lc = c; + } + lastc = lc; + } catch (IOException e) { + } } - public void print(String s) { - int len = s.length(); - for (int i = 0; i < len; i++) { - write(s.charAt(i)); - } + int len = s.length(); + for (int i = 0; i < len; i++) { + write(s.charAt(i)); + } } } diff --git a/pki/base/util/src/netscape/net/smtp/SmtpProtocolException.java b/pki/base/util/src/netscape/net/smtp/SmtpProtocolException.java index d438543f8..bb015f90e 100644 --- a/pki/base/util/src/netscape/net/smtp/SmtpProtocolException.java +++ b/pki/base/util/src/netscape/net/smtp/SmtpProtocolException.java @@ -20,8 +20,8 @@ package netscape.net.smtp; import java.io.IOException; /** - * This exeception is thrown when unexpected results are returned during an SMTP - * session. + * This exeception is thrown when unexpected results are returned during + * an SMTP session. */ public class SmtpProtocolException extends IOException { /** @@ -30,6 +30,7 @@ public class SmtpProtocolException extends IOException { private static final long serialVersionUID = -5586603317525864401L; SmtpProtocolException(String s) { - super(s); + super(s); } } + |