1 package org.kuali.ole.web;
2
3 import com.google.common.io.CharStreams;
4 import org.kuali.ole.docstore.common.exception.DocstoreException;
5 import org.kuali.ole.docstore.common.exception.DocstoreExceptionProcessor;
6 import org.kuali.ole.docstore.common.search.BrowseParams;
7 import org.kuali.ole.docstore.common.search.SearchResponse;
8 import org.kuali.ole.docstore.common.service.DocstoreService;
9 import org.kuali.ole.docstore.service.BeanLocator;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import javax.servlet.ServletException;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16 import java.io.IOException;
17 import java.io.PrintWriter;
18
19
20
21
22
23
24
25
26
27 public class BrowseRestServlet extends HttpServlet {
28 private static final Logger LOG = LoggerFactory.getLogger(BrowseRestServlet.class);
29
30 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
31
32 PrintWriter out = resp.getWriter();
33 resp.setContentType("text/xml;charset=UTF-8");
34 String result = "";
35 try {
36 if (req.getPathInfo().startsWith("/holdings")) {
37 result = browseHoldings(req);
38 } else if (req.getPathInfo().startsWith("/item")) {
39 result = browseItem(req);
40 }
41 out.print(result);
42 } catch (DocstoreException de) {
43 LOG.error("Exception :", de);
44 out.print(DocstoreExceptionProcessor.toXml(de));
45 } catch (Exception e) {
46 LOG.error("Exception :", e);
47 out.print(e);
48 }
49 }
50
51 private String browseHoldings(HttpServletRequest req) throws Exception{
52 String requestBody = CharStreams.toString(req.getReader());
53 DocstoreService ds = BeanLocator.getDocstoreService();
54 BrowseParams browseParams = new BrowseParams();
55 browseParams = (BrowseParams) browseParams.deserialize(requestBody);
56 SearchResponse searchResponse = null;
57 try {
58 searchResponse = ds.browseItems(browseParams);
59 } catch (DocstoreException e) {
60 LOG.info("Exception :", e);
61 return DocstoreExceptionProcessor.toXml(e);
62 }
63 return searchResponse.serialize(searchResponse);
64 }
65
66 private String browseItem(HttpServletRequest req) throws Exception {
67 String requestBody = CharStreams.toString(req.getReader());
68 DocstoreService ds = BeanLocator.getDocstoreService();
69 BrowseParams browseParams = new BrowseParams();
70 browseParams = (BrowseParams) browseParams.deserialize(requestBody);
71 SearchResponse searchResponse = null;
72 try {
73 searchResponse = ds.browseHoldings(browseParams);
74 } catch (DocstoreException e) {
75 LOG.info("Exception :", e);
76 return DocstoreExceptionProcessor.toXml(e);
77 }
78 return searchResponse.serialize(searchResponse);
79 }
80
81
82
83 }