This repository has been archived by the owner on May 13, 2022. It is now read-only.
/
LRExporter.java
456 lines (396 loc) · 11.3 KB
/
LRExporter.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/**
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navnorth.learningregistry;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import java.net.URL;
import java.net.MalformedURLException;
import org.json.*;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.io.IOException;
/**
* Exporter of data to a Learning Registry node
*
* @version 0.1
* @since 2011-11-17
* @author Todd Brown / Navigation North
* <br>
* Copyright © 2011 Navigation North Learning Solutions LLC
* <br>
* Licensed under the Apache License, Version 2.0 (the "License"); See LICENSE
* and README.md files distributed with this work for additional information
* regarding copyright ownership.
*/
public class LRExporter
{
// Static strings
private static final String publishUrlPrefix = "http://";
private static final String publishSSLUrlPrefix = "https://";
private static final String publishUrlPath = "/publish";
// Cofiguration variables
private int batchSize;
private String url;
private boolean publishOverSSL = false;
private String publishAuthUser;
private String publishAuthPassword;
// Booleans to track if configuration is complete
private boolean configured = false;
// Collection of encoded documents to be sent
private List<Object> docs = new ArrayList<Object>();
/**
* Creates the exporter object with the specified details
*
* @param batchSize the number of items to submit per batch to the Learning Registry node
* @param url location of the Learning Registry node to use for export
*/
public LRExporter(int batchSize, String url)
{
this.batchSize = batchSize;
this.url = url;
this.publishAuthUser = null;
this.publishAuthPassword = null;
}
/**
* Creates the exporter object with the specified details
* This version of the constructor sets the exporter up to send a username and password
*
* @param batchSize the number of items to submit per batch to the Learning Registry node
* @param url location of the Learning Registry node to use for export
* @param publishAuthUser user value for authentication
* @param publishAuthPassword password value for authentication
*/
public LRExporter(int batchSize, String url, String publishAuthUser, String publishAuthPassword)
{
this.batchSize = batchSize;
this.url = url;
this.publishAuthUser = publishAuthUser;
this.publishAuthPassword = publishAuthPassword;
}
/**
* Creates the exporter object with the specified details
* This version of the constructor sets the exporter up to use SSL
*
* @param batchSize the number of items to submit per batch to the Learning Registry node
* @param url location of the Learning Registry node to use for export
* @param publishAuthUser user value for authentication
* @param publishAuthPassword password value for authentication
* @param publishOverSSL whether or not to use SSL for publishing
*/
public LRExporter(int batchSize, String url, String publishAuthUser, String publishAuthPassword, boolean publishOverSSL)
{
this.batchSize = batchSize;
this.url = url;
this.publishAuthUser = publishAuthUser;
this.publishAuthPassword = publishAuthPassword;
this.publishOverSSL = publishOverSSL;
}
/**
* Attempt to configure the exporter with the values used in the constructor
* This must be called before an exporter can be used and after any setting of configuration values
*
* @throws LRException
*/
public void configure() throws LRException
{
// Trim or nullify strings
url = LRUtilities.nullifyBadInput(url);
publishAuthUser = LRUtilities.nullifyBadInput(publishAuthUser);
publishAuthPassword = LRUtilities.nullifyBadInput(publishAuthPassword);
// Throw an exception if any of the required fields are null
if (url == null)
{
throw new LRException(LRException.NULL_FIELD);
}
// Throw an error if the batch size is zero
if (batchSize == 0)
{
throw new LRException(LRException.BATCH_ZERO);
}
this.batchSize = batchSize;
// if SSL, use SSL prefix
if (publishOverSSL)
{
this.url = publishSSLUrlPrefix + url + publishUrlPath;
}
// Otherwise, use the non-SSL url
else
{
this.url = publishUrlPrefix + url + publishUrlPath;
}
this.publishAuthUser = publishAuthUser;
this.publishAuthPassword = publishAuthPassword;
this.configured = true;
}
/**
* Adds an envelope to the exporter
*
* @param envelope envelope to add to the exporter
* @throws LRException NOT_CONFIGURED
*/
public void addDocument(LREnvelope envelope) throws LRException
{
if(!configured)
{
throw new LRException(LRException.NOT_CONFIGURED);
}
docs.add(envelope.getSendableData());
}
/**
* Sends documents to the node defined in configuration
*
* @return List of LRResponse packages for each batch of documents sent to the node
* @throws LRException NOT_CONFIGURED, NO_DOCUMENTS, NO_RESPONSE, INVALID_RESPONSE, JSON_FAILED
*/
public List<LRResponse> sendData() throws LRException
{
// Throw an error if configuration has not been performed
if(!configured)
{
throw new LRException(LRException.NOT_CONFIGURED);
}
// Throw an error if no documents have been added for submission
if (docs.size() == 0)
{
throw new LRException(LRException.NO_DOCUMENTS);
}
List<LRResponse> responses = new ArrayList<LRResponse>();
JSONObject jsonObjSend = new JSONObject();
// Figure out how many batches need to be sent
int batches = (int)Math.ceil((float)docs.size() / batchSize);
// Send each batch and add the response to our return value
for (int i = 0; i < batches; i++)
{
int startIndex = i * batchSize;
int endIndex = startIndex + batchSize;
if (endIndex > docs.size())
{
endIndex = docs.size();
}
List<Object> batchDoc = docs.subList(startIndex, endIndex);
// Add this batch of documents to the batch parent document
try
{
jsonObjSend.put("documents", batchDoc);
}
catch (JSONException e)
{
throw new LRException(LRException.JSON_FAILED);
}
String jsonString = jsonObjSend.toString();
HttpResponse response;
String jsonError = "";
StringEntity se;
// Convert this batch into a string for submission
try
{
se = new StringEntity(jsonString);
}
catch (UnsupportedEncodingException e)
{
throw new LRException(LRException.JSON_FAILED);
}
// Send the string to the node
try
{
response = LRClient.executeJsonPost(url, se, publishAuthUser, publishAuthPassword);
}
catch (Exception e)
{
throw new LRException(LRException.NO_RESPONSE);
}
LRResponse responsePackage = null;
// Get the response from the node
if (response != null)
{
try
{
InputStream is = response.getEntity().getContent();
jsonError = IOUtils.toString(is, "UTF-8");
responsePackage = new LRResponse(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
}
catch (IOException e)
{
throw new LRException(LRException.INVALID_RESPONSE);
}
}
// Decode the response and prepare results for return
if (responsePackage != null)
{
try
{
JSONObject jsonObjRes = new JSONObject(jsonError);
boolean batchSuccess = false;
String batchError = "No error reported";
if (jsonObjRes.has("OK"))
{
batchSuccess = jsonObjRes.getBoolean("OK");
}
if (jsonObjRes.has("error"))
{
batchError = jsonObjRes.getString("error");
}
responsePackage.setBatchResponse(batchError, batchSuccess);
if (batchSuccess)
{
JSONArray jarry = jsonObjRes.getJSONArray("document_results");
for(int j = 0; j < jarry.length(); j++)
{
JSONObject job = jarry.getJSONObject(j);
String error = "";
String id = "";
boolean ok = false;
if (job.has("OK"))
ok = job.getBoolean("OK");
if (ok)
{
if (job.has("doc_ID"))
{
id = job.getString("doc_ID");
}
responsePackage.addResourceSuccess(id);
}
else
{
if (job.has("error"))
{
error = job.getString("error");
}
responsePackage.addResourceFailure(error);
}
}
}
}
catch (JSONException e)
{
//Return response package anyway, since it already has the basic information we need
}
responses.add(responsePackage);
}
}
return responses;
}
/**
* Sets the batchSize value
* Must call "configure" on exporter after setting this
*
* @param batchSize value
*/
public void setBatchSize(int batchSize)
{
this.batchSize = batchSize;
configured = false;
}
/**
* Get the batchSize value
*
* @return batchSize value
*/
public int getBatchSize()
{
return batchSize;
}
/**
* Sets the url value
* Must call "configure" on exporter after setting this
*
* @param url value
*/
public void setUrl(String url)
{
this.url = url;
configured = false;
}
/**
* Get the url value
*
* @return url value
*/
public String getUrl()
{
return url;
}
/**
* Sets the publishAuthUser value
* Must call "configure" on exporter after setting this
*
* @param publishAuthUser value
*/
public void setPublishAuthUser(String publishAuthUser)
{
this.publishAuthUser = publishAuthUser;
configured = false;
}
/**
* Get the publishAuthUser value
*
* @return publishAuthUser value
*/
public String getPublishAuthUser()
{
return publishAuthUser;
}
/**
* Sets the publishAuthPassword value
* Must call "configure" on exporter after setting this
*
* @param publishAuthPassword value
*/
public void setPublishAuthPassword(String publishAuthPassword)
{
this.publishAuthPassword = publishAuthPassword;
configured = false;
}
/**
* Get the publishAuthPassword value
*
* @return publishAuthPassword value
*/
public String getPublishAuthPassword()
{
return publishAuthPassword;
}
/**
* Get the publishOverSSL value
*
* @return publishOverSSL value
*/
public boolean getPublishOverSSL()
{
return publishOverSSL;
}
/**
* Sets the publishOverSSL value
* Must call "configure" on exporter after setting this
*
* @param publishOverSSL value
*/
public void setPublishOverSSL(boolean publishOverSSL)
{
this.publishOverSSL = publishOverSSL;
configured = false;
}
}