View Javadoc
1   package org.kuali.ole.systemintegration.rest.servlet;
2   
3   import com.ibm.wsdl.util.StringUtils;
4   import org.kuali.ole.systemintegration.rest.RestConstants;
5   import org.kuali.ole.systemintegration.rest.service.DocstoreDataRetrieveService;
6   import org.kuali.ole.utility.CompressUtils;
7   import org.slf4j.Logger;
8   import org.slf4j.LoggerFactory;
9   
10  import javax.servlet.ServletException;
11  import javax.servlet.http.HttpServlet;
12  import javax.servlet.http.HttpServletRequest;
13  import javax.servlet.http.HttpServletResponse;
14  import java.io.IOException;
15  import java.util.ArrayList;
16  import java.util.Arrays;
17  import java.util.List;
18  
19  /**
20   * Created with IntelliJ IDEA.
21   * User: sheiksalahudeenm
22   * Date: 3/10/14
23   * Time: 12:42 PM
24   * To change this template use File | Settings | File Templates.
25   */
26  public class InstanceDetailRestServlet extends HttpServlet{
27  
28      private static final Logger LOG = LoggerFactory
29              .getLogger(InstanceDetailRestServlet.class);
30      private CompressUtils compressUtils = new CompressUtils();
31  
32      @Override
33      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
34          float start = System.currentTimeMillis();
35          String[] bibUUID = (String[])request.getParameterMap().get(RestConstants.BIB_ID);
36          String acceptType = request.getHeader("Accept");
37          List<String> bibIdList = new ArrayList<>();
38          if(bibUUID != null){
39              bibIdList = Arrays.asList(bibUUID);
40          }
41          boolean doProcess = true;
42          String format = RestConstants.XML;
43          if(acceptType == null || (acceptType!=null && acceptType.contains("*/*"))){
44              format = RestConstants.XML;
45          }else if(! (acceptType.contains("application/xml") || acceptType.equalsIgnoreCase("application/json") || acceptType.contains("text/xml"))){
46              doProcess = false;
47          }else{
48              if(acceptType.contains("application/xml") || acceptType.contains("text/xml")){
49                  format =  RestConstants.XML;
50              } else{
51                  format = RestConstants.JSON;
52              }
53          }
54          if(bibUUID ==null){
55              response.getWriter().println(RestConstants.BIB_ID_NULL);
56          }else{
57              if(doProcess){
58                  DocstoreDataRetrieveService oleDocstoreDataRetrieveService = new DocstoreDataRetrieveService();
59                  String instanceResponse = oleDocstoreDataRetrieveService.getHoldingsTree(bibIdList, format);
60  
61                  if (instanceResponse == null || instanceResponse.isEmpty()) {
62                      response.setStatus(HttpServletResponse.SC_NOT_FOUND);
63                      if (bibIdList.size() == 1)
64                          response.getWriter().println(RestConstants.NO_RECORD_RESPONSE_FOR_SINGLE_BIB + bibIdList.get(0));
65                      else if (bibIdList.size() > 1)
66                          response.getWriter().println(RestConstants.NO_RECORD_RESPONSE_FOR_MULTIPLE_BIBS);
67                  } else {
68                      if (acceptType == null || (acceptType!=null && acceptType.contains("*/*"))) {
69                          response.setContentType(RestConstants.CONTENT_TYPE);
70                      } else if (format.equalsIgnoreCase(RestConstants.XML)) {
71                          response.setContentType(acceptType);
72                      } else if (format.equalsIgnoreCase(RestConstants.JSON)) {
73                          response.setContentType(acceptType);
74                      }
75                      response.getWriter().println(instanceResponse);
76                  }
77              }else{
78                  response.sendError(HttpServletResponse.SC_BAD_REQUEST, RestConstants.INVALID_FORAMAT);
79              }
80          }
81          float end = System.currentTimeMillis();
82          LOG.info("TimeTaken :" + (end-start)/1000 );
83      }
84  
85      private List<String> getBibIdList(String bibUUIDs) {
86          List<String> bibIdsList = new ArrayList<String>();
87          String[] queueArray = bibUUIDs.split(",");
88          for (int i = 0; i < queueArray.length; i++) {
89              bibIdsList.add(queueArray[i]);
90          }
91          return bibIdsList;
92      }
93  
94      @Override
95      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
96          doPost(req,resp);
97      }
98  }