001package org.kuali.ole.servlet; 002 003import org.kuali.ole.OleSRUConstants; 004import org.kuali.ole.bo.diagnostics.OleSRUDiagnostic; 005import org.kuali.ole.bo.diagnostics.OleSRUDiagnostics; 006import org.kuali.ole.bo.serachRetrieve.OleSRUSearchRetrieveResponse; 007import org.kuali.ole.handler.OleSRUOpacXMLResponseHandler; 008import org.kuali.ole.service.OleSRUWebService; 009import org.kuali.ole.serviceimpl.OLESRUServiceImpl; 010import org.kuali.ole.serviceimpl.OleSRUWebServiceImpl; 011import org.kuali.rice.core.api.config.property.ConfigContext; 012import org.slf4j.Logger; 013import org.slf4j.LoggerFactory; 014 015import javax.servlet.ServletException; 016import javax.servlet.http.HttpServlet; 017import javax.servlet.http.HttpServletRequest; 018import javax.servlet.http.HttpServletResponse; 019import java.io.IOException; 020import java.io.PrintWriter; 021import java.util.ArrayList; 022import java.util.HashMap; 023import java.util.List; 024import java.util.Map; 025 026/** 027 * Created with IntelliJ IDEA. 028 * User: ? 029 * Date: 7/10/12 030 * Time: 2:34 PM 031 * To change this template use File | Settings | File Templates. 032 */ 033public class OleSRUServlet extends HttpServlet { 034 035 final Logger log = LoggerFactory.getLogger(OleSRUServlet.class); 036 037 @Override 038 public void init() throws ServletException { 039 040 log.info("init method"); 041 } 042 043 @Override 044 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 045 doGet(request, response); 046 } 047 048 @Override 049 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 050 log.info("doGet method"); 051 int startRecord = -1; 052 OleSRUWebService oleSRUWebService = new OleSRUWebServiceImpl(); 053 if ((request.getParameter(OleSRUConstants.START_RECORD)) != null && !"".equals(request.getParameter(OleSRUConstants.START_RECORD))) { 054 startRecord = Integer.parseInt((String) (request.getParameter(OleSRUConstants.START_RECORD))); 055 } else if (request.getParameter(OleSRUConstants.START_RECORD) == null) { 056 startRecord = -1; 057 } 058 Map reqParamMap = getReqParameters(request); 059 String invalidParam = ((String) reqParamMap.get(OleSRUConstants.INVALID_PARAM)); 060 String xml = ""; 061 OleSRUSearchRetrieveResponse oleSRUSearchRetrieveResponse = new OleSRUSearchRetrieveResponse(); 062 if((String) reqParamMap.get(OleSRUConstants.VERSION)!=null) 063 oleSRUSearchRetrieveResponse.setVersion((String) reqParamMap.get(OleSRUConstants.VERSION)); 064 else 065 oleSRUSearchRetrieveResponse.setVersion(OleSRUConstants.SRU_VERSION); 066 oleSRUSearchRetrieveResponse.setNumberOfRecords(new Long(0)); 067 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))){ 068 OleSRUDiagnostics oleSRUDiagnostics = new OleSRUDiagnostics(); 069 List<OleSRUDiagnostic> oleSRUDiagnosticList = new ArrayList<OleSRUDiagnostic>(); 070 OleSRUDiagnostic oleSRUDiagnostic; 071 oleSRUDiagnostic = new OleSRUDiagnostic(); 072 // oleSRUDiagnostic.setUri("info:srw/diagnostic/1/38"); 073 if(startRecord == 0){ 074 oleSRUDiagnostic.setUri("info:srw/diagnostic/1/6"); 075 oleSRUDiagnostic.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OleSRUConstants.UN_SUPPORTED_PARAM_VALUE)); 076 oleSRUDiagnostic.setDetails(OleSRUConstants.START_RECORD); 077 } 078 if(request.getParameter(OleSRUConstants.OperationType)==null){ 079 oleSRUDiagnostic.setUri("info:srw/diagnostic/1/7"); 080 oleSRUDiagnostic.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OleSRUConstants.MANDATORY)); 081 oleSRUDiagnostic.setDetails(OleSRUConstants.OperationType); 082 } 083 if(request.getParameter(OleSRUConstants.VERSION)==null){ 084 oleSRUDiagnostic.setUri("info:srw/diagnostic/1/7"); 085 oleSRUDiagnostic.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OleSRUConstants.MANDATORY)); 086 if(oleSRUDiagnostic.getDetails()!=null) 087 oleSRUDiagnostic.setDetails(oleSRUDiagnostic.getDetails() + "," + OleSRUConstants.VERSION); 088 else 089 oleSRUDiagnostic.setDetails(OleSRUConstants.VERSION); 090 } 091 if(request.getParameter(OleSRUConstants.QUERY)==null){ 092 oleSRUDiagnostic.setUri("info:srw/diagnostic/1/7"); 093 oleSRUDiagnostic.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OleSRUConstants.MANDATORY)); 094 if(oleSRUDiagnostic.getDetails()!=null) 095 oleSRUDiagnostic.setDetails(oleSRUDiagnostic.getDetails() + "," + OleSRUConstants.QUERY); 096 else 097 oleSRUDiagnostic.setDetails(OleSRUConstants.QUERY); 098 } 099 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}