View Javadoc

1   package org.kuali.ole.servlet;
2   
3   import org.kuali.ole.OleSRUConstants;
4   import org.kuali.ole.PropertyUtil;
5   import org.kuali.ole.service.OleSRUWebService;
6   import org.kuali.ole.serviceimpl.OleSRUWebServiceImpl;
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.io.PrintWriter;
16  import java.util.ArrayList;
17  import java.util.HashMap;
18  import java.util.List;
19  import java.util.Map;
20  
21  /**
22   * Created with IntelliJ IDEA.
23   * User: ?
24   * Date: 7/10/12
25   * Time: 2:34 PM
26   * To change this template use File | Settings | File Templates.
27   */
28  public class OleSRUServlet extends HttpServlet{
29  
30      final Logger log = LoggerFactory.getLogger(OleSRUServlet.class);
31  
32      @Override
33      public void init() throws ServletException {
34  
35          log.info("init method");
36      }
37  
38      @Override
39      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
40          doGet(request,response);
41      }
42  
43      @Override
44      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
45  
46          log.info("doGet method");
47          OleSRUWebService oleSRUWebService=new OleSRUWebServiceImpl();
48          Map reqParamMap=getReqParameters(request);
49          String xml=oleSRUWebService.getOleSRUResponse(reqParamMap);
50          PrintWriter out=response.getWriter();
51          out.write(xml);
52      }
53  
54      public Map getReqParameters(HttpServletRequest request) {
55  
56  
57          HashMap reqParamMap=new HashMap();
58          reqParamMap.put(OleSRUConstants.OPERATION_TYPE,request.getParameter(OleSRUConstants.OPERATION_TYPE));
59          reqParamMap.put(OleSRUConstants.VERSION,request.getParameter(OleSRUConstants.VERSION));
60          reqParamMap.put(OleSRUConstants.QUERY,request.getParameter(OleSRUConstants.QUERY));
61          if((request.getParameter(OleSRUConstants.START_RECORD))!=null && !"".equals(request.getParameter(OleSRUConstants.START_RECORD))) {
62              int startRecord=Integer.parseInt((String)(request.getParameter(OleSRUConstants.START_RECORD)));
63              reqParamMap.put(OleSRUConstants.START_RECORD,startRecord);
64          }else
65              reqParamMap.put(OleSRUConstants.START_RECORD,Integer.parseInt(PropertyUtil.getPropertyUtil().getProperty(OleSRUConstants.STARTRECORD)));
66  
67          if((request.getParameter(OleSRUConstants.MAXIMUM_RECORDS))!=null && !"".equals(request.getParameter(OleSRUConstants.MAXIMUM_RECORDS))) {
68              int maxRecord=Integer.parseInt((String)(request.getParameter(OleSRUConstants.MAXIMUM_RECORDS)));
69              reqParamMap.put(OleSRUConstants.MAXIMUM_RECORDS,maxRecord);
70          }else
71              reqParamMap.put(OleSRUConstants.MAXIMUM_RECORDS,Integer.parseInt(PropertyUtil.getPropertyUtil().getProperty(OleSRUConstants.MAXRECORD)));
72  
73          if((request.getParameter(OleSRUConstants.RECORD_PACKING))!=null && !"".equals(request.getParameter(OleSRUConstants.RECORD_PACKING))) {
74              String recordPacking=(String)(request.getParameter(OleSRUConstants.RECORD_PACKING));
75              reqParamMap.put(OleSRUConstants.RECORD_PACKING,recordPacking);
76          }else
77              reqParamMap.put(OleSRUConstants.RECORD_PACKING,OleSRUConstants.RECORD_PACK_XML);
78  
79         if((request.getParameter(OleSRUConstants.SORTKEYS))!=null &&  !"".equals(request.getParameter(OleSRUConstants.SORTKEYS))) {
80              String sortKey=(String)(request.getParameter(OleSRUConstants.SORTKEYS));
81              reqParamMap.put(OleSRUConstants.SORTKEYS,sortKey);
82          }else
83              reqParamMap.put(OleSRUConstants.SORTKEYS,OleSRUConstants.TITLE_SORT_KEYS);
84  
85          if((request.getParameter(OleSRUConstants.STYLE_SHEET))!=null) {
86              reqParamMap.put(OleSRUConstants.STYLE_SHEET,request.getParameter(OleSRUConstants.STYLE_SHEET));
87          }
88  
89          if((request.getParameter(OleSRUConstants.RECORD_SCHEMA))!=null) {
90              if(OleSRUConstants.DC_RECORD_SCHEMA.equals((String)(request.getParameter(OleSRUConstants.RECORD_SCHEMA))))
91                  reqParamMap.put(OleSRUConstants.RECORD_SCHEMA,OleSRUConstants.DUBLIN_RECORD_SCHEMA);
92              else
93                  reqParamMap.put(OleSRUConstants.RECORD_SCHEMA,request.getParameter(OleSRUConstants.RECORD_SCHEMA));
94          }
95  
96          setExtraRequestData(request,reqParamMap);
97          return reqParamMap;
98  
99      }
100 
101     public void setExtraRequestData(HttpServletRequest request,Map reqParamMap){
102 
103         List<String> reqParams=new ArrayList<String>(request.getParameterMap().keySet());
104         reqParams.remove(OleSRUConstants.OPERATION_TYPE);
105         reqParams.remove(OleSRUConstants.VERSION);
106         reqParams.remove(OleSRUConstants.QUERY);
107         reqParams.remove(OleSRUConstants.START_RECORD);
108         reqParams.remove(OleSRUConstants.MAXIMUM_RECORDS);
109         reqParams.remove(OleSRUConstants.RECORD_PACKING);
110         reqParams.remove(OleSRUConstants.SORTKEYS);
111         reqParams.remove(OleSRUConstants.RECORD_SCHEMA);
112         reqParams.remove(OleSRUConstants.STYLE_SHEET);
113         reqParams.remove(OleSRUConstants.RECORD_XPATH);
114         reqParams.remove(OleSRUConstants.RESULTSET_TTL);
115         if(reqParams.size()>0)
116             for(int i=0;i<reqParams.size();i++){
117              if(reqParams.get(i).contains("x-") || reqParams.get(i).contains("X-")) {
118              String extraDataKeyValue[]=reqParams.get(i).split("-");
119              String extraReqDataKey=extraDataKeyValue[extraDataKeyValue.length-1];
120              String extraReqDataValue=(String)request.getParameter(reqParams.get(i));
121              reqParamMap.put(OleSRUConstants.EXTRA_REQ_DATA_KEY,extraReqDataKey);
122              reqParamMap.put(OleSRUConstants.EXTRA_REQ_DATA_VALUE,extraReqDataValue);
123            }
124         }
125 
126     }
127 }