View Javadoc
1   package org.kuali.ole.servlet;
2   
3   import org.kuali.ole.OleSRUConstants;
4   import org.kuali.ole.bo.diagnostics.OleSRUDiagnostic;
5   import org.kuali.ole.bo.diagnostics.OleSRUDiagnostics;
6   import org.kuali.ole.bo.serachRetrieve.OleSRUSearchRetrieveResponse;
7   import org.kuali.ole.handler.OleSRUOpacXMLResponseHandler;
8   import org.kuali.ole.service.OleSRUWebService;
9   import org.kuali.ole.serviceimpl.OLESRUServiceImpl;
10  import org.kuali.ole.serviceimpl.OleSRUWebServiceImpl;
11  import org.kuali.rice.core.api.config.property.ConfigContext;
12  import org.slf4j.Logger;
13  import org.slf4j.LoggerFactory;
14  
15  import javax.servlet.ServletException;
16  import javax.servlet.http.HttpServlet;
17  import javax.servlet.http.HttpServletRequest;
18  import javax.servlet.http.HttpServletResponse;
19  import java.io.IOException;
20  import java.io.PrintWriter;
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  /**
27   * Created with IntelliJ IDEA.
28   * User: ?
29   * Date: 7/10/12
30   * Time: 2:34 PM
31   * To change this template use File | Settings | File Templates.
32   */
33  public class OleSRUServlet extends HttpServlet {
34  
35      final Logger log = LoggerFactory.getLogger(OleSRUServlet.class);
36  
37      @Override
38      public void init() throws ServletException {
39  
40          log.info("init method");
41      }
42  
43      @Override
44      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
45          doGet(request, response);
46      }
47  
48      @Override
49      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
50          log.info("doGet method");
51         int startRecord = -1;
52          OleSRUWebService oleSRUWebService = new OleSRUWebServiceImpl();
53          if ((request.getParameter(OleSRUConstants.START_RECORD)) != null && !"".equals(request.getParameter(OleSRUConstants.START_RECORD))) {
54              startRecord = Integer.parseInt((String) (request.getParameter(OleSRUConstants.START_RECORD)));
55          } else if (request.getParameter(OleSRUConstants.START_RECORD) == null) {
56              startRecord = -1;
57          }
58          Map reqParamMap = getReqParameters(request);
59          String invalidParam = ((String) reqParamMap.get(OleSRUConstants.INVALID_PARAM));
60          String xml = "";
61          OleSRUSearchRetrieveResponse oleSRUSearchRetrieveResponse = new OleSRUSearchRetrieveResponse();
62          if((String) reqParamMap.get(OleSRUConstants.VERSION)!=null)
63          oleSRUSearchRetrieveResponse.setVersion((String) reqParamMap.get(OleSRUConstants.VERSION));
64          else
65              oleSRUSearchRetrieveResponse.setVersion(OleSRUConstants.SRU_VERSION);
66          oleSRUSearchRetrieveResponse.setNumberOfRecords(new Long(0));
67          if (startRecord == 0 || request.getParameter(OleSRUConstants.OperationType)==null || request.getParameter(OleSRUConstants.VERSION)==null || request.getParameter(OleSRUConstants.QUERY)==null || !((String)request.getParameter(OleSRUConstants.VERSION)).equals(OleSRUConstants.SRU_VERSION) ||((((String)request.getParameter(OleSRUConstants.RECORD_SCHEMA))!=null) && ((String)request.getParameter(OleSRUConstants.RECORD_SCHEMA)).equals(OleSRUConstants.DC_RECORD_SCHEMA))){
68              OleSRUDiagnostics oleSRUDiagnostics = new OleSRUDiagnostics();
69              List<OleSRUDiagnostic> oleSRUDiagnosticList = new ArrayList<OleSRUDiagnostic>();
70              OleSRUDiagnostic oleSRUDiagnostic;
71              oleSRUDiagnostic = new OleSRUDiagnostic();
72             // oleSRUDiagnostic.setUri("info:srw/diagnostic/1/38");
73              if(startRecord == 0){
74              oleSRUDiagnostic.setUri("info:srw/diagnostic/1/6");
75              oleSRUDiagnostic.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OleSRUConstants.UN_SUPPORTED_PARAM_VALUE));
76              oleSRUDiagnostic.setDetails(OleSRUConstants.START_RECORD);
77              }
78              if(request.getParameter(OleSRUConstants.OperationType)==null){
79                  oleSRUDiagnostic.setUri("info:srw/diagnostic/1/7");
80                  oleSRUDiagnostic.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OleSRUConstants.MANDATORY));
81                  oleSRUDiagnostic.setDetails(OleSRUConstants.OperationType);
82              }
83              if(request.getParameter(OleSRUConstants.VERSION)==null){
84                  oleSRUDiagnostic.setUri("info:srw/diagnostic/1/7");
85                  oleSRUDiagnostic.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OleSRUConstants.MANDATORY));
86                 if(oleSRUDiagnostic.getDetails()!=null)
87                       oleSRUDiagnostic.setDetails(oleSRUDiagnostic.getDetails() + "," + OleSRUConstants.VERSION);
88                      else
89                  oleSRUDiagnostic.setDetails(OleSRUConstants.VERSION);
90              }
91              if(request.getParameter(OleSRUConstants.QUERY)==null){
92                  oleSRUDiagnostic.setUri("info:srw/diagnostic/1/7");
93                  oleSRUDiagnostic.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OleSRUConstants.MANDATORY));
94                  if(oleSRUDiagnostic.getDetails()!=null)
95                      oleSRUDiagnostic.setDetails(oleSRUDiagnostic.getDetails() + "," + OleSRUConstants.QUERY);
96                  else
97                      oleSRUDiagnostic.setDetails(OleSRUConstants.QUERY);
98              }
99              if(request.getParameter(OleSRUConstants.RECORD_SCHEMA)!=null &&(request.getParameter(OleSRUConstants.RECORD_SCHEMA).equalsIgnoreCase(OleSRUConstants.DC_RECORD_SCHEMA))){
100                 oleSRUDiagnostic.setUri("info:srw/diagnostic/1/7");
101                 oleSRUDiagnostic.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OleSRUConstants.NORECORDS_DIAGNOSTIC_MSG));
102             }
103             if(request.getParameter(OleSRUConstants.VERSION)!=null && !(request.getParameter(OleSRUConstants.VERSION)).equals(OleSRUConstants.SRU_VERSION)){
104                 oleSRUDiagnostic.setUri("info:srw/diagnostic/1/5");
105                 oleSRUDiagnostic.setMessage(request.getParameter(OleSRUConstants.VERSION) + " " + ConfigContext.getCurrentContextConfig().getProperty(OleSRUConstants.UNSUPPORT_VERSION));
106                 if(oleSRUDiagnostic.getDetails()!=null)  {
107                     oleSRUSearchRetrieveResponse.setVersion((OleSRUConstants.SRU_VERSION));
108                     oleSRUDiagnostic.setDetails(oleSRUDiagnostic.getDetails() + "," + OleSRUConstants.SRU_VERSION);
109                 }else{
110                     oleSRUSearchRetrieveResponse.setVersion((OleSRUConstants.SRU_VERSION));
111                     oleSRUDiagnostic.setDetails("Supported " + OleSRUConstants.VERSION + ":" + OleSRUConstants.SRU_VERSION);
112                 }
113                 }
114             oleSRUDiagnosticList.add(oleSRUDiagnostic);
115             oleSRUDiagnostics.setOleSRUDiagnosticList(oleSRUDiagnosticList);
116             oleSRUSearchRetrieveResponse.setOleSRUDiagnostics(oleSRUDiagnostics);
117             OleSRUOpacXMLResponseHandler oleSRUOpacXMLResponseHandler = new OleSRUOpacXMLResponseHandler();
118             xml = oleSRUOpacXMLResponseHandler.toXML(oleSRUSearchRetrieveResponse,(String)reqParamMap.get("recordSchema"));
119         } else if (invalidParam != null) {
120             String[] invalidParams = invalidParam.split(",");
121             if (invalidParams.length > 1) {
122                 OleSRUDiagnostics oleSRUDiagnostics = new OleSRUDiagnostics();
123                 List<OleSRUDiagnostic> oleSRUDiagnosticList = new ArrayList<OleSRUDiagnostic>();
124                 OleSRUDiagnostic oleSRUDiagnostic;
125                 for (int i = 1; i < invalidParams.length; i++) {
126                     oleSRUDiagnostic = new OleSRUDiagnostic();
127                     oleSRUDiagnostic.setUri("info:srw/diagnostic/1/8");
128                     oleSRUDiagnostic.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OleSRUConstants.UN_SUPPORTED_PARAM));
129                     oleSRUDiagnostic.setDetails(invalidParams[i]);
130                     oleSRUDiagnosticList.add(oleSRUDiagnostic);
131                 }
132                 OleSRUSearchRetrieveResponse oleSRUSearchRetrieveResponses = new OleSRUSearchRetrieveResponse();
133                 oleSRUSearchRetrieveResponses.setVersion((String) reqParamMap.get(OleSRUConstants.VERSION));
134                 oleSRUSearchRetrieveResponses.setNumberOfRecords(new Long(0));
135                 oleSRUDiagnostics.setOleSRUDiagnosticList(oleSRUDiagnosticList);
136                 oleSRUSearchRetrieveResponses.setOleSRUDiagnostics(oleSRUDiagnostics);
137                 OleSRUOpacXMLResponseHandler oleSRUOpacXMLResponseHandler = new OleSRUOpacXMLResponseHandler();
138                 xml = oleSRUOpacXMLResponseHandler.toXML(oleSRUSearchRetrieveResponses,(String)reqParamMap.get("recordSchema"));
139             }
140         } else {
141             xml = oleSRUWebService.getOleSRUResponse(reqParamMap);
142         }
143         PrintWriter out = response.getWriter();
144         response.setContentType("text/xml");
145         response.setCharacterEncoding("UTF-8");
146         out.write(xml);
147 
148     }
149 
150     public Map getReqParameters(HttpServletRequest request) {
151         HashMap reqParamMap = new HashMap();
152         reqParamMap.put(OleSRUConstants.OperationType, request.getParameter(OleSRUConstants.OperationType));
153         reqParamMap.put(OleSRUConstants.VERSION, request.getParameter(OleSRUConstants.VERSION));
154         if (request.getParameter(OleSRUConstants.QUERY) != null) {
155             String query = request.getParameter(OleSRUConstants.QUERY);
156             if (query.startsWith("cql.serverChoice")) {
157                 String[] querySplit = query.split("=");
158                 query = querySplit[1];
159             }
160             reqParamMap.put(OleSRUConstants.QUERY, query);
161         }
162         if ((request.getParameter(OleSRUConstants.START_RECORD)) != null && !"".equals(request.getParameter(OleSRUConstants.START_RECORD))) {
163             int startRecord = Integer.parseInt((String) (request.getParameter(OleSRUConstants.START_RECORD)));
164             reqParamMap.put(OleSRUConstants.START_RECORD, startRecord - 1);
165         } else
166             reqParamMap.put(OleSRUConstants.START_RECORD, Integer.parseInt(ConfigContext.getCurrentContextConfig().getProperty(OleSRUConstants.STARTRECORD)));
167 
168         if ((request.getParameter(OleSRUConstants.MAXIMUM_RECORDS)) != null && !"".equals(request.getParameter(OleSRUConstants.MAXIMUM_RECORDS))) {
169             int maxRecord = Integer.parseInt((String) (request.getParameter(OleSRUConstants.MAXIMUM_RECORDS)));
170             reqParamMap.put(OleSRUConstants.MAXIMUM_RECORDS, maxRecord);
171 
172         } else
173             reqParamMap.put(OleSRUConstants.MAXIMUM_RECORDS, Integer.parseInt(ConfigContext.getCurrentContextConfig().getProperty(OleSRUConstants.MAXRECORD)));
174 
175         if ((request.getParameter(OleSRUConstants.RECORD_PACKING)) != null && !"".equals(request.getParameter(OleSRUConstants.RECORD_PACKING))) {
176             String recordPacking = (String) (request.getParameter(OleSRUConstants.RECORD_PACKING));
177             reqParamMap.put(OleSRUConstants.RECORD_PACKING, recordPacking);
178         } else
179             reqParamMap.put(OleSRUConstants.RECORD_PACKING, OleSRUConstants.RECORD_PACK_XML);
180 
181         if ((request.getParameter(OleSRUConstants.SORTKEYS)) != null && !"".equals(request.getParameter(OleSRUConstants.SORTKEYS))) {
182             String sortKey = (String) (request.getParameter(OleSRUConstants.SORTKEYS));
183             reqParamMap.put(OleSRUConstants.SORTKEYS, sortKey);
184         } else
185             reqParamMap.put(OleSRUConstants.SORTKEYS, OleSRUConstants.TITLE_SORT_KEYS);
186 
187         if ((request.getParameter(OleSRUConstants.STYLE_SHEET)) != null) {
188             reqParamMap.put(OleSRUConstants.STYLE_SHEET, request.getParameter(OleSRUConstants.STYLE_SHEET));
189         }
190 
191         if ((request.getParameter(OleSRUConstants.RECORD_SCHEMA)) != null) {
192             if (OleSRUConstants.DC_RECORD_SCHEMA.equals((String) (request.getParameter(OleSRUConstants.RECORD_SCHEMA))))
193                 reqParamMap.put(OleSRUConstants.RECORD_SCHEMA, OleSRUConstants.DC_RECORD_SCHEMA);
194             else if (OleSRUConstants.MARC_RECORD_SCHEMA.equals((String) (request.getParameter(OleSRUConstants.RECORD_SCHEMA)))) {
195                 reqParamMap.put(OleSRUConstants.RECORD_SCHEMA, OleSRUConstants.MARC_RECORD_SCHEMA);
196             } else
197                 reqParamMap.put(OleSRUConstants.RECORD_SCHEMA, request.getParameter(OleSRUConstants.RECORD_SCHEMA));
198         } else {
199             reqParamMap.put(OleSRUConstants.RECORD_SCHEMA, OleSRUConstants.MARC_RECORD_SCHEMA);
200 
201 
202         }
203 
204         setExtraRequestData(request, reqParamMap);
205         return reqParamMap;
206 
207     }
208 
209     public void setExtraRequestData(HttpServletRequest request, Map reqParamMap) {
210 
211         List<String> reqParams = new ArrayList<String>(request.getParameterMap().keySet());
212         reqParams.remove(OleSRUConstants.OperationType);
213         reqParams.remove(OleSRUConstants.VERSION);
214         reqParams.remove(OleSRUConstants.QUERY);
215         reqParams.remove(OleSRUConstants.START_RECORD);
216         reqParams.remove(OleSRUConstants.MAXIMUM_RECORDS);
217         reqParams.remove(OleSRUConstants.RECORD_PACKING);
218         reqParams.remove(OleSRUConstants.SORTKEYS);
219         reqParams.remove(OleSRUConstants.RECORD_SCHEMA);
220         reqParams.remove(OleSRUConstants.STYLE_SHEET);
221         reqParams.remove(OleSRUConstants.RECORD_XPATH);
222         reqParams.remove(OleSRUConstants.RESULTSET_TTL);
223         if (reqParams.size() > 0) {
224             StringBuffer invalidParameterBuffer = new StringBuffer();
225             for (int i = 0; i < reqParams.size(); i++) {
226                 if (reqParams.get(i).contains("x-") || reqParams.get(i).contains("X-")) {
227                     String extraDataKeyValue[] = reqParams.get(i).split("-");
228                     String extraReqDataKey = extraDataKeyValue[extraDataKeyValue.length - 1];
229                     String extraReqDataValue = (String) request.getParameter(reqParams.get(i));
230                     reqParamMap.put(OleSRUConstants.EXTRA_REQ_DATA_KEY, extraReqDataKey);
231                     reqParamMap.put(OleSRUConstants.EXTRA_REQ_DATA_VALUE, extraReqDataValue);
232                 }
233                 invalidParameterBuffer.append("," + reqParams.get(i));
234             }
235             reqParamMap.put(OleSRUConstants.INVALID_PARAM, invalidParameterBuffer.toString());
236         }
237 
238     }
239 
240 }