001package org.kuali.ole.systemintegration.rest.servlet;
002
003import com.ibm.wsdl.util.StringUtils;
004import org.kuali.ole.systemintegration.rest.RestConstants;
005import org.kuali.ole.systemintegration.rest.service.DocstoreDataRetrieveService;
006import org.kuali.ole.utility.CompressUtils;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010import javax.servlet.ServletException;
011import javax.servlet.http.HttpServlet;
012import javax.servlet.http.HttpServletRequest;
013import javax.servlet.http.HttpServletResponse;
014import java.io.IOException;
015import java.util.ArrayList;
016import java.util.Arrays;
017import java.util.List;
018
019/**
020 * Created with IntelliJ IDEA.
021 * User: sheiksalahudeenm
022 * Date: 3/10/14
023 * Time: 12:42 PM
024 * To change this template use File | Settings | File Templates.
025 */
026public class InstanceDetailRestServlet extends HttpServlet{
027
028    private static final Logger LOG = LoggerFactory
029            .getLogger(InstanceDetailRestServlet.class);
030    private CompressUtils compressUtils = new CompressUtils();
031
032    @Override
033    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
034        float start = System.currentTimeMillis();
035        String[] bibUUID = (String[])request.getParameterMap().get(RestConstants.BIB_ID);
036        String acceptType = request.getHeader("Accept");
037        List<String> bibIdList = new ArrayList<>();
038        if(bibUUID != null){
039            bibIdList = Arrays.asList(bibUUID);
040        }
041        boolean doProcess = true;
042        String format = RestConstants.XML;
043        if(acceptType == null || (acceptType!=null && acceptType.contains("*/*"))){
044            format = RestConstants.XML;
045        }else if(! (acceptType.contains("application/xml") || acceptType.equalsIgnoreCase("application/json") || acceptType.contains("text/xml"))){
046            doProcess = false;
047        }else{
048            if(acceptType.contains("application/xml") || acceptType.contains("text/xml")){
049                format =  RestConstants.XML;
050            } else{
051                format = RestConstants.JSON;
052            }
053        }
054        if(bibUUID ==null){
055            response.getWriter().println(RestConstants.BIB_ID_NULL);
056        }else{
057            if(doProcess){
058                DocstoreDataRetrieveService oleDocstoreDataRetrieveService = new DocstoreDataRetrieveService();
059                String instanceResponse = oleDocstoreDataRetrieveService.getHoldingsTree(bibIdList, format);
060
061                if (instanceResponse == null || instanceResponse.isEmpty()) {
062                    response.setStatus(HttpServletResponse.SC_NOT_FOUND);
063                    if (bibIdList.size() == 1)
064                        response.getWriter().println(RestConstants.NO_RECORD_RESPONSE_FOR_SINGLE_BIB + bibIdList.get(0));
065                    else if (bibIdList.size() > 1)
066                        response.getWriter().println(RestConstants.NO_RECORD_RESPONSE_FOR_MULTIPLE_BIBS);
067                } else {
068                    if (acceptType == null || (acceptType!=null && acceptType.contains("*/*"))) {
069                        response.setContentType(RestConstants.CONTENT_TYPE);
070                    } else if (format.equalsIgnoreCase(RestConstants.XML)) {
071                        response.setContentType(acceptType);
072                    } else if (format.equalsIgnoreCase(RestConstants.JSON)) {
073                        response.setContentType(acceptType);
074                    }
075                    response.getWriter().println(instanceResponse);
076                }
077            }else{
078                response.sendError(HttpServletResponse.SC_BAD_REQUEST, RestConstants.INVALID_FORAMAT);
079            }
080        }
081        float end = System.currentTimeMillis();
082        LOG.info("TimeTaken :" + (end-start)/1000 );
083    }
084
085    private List<String> getBibIdList(String bibUUIDs) {
086        List<String> bibIdsList = new ArrayList<String>();
087        String[] queueArray = bibUUIDs.split(",");
088        for (int i = 0; i < queueArray.length; i++) {
089            bibIdsList.add(queueArray[i]);
090        }
091        return bibIdsList;
092    }
093
094    @Override
095    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
096        doPost(req,resp);
097    }
098}