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.common.MessageUtil;
6   import org.kuali.ole.sip2.constants.OLESIP2Constants;
7   import org.kuali.ole.sip2.service.impl.OLESIP2ServiceImpl;
8   import org.kuali.ole.sys.OLEPropertyConstants;
9   import org.kuali.rice.kim.impl.identity.principal.PrincipalBo;
10  import org.kuali.rice.krad.service.BusinessObjectService;
11  import org.kuali.rice.krad.service.KRADServiceLocator;
12  
13  import javax.servlet.ServletException;
14  import javax.servlet.http.HttpServlet;
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpServletResponse;
17  import java.io.IOException;
18  import java.io.PrintWriter;
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          LOG.info("Inside OLESIP2Servlet.doGet olefs");
43  
44          String responseString = "";
45          Map<String, String[]> parameterMap = null;
46          String outputFormat = OLESIP2Constants.SIP2_FORMAT;
47          parameterMap = request.getParameterMap();
48          if (parameterMap.containsKey(OLENCIPConstants.OLE_CIRCULATION_SERVICE)) {
49              service = parameterMap.get(OLENCIPConstants.OLE_CIRCULATION_SERVICE)[0];
50          }
51  
52          if (service != null && !service.equalsIgnoreCase("")) {
53              if (service.equals(OLESIP2Constants.SIP_SERVICE)) {
54                  if (parameterMap.containsKey(OLESIP2Constants.REQUEST_DATA)) {
55  
56                      if (parameterMap.size() >= 2) {
57                          String operatorId = OLESIP2Constants.OPERATOR_ID;
58                          if (parameterMap.containsKey(OLESIP2Constants.LOGIN_USER)) {
59                              Map userMap = new HashMap();
60                              userMap.put(OLEPropertyConstants.PERSON_USER_IDENTIFIER, parameterMap.get(OLESIP2Constants.LOGIN_USER)[0]);
61                              List<PrincipalBo> matching = (List<PrincipalBo>) getBusinessObjectService().findMatching(PrincipalBo.class, userMap);
62                              if (matching != null && matching.size() >= 1) {
63                                  operatorId = matching.get(0).getPrincipalId();
64                              }
65                          }
66                          responseString = oleSip2Service.processRequest(parameterMap.get(OLESIP2Constants.REQUEST_DATA)[0], service, operatorId);
67                          if(parameterMap.get(OLESIP2Constants.REQUEST_DATA)[0].startsWith("99")){
68                              String requestData = parameterMap.get(OLESIP2Constants.REQUEST_DATA)[0].trim();
69                              if(requestData.length() == 19){
70                                  if (requestData.substring(10, 12).equalsIgnoreCase(OLESIP2Constants.SEQUENCE_NUM_CODE)) {
71                                      if (!responseString.contains("|AY")) {
72                                          responseString=responseString.trim()+"|AY"+requestData.substring(12, 15);
73                                          responseString = responseString+MessageUtil.computeChecksum(responseString)+ '\r';
74                                      }
75                                  }
76                              }
77                          }else{
78                              if(parameterMap.get(OLESIP2Constants.REQUEST_DATA)[0].contains("|AY")){
79                                  if(!responseString.contains("|AY")){
80                                      String[] requestDataArray = parameterMap.get(OLESIP2Constants.REQUEST_DATA)[0].split("\\|");
81                                      for (String data : requestDataArray) {
82                                          if(data.startsWith("AY")) {
83                                              responseString = responseString.trim() + "|" + data.substring(0, 5);
84                                              responseString = responseString + MessageUtil.computeChecksum(responseString) + '\r';
85                                          }
86                                      }
87                                  }
88                              }
89                          }
90                      } else {
91                          responseString = oleSip2Service.getCirculationErrorMessage(service, OLENCIPConstants.PARAMETER_MISSING, "502", OLENCIPConstants.LOOKUPUSER, outputFormat);
92                      }
93  
94                  } else {
95  
96                      LOG.info("Unknown Service Name: " + service + "   Parameter is missing");
97                      responseString = oleSip2Service.getCirculationErrorMessage(service, OLENCIPConstants.UNKNOWN_SERVICE, "503", null, outputFormat);
98                      response.sendError(405, "Method Not Supported");
99                  }
100             } else {
101                 responseString = OLENCIPConstants.NULL_SERVICE;
102             }
103             if (responseString != null) {
104                 responseString = responseString.replaceAll("errorMessage", OLENCIPConstants.MESSAGE);
105                 responseString = responseString.replaceAll("<br/>", "");
106             }
107 
108             PrintWriter out = response.getWriter();
109 
110             if (responseString.contains(OLENCIPConstants.INVALID_FORMAT)) {
111                 response.setStatus(406);
112             } else if (responseString.contains(OLENCIPConstants.PARAMETER_MISSING)) {
113                 response.setStatus(422);
114             } else {
115                 response.setStatus(200);
116             }
117             //out.write(URLEncoder.encode(responseString, "UTF-8"));
118             LOG.info("LOG OLESIP2Servlet.doGet olefs  :  "+responseString);
119             out.write(responseString);
120 
121         }
122     }
123 
124 
125 }