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
33
34
35
36
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 {
90 result = createBib(req);
91 }
92 out.print(result);
93 } catch (DocstoreException de) {
94 LOG.error("Exception :", de);
95 out.print(DocstoreExceptionProcessor.toXml(de));
96 } catch (Exception e) {
97 LOG.error("Exception :", e);
98 out.print(e);
99 }
100 }
101
102
103 protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
104
105 PrintWriter out = resp.getWriter();
106 resp.setContentType("application/xml;charset=UTF-8");
107 String result = "";
108 try {
109 if (req.getPathInfo().startsWith("/doc/trees")) {
110
111 } else {
112 result = updateBib(req);
113 }
114 out.print(result);
115 } catch (DocstoreException de) {
116 LOG.error("Exception :", de);
117 out.print(DocstoreExceptionProcessor.toXml(de));
118 } catch (Exception e) {
119 LOG.error("Exception :", e);
120 out.print(e);
121 }
122 }
123
124 protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
125 PrintWriter out = resp.getWriter();
126 resp.setContentType("application/xml;charset=UTF-8");
127 String result = "";
128
129 try {
130 result = deleteBibs(req);
131 out.print(result);
132 } catch (DocstoreException de) {
133 LOG.error("Exception :", de);
134 out.print(DocstoreExceptionProcessor.toXml(de));
135 } catch (Exception e) {
136 LOG.error("Exception :", e);
137 out.print(e);
138 }
139 }
140
141 private String deleteBib(HttpServletRequest req) {
142 DocstoreService ds = BeanLocator.getDocstoreService();
143 String bibId = getIdFromPathInfo(req.getPathInfo());
144 ds.deleteBib(bibId);
145 return "Success";
146 }
147
148 private String deleteBibs(HttpServletRequest req) {
149 DocstoreService ds = BeanLocator.getDocstoreService();
150 Map params = req.getParameterMap();
151 String[] bibIds = (String[]) params.get("bibId");
152 List<String> bibIdList = new ArrayList<String>();
153 for (String bibId : bibIds) {
154 bibIdList.add(bibId);
155 }
156 ds.deleteBibs(bibIdList);
157 return "Success";
158 }
159
160 private String getIdFromPathInfo(String pathInfo) {
161 String id = "";
162 if (StringUtils.isNotEmpty(pathInfo)) {
163 int length = pathInfo.length();
164 if (pathInfo.endsWith("/")) {
165 pathInfo = pathInfo.substring(0, length - 1);
166 }
167 id = pathInfo.substring(pathInfo.lastIndexOf("/") + 1);
168 }
169 return id;
170 }
171
172 private String findBibTree(HttpServletRequest req) throws IOException {
173 DocstoreService ds = BeanLocator.getDocstoreService();
174 String requestBody = CharStreams.toString(req.getReader());
175 BibTree bibTree = new BibTree();
176 FindParams findParams = new FindParams();
177 findParams = (FindParams) findParams.deserialize(requestBody);
178 HashMap<String, String> hashMap = new HashMap();
179 List<FindParams.Map.Entry> entries = findParams.getMap().getEntry();
180 for (FindParams.Map.Entry entry : entries) {
181 hashMap.put(entry.getKey(), entry.getValue());
182 }
183 bibTree = ds.findBibTree(hashMap);
184 return bibTree.serialize(bibTree);
185
186 }
187
188 private String findBib(HttpServletRequest req) throws IOException {
189 DocstoreService ds = BeanLocator.getDocstoreService();
190 String requestBody = CharStreams.toString(req.getReader());
191 Bib bib = new BibMarc();
192 FindParams findParams = new FindParams();
193 findParams = (FindParams) findParams.deserialize(requestBody);
194 HashMap<String, String> hashMap = new HashMap();
195 List<FindParams.Map.Entry> entries = findParams.getMap().getEntry();
196 for (FindParams.Map.Entry entry : entries) {
197 hashMap.put(entry.getKey(), entry.getValue());
198 }
199 bib = ds.findBib(hashMap);
200 return (bib.serialize(bib));
201 }
202
203 private String createBib(HttpServletRequest req) throws IOException {
204 DocstoreService ds = BeanLocator.getDocstoreService();
205 String requestBody = CharStreams.toString(req.getReader());
206 Bib bib = new BibMarc();
207 Bib bibObj = (Bib) bib.deserialize(requestBody);
208 ds.createBib(bibObj);
209 String responseStr = responseUrl + bibObj.getId();
210 return responseStr;
211 }
212
213 private String updateBib(HttpServletRequest req) throws IOException {
214 DocstoreService ds = BeanLocator.getDocstoreService();
215 String requestBody = CharStreams.toString(req.getReader());
216
217 Bib bib = new BibMarc();
218 Bib bibObj = (Bib) bib.deserialize(requestBody);
219 ds.updateBib(bibObj);
220 return responseUrl + bibObj.getId();
221 }
222
223
224 private String retrieveBib(HttpServletRequest req) {
225 DocstoreService ds = BeanLocator.getDocstoreService();
226 Map params = req.getParameterMap();
227 String[] bibIds = (String[]) params.get("bibId");
228 List<String> bibIdList = new ArrayList<String>();
229
230 for (String bibId : bibIds) {
231 bibIdList.add(bibId);
232 }
233 List<Bib> bibs = null;
234 bibs = ds.retrieveBibs(bibIdList);
235 if (bibs.size() == 1) {
236 Bib bib = new Bib();
237 return bib.serialize(bibs.get(0));
238 }
239 Bibs bibsObj = new Bibs();
240 bibsObj.getBibs().addAll(bibs);
241 return Bibs.serialize(bibsObj);
242 }
243
244 private String retrieveBibContent(HttpServletRequest req) {
245 DocstoreService ds = BeanLocator.getDocstoreService();
246 StringBuffer contentBuffer = new StringBuffer();
247 BibMarcRecords bibMarcRecords = new BibMarcRecords();
248 BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
249 List<String> bibIdList = new ArrayList<String>();
250 Map params = req.getParameterMap();
251 String[] queryVariables = (String[]) params.get("bibId");
252 if (queryVariables.length > 0) {
253 for (String bibId : queryVariables) {
254 bibIdList.add(bibId);
255 }
256 }
257 if (bibIdList != null && bibIdList.size() > 1) {
258 List<Bib> bibs = ds.retrieveBibs(bibIdList);
259 for (Bib bibRecord : bibs) {
260 bibMarcRecords.getRecords().addAll(bibMarcRecordProcessor.fromXML(bibRecord.getContent()).getRecords());
261 }
262 if (bibMarcRecords != null) {
263 contentBuffer.append(bibMarcRecordProcessor.generateXML(bibMarcRecords.getRecords()));
264 }
265 } else if (bibIdList != null && bibIdList.size() == 1) {
266 Bib bib = ds.retrieveBib(bibIdList.get(0));
267 bibMarcRecords = bibMarcRecordProcessor.fromXML(bib.getContent());
268 contentBuffer.append(bibMarcRecordProcessor.generateXML(bibMarcRecords.getRecords()));
269 }
270 return contentBuffer.toString();
271 }
272
273 private String retrieveBibTree(HttpServletRequest req) {
274 DocstoreService ds = BeanLocator.getDocstoreService();
275 String id = req.getParameter("bibId");
276 BibTree bibTree = ds.retrieveBibTree(id);
277 return bibTree.serialize(bibTree);
278 }
279
280 private String createBibTree(HttpServletRequest req) throws IOException {
281 DocstoreService ds = BeanLocator.getDocstoreService();
282 String requestBody = CharStreams.toString(req.getReader());
283 BibTree bibTree = new BibTree();
284 bibTree = (BibTree) bibTree.deserialize(requestBody);
285 ds.createBibTree(bibTree);
286 return responseTreeUrl + bibTree.getBib().getId();
287 }
288
289
290 private String processBibTrees(HttpServletRequest req) throws IOException {
291 DocstoreService ds = BeanLocator.getDocstoreService();
292 String requestBody = CharStreams.toString(req.getReader());
293 BibTrees bibTrees = new BibTrees();
294 bibTrees = (BibTrees) bibTrees.deserialize(requestBody);
295 bibTrees = ds.processBibTrees(bibTrees);
296 return bibTrees.serialize(bibTrees);
297 }
298
299 private String transferHoldings(HttpServletRequest req) {
300 DocstoreService ds = BeanLocator.getDocstoreService();
301
302 String bibId = req.getPathInfo().split("/")[2];
303 Map params = req.getParameterMap();
304 String[] splitHoldingIds = (String[]) params.get("holdingsId");
305 List<String> holdingsIds = new ArrayList<String>();
306 for (String holdingsId : splitHoldingIds) {
307 holdingsIds.add(holdingsId);
308 }
309 ds.transferHoldings(holdingsIds, bibId);
310 return "Success";
311 }
312
313 private String retrieveBibTrees(HttpServletRequest req) {
314 DocstoreService ds = BeanLocator.getDocstoreService();
315 List<String> bibIdList = new ArrayList<String>();
316 Map params = req.getParameterMap();
317 String[] bibIds = (String[]) params.get("bibId");
318 for (String bibId : bibIds) {
319 bibIdList.add(bibId);
320 }
321 return BibTrees.serialize(ds.retrieveBibTrees(bibIdList));
322 }
323
324 }