View Javadoc
1   package org.kuali.ole.docstore.engine.service.rest;
2   
3   import com.google.common.io.CharStreams;
4   import org.apache.commons.lang.StringUtils;
5   import org.kuali.ole.docstore.common.document.*;
6   import org.kuali.ole.docstore.common.document.config.DocumentSearchConfig;
7   import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecords;
8   import org.kuali.ole.docstore.common.document.content.bib.marc.OrderBibMarcRecord;
9   import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
10  import org.kuali.ole.docstore.common.document.ids.BibId;
11  import org.kuali.ole.docstore.common.document.ids.BibIds;
12  import org.kuali.ole.docstore.common.document.ids.HoldingsId;
13  import org.kuali.ole.docstore.common.exception.*;
14  import org.kuali.ole.docstore.common.find.FindParams;
15  import org.kuali.ole.docstore.common.service.DocstoreService;
16  import org.kuali.ole.docstore.service.BeanLocator;
17  import org.slf4j.Logger;
18  import org.slf4j.LoggerFactory;
19  import org.kuali.ole.docstore.common.exception.DocstoreExceptionProcessor;
20  
21  import javax.servlet.ServletException;
22  import javax.servlet.http.HttpServlet;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  import java.io.*;
26  import java.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  /**
32   * Created with IntelliJ IDEA.
33   * User: mjagan
34   * Date: 3/2/14
35   * Time: 7:28 PM
36   * To change this template use File | Settings | File Templates.
37   */
38  public class BibRestServlet extends HttpServlet {
39      private static final Logger LOG = LoggerFactory.getLogger(BibRestServlet.class);
40      private static String responseUrl = "documentrest/bib/doc/";
41      private static String responseTreeUrl = "documentrest/bib/doc/tree/";
42      public static BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
43      @Override
44      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
45          PrintWriter out = resp.getWriter();
46          resp.setContentType("application/xml;charset=UTF-8");
47          String result = "";
48          try {
49              if (req.getPathInfo() != null && req.getPathInfo().startsWith("/reload")) {
50                  DocumentSearchConfig.getDocumentSearchConfig();
51                  DocumentSearchConfig.reloadDocumentConfig();
52              } else if (req.getPathInfo() != null && req.getPathInfo().contains("doc")) {
53                  if (req.getPathInfo().startsWith("/doc/trees")) {
54                      result = retrieveBibTrees(req);
55                  } else if (req.getPathInfo().startsWith("/doc/tree")) {
56                      result = retrieveBibTree(req);
57                  } else {
58                      result = retrieveBib(req);
59                  }
60              } else {
61                  result = retrieveBibContent(req);
62              }
63              out.print(result);
64          } catch (DocstoreException de) {
65              LOG.error("Docstore Exception :", de);
66              out.print(DocstoreExceptionProcessor.toXml(de));
67          } catch (Exception e) {
68              LOG.error("Exception :", e);
69              out.print(e);
70          }
71      }
72  
73      protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
74  
75          PrintWriter out = resp.getWriter();
76          resp.setContentType("application/xml;charset=UTF-8");
77          String result = "";
78          try {
79              if (req.getPathInfo().startsWith("/tree/doc/find")) {
80                  result = findBibTree(req);
81              } else if (req.getPathInfo().startsWith("/doc/find")) {
82                  result = findBib(req);
83              } else if (req.getPathInfo().startsWith("/doc/tree")) {
84                  result = createBibTree(req);
85              } else if (req.getPathInfo().contains("/transfer")) {
86                  result = transferHoldings(req);
87              } else if (req.getPathInfo().contains("/process")) {
88                  result = processBibTrees(req);
89              }else if (req.getPathInfo().contains("/searchAcquistion")) {
90                  result = retrieveBib(req);
91              } else {
92                  result = createBib(req);
93              }
94              out.print(result);
95          } catch (DocstoreException de) {
96              LOG.error("Exception :", de);
97              out.print(DocstoreExceptionProcessor.toXml(de));
98          } catch (Exception e) {
99              LOG.error("Exception :", e);
100             out.print(e);
101         }
102     }
103 
104 
105     protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
106 
107         PrintWriter out = resp.getWriter();
108         resp.setContentType("application/xml;charset=UTF-8");
109         String result = "";
110         try {
111             if (req.getPathInfo().startsWith("/doc/trees")) {
112 
113             } else {
114                 result = updateBib(req);
115             }
116             out.print(result);
117         } catch (DocstoreException de) {
118             LOG.error("Exception :", de);
119             out.print(DocstoreExceptionProcessor.toXml(de));
120         } catch (Exception e) {
121             LOG.error("Exception :", e);
122             out.print(e);
123         }
124     }
125 
126     protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
127         PrintWriter out = resp.getWriter();
128         resp.setContentType("application/xml;charset=UTF-8");
129         String result = "";
130 
131         try {
132             result = deleteBibs(req);
133             out.print(result);
134         } catch (DocstoreException de) {
135             LOG.error("Exception :", de);
136             out.print(DocstoreExceptionProcessor.toXml(de));
137         } catch (Exception e) {
138             LOG.error("Exception :", e);
139             out.print(e);
140         }
141     }
142 
143     private String deleteBib(HttpServletRequest req) {
144         DocstoreService ds = BeanLocator.getDocstoreService();
145         String bibId = getIdFromPathInfo(req.getPathInfo());
146         ds.deleteBib(bibId);
147         return "Success";
148     }
149 
150     private String deleteBibs(HttpServletRequest req) {
151         DocstoreService ds = BeanLocator.getDocstoreService();
152         Map params = req.getParameterMap();
153         String[] bibIds = (String[]) params.get("bibId");
154         List<String> bibIdList = new ArrayList<String>();
155         for (String bibId : bibIds) {
156             bibIdList.add(bibId);
157         }
158             ds.deleteBibs(bibIdList);
159         return "Success";
160     }
161 
162     private String getIdFromPathInfo(String pathInfo) {
163         String id = "";
164         if (StringUtils.isNotEmpty(pathInfo)) {
165             int length = pathInfo.length();
166             if (pathInfo.endsWith("/")) {
167                 pathInfo = pathInfo.substring(0, length - 1);
168             }
169             id = pathInfo.substring(pathInfo.lastIndexOf("/") + 1);
170         }
171         return id;
172     }
173 
174     private String findBibTree(HttpServletRequest req) throws IOException {
175         DocstoreService ds = BeanLocator.getDocstoreService();
176         String requestBody = CharStreams.toString(req.getReader());
177         BibTree bibTree = new BibTree();
178         FindParams findParams = new FindParams();
179         findParams = (FindParams) findParams.deserialize(requestBody);
180         HashMap<String, String> hashMap = new HashMap();
181         List<FindParams.Map.Entry> entries = findParams.getMap().getEntry();
182         for (FindParams.Map.Entry entry : entries) {
183             hashMap.put(entry.getKey(), entry.getValue());
184         }
185         bibTree = ds.findBibTree(hashMap);
186         return bibTree.serialize(bibTree);
187 
188     }
189 
190     private String findBib(HttpServletRequest req) throws IOException {
191         DocstoreService ds = BeanLocator.getDocstoreService();
192         String requestBody = CharStreams.toString(req.getReader());
193         Bib bib = new BibMarc();
194         FindParams findParams = new FindParams();
195         findParams = (FindParams) findParams.deserialize(requestBody);
196         HashMap<String, String> hashMap = new HashMap();
197         List<FindParams.Map.Entry> entries = findParams.getMap().getEntry();
198         for (FindParams.Map.Entry entry : entries) {
199             hashMap.put(entry.getKey(), entry.getValue());
200         }
201         bib = ds.findBib(hashMap);
202         return (bib.serialize(bib));
203     }
204 
205     private String createBib(HttpServletRequest req) throws IOException {
206         DocstoreService ds = BeanLocator.getDocstoreService();
207         String requestBody = CharStreams.toString(req.getReader());
208         Bib bib = new BibMarc();
209         Bib bibObj = (Bib) bib.deserialize(requestBody);
210         String [] reqString= req.getPathInfo().split("/");
211         if(reqString.length == 3 ){
212             bibObj.setId(reqString[2]);
213         }
214         ds.createBib(bibObj);
215         String responseStr = responseUrl + bibObj.getId();
216         return responseStr;
217     }
218 
219     private String updateBib(HttpServletRequest req) throws IOException {
220         DocstoreService ds = BeanLocator.getDocstoreService();
221         String requestBody = CharStreams.toString(req.getReader());
222 
223         Bib bib = new BibMarc();
224         Bib bibObj = (Bib) bib.deserialize(requestBody);
225         ds.updateBib(bibObj);
226         return responseUrl + bibObj.getId();
227     }
228 
229 
230     private String retrieveBib(HttpServletRequest req) {
231         DocstoreService ds = BeanLocator.getDocstoreService();
232         Map params = req.getParameterMap();
233         String[] bibIds = (String[]) params.get("bibId");
234         List<String> bibIdList = new ArrayList<String>();
235 
236         for (String bibId : bibIds) {
237             bibIdList.add(bibId);
238         }
239         List<Bib> bibs = null;
240         bibs = ds.retrieveBibs(bibIdList);
241         if (bibs.size() == 1) {
242             Bib bib = bibs.get(0);
243             return bib.serialize(bib);
244         }
245         Bibs bibsObj = new Bibs();
246         bibsObj.getBibs().addAll(bibs);
247         return Bibs.serialize(bibsObj);
248     }
249 
250     private String retrieveBibContent(HttpServletRequest req) {
251         DocstoreService ds = BeanLocator.getDocstoreService();
252         StringBuffer contentBuffer = new StringBuffer();
253         BibMarcRecords bibMarcRecords = new BibMarcRecords();
254         BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
255         List<String> bibIdList = new ArrayList<String>();
256         Map params = req.getParameterMap();
257         String[] queryVariables = (String[]) params.get("bibId");
258         if (queryVariables.length > 0) {
259             for (String bibId : queryVariables) {
260                 bibIdList.add(bibId);
261             }
262         }
263         if (bibIdList != null && bibIdList.size() > 1) {
264             List<Bib> bibs = ds.retrieveBibs(bibIdList);
265             for (Bib bibRecord : bibs) {
266                 bibMarcRecords.getRecords().addAll(bibMarcRecordProcessor.fromXML(bibRecord.getContent()).getRecords());
267             }
268             if (bibMarcRecords != null) {
269                 contentBuffer.append(bibMarcRecordProcessor.generateXML(bibMarcRecords.getRecords()));
270             }
271         } else if (bibIdList != null && bibIdList.size() == 1) {
272             Bib bib = ds.retrieveBib(bibIdList.get(0));
273             bibMarcRecords = bibMarcRecordProcessor.fromXML(bib.getContent());
274             contentBuffer.append(bibMarcRecordProcessor.generateXML(bibMarcRecords.getRecords()));
275         }
276         return contentBuffer.toString();
277     }
278 
279     private String retrieveBibTree(HttpServletRequest req) {
280         DocstoreService ds = BeanLocator.getDocstoreService();
281         String id = req.getParameter("bibId");
282         BibTree bibTree = ds.retrieveBibTree(id);
283         return bibTree.serialize(bibTree);
284     }
285 
286     private String createBibTree(HttpServletRequest req) throws IOException {
287         DocstoreService ds = BeanLocator.getDocstoreService();
288         String requestBody = CharStreams.toString(req.getReader());
289         BibTree bibTree = new BibTree();
290             bibTree = (BibTree) bibTree.deserialize(requestBody);
291             ds.createBibTree(bibTree);
292         return responseTreeUrl + bibTree.getBib().getId();
293     }
294 
295 
296     private String processBibTrees(HttpServletRequest req) throws IOException {
297         DocstoreService ds = BeanLocator.getDocstoreService();
298         String requestBody = CharStreams.toString(req.getReader());
299         BibTrees bibTrees = new BibTrees();
300         bibTrees = (BibTrees) bibTrees.deserialize(requestBody);
301         bibTrees = ds.processBibTrees(bibTrees);
302         return bibTrees.serialize(bibTrees);
303     }
304 
305     private String transferHoldings(HttpServletRequest req) {
306         DocstoreService ds = BeanLocator.getDocstoreService();
307         //String[] transferStr = req.getPathInfo().split("transfer");
308         String bibId = req.getPathInfo().split("/")[2];
309         Map params = req.getParameterMap();
310         String[] splitHoldingIds = (String[]) params.get("holdingsId");
311         List<String> holdingsIds = new ArrayList<String>();
312         for (String holdingsId : splitHoldingIds) {
313             holdingsIds.add(holdingsId);
314         }
315         ds.transferHoldings(holdingsIds, bibId);
316         return "Success";
317     }
318 
319     private String retrieveBibTrees(HttpServletRequest req) {
320         DocstoreService ds = BeanLocator.getDocstoreService();
321         List<String> bibIdList = new ArrayList<String>();
322         Map params = req.getParameterMap();
323         String[] bibIds = (String[]) params.get("bibId");
324         for (String bibId : bibIds) {
325             bibIdList.add(bibId);
326         }
327         return BibTrees.serialize(ds.retrieveBibTrees(bibIdList));
328     }
329 
330 }