KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcommercesql > gateway > authorizenet > Gateway


1 package com.jcommercesql.gateway.authorizenet;
2
3 /**
4  * This file is part of JCommerceSQL Authorize.net Gateway
5  *
6  * <b>Copyright 2003 by Richard Roehl. All Rights Reserved.</b>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */

23
24 import java.io.*;
25 import javax.net.ssl.*;
26
27 public class Gateway {
28
29   private String JavaDoc method = null; // GET or POST
30
private String JavaDoc host = null;
31   private int port=443;
32   private String JavaDoc path=null;
33   private byte[] data = null;
34
35   private static int READSIZE=10000;
36
37   public Gateway() {}
38
39 //----------------------------------------------------------------------------
40
protected byte[] submitGet(String JavaDoc host, int port, String JavaDoc path)
41     throws Exception JavaDoc {
42     this.host=host;
43     this.port=port;
44     if ( path==null || path.equals("") ) {
45     path="/"; }
46     else {
47     this.path=path;//URLEncoder.encode(path);
48
}
49     method="GET";
50     return(submit());
51   }
52
53 //----------------------------------------------------------------------------
54
protected byte[] submitPost(String JavaDoc host, int port, String JavaDoc path, String JavaDoc data)
55     throws Exception JavaDoc {
56     return submitPost(host,port,path,data.getBytes());
57   }
58
59 //----------------------------------------------------------------------------
60
protected byte[] submitPost(String JavaDoc host, int port, String JavaDoc path, byte[] data)
61     throws Exception JavaDoc {
62
63     this.host=host;
64     this.port=port;
65     if ( path==null || path.equals("") ) {
66     path="/"; }
67     else {
68     this.path=path;//URLEncoder.encode(path);
69
}
70     if (data==null) { data = new byte[1]; } // throw an exception
71
this.data=data;
72     method="POST";
73     return(submit());
74   }
75  
76 //----------------------------------------------------------------------------
77
private byte[] submit() throws Exception JavaDoc {
78
79     int bytesRead = 0;
80     byte[] result = null;
81     byte[] temp = null;
82
83     ByteArrayOutputStream sendBytes = new ByteArrayOutputStream();
84     ByteArrayOutputStream receiveBytes = new ByteArrayOutputStream();
85
86     try {
87       SSLSocketFactory factory=(SSLSocketFactory)SSLSocketFactory.getDefault();
88       SSLSocket socket = (SSLSocket)factory.createSocket(host,port);
89       socket.startHandshake();
90
91       OutputStream socketOut =( socket.getOutputStream() );
92
93       String JavaDoc command = method+" "+path+" HTTP/1.0\n";
94       //command+="Host: "+host+"\nConnection: close\n";
95

96       if (data!=null) {
97     command+="Content-Length: " + data.length +"\n\n";
98         sendBytes.write(command.getBytes(),0,command.length());
99         sendBytes.write(data,0,data.length);
100       } else {
101         // if data == null then we are most likely doing a GET request
102
sendBytes.write(command.getBytes(),0,command.length());
103       }
104
105       sendBytes.write("\n".getBytes(),0,"\n".length());
106
107       temp = sendBytes.toByteArray();
108       socketOut.write(temp,0,temp.length);
109       socketOut.flush();
110
111       /* read response */
112       BufferedInputStream socketIn =
113         new BufferedInputStream( socket.getInputStream() );
114
115       byte[] inputData = new byte[READSIZE];
116       while ((bytesRead=socketIn.read(inputData,0,READSIZE)) > -1) {
117           receiveBytes.write(inputData,0,bytesRead);
118       }
119       result=receiveBytes.toByteArray();
120
121       receiveBytes.close();
122       sendBytes.close();
123       socket.close();
124
125     } catch (Exception JavaDoc e) {
126       throw e;
127     }
128
129     return result;
130   } // end submit
131

132 } // end class
133
Popular Tags