[Webfunds-commits] java/webfunds/sox SOXArgsException.java HttpAgent.java SimpleIssuer.java SmartIssuer.java ValueAccount.java

Ian Grigg iang@cypherpunks.ai
Mon, 10 Jul 2000 13:37:56 -0400 (AST)


iang        00/07/10 13:37:56

  Modified:    webfunds/utils RawHttp.java
               webfunds/sox HttpAgent.java SimpleIssuer.java
                        SmartIssuer.java ValueAccount.java
  Added:       webfunds/sox SOXArgsException.java
  Log:
  moved the Raw Socket code over to utils.  might one day want
  to sort out the Connect black hole problem, perhaps with callbacks,
  perhaps with an internal thread :-(  Also added a SOXArgsEx so
  that proper indication occurs rather than stealing it from SOXPEx
  which caused caller code to assume backend send back rubbish.

Revision  Changes    Path
1.4       +4 -4      java/webfunds/utils/RawHttp.java

Index: RawHttp.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/utils/RawHttp.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- RawHttp.java	2000/05/27 02:53:54	1.3
+++ RawHttp.java	2000/07/10 17:37:54	1.4
@@ -1,5 +1,5 @@
 /*
- * $Id: RawHttp.java,v 1.3 2000/05/27 02:53:54 iang Exp $
+ * $Id: RawHttp.java,v 1.4 2000/07/10 17:37:54 iang Exp $
  *
  * Copyright (c) 2000 Systemics Inc on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -42,6 +42,7 @@
      * @param host is the domain to open to
      * @param request the bytes to write to opened socket
      * @param port is the port, 80 if <= 0
+     * @param size is the port, some default if <= 0
      * @return the bytes read
      *
      * @except RawURLException connect details bad, check them
@@ -52,8 +53,8 @@
         throws RawURLException, RawConnectException
     {
 
-        if (port <= 0)
-            port = 80;
+        if (port <= 0) port = 80;
+        if (size <= 0) size = 10240;
 
         Socket sock;
         try {
@@ -116,7 +117,6 @@
         }
 
 
-        // int size = 10240;
         byte[] buf = new byte[size];
 
         int i = 0;



1.19      +36 -211   java/webfunds/sox/HttpAgent.java

Index: HttpAgent.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/sox/HttpAgent.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- HttpAgent.java	2000/05/16 11:17:45	1.18
+++ HttpAgent.java	2000/07/10 17:37:55	1.19
@@ -1,5 +1,5 @@
 /*
- * $Id: HttpAgent.java,v 1.18 2000/05/16 11:17:45 iang Exp $
+ * $Id: HttpAgent.java,v 1.19 2000/07/10 17:37:55 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -9,6 +9,10 @@
 import java.io.*;
 import java.net.*;
 
+import webfunds.utils.RawHttp;
+import webfunds.utils.RawURLException;
+import webfunds.utils.RawConnectException;
+
 /**
  * Instances of this class are used to perform Http requests.
  */
@@ -136,147 +140,7 @@
     }
 
 
-    /**
-     * Send a request and read the reply from a socket.
-     *
-     * @param host is the domain to open to
-     * @param request the bytes to write to opened socket
-     * @param port is the port, 80 if <= 0
-     * @return the bytes read
-     * @except SOXAgentReplyException could not interpret reply
-     * @except SOXAgentURLException connect details bad, check them
-     * @except SOXAgentConnectException connection refused (try again later)
-     * @except IOException code error, catch and die
-     */
-    public static byte[] getSocket(String host, int port, byte[] request, int size)
-        throws SOXAgentURLException, SOXAgentConnectException
-    {
-
-// remove this when all using utils.RawHttp
-        if (port <= 0)
-            port = 80;
-
-        Socket sock;
-        try {
-            sock = new Socket(host, port);
-        } catch (java.net.UnknownHostException ex) {
-            throw new SOXAgentURLException("host: " + host + ":" + port +
-                                           "\n" + ex);
-        } catch (java.net.ConnectException ex) {  // from IOEx
-            throw new SOXAgentConnectException("down: " + host + ":" + port);
-        } catch (java.net.NoRouteToHostException ex) {  // from IOEx
-            throw new SOXAgentConnectException("no route: "+host+":"+port);
-        } catch (IOException ex) {
-            //
-            // Damn.
-            // Macs throw IOException instead of something more specific.
-            // java.io.IOException: OpenTransport error -3155; connect failed1
-            // Checking on meaning...
-            //
-            ex.printStackTrace();
-            throw new SOXAgentURLException("(MAC) URL bad ?: "+host+":"+port);
-        }
-
-        //
-        //  This is most unsatisfactory.
-        //  Can't set the timeout on the connect!
-        //  Note that this sets the timeout for each read, not them all.
-        //
-        try {
-            sock.setSoTimeout(10 * 1000);
-            sock.setTcpNoDelay(true);
-        } catch (SocketException ex) {
-            ex.printStackTrace();
-            throw new RuntimeException("setSocket: " + ex);
-        }
 
-        OutputStream os;
-        InputStream is;
-        try {
-            os = sock.getOutputStream();
-        } catch (IOException ex) {
-            ex.printStackTrace();
-            throw new SOXAgentConnectException("host bad (getOut IOEx): " + ex);
-        }
-
-        try {
-            is = sock.getInputStream();
-        } catch (NullPointerException ex) {
-            // blech.  if the conn goes before open, socket blows up
-            throw new SOXAgentConnectException("dropped con (NPE): " + ex);
-        } catch (IOException ex) {
-            ex.printStackTrace();
-            throw new SOXAgentConnectException("host bad (getIn IOEx): " + ex);
-        }
-
-        try {
-            os.write(request);
-        } catch (IOException ex) {
-            ex.printStackTrace();
-            throw new SOXAgentConnectException("host bad (write IOEx): " + ex);
-        }
-
-
-        // int size = 10240;
-        byte[] buf = new byte[size];
-
-        int i = 0;
-        int offset = 0;
-        while (i >= 0 && (offset < size))
-        {
-            offset += i;
-            try {
-                i = is.read(buf, offset, size - offset);
-            } catch (IOException ex) {
-                ex.printStackTrace();
-                throw new SOXAgentConnectException("sock bad (read IOEx): "+ex);
-            }
-        }
-
-//        int avail = -1;
-//        try {
-//            avail = is.available();
-//System.err.println("avail " + avail);
-//        } catch (IOException ex) {
-//            ex.printStackTrace();
-//            throw new SOXAgentConnectException("socket bad (avail IOEx): "+ex);
-//        }
-//
-//        byte buf[] = new byte[avail];
-//        int i = -2;
-//        try {
-//            i = is.read(buf);
-//        } catch (IOException ex) {
-//            ex.printStackTrace();
-//            throw new SOXAgentConnectException("socket bad (read IOEx): "+ex);
-//        }
-//System.err.println("i == " + i + "   of " + avail);
-//
-//        try {
-//            avail = is.available();
-//        } catch (IOException ex) {
-//            ex.printStackTrace();
-//            throw new SOXAgentConnectException("socket bad (avail IOEx): "+ex);
-//        }
-//
-//        if (avail > 0)
-//            throw new RuntimeException("still some bytes left? " + avail);
-
-        try
-        {
-            is.close();
-            sock.close();
-        }
-        catch (IOException ex)
-        {
-            ex.printStackTrace();
-            throw new SOXAgentConnectException("close (IOEx): "+ex);
-        }
-
-        return buf;
-    }
-
-
     /**
      * Send a Http request and await the reply.
      * This method uses the low level socket code to get more control.
@@ -293,80 +157,27 @@
                IOException  // failure!  catch and convert
     {
 
-// switch this over to utils.RawURL when it is tested some...
-
         String host = url.getHost();
         int    port = url.getPort();
-        if (port == -1)
-            port = 80;
+        byte[] post = getPostData(request, host + ":" + port);
 
-        logmsg("Opening Socket to: " + host + " : " + port);
-        Socket sock;
+        logmsg("Opening Socket to: " + host + " : " + port + " (" + 0 + ")");
+        byte[] out;
         try {
-            sock = new Socket(host, port);
-        } catch (java.net.UnknownHostException ex) {
-            throw new SOXAgentURLException("host: " + host + ":" + port +
-                                           "\n" + ex);
-        } catch (java.net.ConnectException ex) {  // from IOEx
-            logmsg("CE: server down " + ex);
-            throw new SOXAgentConnectException("down: " + host + ":" + port);
-        } catch (java.net.NoRouteToHostException ex) {  // from IOEx
-            logmsg("CE: server down " + ex);
-            throw new SOXAgentConnectException("no route: "+host+":"+port);
-        } catch (IOException ex) {
-            //
-            // Damn.
-            // Macs throw IOException instead of something more specific.
-            // java.io.IOException: OpenTransport error -3155; connect failed1
-            // Checking on meaning...
-            //
-            logmsg("what's this? " + ex);
-            ex.printStackTrace();
-            throw new SOXAgentURLException("URL bad ?: " + host + ":" + port);
-        }
-
-        //
-        //  This is most unsatisfactory.
-        //  Can't set the timeout on the connect!
-        //  Note that this sets the timeout for each read, not them all.
-        //
-        try {
-            sock.setSoTimeout(10 * 1000);
-            sock.setTcpNoDelay(true);
-        } catch (SocketException ex) {
-            ex.printStackTrace();
-            throw new RuntimeException("setSocket: " + ex);
-        }
-
-        byte[] buf = getPostData(request, host + ":" + port);
-
-        OutputStream os;
-        InputStream is;
-            os = sock.getOutputStream();
-            try {    // blech.  if the conn goes before open, socket blows up
-                is = sock.getInputStream();
-            } catch (NullPointerException ex) {
-                logmsg("NPE: server dropped connection?   " + ex);
-                throw new SOXAgentConnectException("issuer bad (NPE): " + ex);
-            }
-            os.write(buf);
-            // os.close();
-
-        buf = getPostReply(is);        // throws ReplyEx
-
-        try
-        {
-            is.close();
-            sock.close();
-        }
-        catch (IOException ex)
-        {
-            ex.printStackTrace();
-            logmsg("close errors?  ignoring: " + ex);
+            out = RawHttp.getSocket(host, port, post, 0);
+        } catch (RawURLException ex) {
+            String s = "RawURLEx: " + ex;
+            logmsg(s);
+            throw new SOXAgentURLException(s);
+        } catch (RawConnectException ex) {
+            String s = "RawConnectEx: " + ex;
+            logmsg(s);
+            throw new SOXAgentConnectException(s);
         }
 
+        byte[] reply = getPostReply(out);        // throws ReplyEx
+        return reply ;
 
-        return buf;
     }
 
 
@@ -375,7 +186,7 @@
      * @Return the next line
      * @except IOException some unknown failure (others inherit from IOEx)
      */
-    public String getLine(InputStream is)
+    protected String getLine(InputStream is)
         throws IOException
     {
         byte[] buf   = new byte[128];
@@ -405,10 +216,24 @@
 
     /**
      * Return the message from a POST reply
+     * @param buf the byte array with the reply in it
+     *
+     * @return the reply body
+     * @except IOException some unknown failure (others inherit from IOEx)
+     */
+    protected byte[] getPostReply(byte[] buf)
+        throws SOXAgentReplyException
+    {
+        ByteArrayInputStream bais = new ByteArrayInputStream(buf);
+        return getPostReply(bais);
+    }
+
+    /**
+     * Return the message from a POST reply
      * @return the reply body
      * @except IOException some unknown failure (others inherit from IOEx)
      */
-    public byte[] getPostReply(InputStream is)
+    protected byte[] getPostReply(InputStream is)
         throws SOXAgentReplyException
     {
         boolean ok = true;
@@ -527,7 +352,7 @@
     protected static final String type = "x-application/sox-2";
     protected static final String Type = "Content-Type: " + type + end;
 
-    public byte[] getPostData(byte[] request, String whereTo)
+    protected byte[] getPostData(byte[] request, String whereTo)
     {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 



1.9       +2 -2      java/webfunds/sox/SimpleIssuer.java

Index: SimpleIssuer.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/sox/SimpleIssuer.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- SimpleIssuer.java	2000/04/15 19:05:44	1.8
+++ SimpleIssuer.java	2000/07/10 17:37:55	1.9
@@ -1,5 +1,5 @@
 /*
- * $Id: SimpleIssuer.java,v 1.8 2000/04/15 19:05:44 iang Exp $
+ * $Id: SimpleIssuer.java,v 1.9 2000/07/10 17:37:55 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -241,7 +241,7 @@
         if (commsKey != null)
             return ;
 
-        logmsg("Fetching the issuer comms certificate");
+        logmsg("Fetching the SOX Server comms certificate");
 
         X509Cert commsCert;
         try {



1.9       +11 -4     java/webfunds/sox/SmartIssuer.java

Index: SmartIssuer.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/sox/SmartIssuer.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- SmartIssuer.java	2000/05/16 11:17:45	1.8
+++ SmartIssuer.java	2000/07/10 17:37:55	1.9
@@ -1,5 +1,5 @@
 /*
- * $Id: SmartIssuer.java,v 1.8 2000/05/16 11:17:45 iang Exp $
+ * $Id: SmartIssuer.java,v 1.9 2000/07/10 17:37:55 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -16,6 +16,9 @@
 import java.util.Vector;
 
 import webfunds.utils.Debug;
+import webfunds.utils.RawHttp;
+import webfunds.utils.RawConnectException;
+import webfunds.utils.RawURLException;
 
 /**
  * Make out like an Issuer.
@@ -249,10 +252,14 @@
             String s = httpGet + domain + httpEnd;
             logmsg(i + ": " + domain);
             byte[] page;
-            try {    // this should use utils.RawHttp not HttpAgent
-                page = HttpAgent.getSocket(domain, url.getPort(),
+            try {
+                page = RawHttp.getSocket(domain, url.getPort(),
                                            s.getBytes(), 256);
-            } catch (SOXAgentException ex) {
+            } catch (RawURLException ex) {
+                failures++;
+                logmsg(url + " : is bad?");
+                continue ;
+            } catch (RawConnectException ex) {
                 failures++;
                 logmsg(url + " : later?");
                 continue ;



1.16      +9 -7      java/webfunds/sox/ValueAccount.java

Index: ValueAccount.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/sox/ValueAccount.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- ValueAccount.java	2000/07/06 16:24:14	1.15
+++ ValueAccount.java	2000/07/10 17:37:55	1.16
@@ -1,5 +1,5 @@
 /*
- * $Id: ValueAccount.java,v 1.15 2000/07/06 16:24:14 gelderen Exp $
+ * $Id: ValueAccount.java,v 1.16 2000/07/10 17:37:55 iang Exp $
  *
  * Copyright (c) Systemics Ltd 1995-1999 on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -42,12 +42,14 @@
      * Deposit a payment.
      * The deposit is what makes this a ValueAccount, over the
      * top of the standard SOXAccount.
-     * @throws SOXPacketException if details don't match, need SOXArgs
+     * @throws SOXArgsException if item in payment is invalid
+     * @throws SOXLaterException if there is no ability to talk to the server
      * @throws SOXSubAccountException if something goes wrong with access
+     * @throws SOXKeyException if something goes wrong with signing
      */
     public MailItem[] deposit(Payment payment, String desc)
           throws SOXSubAccountException, SOXLaterException, SOXKeyException,
-                 SOXPacketException, SOXDepositException
+                 SOXArgsException, SOXDepositException
     {
         checkFrozen("deposit");
         if (!NetWatcher.netAvailability())
@@ -55,11 +57,11 @@
         if (isNew())          // also see retry code in doDepositGetMail()
             register();
 
-        if (!payment.getItem().equals(itemId))
+        ItemId payId = payment.getItem();
+        if (!payId.equals(itemId))
         {
-            System.err.println("Payment ItemID = "  + payment.getItem());
-            System.err.println("Account ItemID = " + itemId);
-            throw new SOXPacketException("Items are not the same");
+            String s = "subAccount item is " + itemId + " not " + payId;
+            throw new SOXArgsException(s);
         }
 
         String did = "D" + System.currentTimeMillis();



1.1                  java/webfunds/sox/SOXArgsException.java

Index: SOXArgsException.java
===================================================================
/*
 * $Id: SOXArgsException.java,v 1.1 2000/07/10 17:37:55 iang Exp $
 *
 * Copyright (c) 2000 Systemics Inc on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */
package webfunds.sox;

/**
 * This exception class is thrown when arguments to the method are bad.
 */
public class SOXArgsException
    extends SOXException
{
    /**
     * Create a new SOXArgsException object
     * @param msg a short message describing the exception
     */
    public SOXArgsException(String msg)
    {
        super(1, msg);
    }

    public SOXArgsException(int num, String msg)
    {
        super(num, msg);
    }
}