Revision 55643
Added by Katerina Iatropoulou almost 5 years ago
SearchRequestController.java | ||
---|---|---|
31 | 31 |
@Controller |
32 | 32 |
public class SearchRequestController { |
33 | 33 |
|
34 |
@Autowired
|
|
35 |
private SearchServiceImpl searchService = null;
|
|
36 |
@Autowired
|
|
37 |
private VocabularyManager vocabularyManager = null;
|
|
34 |
@Autowired
|
|
35 |
private SearchServiceImpl searchService = null;
|
|
36 |
@Autowired
|
|
37 |
private VocabularyManager vocabularyManager = null;
|
|
38 | 38 |
|
39 |
@Resource
|
|
40 |
private String maxResults = null;
|
|
39 |
@Resource
|
|
40 |
private String maxResults = null;
|
|
41 | 41 |
|
42 |
@Resource
|
|
43 |
private String maxSize = null;
|
|
42 |
@Resource
|
|
43 |
private String maxSize = null;
|
|
44 | 44 |
|
45 |
private static Logger logger = Logger.getLogger(SearchRequestController.class);
|
|
45 |
private static Logger logger = Logger.getLogger(SearchRequestController.class);
|
|
46 | 46 |
|
47 |
private static final String XML_CONTENT_TYPE = "application/xml;charset=UTF-8";
|
|
48 |
private static final String JSON_CONTENT_TYPE = "application/json;charset=UTF-8;";
|
|
47 |
private static final String XML_CONTENT_TYPE = "application/xml;charset=UTF-8";
|
|
48 |
private static final String JSON_CONTENT_TYPE = "application/json;charset=UTF-8;";
|
|
49 | 49 |
private static final String CSV_CONTENT_TYPE = "text/csv;charset=UTF-8;"; |
50 | 50 |
private static final String TSV_CONTENT_TYPE = "text/tab-separated-values;charset=UTF-8;"; |
51 | 51 |
private static final String HTML_CONTENT_TYPE = "text/html;charset=UTF-8;"; |
... | ... | |
76 | 76 |
private static final List<String> PROJECT_FORMATS = Arrays.asList("xml", "json", "csv", "tsv", "html"); |
77 | 77 |
|
78 | 78 |
//TODO this is for joomla - to be removed soon |
79 |
@RequestMapping(value = "/search", method = RequestMethod.GET)
|
|
80 |
public void search(HttpServletRequest request, HttpServletResponse response) {
|
|
81 |
PrintWriter writer = null;
|
|
82 |
FormattedSearchResult formattedSearchResult = null;
|
|
79 |
@RequestMapping(value = "/search", method = RequestMethod.GET)
|
|
80 |
public void search(HttpServletRequest request, HttpServletResponse response) {
|
|
81 |
PrintWriter writer = null;
|
|
82 |
FormattedSearchResult formattedSearchResult = null;
|
|
83 | 83 |
|
84 | 84 |
String action = readActionParameter(request); |
85 | 85 |
String query = readQueryParameter(request); |
86 |
String format = (request.getParameter("format") != null) ? request.getParameter("format") : "xml";
|
|
86 |
String format = (request.getParameter("format") != null) ? request.getParameter("format") : "xml";
|
|
87 | 87 |
createResponseMeta(response, format); |
88 | 88 |
|
89 |
String locale = request.getParameter("locale");
|
|
89 |
String locale = request.getParameter("locale");
|
|
90 | 90 |
|
91 | 91 |
int page= 0; |
92 | 92 |
int size =-1; |
... | ... | |
95 | 95 |
page = readParameter(request, "page", 1); |
96 | 96 |
size = readParameter(request, "size", 10); |
97 | 97 |
} |
98 |
|
|
99 |
if (size > Integer.parseInt(maxSize)) { |
|
100 |
throw new IllegalArgumentException("Size argument have exceeded the maximum allowed number."); |
|
101 |
} |
|
102 | 98 |
|
103 |
String sTransformer = request.getParameter("sTransformer");
|
|
104 |
String rTransformer = request.getParameter("rTransformer");
|
|
105 |
Collection<String> fields = readParameter(request, "fields");
|
|
99 |
if (size > Integer.parseInt(maxSize)) {
|
|
100 |
throw new IllegalArgumentException("Size argument have exceeded the maximum allowed number.");
|
|
101 |
}
|
|
106 | 102 |
|
103 |
String sTransformer = request.getParameter("sTransformer"); |
|
104 |
String rTransformer = request.getParameter("rTransformer"); |
|
105 |
Collection<String> fields = readParameter(request, "fields"); |
|
106 |
|
|
107 | 107 |
checkTransformerParameters(action, sTransformer, rTransformer, fields); |
108 | 108 |
|
109 | 109 |
try { |
... | ... | |
114 | 114 |
sTransformer, rTransformer, format, locale, page, size, |
115 | 115 |
fields); |
116 | 116 |
|
117 |
writer.append(formattedSearchResult.getFormattedResult());
|
|
117 |
writer.append(formattedSearchResult.getFormattedResult());
|
|
118 | 118 |
|
119 |
} catch (Exception e) {
|
|
120 |
logger.error("Fail to execute search.", e);
|
|
121 |
createXmlErrorPage(writer, e);
|
|
119 |
} catch (Exception e) {
|
|
120 |
logger.error("Fail to execute search.", e);
|
|
121 |
createXmlErrorPage(writer, e);
|
|
122 | 122 |
|
123 |
} finally {
|
|
123 |
} finally {
|
|
124 | 124 |
if (writer != null) { |
125 | 125 |
writer.close(); |
126 | 126 |
} |
127 |
}
|
|
128 |
}
|
|
127 |
}
|
|
128 |
}
|
|
129 | 129 |
|
130 | 130 |
private void checkTransformerParameters(String action, String sTransformer, String rTransformer, Collection<String> fields) { |
131 | 131 |
if (action.equals("search")) { |
... | ... | |
189 | 189 |
return "openSearchDescriptor"; |
190 | 190 |
} |
191 | 191 |
|
192 |
@RequestMapping(value = "/api/publications", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
|
|
193 |
public void searchPublications(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
192 |
@RequestMapping(value = "/api/publications", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
|
|
193 |
public void searchPublications(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
194 | 194 |
|
195 |
long time = System.currentTimeMillis();
|
|
195 |
long time = System.currentTimeMillis();
|
|
196 | 196 |
checkParameters(ListUtils.union(PUB_N_DATA_COMMON_PARAMETERS, PUB_PARAMETERS),request.getParameterMap()); |
197 | 197 |
|
198 | 198 |
int page = readParameter(request, "page", 1); |
... | ... | |
231 | 231 |
try { |
232 | 232 |
formattedSearchResult = searchService.search(queryBuilder.toString(),sTransformer, (newFormat!=null)?newFormat:format, locale, page, size); |
233 | 233 |
|
234 |
} catch (Exception e) {
|
|
235 |
logger.error("Fail to execute search.", e);
|
|
236 |
throw new Exception("Fail to execute search", e);
|
|
234 |
} catch (Exception e) {
|
|
235 |
logger.error("Fail to execute search.", e);
|
|
236 |
throw new Exception("Fail to execute search", e);
|
|
237 | 237 |
|
238 |
}
|
|
238 |
}
|
|
239 | 239 |
|
240 | 240 |
PrintWriter writer = response.getWriter(); |
241 | 241 |
writer.append(formattedSearchResult.getFormattedResult()); |
... | ... | |
243 | 243 |
|
244 | 244 |
time = System.currentTimeMillis() - time; |
245 | 245 |
logger.debug("Answer old time " + time); |
246 |
}
|
|
246 |
}
|
|
247 | 247 |
|
248 |
@RequestMapping(value = "/api/datasets", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
|
|
249 |
public void searchData(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
248 |
@RequestMapping(value = "/api/datasets", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
|
|
249 |
public void searchData(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
250 | 250 |
|
251 | 251 |
checkParameters(ListUtils.union(PUB_N_DATA_COMMON_PARAMETERS, DATA_PARAMETERS),request.getParameterMap()); |
252 | 252 |
|
... | ... | |
286 | 286 |
try { |
287 | 287 |
formattedSearchResult = searchService.search(queryBuilder.toString(),sTransformer, (newFormat!=null)?newFormat:format, locale, page, size); |
288 | 288 |
|
289 |
} catch (Exception e) {
|
|
289 |
} catch (Exception e) {
|
|
290 | 290 |
logger.error("Fail to execute search.", e); |
291 | 291 |
throw new Exception("Fail to execute search.", e); |
292 | 292 |
} |
... | ... | |
295 | 295 |
writer.append(formattedSearchResult.getFormattedResult()); |
296 | 296 |
writer.close(); |
297 | 297 |
|
298 |
}
|
|
298 |
}
|
|
299 | 299 |
|
300 | 300 |
@RequestMapping(value = "/api/software", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE}) |
301 | 301 |
public void searchSoftware(HttpServletRequest request, HttpServletResponse response) throws Exception { |
... | ... | |
403 | 403 |
|
404 | 404 |
} |
405 | 405 |
|
406 |
@RequestMapping(value = "/api/projects", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
|
|
407 |
public void searchProjects(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
406 |
@RequestMapping(value = "/api/projects", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
|
|
407 |
public void searchProjects(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
408 | 408 |
|
409 |
checkParameters(PROJECT_PARAMETERS, request.getParameterMap());
|
|
409 |
checkParameters(PROJECT_PARAMETERS, request.getParameterMap());
|
|
410 | 410 |
|
411 | 411 |
int page = readParameter(request, "page", 1); |
412 | 412 |
int size = readParameter(request, "size", 10); |
... | ... | |
445 | 445 |
PrintWriter writer = response.getWriter(); |
446 | 446 |
writer.append(formattedSearchResult.getFormattedResult()); |
447 | 447 |
writer.close(); |
448 |
}
|
|
448 |
}
|
|
449 | 449 |
|
450 | 450 |
|
451 | 451 |
@ExceptionHandler(IllegalArgumentException.class) |
... | ... | |
497 | 497 |
* @return |
498 | 498 |
|
499 | 499 |
private String defineTransformer(String model) { |
500 |
if (model!=null && !model.trim().isEmpty()) {
|
|
501 |
if (model.equals("openaire")) {
|
|
502 |
return null;
|
|
500 |
if (model!=null && !model.trim().isEmpty()) { |
|
501 |
if (model.equals("openaire")) { |
|
502 |
return null; |
|
503 | 503 |
|
504 |
} else if (model.equals("sygma")) {
|
|
505 |
return "results_openaire";
|
|
506 |
}
|
|
507 |
else if (model.equals("dc")) {
|
|
508 |
return "dc";
|
|
509 |
}
|
|
504 |
} else if (model.equals("sygma")) { |
|
505 |
return "results_openaire"; |
|
506 |
} |
|
507 |
else if (model.equals("dc")) { |
|
508 |
return "dc"; |
|
509 |
} |
|
510 | 510 |
|
511 |
throw new IllegalArgumentException("model '" + model + "' is not supported.");
|
|
511 |
throw new IllegalArgumentException("model '" + model + "' is not supported."); |
|
512 | 512 |
|
513 |
} else {
|
|
514 |
return null;
|
|
515 |
}
|
|
513 |
} else { |
|
514 |
return null; |
|
515 |
} |
|
516 | 516 |
}*/ |
517 | 517 |
|
518 | 518 |
//TODO: check if needed |
519 | 519 |
private String defineTransformer(String model, String format) { |
520 |
if (model != null && !model.trim().isEmpty() && format != null && (format.equals("json")||format.equals("xml"))) { |
|
520 |
if (model != null && !model.trim().isEmpty() && format != null && (format.equals("json")||format.equals("xml"))) {
|
|
521 | 521 |
if (model.equals("openaire")) { |
522 | 522 |
return null; |
523 | 523 |
|
... | ... | |
530 | 530 |
|
531 | 531 |
throw new IllegalArgumentException("model '" + model + "' is not supported."); |
532 | 532 |
|
533 |
} else if (format != null && !format.trim().isEmpty()) { |
|
534 |
if (format.equals("rss") || format.equals("csv") || format.equals("tsv") || format.equals("html")) { |
|
535 |
return "results_openaire"; |
|
533 |
} else if (format != null && !format.trim().isEmpty()) { |
|
534 |
if (format.equals("rss") || format.equals("csv") || format.equals("tsv") || format.equals("html")) { |
|
535 |
return "results_openaire"; |
|
536 |
} |
|
536 | 537 |
} |
537 |
} |
|
538 | 538 |
|
539 |
return null; |
|
540 |
|
|
539 |
return null;
|
|
540 |
|
|
541 | 541 |
} |
542 | 542 |
|
543 | 543 |
private String defineFormatter(String model, String format, boolean isPublications, Collection<String> referrers) { |
... | ... | |
562 | 562 |
|
563 | 563 |
@Deprecated |
564 | 564 |
private void createXmlErrorPage(PrintWriter writer, Exception e) { |
565 |
writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
|
|
566 |
writer.append("<error>");
|
|
567 |
writer.append("<exception>")
|
|
568 |
.append(StringEscapeUtils.escapeXml(e.getClass().getName()))
|
|
569 |
.append("</exception>");
|
|
570 |
if (e.getMessage() != null) {
|
|
571 |
writer.append("<message>")
|
|
572 |
.append(StringEscapeUtils.escapeXml(e.getMessage()))
|
|
573 |
.append("</message>");
|
|
574 |
}
|
|
575 |
if (e.getCause() != null) {
|
|
576 |
writer.append("<cause>")
|
|
577 |
.append(StringEscapeUtils.escapeXml(e.getCause()
|
|
578 |
.toString())).append("</cause>");
|
|
579 |
}
|
|
565 |
writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
|
|
566 |
writer.append("<error>");
|
|
567 |
writer.append("<exception>")
|
|
568 |
.append(StringEscapeUtils.escapeXml(e.getClass().getName()))
|
|
569 |
.append("</exception>");
|
|
570 |
if (e.getMessage() != null) {
|
|
571 |
writer.append("<message>")
|
|
572 |
.append(StringEscapeUtils.escapeXml(e.getMessage()))
|
|
573 |
.append("</message>");
|
|
574 |
}
|
|
575 |
if (e.getCause() != null) {
|
|
576 |
writer.append("<cause>")
|
|
577 |
.append(StringEscapeUtils.escapeXml(e.getCause()
|
|
578 |
.toString())).append("</cause>");
|
|
579 |
}
|
|
580 | 580 |
|
581 |
StackTraceElement[] trace = e.getStackTrace();
|
|
582 |
writer.append("<trace>");
|
|
583 |
for (int i = 0; i < trace.length; i++) {
|
|
584 |
writer.append(StringEscapeUtils.escapeXml(trace[i].toString()))
|
|
585 |
.append("\n");
|
|
586 |
}
|
|
587 |
writer.append("</trace>");
|
|
581 |
StackTraceElement[] trace = e.getStackTrace();
|
|
582 |
writer.append("<trace>");
|
|
583 |
for (int i = 0; i < trace.length; i++) {
|
|
584 |
writer.append(StringEscapeUtils.escapeXml(trace[i].toString()))
|
|
585 |
.append("\n");
|
|
586 |
}
|
|
587 |
writer.append("</trace>");
|
|
588 | 588 |
|
589 |
writer.append("</error>");
|
|
590 |
}
|
|
589 |
writer.append("</error>");
|
|
590 |
}
|
|
591 | 591 |
|
592 | 592 |
// Reading parameters from url and adding default values |
593 | 593 |
public int readParameter(HttpServletRequest request, String parameterName, int defaultValue) { |
... | ... | |
608 | 608 |
} |
609 | 609 |
|
610 | 610 |
/** |
611 |
* Checks if the requested result size exceeds the maximum allowed numbers of returned results
|
|
612 |
* @param page the page parameter
|
|
613 |
* @param size the size parameter
|
|
614 |
*/
|
|
615 |
private void checkRequestSize(int page, int size) {
|
|
616 |
int total = page*size;
|
|
617 |
if (total > Integer.parseInt(maxResults)) {
|
|
618 |
throw new IllegalArgumentException("Size and page arguments have exceeded the maximum number of returned results.");
|
|
619 |
}
|
|
620 |
}
|
|
611 |
* Checks if the requested result size exceeds the maximum allowed numbers of returned results
|
|
612 |
* @param page the page parameter
|
|
613 |
* @param size the size parameter
|
|
614 |
*/
|
|
615 |
private void checkRequestSize(int page, int size) {
|
|
616 |
int total = page*size;
|
|
617 |
if (total > Integer.parseInt(maxResults)) {
|
|
618 |
throw new IllegalArgumentException("Size and page arguments have exceeded the maximum number of returned results.");
|
|
619 |
}
|
|
620 |
}
|
|
621 | 621 |
|
622 | 622 |
private String defineContentType(String format) { |
623 | 623 |
if (format != null) { |
... | ... | |
626 | 626 |
} else if (format.equalsIgnoreCase("json")) { |
627 | 627 |
return JSON_CONTENT_TYPE; |
628 | 628 |
|
629 |
} else if (format.equalsIgnoreCase("csv")) {
|
|
629 |
} else if (format.equalsIgnoreCase("csv")) {
|
|
630 | 630 |
return CSV_CONTENT_TYPE; |
631 | 631 |
|
632 | 632 |
} else if (format.equalsIgnoreCase("tsv")) { |
Also available in: Unified diff
merge uoa-search solr7 branch 50700:55641 into trunk