This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
/
ChargifyWrapper.java
266 lines (232 loc) · 10.7 KB
/
ChargifyWrapper.java
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
/*
* Copyright (c) 2009, GoodData Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and
* the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the distribution.
* * Neither the name of the GoodData Corporation nor the names of its contributors may be used to endorse
* or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.gooddata.chargify;
import com.gooddata.exception.HttpMethodException;
import com.gooddata.util.CSVWriter;
import com.gooddata.util.FileUtil;
import com.gooddata.util.NetUtil;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.*;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Java wrapper of selected MS Sugar CRM Online web services
*
* @author zd <zd@gooddata.com>
* @version 1.0
*/
public class ChargifyWrapper {
private static Logger l = Logger.getLogger(ChargifyWrapper.class);
private final static String HTTPS = "https://";
private final static String CHARGIFY_AUTH_REALM = "Chargify API";
private final static String CHARGIFY_ENDPOINT = "chargify.com";
private static final int PAGE_COUNT = 20;
// Chargify domain
private String domain;
// Chargify API token
private String apiToken;
private HttpClient client;
/**
* Constructor
*
* @param hostName Chargify domain
* @param apiToken Chargify password
*/
public ChargifyWrapper(String hostName, String apiToken) {
setDomain(hostName);
setApiToken(apiToken);
client = new HttpClient();
NetUtil.configureHttpProxy(client);
client.getHostConfiguration().setHost(getDomain());
client.getState().setCredentials(
new AuthScope(getDomain() + "." + CHARGIFY_ENDPOINT, 443, CHARGIFY_AUTH_REALM),
new UsernamePasswordCredentials(getApiToken(), "x")
);
}
public int getAllData(String entity, String[] fields, String csvFile)
throws IOException, SAXException, XPathExpressionException, ParserConfigurationException {
int cnt = 0;
CSVWriter cw = FileUtil.createUtf8CsvEscapingWriter(new File(csvFile));
try {
int pageNumber = 1;
String cookie = "";
boolean hasNext = true;
while (hasNext) {
List<Map<String, String>> ret = new ArrayList<Map<String, String>>();
getData(entity, pageNumber++, ret);
for (Map<String, String> m : ret) {
String[] row = new String[fields.length];
for (int i = 0; i < fields.length; i++) {
row[i] = m.get(fields[i]);
}
cw.writeNext(row);
}
int count = ret.size();
cnt += count;
cw.flush();
if (count < PAGE_COUNT)
hasNext = false;
}
} finally {
cw.close();
}
return cnt;
}
public void getData(String entity, int page, List<Map<String, String>> ret)
throws IOException, ParserConfigurationException, SAXException, XPathExpressionException {
String path = HTTPS + getDomain() + "." + CHARGIFY_ENDPOINT + "/" + entity + ".xml?page=" + page;
GetMethod m = createGetMethod(path);
int rc = executeHttpMethod(m);
if (rc == HttpStatus.SC_OK) {
String payload = readRespone(m);
//System.err.println(payload);
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(payload)));
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("//" + entity);
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
if (nodes != null && nodes.getLength() > 0) {
Node root = nodes.item(0);
NodeList rows = root.getChildNodes();
if (rows != null && rows.getLength() > 0) {
for (int i = 0; i < rows.getLength(); i++) {
Node row = rows.item(i);
if (row != null && row instanceof Element) {
NodeList columns = row.getChildNodes();
Map<String, String> r = new HashMap<String, String>();
if (columns != null && columns.getLength() > 0) {
for (int j = 0; j < columns.getLength(); j++) {
Node name = columns.item(j);
boolean isNested = false;
if (name != null && name instanceof Element) {
NodeList nestedNodes = name.getChildNodes();
if (nestedNodes != null && nestedNodes.getLength() > 0) {
for (int k = 0; k < nestedNodes.getLength(); k++) {
Node nestedNode = nestedNodes.item(k);
if (nestedNode != null && nestedNode instanceof Element) {
isNested = true;
Node value = nestedNode.getFirstChild();
if (value != null) {
r.put(name.getNodeName() + "_" + nestedNode.getNodeName(), value.getNodeValue());
} else {
r.put(name.getNodeName() + "_" + nestedNode.getNodeName(), "");
}
}
}
}
if (!isNested) {
Node value = name.getFirstChild();
if (value != null) {
r.put(name.getNodeName(), value.getNodeValue());
} else {
r.put(name.getNodeName(), "");
}
}
}
}
} else {
throw new IOException("getData: No columns in the row.");
}
ret.add(r);
}
}
}
}
} else {
System.err.println("HTTP response " + rc);
}
}
/**
* Executes HttpMethod and test if the response if 200(OK)
*
* @param method the HTTP method
* @return HTTP return code
* @throws com.gooddata.exception.HttpMethodException
*
*/
private int executeHttpMethod(HttpMethod method) throws HttpMethodException {
try {
client.executeMethod(method);
return method.getStatusCode();
} catch (HttpException e) {
l.debug("Error invoking Chargify REST API.", e);
throw new HttpMethodException("Error invoking Chargify REST API.", e);
} catch (IOException e) {
l.debug("Error invoking GoodData REST API.", e);
throw new HttpMethodException("Error invoking Chargify REST API.", e);
}
}
private String readRespone(HttpMethod m) throws IOException {
return m.getResponseBodyAsString();
}
private static GetMethod createGetMethod(String path) {
return configureHttpMethod(new GetMethod(path));
}
private static PostMethod createPostMethod(String path) {
return configureHttpMethod(new PostMethod(path));
}
private static DeleteMethod createDeleteMethod(String path) {
return configureHttpMethod(new DeleteMethod(path));
}
private static <T extends HttpMethod> T configureHttpMethod(T request) {
request.setRequestHeader("Content-Type", "text/xml");
request.setRequestHeader("Accept", "text/xml");
request.setRequestHeader("User-Agent", "GoodData CL/1.2.65");
return request;
}
// Getters and Setters
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getApiToken() {
return apiToken;
}
public void setApiToken(String apiToken) {
this.apiToken = apiToken;
}
}