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