1 package org.kuali.ole.docstore.common.client;
2
3 import org.apache.commons.io.FileUtils;
4 import org.apache.commons.io.IOUtils;
5 import org.apache.http.HttpEntity;
6 import org.apache.http.HttpResponse;
7 import org.apache.http.ProtocolVersion;
8 import org.apache.http.StatusLine;
9 import org.apache.http.client.HttpClient;
10 import org.apache.http.client.methods.*;
11 import org.apache.http.entity.StringEntity;
12 import org.apache.http.entity.mime.Header;
13 import org.apache.http.entity.mime.MultipartEntity;
14 import org.apache.http.entity.mime.content.FileBody;
15 import org.apache.http.impl.client.DefaultHttpClient;
16 import org.apache.http.message.BasicHttpResponse;
17 import org.apache.http.message.BasicStatusLine;
18 import org.apache.http.util.EntityUtils;
19 import org.kuali.ole.docstore.common.document.*;
20 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecords;
21 import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
22 import org.kuali.ole.docstore.common.exception.BibImportResponse;
23 import org.kuali.ole.docstore.common.exception.DocstoreExceptionProcessor;
24 import org.kuali.ole.docstore.common.find.FindParams;
25 import org.kuali.ole.docstore.common.search.BrowseParams;
26 import org.kuali.ole.docstore.common.search.SearchParams;
27 import org.kuali.ole.docstore.common.search.SearchResponse;
28 import org.kuali.rice.core.api.config.property.ConfigContext;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import java.io.*;
33 import java.net.URL;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.zip.ZipEntry;
39 import java.util.zip.ZipOutputStream;
40
41
42
43
44
45
46
47
48
49
50
51 public class DocstoreRestClient implements DocstoreClient {
52
53
54 private static String DOCSTORE_URL = ConfigContext.getCurrentContextConfig().getProperty("ole.docstore.Documentrest.url");
55 private static String BIB_URL = "bib/doc/";
56 private static String RELOAD_URL = "config/reload";
57 private static String BIB_CONTENT_URL = "bib/";
58 private static String SEARCH_URL = "search/";
59 private static String HOLDINGS_URL = "holdings/doc/";
60 private static String ITEMS_URL = "item/doc/";
61 private static String ITEM_MAP_URL = "item/doc/map";
62 private static String ITEM_CONTENT_URL = "item/";
63 private static String HOLDINGS_TREE_URL = "holdings/doc/tree/";
64 private static String HOLDINGS_TREES_URL = "holdings/doc/trees/";
65 private static String HOLDINGS_TREES_CONTENT_URL = "holdings/tree/";
66 private static String BIB_TREE_URL = "bib/doc/tree/";
67 private static String BIB_TREES_URL = "bib/doc/trees/";
68 private static String FIND_URL = "/doc/find";
69 private static String BROWSE_URL = "browse/";
70 private static String BOUND_URL = "/bound";
71 private static String ANALYTIC_URL = "/analytic";
72 private static String BREAK_ANALYTIC_URL = "/breakAnalytic";
73 private static String TRANSFER_URL = "/transfer/";
74 private static String LICENSES_URL = "license/";
75 private static String LICENSES_TREES_URL = "license/trees/";
76 private static String BULK_UPDATE = "/bulkUpdate";
77
78 private Logger logger = LoggerFactory.getLogger(DocstoreRestClient.class);
79
80 @Override
81 public void createBib(Bib bib) {
82 String requestBody = bib.serialize(bib);
83 RestResponse restResponse = postRequest(requestBody, BIB_URL);
84 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
85 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
86 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
87 }
88 String[] responseString = restResponse.getResponseBody().split("/");
89 if (responseString.length == 4) {
90 bib.setId(responseString[3]);
91 }
92 }
93 }
94
95 @Override
96 public void createHoldings(Holdings holdings) {
97 String requestBody = holdings.serialize(holdings);
98 RestResponse restResponse = postRequest(requestBody, HOLDINGS_URL);
99 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
100 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
101 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
102 }
103 String[] responseString = restResponse.getResponseBody().split("/");
104 if (responseString.length == 4) {
105 holdings.setId(responseString[3]);
106 }
107 }
108 }
109
110 @Override
111 public void createItem(Item item) {
112 String requestBody = item.serialize(item);
113 RestResponse restResponse = postRequest(requestBody, ITEMS_URL);
114 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
115 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
116 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
117 }
118 String[] responseString = restResponse.getResponseBody().split("/");
119 if (responseString.length == 4) {
120 item.setId(responseString[3]);
121 }
122 }
123 }
124
125 @Override
126 public void createHoldingsTree(HoldingsTree holdingsTree) {
127 String requestBody = holdingsTree.serialize(holdingsTree);
128 RestResponse restResponse = postRequest(requestBody, HOLDINGS_TREE_URL);
129 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
130 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
131 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
132 }
133 String[] responseString = restResponse.getResponseBody().split("/");
134 holdingsTree.getHoldings().setId(responseString[4]);
135 if (responseString.length > 4) {
136 int j = 0;
137 for (int i = 5; i < responseString.length; i++) {
138 if (responseString[i] != null) {
139 holdingsTree.getItems().get(j).setId(responseString[i]);
140 }
141 j++;
142 }
143 }
144 }
145 }
146
147 @Override
148 public BibImportResponse createHoldingsTrees(HoldingsTrees holdingsTrees) {
149 BibImportResponse bibImportResponse = new BibImportResponse();
150 String requestBody = holdingsTrees.serialize(holdingsTrees);
151 RestResponse restResponse = postRequest(requestBody, HOLDINGS_TREES_URL);
152 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
153 bibImportResponse = (BibImportResponse) BibImportResponse.deserialize(restResponse.getResponseBody());
154 }
155
156 return bibImportResponse;
157 }
158
159
160 @Override
161 public void createBibTree(BibTree bibTree) {
162 String requestBody = bibTree.serialize(bibTree);
163 RestResponse restResponse = postRequest(requestBody, BIB_TREE_URL);
164 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
165 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
166 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
167 }
168 String[] responseString = restResponse.getResponseBody().split("/");
169 if (responseString.length == 5) {
170 bibTree.getBib().setId(responseString[4]);
171 }
172 }
173 }
174
175 @Override
176 public BibImportResponse createBibTrees(BibTrees bibTrees) {
177 BibImportResponse bibImportResponse = new BibImportResponse();
178 String requestBody = BibTrees.serialize(bibTrees);
179 RestResponse restResponse = postRequest(requestBody, BIB_TREES_URL);
180 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
181 bibImportResponse = (BibImportResponse) BibImportResponse.deserialize(restResponse.getResponseBody());
182 }
183
184 return bibImportResponse;
185 }
186
187 @Override
188 public List<Bib> retrieveBibs(List<String> bibIds) {
189
190 RestResponse restResponse = getBibResponse(buildQueryString(bibIds, "bibId"), BIB_URL);
191 Bibs bibsObj = new Bibs();
192 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
193 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
194 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
195 } else {
196 bibsObj = (Bibs) Bibs.deserialize(restResponse.getResponseBody());
197
198 }
199 }
200 return bibsObj.getBibs();
201
202 }
203
204 @Override
205 public List<Item> retrieveItems(List<String> itemIds) {
206 RestResponse restResponse = getRequest(buildQueryString(itemIds, "itemId"), ITEMS_URL);
207 Items itemsObj = new Items();
208 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
209 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
210 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
211 } else {
212 itemsObj = (Items) Items.deserialize(restResponse.getResponseBody());
213 }
214 }
215 return itemsObj.getItems();
216 }
217
218 @Override
219 public HashMap<String, Item> retrieveItemMap(List<String> itemIds) {
220 RestResponse restResponse = getRequest(buildQueryString(itemIds, "itemId"), ITEM_MAP_URL);
221 ItemMap itemsObj = new ItemMap();
222 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
223 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
224 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
225 } else {
226 itemsObj = (ItemMap) ItemMap.deserialize(restResponse.getResponseBody());
227 }
228 }
229 return itemsObj.getItemMap();
230 }
231
232 @Override
233 public void createLicenses(Licenses licenses) {
234
235 File bagitFile = null;
236 try {
237 bagitFile = createBagItfile(licenses);
238 } catch (IOException e) {
239 e.printStackTrace();
240 }
241 RestResponse restResponse = postMultiPartRequest(bagitFile);
242 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
243 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
244 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
245 }
246 String[] responseString = restResponse.getResponseBody().split("/");
247 int idPos = 2;
248 for (License license : licenses.getLicenses()) {
249 license.setId(responseString[idPos]);
250 idPos += 1;
251 }
252 }
253 bagitFile.delete();
254 }
255
256 @Override
257 public License retrieveLicense(String id) {
258 RestResponse restResponse = getRequest(id, LICENSES_URL);
259 License license = new License();
260 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
261 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
262 logger.info(" rest response " + restResponse.getResponseBody());
263 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
264 } else {
265 license = (License) license.deserialize(restResponse.getResponseBody());
266 }
267 }
268 return license;
269
270 }
271
272 private RestResponse postMultiPartRequest(File bagitFile) {
273 HttpClient httpclient = new DefaultHttpClient();
274 FileBody uploadFilePart = new FileBody(bagitFile);
275 MultipartEntity reqEntity = new MultipartEntity();
276 reqEntity.addPart("upload-file", uploadFilePart);
277 HttpPost httpPost = new HttpPost(DOCSTORE_URL + LICENSES_URL);
278 httpPost.setEntity(reqEntity);
279 httpPost.addHeader("multipart/form-data", "text/xml");
280 RestResponse restResponse = new RestResponse();
281 try {
282 EntityUtils.consume(reqEntity);
283 HttpResponse response = httpclient.execute(httpPost);
284 restResponse.setResponse(response);
285 restResponse.setResponseBody(getEncodeEntityValue(response.getEntity()));
286
287 } catch (Exception e) {
288 e.printStackTrace();
289 }
290
291 return restResponse;
292 }
293
294 private File createBagItfile(Licenses licenses) throws IOException {
295 File bagitFolder = new File(FileUtils.getTempDirectoryPath() + File.separator + "bagit");
296 String licensesXml = licenses.serialize(licenses);
297 File licensesXmlFile = new File(bagitFolder + File.separator + "licenses.xml");
298 FileUtils.writeStringToFile(licensesXmlFile, licensesXml);
299 for (License license : licenses.getLicenses()) {
300 if (license instanceof LicenseAttachment) {
301 LicenseAttachment licenseAttachment = (LicenseAttachment) license;
302 File contentFile = new File(licenseAttachment.getFilePath() + File.separator + licenseAttachment.getFileName());
303 FileUtils.copyFileToDirectory(contentFile, bagitFolder);
304 }
305 }
306
307 File bagitFile = createZipFile(bagitFolder);
308 deleteFiles(bagitFile.listFiles());
309 bagitFolder.delete();
310 return bagitFile;
311 }
312
313 @Override
314 public Licenses retrieveLicenses(List<String> ids) {
315 String licenseIds = buildIds(ids, "licenseIds=");
316 RestResponse restResponse = getRequest(licenseIds, LICENSES_URL);
317 Licenses licenses = new Licenses();
318 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
319 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
320 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
321 } else {
322 licenses = (Licenses) Licenses.deserialize(restResponse.getResponseBody());
323 }
324 }
325 return licenses;
326 }
327
328 @Override
329 public void updateLicense(License license) {
330 String requestBody = license.serialize(license);
331 RestResponse restResponse = putRequest(requestBody, LICENSES_URL);
332 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
333 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
334 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
335 }
336 }
337 }
338
339 @Override
340 public void updateLicenses(Licenses licenses) {
341 String requestBody = Licenses.serialize(licenses);
342 RestResponse restResponse = putRequest(requestBody, LICENSES_TREES_URL);
343 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
344 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
345 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
346 }
347 }
348
349 }
350
351 @Override
352 public void deleteLicense(String id) {
353 RestResponse restResponse = deleteRequest(id, LICENSES_URL);
354 if (restResponse.getResponse().getStatusLine().getStatusCode() != 200) {
355 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
356 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
357 }
358 }
359
360 }
361
362 @Override
363 public void createAnalyticsRelation(String seriesHoldingsId, List<String> itemIds) {
364
365 String requestBody = buildIds(itemIds, "");
366 RestResponse restResponse = postRequest(requestBody, HOLDINGS_URL + seriesHoldingsId + ANALYTIC_URL);
367 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
368 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
369 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
370 }
371 }
372 }
373
374 @Override
375 public void bulkUpdateHoldings(Holdings holdings, List<String> holdingIds, String canUpdateStaffOnlyFlag) {
376 String requestBody = holdings.serialize(holdings);
377 String holdingUpdateIds = buildIds(holdingIds, "");
378 RestResponse restResponse = putRequest(holdingUpdateIds + "\n" + canUpdateStaffOnlyFlag + "\n" + requestBody, HOLDINGS_URL + "/" + BULK_UPDATE);
379 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
380 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
381 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
382 }
383 }
384 }
385
386 @Override
387 public void bulkUpdateItem(Item item, List<String> itemIds, String canUpdateStaffOnlyFlag) {
388 String requestBody = item.serialize(item);
389 String itemUpdateIds = buildIds(itemIds, "");
390 RestResponse restResponse = putRequest(itemUpdateIds + "\n" + canUpdateStaffOnlyFlag + "\n" + requestBody, ITEMS_URL + "/" + BULK_UPDATE);
391 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
392 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
393 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
394 }
395 }
396 }
397
398 @Override
399 public void breakAnalyticsRelation(String seriesHoldingsId, List<String> itemIds) {
400 String requestBody = buildIds(itemIds, "");
401 RestResponse restResponse = postRequest(requestBody, HOLDINGS_URL + seriesHoldingsId + BREAK_ANALYTIC_URL);
402 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
403 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
404 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
405 }
406 }
407 }
408
409 @Override
410 public Bib retrieveBib(String bibId) {
411 String reqParam = "?bibId=" + bibId;
412 RestResponse restResponse = getBibResponse(reqParam, BIB_URL);
413 Bib bib = new Bib();
414 bib = (Bib) bib.deserialize(restResponse.getResponseBody());
415 return bib;
416 }
417
418 @Override
419 public BibMarcRecords retrieveBibContent(List<String> bibIds) {
420 RestResponse restResponse = getBibResponse(buildQueryString(bibIds, "bibId"), BIB_CONTENT_URL);
421 BibMarcRecords bibMarcRecords = new BibMarcRecords();
422 BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
423 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
424 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
425 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
426 } else {
427 bibMarcRecords = bibMarcRecordProcessor.fromXML(restResponse.getResponseBody());
428 }
429 }
430 return bibMarcRecords;
431 }
432
433 @Override
434 public HoldingsTrees retrieveHoldingsTrees(List<String> bibIds) {
435 RestResponse restResponse = getRequest(buildQueryString(bibIds, "bibId"), HOLDINGS_TREES_CONTENT_URL);
436 HoldingsTrees holdingsTrees = new HoldingsTrees();
437 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
438 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
439 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
440 } else {
441 holdingsTrees = (HoldingsTrees) holdingsTrees.deserialize(restResponse.getResponseBody());
442 }
443 }
444 return holdingsTrees;
445 }
446
447 @Override
448 public Item retrieveItemByBarcode(String barcode) {
449 RestResponse restResponse = getRequest("?barcode=" + barcode, ITEM_CONTENT_URL);
450 Item item = new Item();
451 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
452 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
453 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
454 } else {
455 item = (Item) item.deserialize(restResponse.getResponseBody());
456 }
457 }
458 return item;
459 }
460
461 @Override
462 public void reloadConfiguration() {
463 RestResponse restResponse = getRequest("", RELOAD_URL);
464 }
465
466 @Override
467 public Holdings retrieveHoldings(String holdingsId) {
468 String reqParam = "?holdingsId=" + holdingsId;
469 RestResponse restResponse = getRequest(reqParam, HOLDINGS_URL);
470 Holdings holdings = new PHoldings();
471 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
472 holdings = (Holdings) holdings.deserialize(restResponse.getResponseBody());
473 if (!holdings.getHoldingsType().equalsIgnoreCase("print")) {
474 holdings = new EHoldings();
475 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
476 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
477 } else {
478 holdings = (EHoldings) holdings.deserialize(restResponse.getResponseBody());
479 }
480 }
481 }
482 return holdings;
483 }
484
485 @Override
486 public Item retrieveItem(String itemId) {
487 String queryParam = "?itemId=" + itemId;
488 RestResponse restResponse = getRequest(queryParam, ITEMS_URL);
489 Item item = new Item();
490 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
491 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
492 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
493 } else {
494 item = (Item) item.deserialize(restResponse.getResponseBody());
495 }
496 }
497 return item;
498 }
499
500 @Override
501 public HoldingsTree retrieveHoldingsTree(String holdingsId) {
502 HoldingsTree holdingsTree = new HoldingsTree();
503 String reqParam = "?holdingsId=" + holdingsId;
504 RestResponse restResponse = getRequest(reqParam, HOLDINGS_TREE_URL);
505 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
506 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
507 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
508 } else {
509 holdingsTree = (HoldingsTree) holdingsTree.deserialize(restResponse.getResponseBody());
510 }
511 }
512 return holdingsTree;
513 }
514
515 @Override
516 public BibTree retrieveBibTree(String bibId) {
517 BibTree bibTree = new BibTree();
518 String reqParam = "?bibId=" + bibId;
519 RestResponse restResponse = getBibResponse(reqParam, BIB_TREE_URL);
520 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
521 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
522 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
523 } else {
524 bibTree = (BibTree) bibTree.deserialize(restResponse.getResponseBody());
525 }
526 }
527 return bibTree;
528 }
529
530 @Override
531 public BibTrees retrieveBibTrees(List<String> bibIds) {
532 BibTrees bibTrees = new BibTrees();
533
534 RestResponse restResponse = getBibResponse(buildQueryString(bibIds, "bibId"), BIB_TREES_URL);
535 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
536 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
537 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
538 } else {
539 bibTrees = (BibTrees) BibTrees.deserialize(restResponse.getResponseBody());
540 }
541 }
542
543 return bibTrees;
544 }
545
546 @Override
547 public Bib updateBib(Bib bib) {
548 String requestBody = bib.serialize(bib);
549 RestResponse restResponse = putRequest(requestBody, BIB_URL);
550 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
551 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
552 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
553 }
554 }
555 return bib;
556 }
557
558 @Override
559 public BibImportResponse updateBibs(List<Bib> bibs) {
560 BibImportResponse bibImportResponse = new BibImportResponse();
561 Bibs bibsObj = new Bibs();
562 bibsObj.getBibs().addAll(bibs);
563 String requestBody = Bibs.serialize(bibsObj);
564 RestResponse restResponse = putRequest(requestBody, BIB_TREES_URL);
565 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
566 bibImportResponse = (BibImportResponse) BibImportResponse.deserialize(restResponse.getResponseBody());
567 }
568 return bibImportResponse;
569 }
570
571 @Override
572 public Holdings updateHoldings(Holdings holdings) {
573 String requestBody = holdings.serialize(holdings);
574 RestResponse restResponse = putRequest(requestBody, HOLDINGS_URL);
575 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
576 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
577 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
578 }
579 }
580 return holdings;
581 }
582
583 @Override
584 public Item updateItem(Item item) {
585 String requestBody = item.serialize(item);
586 RestResponse restResponse = putRequest(requestBody, ITEMS_URL);
587 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
588 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
589 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
590 }
591 }
592 return item;
593 }
594
595 @Override
596 public String updateItemByBarcode(String barcode, String requestBody) {
597 RestResponse restResponse = patchRequest(requestBody, ITEM_CONTENT_URL + "?barcode=" + barcode);
598 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
599 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
600 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
601 }
602 }
603 return restResponse.getResponseBody();
604 }
605
606 @Override
607 public String patchItem(String requestBody) {
608 RestResponse restResponse = patchRequest(requestBody, ITEM_CONTENT_URL);
609 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
610 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
611 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
612 }
613 }
614 return restResponse.getResponseBody();
615 }
616
617 @Override
618 public void deleteBib(String bibId) {
619 String reqParam = "?bibId=" + bibId;
620 RestResponse restResponse = deleteRequest(reqParam, BIB_URL);
621 if (restResponse.getResponse().getStatusLine().getStatusCode() != 200) {
622 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
623 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
624 }
625 }
626 }
627
628 @Override
629 public void deleteHoldings(String holdingsId) {
630 String reqParam = "?holdingsId=" + holdingsId;
631 RestResponse restResponse = deleteRequest(reqParam, HOLDINGS_URL);
632 if (restResponse.getResponse().getStatusLine().getStatusCode() != 200) {
633 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
634 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
635 }
636 }
637 }
638
639 @Override
640 public void deleteItem(String itemId) {
641 String queryString = "?itemId=" + itemId;
642 RestResponse restResponse = deleteRequest(queryString, ITEMS_URL);
643 if (restResponse.getResponse().getStatusLine().getStatusCode() != 200) {
644 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
645 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
646 }
647 }
648 }
649
650 @Override
651 public SearchResponse search(SearchParams searchParams) {
652 String requestBody = searchParams.serialize(searchParams);
653 RestResponse restResponse = postRequest(requestBody, SEARCH_URL);
654 SearchResponse searchResponse = new SearchResponse();
655 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
656 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
657 logger.info("DocstoreRestClient search : " + restResponse.getResponseBody());
658 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
659 } else {
660 searchResponse = (SearchResponse) searchResponse.deserialize(restResponse.getResponseBody());
661 }
662 }
663 return searchResponse;
664 }
665
666 @Override
667 public Bib findBib(Map<String, String> map) {
668 Bib bib = new Bib();
669 FindParams findParams = buildFindParams(map);
670 String requestBody = findParams.serialize(findParams);
671 RestResponse restResponse = postRequest(requestBody, BIB_URL + FIND_URL);
672 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
673 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
674 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
675 } else {
676 bib = (Bib) bib.deserialize(restResponse.getResponseBody());
677 }
678 }
679 return bib;
680 }
681
682 @Override
683 public BibTree findBibTree(Map<String, String> map) {
684 BibTree bibTree = new BibTree();
685 FindParams findParams = buildFindParams(map);
686 String requestBody = findParams.serialize(findParams);
687 RestResponse restResponse = postRequest(requestBody, BIB_TREE_URL + FIND_URL);
688 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
689 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
690 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
691 } else {
692 bibTree = (BibTree) bibTree.deserialize(restResponse.getResponseBody());
693 }
694 }
695 return bibTree;
696 }
697
698 @Override
699 public Holdings findHoldings(Map<String, String> map) {
700 Holdings holdings = new PHoldings();
701 FindParams findParams = buildFindParams(map);
702 String requestBody = findParams.serialize(findParams);
703 RestResponse restResponse = postRequest(requestBody, HOLDINGS_URL + FIND_URL);
704 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
705 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
706 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
707 } else {
708 holdings = (Holdings) holdings.deserialize(restResponse.getResponseBody());
709 }
710 }
711 return holdings;
712 }
713
714 @Override
715 public HoldingsTree findHoldingsTree(Map<String, String> map) {
716 HoldingsTree holdingsTree = new HoldingsTree();
717 FindParams findParams = buildFindParams(map);
718 String requestBody = findParams.serialize(findParams);
719 RestResponse restResponse = postRequest(requestBody, HOLDINGS_TREE_URL + FIND_URL);
720 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
721 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
722 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
723 } else {
724 holdingsTree = (HoldingsTree) holdingsTree.deserialize(restResponse.getResponseBody());
725 }
726 }
727 return holdingsTree;
728 }
729
730 @Override
731 public Item findItem(Map<String, String> map) {
732 Item item = new Item();
733 FindParams findParams = buildFindParams(map);
734 String requestBody = findParams.serialize(findParams);
735 RestResponse restResponse = postRequest(requestBody, ITEMS_URL + FIND_URL);
736 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
737 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
738 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
739 } else {
740 item = (Item) item.deserialize(restResponse.getResponseBody());
741 }
742 }
743 return item;
744 }
745
746 @Override
747 public SearchResponse browseItems(BrowseParams browseParams) {
748 String requestBody = browseParams.serialize(browseParams);
749 RestResponse restResponse = postRequest(requestBody, BROWSE_URL + ITEMS_URL);
750 SearchResponse searchResponse = new SearchResponse();
751 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
752 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
753 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
754 } else {
755 searchResponse = (SearchResponse) searchResponse.deserialize(restResponse.getResponseBody());
756 }
757 }
758 return searchResponse;
759 }
760
761 @Override
762 public SearchResponse browseHoldings(BrowseParams browseParams) {
763 String requestBody = browseParams.serialize(browseParams);
764 RestResponse restResponse = postRequest(requestBody, BROWSE_URL + HOLDINGS_URL);
765 SearchResponse searchResponse = new SearchResponse();
766 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
767 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
768 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
769 } else {
770 searchResponse = (SearchResponse) searchResponse.deserialize(restResponse.getResponseBody());
771 }
772 }
773 return searchResponse;
774 }
775
776 @Override
777 public void boundWithBibs(String holdingsId, List<String> bibIds) {
778 String requestBody = buildIds(bibIds, "");
779 RestResponse restResponse = postRequest(requestBody, HOLDINGS_URL + holdingsId + BOUND_URL);
780 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
781 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
782 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
783 }
784 }
785 }
786
787 @Override
788 public void transferHoldings(List<String> holdingsIds, String bibId) {
789
790 RestResponse restResponse = postRequest(" ", BIB_URL + bibId + TRANSFER_URL + buildQueryString(holdingsIds, "holdingsId"));
791 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
792 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
793 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
794 }
795 }
796 }
797
798 @Override
799 public void transferItems(List<String> itemIds, String holdingsId) {
800 String reqParam = buildQueryString(itemIds, "itemId");
801 RestResponse restResponse = postRequest(" ", HOLDINGS_URL + holdingsId + TRANSFER_URL + reqParam);
802 if (restResponse.getResponse().getStatusLine().getStatusCode() == 200) {
803 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
804 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
805 }
806 }
807 }
808
809 @Override
810 public void deleteBibs(List<String> bibIds) {
811 RestResponse restResponse = deleteRequest(buildQueryString(bibIds, "bibId"), BIB_URL);
812 if (restResponse.getResponse().getStatusLine().getStatusCode() != 200) {
813 if (restResponse.getResponseBody().startsWith("<org.kuali.ole.docstore.common.exception")) {
814 throw DocstoreExceptionProcessor.fromXML(restResponse.getResponseBody());
815 }
816 }
817 }
818
819
820
821
822
823 private String buildQueryString(List<String> ids, String queryParam) {
824 StringBuilder reqParam = new StringBuilder("?");
825 int size = ids.size();
826 for (int i = 0; i < size; i++) {
827 reqParam.append(queryParam);
828 reqParam.append("=");
829 reqParam.append(ids.get(i));
830 if (i != (size - 1)) {
831 reqParam.append("&");
832 }
833 }
834 return reqParam.toString();
835 }
836
837 public String buildIds(List<String> ids, String value) {
838 StringBuilder idSb = new StringBuilder(value);
839 for (String id : ids) {
840 if (ids.get(ids.size() - 1).equals(id)) {
841 idSb.append(id);
842 } else {
843 idSb.append(id + ",");
844 }
845 }
846 return idSb.toString();
847 }
848
849 public FindParams buildFindParams(Map<String, String> map) {
850 FindParams findParams = new FindParams();
851 FindParams.Map findMap = new FindParams.Map();
852 for (Map.Entry mapEntry : map.entrySet()) {
853 FindParams.Map.Entry entry = new FindParams.Map.Entry();
854 entry.setKey((String) mapEntry.getKey());
855 entry.setValue((String) mapEntry.getValue());
856 findMap.getEntry().add(entry);
857 }
858 findParams.setMap(findMap);
859 return findParams;
860 }
861
862 public RestResponse patchRequest(String requestBody, String param) {
863 HttpClient client = new DefaultHttpClient();
864 String result = new String();
865 RestResponse response = new RestResponse();
866 HttpPatch patch = new HttpPatch(DOCSTORE_URL + param);
867 try {
868 StringEntity stringEntity = new StringEntity(requestBody, "UTF-8");
869 patch.setEntity(stringEntity);
870 response.setResponse(client.execute(patch));
871 result = getEncodeEntityValue(response.getResponse().getEntity());
872 } catch (Exception e) {
873 logger.error("PATCH response error is ::", e);
874 }
875 response.setResponseBody(result);
876 logger.debug(" PATCH Response Body :: ", response.getResponseBody());
877 return response;
878 }
879
880 public RestResponse postRequest(String requestBody, String param) {
881 HttpClient client = new DefaultHttpClient();
882 String result = new String();
883 RestResponse response = new RestResponse();
884 HttpPost post = new HttpPost(DOCSTORE_URL + param);
885 try {
886 StringEntity stringEntity = new StringEntity(requestBody, "UTF-8");
887 post.setEntity(stringEntity);
888 response.setResponse(client.execute(post));
889 result = getEncodeEntityValue(response.getResponse().getEntity());
890 } catch (Exception e) {
891 logger.error("POST response error is ::", e);
892 }
893 response.setResponseBody(result);
894 logger.debug(" POST Response Body :: ", response.getResponseBody());
895 return response;
896 }
897
898 public RestResponse putRequest(String requestBody, String param) {
899 HttpClient client = new DefaultHttpClient();
900 String result = new String();
901 HttpPut put = new HttpPut(DOCSTORE_URL + param);
902 RestResponse response = new RestResponse();
903 try {
904 StringEntity stringEntity = new StringEntity(requestBody, "UTF-8");
905 put.setEntity(stringEntity);
906 response.setResponse(client.execute(put));
907 result = getEncodeEntityValue(response.getResponse().getEntity());
908 } catch (Exception e) {
909 logger.error("PUT response error is :: ", e);
910 }
911 response.setResponseBody(result);
912 logger.debug(" PUT Response Body :: ", response.getResponseBody());
913 return response;
914 }
915
916 private RestResponse getRequest(String id, String param) {
917 HttpClient client = new DefaultHttpClient();
918 HttpGet get = new HttpGet(DOCSTORE_URL + param + id);
919 String result = new String();
920 RestResponse response = new RestResponse();
921 try {
922 response.setResponse(client.execute(get));
923
924 result = getEncodeEntityValue(response.getResponse().getEntity());
925 } catch (Exception e) {
926 logger.error("GET response error is ::", e);
927 }
928 response.setResponseBody(result);
929 logger.debug(" GET Response Body :: ", response.getResponseBody());
930 return response;
931 }
932
933 private RestResponse getBibResponse(String id, String param) {
934 RestResponse response = new RestResponse();
935 response.setContentType("text/html; charset=utf-8");
936 try {
937 URL aURL = new URL(DOCSTORE_URL + param + id);
938 StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("http", 1, 1), 200, "OK");
939 HttpResponse httpResponse = new BasicHttpResponse(statusLine);
940 String result = getHttpResponse(new InputStreamReader(aURL.openStream()), response.getContentType());
941 response.setResponseBody(result);
942 response.setResponse(httpResponse);
943 logger.debug(" GET Response Body :: ", response.getResponseBody());
944 } catch (Exception ex) {
945 ex.printStackTrace();
946 logger.error("Exception :", ex);
947 }
948 return response;
949 }
950
951 public RestResponse deleteRequest(String id, String param) {
952 String result = new String();
953 RestResponse response = new RestResponse();
954 HttpClient client = new DefaultHttpClient();
955 HttpDelete delete = new HttpDelete(DOCSTORE_URL + param + id);
956 try {
957 response.setResponse(client.execute(delete));
958 result = getEncodeEntityValue(response.getResponse().getEntity());
959 } catch (Exception e) {
960 logger.error("DELETE response error is ::", e);
961 }
962 response.setResponseBody(result);
963 logger.debug("DELETE Response Body :: ", response.getResponseBody());
964 return response;
965 }
966
967 private String getEncodeEntityValue(HttpEntity entity) throws Exception {
968 String result = EntityUtils.toString(entity, "UTF-8");
969 return result;
970 }
971
972 private String getHttpResponse(InputStreamReader inputStreamReader, String contentType) throws Exception {
973 StringWriter writer = new StringWriter();
974
975 IOUtils.copy(inputStreamReader, writer);
976 return writer.toString();
977 }
978
979 public File createZipFile(File sourceDir) throws IOException {
980 File zipFile = File.createTempFile("tmp", ".zip");
981 String path = sourceDir.getAbsolutePath();
982 ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(zipFile));
983 ArrayList<File> fileList = getAllFilesList(sourceDir);
984 for (File file : fileList) {
985 ZipEntry ze = new ZipEntry(file.getAbsolutePath().substring(path.length() + 1));
986 zip.putNextEntry(ze);
987 FileInputStream fis = new FileInputStream(file);
988 org.apache.commons.compress.utils.IOUtils.copy(fis, zip);
989 fis.close();
990 zip.closeEntry();
991 }
992 zip.close();
993 return zipFile;
994 }
995
996 public ArrayList<File> getAllFilesList(File directory) {
997 ArrayList<File> fileList = new ArrayList<File>();
998 if (directory.isFile())
999 fileList.add(directory);
1000 else if (directory.isDirectory())
1001 for (File innerFile : directory.listFiles())
1002 fileList.addAll(getAllFilesList(innerFile));
1003 return fileList;
1004 }
1005
1006
1007 public void deleteFiles(File[] files) {
1008 try {
1009 for (File file : files) {
1010 try {
1011 file.delete();
1012 } catch (Exception e) {
1013 }
1014 }
1015 } catch (Exception e) {
1016 }
1017 }
1018
1019 }