KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > accounting > thirdparty > gosoftware > RitaApi


1 /*
2  * $Id: RitaApi.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.accounting.thirdparty.gosoftware;
26
27 import java.io.IOException JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.ofbiz.base.util.Debug;
31 import org.ofbiz.base.util.GeneralException;
32 import org.ofbiz.base.util.HttpClient;
33 import org.ofbiz.base.util.HttpClientException;
34 import org.ofbiz.base.util.ObjectType;
35
36 import org.apache.commons.collections.MapIterator;
37 import org.apache.commons.collections.map.LinkedMap;
38
39 /**
40  *
41  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
42  * @version $Rev: 5462 $
43  * @since 3.2
44  */

45 public class RitaApi {
46
47     public static final String JavaDoc module = RitaApi.class.getName();
48     public static final String JavaDoc xschema = "x-schema:..\\dtd\\stnd.xdr";
49     public static final String JavaDoc rootElement = "XML_FILE";
50     public static final String JavaDoc reqElement = "XML_REQUEST";
51
52     // request fields
53
public static final String JavaDoc FUNCTION_TYPE = "FUNCTION_TYPE";
54     public static final String JavaDoc PAYMENT_TYPE = "PAYMENT_TYPE";
55     public static final String JavaDoc USER_ID = "USER_ID";
56     public static final String JavaDoc USER_PW = "USER_PW";
57     public static final String JavaDoc COMMAND = "COMMAND";
58     public static final String JavaDoc CLIENT_ID = "CLIENT_ID";
59
60     public static final String JavaDoc ACCT_NUM = "ACCT_NUM";
61     public static final String JavaDoc EXP_MONTH = "EXP_MONTH";
62     public static final String JavaDoc EXP_YEAR = "EXP_YEAR";
63     public static final String JavaDoc TRANS_AMOUNT = "TRANS_AMOUNT";
64
65     public static final String JavaDoc CARDHOLDER = "CARDHOLDER";
66     public static final String JavaDoc TRACK_DATA = "TRACK_DATA";
67     public static final String JavaDoc INVOICE = "INVOICE";
68     public static final String JavaDoc PRESENT_FLAG = "PRESENT_FLAG";
69     public static final String JavaDoc CUSTOMER_STREET = "CUSTOMER_STREET";
70     public static final String JavaDoc CUSTOMER_ZIP = "CUSTOMER_ZIP";
71     public static final String JavaDoc CVV2 = "CVV2";
72     public static final String JavaDoc TAX_AMOUNT = "TAX_AMOUNT";
73     public static final String JavaDoc PURCHASE_ID = "PURCHASE_ID";
74     public static final String JavaDoc FORCE_FLAG = "FORCE_FLAG";
75     public static final String JavaDoc ORIG_SEQ_NUM = "ORIG_SEQ_NUM";
76
77     // response fields
78
public static final String JavaDoc TERMINATION_STATUS = "TERMINATION_STATUS";
79     public static final String JavaDoc INTRN_SEQ_NUM = "INTRN_SEQ_NUM";
80     public static final String JavaDoc RESULT = "RESULT";
81     public static final String JavaDoc RESULT_CODE = "RESULT_CODE";
82     public static final String JavaDoc RESPONSE_TEXT = "RESPONSE_TEXT";
83
84     public static final String JavaDoc AUTH_CODE = "AUTH_CODE";
85     public static final String JavaDoc AVS_CODE = "AVS_CODE";
86     public static final String JavaDoc CVV2_CODE = "CVV2_CODE";
87     public static final String JavaDoc REFERENCE = "REFERENCE";
88     public static final String JavaDoc TRANS_DATE = "TRANS_DATE";
89     public static final String JavaDoc TRANS_TIME = "TRANS_TIME";
90     public static final String JavaDoc ORIG_TRANS_AMOUNT = "ORIG_TRANS_AMOUNT";
91
92     // IN/OUT validation array
93
protected static final String JavaDoc[] validOut = { TERMINATION_STATUS, INTRN_SEQ_NUM, RESULT, RESULT_CODE, RESPONSE_TEXT,
94                                                  AUTH_CODE, AVS_CODE, CVV2_CODE, REFERENCE, TRANS_DATE, TRANS_TIME,
95                                                  ORIG_TRANS_AMOUNT };
96
97     protected static final String JavaDoc[] validIn = { FUNCTION_TYPE, PAYMENT_TYPE, USER_ID, USER_PW, COMMAND, CLIENT_ID,
98                                                 ACCT_NUM, EXP_MONTH, EXP_YEAR, TRANS_AMOUNT, CARDHOLDER, TRACK_DATA,
99                                                 INVOICE, PRESENT_FLAG, CUSTOMER_STREET, CUSTOMER_ZIP, CVV2, TAX_AMOUNT,
100                                                 PURCHASE_ID, FORCE_FLAG, ORIG_TRANS_AMOUNT, ORIG_SEQ_NUM };
101
102     // mode definition
103
protected static final int MODE_OUT = 20;
104     protected static final int MODE_IN = 10;
105
106     // instance variables
107
protected LinkedMap document = null;
108     protected String JavaDoc host = null;
109     protected boolean ssl = false;
110     protected int port = 0;
111     protected int mode = 0;
112
113     public RitaApi(Map JavaDoc document) {
114         this.document = new LinkedMap(document);
115         this.mode = MODE_OUT;
116     }
117
118     public RitaApi() {
119         this.document = new LinkedMap();
120         this.mode = MODE_IN;
121     }
122
123     public RitaApi(String JavaDoc host, int port, boolean ssl) {
124         this();
125         this.host = host;
126         this.port = port;
127         this.ssl = ssl;
128     }
129
130     public void set(String JavaDoc name, Object JavaDoc value) {
131         if (!checkIn(name)) {
132             throw new IllegalArgumentException JavaDoc("Field [" + name + "] is not a valid IN parameter");
133         }
134
135         String JavaDoc objString = null;
136         try {
137             objString = (String JavaDoc) ObjectType.simpleTypeConvert(value, "java.lang.String", null, null);
138         } catch (GeneralException e) {
139             Debug.logError(e, module);
140             throw new IllegalArgumentException JavaDoc("Unable to convert value to String");
141         } catch (ClassCastException JavaDoc e) {
142             Debug.logError(e, module);
143             throw new IllegalArgumentException JavaDoc("Unable to convert value to String");
144         }
145         if (objString == null && value != null) {
146             throw new IllegalArgumentException JavaDoc("Unable to convert value to String");
147         } else if (objString == null) {
148             objString = "";
149         }
150
151         // append to the XML document
152
document.put(name, objString);
153     }
154
155     public String JavaDoc get(String JavaDoc name) {
156         if (!checkOut(name)) {
157             throw new IllegalArgumentException JavaDoc("Field [" + name + "] is not a valid OUT parameter");
158         }
159
160         return (String JavaDoc) document.get(name);
161     }
162
163     public String JavaDoc toString() {
164         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
165         MapIterator i = document.mapIterator();
166         while (i.hasNext()) {
167             String JavaDoc name = (String JavaDoc) i.next();
168             String JavaDoc value = (String JavaDoc) i.getValue();
169             buf.append(name);
170             buf.append(" ");
171             buf.append(value);
172             buf.append("\r\n");
173         }
174         buf.append(".\r\n");
175         return buf.toString();
176     }
177
178     public Map JavaDoc getDocument() {
179         return this.document;
180     }
181
182     public RitaApi send() throws IOException JavaDoc, GeneralException {
183         if (host == null || port == 0) {
184             throw new GeneralException("TCP transaction not supported without valid host/port configuration");
185         }
186
187         if (mode == MODE_IN) {
188             String JavaDoc stream = this.toString() + "..\r\n";
189             Debug.log("Sending - \n" + stream, module);
190             String JavaDoc urlString = "http://" + host + ":" + port;
191             HttpClient http = new HttpClient(urlString);
192             http.setDebug(true);
193
194             /*
195             SocketFactory sf = null;
196             if (ssl) {
197                 sf = SSLSocketFactory.getDefault();
198             } else {
199                 sf = SocketFactory.getDefault();
200             }
201             Socket sock = sf.createSocket(host, port);
202
203             // get the streams
204             BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
205             PrintStream ps = new PrintStream(sock.getOutputStream());
206
207             // send the request
208             ps.print(this.toString());
209             ps.flush();
210
211             // the output map
212             LinkedMap docMap = new LinkedMap();
213             String line;
214
215             // read the response
216             while ((line = br.readLine()) != null) {
217                 Debug.log(line, module);
218                 if (!line.trim().equals(".")) {
219                     String[] lineSplit = line.trim().split(" ");
220                     if (lineSplit != null && lineSplit.length == 2) {
221                         docMap.put(lineSplit[0], lineSplit[1]);
222                     } else {
223                         Debug.logWarning("Line split error - " + line, module);
224                     }
225                 } else {
226                     break;
227                 }
228             }
229             Debug.log("Reading finished.", module);
230
231             // send session finished signal
232             ps.print("..\r\n");
233             ps.flush();
234
235             // close the streams
236             ps.close();
237             br.close();
238             */

239
240             LinkedMap docMap = new LinkedMap();
241             String JavaDoc resp = null;
242             try {
243                 resp = http.post(stream);
244             } catch (HttpClientException e) {
245                 Debug.logError(e, module);
246                 throw new IOException JavaDoc(e.getMessage());
247             }
248
249             String JavaDoc[] lines = resp.split("\n");
250             for (int i = 0; i < lines.length; i++) {
251                 Debug.log(lines[i], module);
252                 if (!lines[i].trim().equals(".")) {
253                     String JavaDoc[] lineSplit = lines[i].trim().split(" ", 2);
254                     if (lineSplit != null && lineSplit.length == 2) {
255                         docMap.put(lineSplit[0], lineSplit[1]);
256                     } else {
257                         Debug.logWarning("Line split error - " + lines[i], module);
258                     }
259                 } else {
260                     break;
261                 }
262             }
263             RitaApi out = new RitaApi(docMap);
264             return out;
265         } else {
266             throw new IllegalStateException JavaDoc("Cannot send output object");
267         }
268     }
269
270     private boolean checkIn(String JavaDoc name) {
271         for (int i = 0; i < validOut.length; i++) {
272             if (name.equals(validOut[i])) {
273                 return false;
274             }
275         }
276         return true;
277     }
278
279     private boolean checkOut(String JavaDoc name) {
280         for (int i = 0; i < validIn.length; i++) {
281             if (name.equals(validIn[i])) {
282                 return false;
283             }
284         }
285         return true;
286     }
287 }
288
Popular Tags