View Javadoc
1   package org.kuali.ole.sip2.servlet;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.ncip.bo.OLENCIPConstants;
5   import org.kuali.ole.sip2.constants.OLESIP2Constants;
6   import org.kuali.ole.sip2.service.impl.OLESIP2ServiceImpl;
7   import org.kuali.ole.sys.OLEPropertyConstants;
8   import org.kuali.rice.kim.impl.identity.principal.PrincipalBo;
9   import org.kuali.rice.krad.service.BusinessObjectService;
10  import org.kuali.rice.krad.service.KRADServiceLocator;
11  
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  import java.net.URLEncoder;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  public class OLESIP2Servlet extends HttpServlet {
24      private static final Logger LOG = Logger.getLogger(OLESIP2Servlet.class);
25      private String service;
26  
27      OLESIP2ServiceImpl oleSip2Service = new OLESIP2ServiceImpl();
28  
29  
30      private BusinessObjectService businessObjectService;
31  
32      protected BusinessObjectService getBusinessObjectService() {
33          if (businessObjectService == null) {
34              businessObjectService = KRADServiceLocator.getBusinessObjectService();
35          }
36          return businessObjectService;
37      }
38  
39  
40      public void doGet(HttpServletRequest request, HttpServletResponse response)
41              throws ServletException, IOException {
42          String responseString = "";
43          Map<String, String[]> parameterMap = null;
44          String outputFormat = OLESIP2Constants.SIP2_FORMAT;
45          parameterMap = request.getParameterMap();
46          if (parameterMap.containsKey(OLENCIPConstants.OLE_CIRCULATION_SERVICE)) {
47              service = parameterMap.get(OLENCIPConstants.OLE_CIRCULATION_SERVICE)[0];
48          }
49  
50          if (service != null && !service.equalsIgnoreCase("")) {
51              if (service.equals(OLESIP2Constants.SIP_SERVICE)) {
52                  if (parameterMap.containsKey(OLESIP2Constants.REQUEST_DATA)) {
53  
54                      if (parameterMap.size() >= 2) {
55                          String operatorId = OLESIP2Constants.OPERATOR_ID;
56                          if (parameterMap.containsKey(OLESIP2Constants.LOGIN_USER)) {
57                              Map userMap = new HashMap();
58                              userMap.put(OLEPropertyConstants.PERSON_USER_IDENTIFIER, parameterMap.get(OLESIP2Constants.LOGIN_USER)[0]);
59                              List<PrincipalBo> matching = (List<PrincipalBo>) getBusinessObjectService().findMatching(PrincipalBo.class, userMap);
60                              if (matching != null && matching.size() >= 1) {
61                                  operatorId = matching.get(0).getPrincipalId();
62                              }
63                          }
64                          responseString = oleSip2Service.processRequest(parameterMap.get(OLESIP2Constants.REQUEST_DATA)[0], service, operatorId);
65                      } else {
66                          responseString = oleSip2Service.getCirculationErrorMessage(service, OLENCIPConstants.PARAMETER_MISSING, "502", OLENCIPConstants.LOOKUPUSER, outputFormat);
67                      }
68  
69                  } else {
70  
71                      LOG.info("Unknown Service Name: " + service + "   Parameter is missing");
72                      responseString = oleSip2Service.getCirculationErrorMessage(service, OLENCIPConstants.UNKNOWN_SERVICE, "503", null, outputFormat);
73                      response.sendError(405, "Method Not Supported");
74                  }
75              } else {
76                  responseString = OLENCIPConstants.NULL_SERVICE;
77              }
78              if (responseString != null) {
79                  responseString = responseString.replaceAll("errorMessage", OLENCIPConstants.MESSAGE);
80                  responseString = responseString.replaceAll("<br/>", "");
81              }
82  
83              PrintWriter out = response.getWriter();
84  
85              if (responseString.contains(OLENCIPConstants.INVALID_FORMAT)) {
86                  response.setStatus(406);
87              } else if (responseString.contains(OLENCIPConstants.PARAMETER_MISSING)) {
88                  response.setStatus(422);
89              } else {
90                  response.setStatus(200);
91              }
92              //out.write(URLEncoder.encode(responseString, "UTF-8"));
93              out.write(responseString);
94  
95          }
96      }
97  
98  
99  }