001package org.kuali.ole.web;
002
003import com.google.common.io.CharStreams;
004import org.kuali.ole.docstore.common.exception.DocstoreException;
005import org.kuali.ole.docstore.common.exception.DocstoreExceptionProcessor;
006import org.kuali.ole.docstore.common.search.SearchParams;
007import org.kuali.ole.docstore.common.search.SearchResponse;
008import org.kuali.ole.docstore.common.service.DocstoreService;
009import org.kuali.ole.docstore.service.BeanLocator;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012import javax.servlet.ServletException;
013import javax.servlet.http.HttpServlet;
014import javax.servlet.http.HttpServletRequest;
015import javax.servlet.http.HttpServletResponse;
016import java.io.IOException;
017import java.io.PrintWriter;
018
019
020/**
021 * Created with IntelliJ IDEA.
022 * User: mjagan
023 * Date: 3/2/14
024 * Time: 7:28 PM
025 * To change this template use File | Settings | File Templates.
026 */
027public class SearchRestServlet extends HttpServlet {
028    private static final Logger LOG = LoggerFactory.getLogger(SearchRestServlet.class);
029
030
031    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
032        PrintWriter out = resp.getWriter();
033        String requestBody = CharStreams.toString(req.getReader());
034        DocstoreService ds = BeanLocator.getDocstoreService();
035        SearchParams searchParams = new SearchParams();
036        searchParams = (SearchParams) searchParams.deserialize(requestBody);
037        SearchResponse searchResponse = null;
038        try {
039            searchResponse = ds.search(searchParams);
040        } catch (DocstoreException e) {
041            LOG.error("Exception : ", e);
042            out.print(DocstoreExceptionProcessor.toXml(e));
043        }
044        out.print(searchResponse.serialize(searchResponse));
045
046    }
047}