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