-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathQuickBaseConnection.java
More file actions
executable file
·337 lines (304 loc) · 12.4 KB
/
QuickBaseConnection.java
File metadata and controls
executable file
·337 lines (304 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Copyright (c) 2009 Intuit Inc. All Rights reserved.
* -------------------------------------------------------------------------------------------------
*
* File name : QuickBaseConnection.java
* Created on : Dec 31, 2008
* @author Mirko Raner
* -------------------------------------------------------------------------------------------------
*
*
* *************************************************************************************************
*/
package com.intuit.quickbase.api;
import java.io.IOException;
import java.net.PasswordAuthentication;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import static com.intuit.quickbase.api.QuickBaseAPICall.API_Authenticate;
import static com.intuit.quickbase.api.QuickBaseAPICall.API_FindDBByName;
/**
* The class {@link QuickBaseConnection} represents an HTTP connection to a QuickBase server.
* A connection by itself is not specific to a particular database yet. The connection can be used
* to obtain one or more {@link QuickBaseDatabase} objects. The connection object represents a
* simulated HTTP "session", which is established by using cookies.
*
* @author Mirko Raner
* @version $Revision: 13 $ $Change: 714052 $
*/
public class QuickBaseConnection
{
private final static NameValuePair[] NO_PARAMETERS = {};
private static String HTTPS_QB_MAIN = "https://www.quickbase.com/db/main?"; //$NON-NLS-1$
private static String HTTPS_QB = "https://www.quickbase.com/db/"; //$NON-NLS-1$
private final static String USERNAME = "username"; //$NON-NLS-1$
private final static String PASSWORD = "password"; //$NON-NLS-1$
private final static String DBNAME = "dbname"; //$NON-NLS-1$
private final static String TICKET = "TICKET"; //$NON-NLS-1$
private final static String ACT = "act"; //$NON-NLS-1$
private final static String OK = "0"; //$NON-NLS-1$
private final static int FIRST = 0;
private final static char QUERY = '?';
private HttpClient httpClient;
private String appToken = null;
public QuickBaseConnection(PasswordAuthentication credentials) throws QuickBaseException
{
httpClient = new HttpClient();
execute(authenticate(credentials.getUserName(), credentials.getPassword()));
}
/**
* Finds a database by its name. If multiple databases exist with that name only the first
* database will be returned. In QuickBase, only database IDs are unique (but database names
* not necessarily).
*
* @param databaseName the name of the data base
* @return the {@link QuickBaseDatabase} object of the database
* @throws QuickBaseException if no database of that name could be found
*/
public QuickBaseDatabase findDBByName(String databaseName) throws QuickBaseException
{
return findDBsByName(databaseName).get(FIRST);
}
/**
* Finds all databases with a given name. In QuickBase, only database IDs are unique (but
* database names not necessarily).
*
* @param databaseName the name of the data base
* @return a {@link List} of {@link QuickBaseDatabase} objects
* @throws QuickBaseException if no database of that name could be found
*/
public List<QuickBaseDatabase> findDBsByName(String databaseName) throws QuickBaseException
{
Document response = execute(method(API_FindDBByName, dbname(databaseName)));
NodeList dbids;
try
{
dbids = (NodeList)QuickBaseXPath.QDBAPI_DBID.evaluate(response, XPathConstants.NODESET);
}
catch (XPathExpressionException xpathException)
{
throw new QuickBaseException(xpathException);
}
// TODO: com.intuit.tcr.utilities.NodeListIterable would come in handy here:
//
int numberOfChildren = dbids.getLength();
List<QuickBaseDatabase> databases = new ArrayList<QuickBaseDatabase>();
for (int index = 0; index < numberOfChildren; index++)
{
Node dbidNode = dbids.item(index);
String dbid = dbidNode.getTextContent();
databases.add(new QuickBaseDatabase(this, dbid));
}
return databases;
}
/**
* Returns the "ticket" for the current connection. The ticket is sent as a cookie to
* authenticate QuickBase users.
*
* @return the ticket string or <code>null</code> if no ticket is available
*/
public String getTicket()
{
for (Cookie cookie: httpClient.getState().getCookies())
{
if (TICKET.equals(cookie.getName()))
{
//return TICKET + '=' + cookie.getValue();
return cookie.getValue();
}
}
return null;
}
public void setAppToken(String appToken)
{
this.appToken = appToken;
}
public void setQBLocation(String https_qb, String https_qb_main)
{
QuickBaseConnection.HTTPS_QB = https_qb;
QuickBaseConnection.HTTPS_QB_MAIN = https_qb_main;
}
/**
* Executes a {@link QuickBaseAPICall} for a certain database and returns the database response
* as a SAX {@link InputSource}.
*
* @param dbid the database ID
* @param call the {@link QuickBaseAPICall} to be executed
* @param parameters {@link NameValuePair}s for additional parameters
* @return a SAX {@link InputSource} that contains the server's response
* @throws QuickBaseException if the execution was unsuccessful
*/
public InputSource execute(String dbid, QuickBaseAPICall call, NameValuePair... parameters)
throws QuickBaseException
{
HttpMethod method = new GetMethod(HTTPS_QB + dbid + QUERY);
NameValuePair[] query = new NameValuePair[parameters.length+1];
query[FIRST] = act(call);
System.arraycopy(parameters, 0, query, 1, parameters.length);
method.setQueryString(query);
try
{
httpClient.executeMethod(method);
//
// TODO: evaluate the returned HTTP response code
return new InputSource(method.getResponseBodyAsStream());
}
catch (IOException exception)
{
throw new QuickBaseException(exception);
}
}
/**
* Executes a {@link QuickBaseAPICall} for a certain QuickBase object as an XML payload
* and returns the response as a SAX {@link InputSource}.
* @param qbid The id of the object the call is acting upon
* @param the {@link QuickBaseAPICall} to be executed
* @param elements the XML elements to put into the payload
* @return a SAX {@link InputSource} that contains the server's response
* @throws QuickBaseException if the execution was unsuccessful
*/
public InputSource executeXml(String qbid, QuickBaseAPICall call, String... elements)
throws QuickBaseException
{
StringBuffer xml = new StringBuffer();
xml.append("<qdbapi>\n");
xml.append("<ticket>");
xml.append(getTicket());
xml.append("</ticket>");
if (appToken != null)
{
xml.append("<apptoken>");
xml.append(appToken);
xml.append("</apptoken>");
}
for (String element: elements) {
xml.append(element);
xml.append("\n");
}
xml.append("</qdbapi>\n");
String payload = xml.toString();
try
{
PostMethod method = new PostMethod(HTTPS_QB + qbid + QUERY);
NameValuePair[] query = new NameValuePair[1];
query[0] = act(call);
method.setQueryString(query);
StringRequestEntity entity = new StringRequestEntity(payload, "application/xml", "UTF-8");
method.setRequestEntity(entity);
httpClient.executeMethod(method);
//
// TODO: evaluate the returned HTTP response code
return new InputSource(method.getResponseBodyAsStream());
}
catch (IOException exception)
{
throw new QuickBaseException(exception);
}
}
/**
* Executes a {@link QuickBaseAPICall} without additional parameters for a certain database and
* returns the database response as a SAX {@link InputSource}.
*
* @param dbid the database ID
* @param call the {@link QuickBaseAPICall} to be executed
* @return a SAX {@link InputSource} that contains the server's response
* @throws QuickBaseException if the execution was unsuccessful
*/
public InputSource execute(String dbid, QuickBaseAPICall call)
throws QuickBaseException
{
return execute(dbid, call, NO_PARAMETERS);
}
//------------------------------------- PRIVATE SECTION --------------------------------------//
private Document execute(HttpMethod authenticate) throws QuickBaseException
{
try
{
httpClient.executeMethod(authenticate);
Document response = getResponse(authenticate);
String errorCode = QuickBaseXPath.QDBAPI_ERRCODE.evaluate(response);
if (OK.equals(errorCode))
{
return response;
}
String errorText = QuickBaseXPath.QDBAPI_ERRTEXT.evaluate(response);
errorText += " (error code " + errorCode + ')'; //$NON-NLS-1$
throw new QuickBaseException(errorText);
}
catch (IOException ioException)
{
throw new QuickBaseException(ioException);
}
catch (SAXException saxException)
{
throw new QuickBaseException(saxException);
}
catch (XPathExpressionException xpathException)
{
throw new QuickBaseException(xpathException);
}
catch (ParserConfigurationException parserException)
{
throw new QuickBaseException(parserException);
}
}
private static Document getResponse(HttpMethod method)
throws IOException, SAXException, ParserConfigurationException
{
InputSource inputSource = new InputSource(method.getResponseBodyAsStream());
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
return documentBuilder.parse(inputSource);
}
private static HttpMethod method(QuickBaseAPICall call, NameValuePair... parameters)
{
HttpMethod method = new GetMethod(HTTPS_QB_MAIN);
NameValuePair[] query = new NameValuePair[parameters.length+1];
System.arraycopy(parameters, 0, query, 1, parameters.length);
query[FIRST] = act(call);
method.setQueryString(query);
return method;
}
private static HttpMethod authenticate(String username, char[] password)
{
GetMethod authenticate = new GetMethod(HTTPS_QB_MAIN);
return query(authenticate, act(API_Authenticate), username(username), password(password));
}
private static HttpMethod query(HttpMethod method, NameValuePair... parameters)
{
method.setQueryString(parameters);
return method;
}
private static NameValuePair act(QuickBaseAPICall action)
{
return new NameValuePair(ACT, action.toString());
}
private static NameValuePair username(String username)
{
return new NameValuePair(USERNAME, username);
}
private static NameValuePair password(char[] password)
{
return new NameValuePair(PASSWORD, String.valueOf(password));
}
private static NameValuePair dbname(String dbname)
{
return new NameValuePair(DBNAME, dbname);
}
}