Revision 55639
Added by Katerina Iatropoulou almost 5 years ago
SearchRequestController.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.data.search.web.api; |
2 | 2 |
|
3 | 3 |
import com.google.common.collect.Iterables; |
4 |
import eu.dnetlib.api.data.SearchServiceException; |
|
4 | 5 |
import eu.dnetlib.data.search.app.SearchServiceImpl; |
5 | 6 |
import eu.dnetlib.data.search.utils.cql.ParameterQueryEnhancer; |
6 | 7 |
import eu.dnetlib.data.search.utils.vocabulary.VocabularyManager; |
... | ... | |
11 | 12 |
import org.apache.commons.lang.StringEscapeUtils; |
12 | 13 |
import org.apache.log4j.Logger; |
13 | 14 |
import org.springframework.beans.factory.annotation.Autowired; |
15 |
import org.springframework.http.*; |
|
14 | 16 |
import org.springframework.stereotype.Controller; |
15 | 17 |
import org.springframework.ui.ModelMap; |
18 |
import org.springframework.web.bind.annotation.ExceptionHandler; |
|
16 | 19 |
import org.springframework.web.bind.annotation.RequestMapping; |
17 | 20 |
import org.springframework.web.bind.annotation.RequestMethod; |
21 |
import org.springframework.web.bind.annotation.ResponseBody; |
|
18 | 22 |
|
19 | 23 |
import javax.annotation.Resource; |
20 | 24 |
import javax.servlet.http.HttpServletRequest; |
21 | 25 |
import javax.servlet.http.HttpServletResponse; |
26 |
import java.io.IOException; |
|
22 | 27 |
import java.io.PrintWriter; |
23 | 28 |
import java.text.SimpleDateFormat; |
24 | 29 |
import java.util.*; |
... | ... | |
48 | 53 |
private static final String PUBLICATION_BASIC_QUERY = "(oaftype exact result) and (resulttypeid exact publication)"; |
49 | 54 |
private static final String DATASET_BASIC_QUERY = "(oaftype exact result) and (resulttypeid exact dataset)"; |
50 | 55 |
private static final String SOFTWARE_BASIC_QUERY = "(oaftype exact result) and (resulttypeid exact software)"; |
56 |
private static final String OTHER_BASIC_QUERY = "(oaftype exact result) and (resulttypeid exact other)"; |
|
51 | 57 |
private static final String PROJECT_BASIC_QUERY = "(oaftype exact project)"; |
52 | 58 |
|
53 | 59 |
private static final List<String> GLOBAL_PARAMETERS = Arrays.asList("page", "size", "format", "sortBy"); |
54 | 60 |
|
55 |
private static final List<String> PUB_N_DATA_COMMON_PARAMETERS = Arrays.asList("author", "doi", "FP7ProjectID", "FP7scientificArea", |
|
61 |
private static final List<String> PUB_N_DATA_COMMON_PARAMETERS = Arrays.asList("author", "doi", "community", "FP7ProjectID", "FP7scientificArea",
|
|
56 | 62 |
"fromDateAccepted", "funder", "fundingStream", "hasECFunding", "hasProject", "hasWTFunding", "keywords", |
57 | 63 |
"model", "OA", "openaireProjectID", "openaireProviderID", "projectID", "title", "toDateAccepted"); |
58 | 64 |
|
59 | 65 |
private static final List<String> PUB_PARAMETERS = Arrays.asList("openairePublicationID"); |
60 | 66 |
private static final List<String> DATA_PARAMETERS = Arrays.asList("openaireDatasetID"); |
61 | 67 |
private static final List<String> SOFTWARE_PARAMETERS = Arrays.asList("openaireSoftwareID"); |
68 |
private static final List<String> OTHER_PARAMETERS = Arrays.asList("openaireOtherID"); |
|
62 | 69 |
|
63 | 70 |
private static final List<String> PUB_N_DATASET_MODELS = Arrays.asList("dc", "openaire", "sygma"); |
64 | 71 |
private static final List<String> PUB_N_DATASET_FORMATS = Arrays.asList("json", "rss", "xml", "csv", "tsv", "html"); |
... | ... | |
68 | 75 |
"participantAcronyms", "participantCountries", "startYear", "sc39", "openaireParticipantID", "openaireProjectID"); |
69 | 76 |
private static final List<String> PROJECT_FORMATS = Arrays.asList("xml", "json", "csv", "tsv", "html"); |
70 | 77 |
|
78 |
//TODO this is for joomla - to be removed soon |
|
71 | 79 |
@RequestMapping(value = "/search", method = RequestMethod.GET) |
72 | 80 |
public void search(HttpServletRequest request, HttpServletResponse response) { |
73 | 81 |
PrintWriter writer = null; |
... | ... | |
185 | 193 |
return "openSearchDescriptor"; |
186 | 194 |
} |
187 | 195 |
|
188 |
@RequestMapping(value = "/api/publications", method = RequestMethod.GET) |
|
189 |
public void searchPublications(HttpServletRequest request, HttpServletResponse response) { |
|
196 |
@RequestMapping(value = "/api/publications", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
|
|
197 |
public void searchPublications(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
190 | 198 |
|
191 | 199 |
long time = System.currentTimeMillis(); |
200 |
checkParameters(ListUtils.union(PUB_N_DATA_COMMON_PARAMETERS, PUB_PARAMETERS),request.getParameterMap()); |
|
192 | 201 |
|
193 |
PrintWriter writer = null; |
|
194 |
|
|
195 |
try { |
|
196 |
writer = response.getWriter(); |
|
197 |
|
|
198 |
checkParameters(ListUtils.union(PUB_N_DATA_COMMON_PARAMETERS, PUB_PARAMETERS),request.getParameterMap()); |
|
199 |
|
|
200 | 202 |
int page = readParameter(request, "page", 1); |
201 | 203 |
int size = readParameter(request, "size", 10); |
202 | 204 |
checkRequestSize(page, size); |
... | ... | |
221 | 223 |
ParameterQueryEnhancer.enhanceQueryWithOpenAIREIds(queryBuilder, request); |
222 | 224 |
ParameterQueryEnhancer.enhanceQueryWithMetadataKeywords(queryBuilder, request); |
223 | 225 |
ParameterQueryEnhancer.enhanceQueryWithFundingParams(queryBuilder, request); |
226 |
ParameterQueryEnhancer.enhanceQueryWithCommunityParams(queryBuilder, request); |
|
224 | 227 |
ParameterQueryEnhancer.enhanceQueryWithRelProjectParams(queryBuilder, request); |
225 | 228 |
ParameterQueryEnhancer.enhanceQueryWithAccessRights(queryBuilder, request); |
226 | 229 |
ParameterQueryEnhancer.enhanceQueryWithDate(queryBuilder, request); |
227 | 230 |
ParameterQueryEnhancer.enhanceQueryWithDoi(queryBuilder, request); |
228 | 231 |
|
229 | 232 |
ParameterQueryEnhancer.enhanceQueryWithResultsSortParameters(queryBuilder, request); |
233 |
FormattedSearchResult formattedSearchResult = null; |
|
230 | 234 |
|
231 |
FormattedSearchResult formattedSearchResult = searchService.search(queryBuilder.toString(),sTransformer, (newFormat!=null)?newFormat:format, locale, page, size);
|
|
232 |
writer.append(formattedSearchResult.getFormattedResult());
|
|
235 |
try {
|
|
236 |
formattedSearchResult = searchService.search(queryBuilder.toString(),sTransformer, (newFormat!=null)?newFormat:format, locale, page, size);
|
|
233 | 237 |
|
234 | 238 |
} catch (Exception e) { |
235 | 239 |
logger.error("Fail to execute search.", e); |
236 |
createXmlErrorPage(writer, e);
|
|
240 |
throw new Exception("Fail to execute search", e);
|
|
237 | 241 |
|
238 |
} finally { |
|
239 |
if (writer != null) { |
|
240 |
IOUtils.closeQuietly(writer); |
|
241 |
} |
|
242 | 242 |
} |
243 | 243 |
|
244 |
PrintWriter writer = response.getWriter(); |
|
245 |
writer.append(formattedSearchResult.getFormattedResult()); |
|
246 |
writer.close(); |
|
247 |
|
|
244 | 248 |
time = System.currentTimeMillis() - time; |
245 | 249 |
logger.debug("Answer old time " + time); |
246 | 250 |
} |
247 | 251 |
|
248 |
@RequestMapping(value = "/api/datasets", method = RequestMethod.GET) |
|
249 |
public void searchData(HttpServletRequest request, HttpServletResponse response) { |
|
252 |
@RequestMapping(value = "/api/datasets", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
|
|
253 |
public void searchData(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
250 | 254 |
|
251 |
PrintWriter writer = null; |
|
252 |
|
|
253 |
try { |
|
254 |
writer = response.getWriter(); |
|
255 |
|
|
256 | 255 |
checkParameters(ListUtils.union(PUB_N_DATA_COMMON_PARAMETERS, DATA_PARAMETERS),request.getParameterMap()); |
257 | 256 |
|
258 | 257 |
int page = readParameter(request, "page", 1); |
... | ... | |
287 | 286 |
|
288 | 287 |
ParameterQueryEnhancer.enhanceQueryWithResultsSortParameters(queryBuilder, request); |
289 | 288 |
|
290 |
FormattedSearchResult formattedSearchResult = searchService.search(queryBuilder.toString(),sTransformer, (newFormat!=null)?newFormat:format, locale, page, size); |
|
291 |
writer.append(formattedSearchResult.getFormattedResult()); |
|
289 |
FormattedSearchResult formattedSearchResult = null; |
|
290 |
try { |
|
291 |
formattedSearchResult = searchService.search(queryBuilder.toString(),sTransformer, (newFormat!=null)?newFormat:format, locale, page, size); |
|
292 | 292 |
|
293 | 293 |
} catch (Exception e) { |
294 | 294 |
logger.error("Fail to execute search.", e); |
295 |
createXmlErrorPage(writer, e); |
|
296 |
|
|
297 |
} finally { |
|
298 |
if (writer != null) { |
|
299 |
IOUtils.closeQuietly(writer); |
|
295 |
throw new Exception("Fail to execute search.", e); |
|
300 | 296 |
} |
301 |
} |
|
302 | 297 |
|
298 |
PrintWriter writer = response.getWriter(); |
|
299 |
writer.append(formattedSearchResult.getFormattedResult()); |
|
300 |
writer.close(); |
|
301 |
|
|
303 | 302 |
} |
304 | 303 |
|
305 |
@RequestMapping(value = "/api/software", method = RequestMethod.GET) |
|
306 |
public void searchSoftware(HttpServletRequest request, HttpServletResponse response) { |
|
304 |
@RequestMapping(value = "/api/software", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
|
|
305 |
public void searchSoftware(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
307 | 306 |
|
308 |
PrintWriter writer = null; |
|
309 |
|
|
310 |
try { |
|
311 |
writer = response.getWriter(); |
|
312 |
|
|
313 | 307 |
checkParameters(ListUtils.union(PUB_N_DATA_COMMON_PARAMETERS, SOFTWARE_PARAMETERS),request.getParameterMap()); |
314 | 308 |
|
315 | 309 |
int page = readParameter(request, "page", 1); |
... | ... | |
344 | 338 |
|
345 | 339 |
ParameterQueryEnhancer.enhanceQueryWithResultsSortParameters(queryBuilder, request); |
346 | 340 |
|
347 |
FormattedSearchResult formattedSearchResult = searchService.search(queryBuilder.toString(),sTransformer, (newFormat!=null)?newFormat:format, locale, page, size); |
|
348 |
writer.append(formattedSearchResult.getFormattedResult()); |
|
341 |
FormattedSearchResult formattedSearchResult = null; |
|
349 | 342 |
|
343 |
try { |
|
344 |
formattedSearchResult = searchService.search(queryBuilder.toString(),sTransformer, (newFormat!=null)?newFormat:format, locale, page, size); |
|
345 |
|
|
350 | 346 |
} catch (Exception e) { |
351 | 347 |
logger.error("Fail to execute search.", e); |
352 |
createXmlErrorPage(writer, e); |
|
353 |
|
|
354 |
} finally { |
|
355 |
if (writer != null) { |
|
356 |
IOUtils.closeQuietly(writer); |
|
348 |
throw new Exception("Fail to execute search", e); |
|
357 | 349 |
} |
358 |
} |
|
359 | 350 |
|
351 |
PrintWriter writer = response.getWriter(); |
|
352 |
writer.append(formattedSearchResult.getFormattedResult()); |
|
353 |
|
|
360 | 354 |
} |
361 | 355 |
|
362 |
@RequestMapping(value = "/api/projects", method = RequestMethod.GET)
|
|
363 |
public void searchProjects(HttpServletRequest request, HttpServletResponse response) {
|
|
356 |
@RequestMapping(value = "/api/other", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE})
|
|
357 |
public void searchOther(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
364 | 358 |
|
365 |
PrintWriter writer = null;
|
|
359 |
checkParameters(ListUtils.union(PUB_N_DATA_COMMON_PARAMETERS, OTHER_PARAMETERS),request.getParameterMap());
|
|
366 | 360 |
|
361 |
int page = readParameter(request, "page", 1); |
|
362 |
int size = readParameter(request, "size", 10); |
|
363 |
checkRequestSize(page, size); |
|
364 |
|
|
365 |
String format = (request.getParameter("format") != null) ? request.getParameter("format") : "xml"; |
|
366 |
createResponseMeta(response, format); |
|
367 |
|
|
368 |
checkFormatParameter(PUB_N_DATASET_FORMATS, format); |
|
369 |
|
|
370 |
String model = request.getParameter("model"); |
|
371 |
checkModelParameter(PUB_N_DATASET_MODELS, model); |
|
372 |
String sTransformer = defineTransformer(model,format); |
|
373 |
|
|
374 |
Collection<String> referrers = readParameter(request,"referrer"); |
|
375 |
String newFormat = defineFormatter(model, format, false, referrers); |
|
376 |
|
|
377 |
String locale = request.getParameter("locale"); |
|
378 |
|
|
379 |
StringBuilder queryBuilder = new StringBuilder(); |
|
380 |
queryBuilder.append(OTHER_BASIC_QUERY); |
|
381 |
|
|
382 |
ParameterQueryEnhancer.enhanceQueryWithFundingLevelParams(queryBuilder, request, vocabularyManager, isModelSygma(model)); |
|
383 |
ParameterQueryEnhancer.enhanceQueryWithOpenAIREIds(queryBuilder, request); |
|
384 |
ParameterQueryEnhancer.enhanceQueryWithMetadataKeywords(queryBuilder, request); |
|
385 |
ParameterQueryEnhancer.enhanceQueryWithFundingParams(queryBuilder, request); |
|
386 |
ParameterQueryEnhancer.enhanceQueryWithCommunityParams(queryBuilder, request); |
|
387 |
ParameterQueryEnhancer.enhanceQueryWithRelProjectParams(queryBuilder, request); |
|
388 |
ParameterQueryEnhancer.enhanceQueryWithAccessRights(queryBuilder, request); |
|
389 |
ParameterQueryEnhancer.enhanceQueryWithDate(queryBuilder, request); |
|
390 |
ParameterQueryEnhancer.enhanceQueryWithDoi(queryBuilder, request); |
|
391 |
|
|
392 |
ParameterQueryEnhancer.enhanceQueryWithResultsSortParameters(queryBuilder, request); |
|
393 |
|
|
394 |
FormattedSearchResult formattedSearchResult = null; |
|
395 |
|
|
367 | 396 |
try { |
368 |
writer = response.getWriter();
|
|
397 |
formattedSearchResult = searchService.search(queryBuilder.toString(),sTransformer, (newFormat!=null)?newFormat:format, locale, page, size);
|
|
369 | 398 |
|
399 |
} catch (Exception e) { |
|
400 |
logger.error("Fail to execute search.", e); |
|
401 |
throw new Exception("Fail to execute search", e); |
|
402 |
} |
|
403 |
|
|
404 |
PrintWriter writer = response.getWriter(); |
|
405 |
writer.append(formattedSearchResult.getFormattedResult()); |
|
406 |
|
|
407 |
} |
|
408 |
|
|
409 |
@RequestMapping(value = "/api/projects", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE,MediaType.APPLICATION_JSON_VALUE}) |
|
410 |
public void searchProjects(HttpServletRequest request, HttpServletResponse response) throws Exception { |
|
411 |
|
|
370 | 412 |
checkParameters(PROJECT_PARAMETERS, request.getParameterMap()); |
371 | 413 |
|
372 | 414 |
int page = readParameter(request, "page", 1); |
... | ... | |
383 | 425 |
|
384 | 426 |
format = finalFormat(format); |
385 | 427 |
|
386 |
|
|
387 | 428 |
String sTransformer = request.getParameter("sTransformer"); |
388 | 429 |
|
389 |
FormattedSearchResult formattedSearchResult = null; |
|
390 |
|
|
391 | 430 |
ParameterQueryEnhancer.enhanceProjectQueryWithOpenAIREIds(queryBuilder, request); |
392 | 431 |
ParameterQueryEnhancer.enhanceQueryWithProjectMetadataKeywords(queryBuilder, request); |
393 | 432 |
ParameterQueryEnhancer.enhanceQueryWithProjectFundingParams(queryBuilder, request); |
... | ... | |
398 | 437 |
|
399 | 438 |
ParameterQueryEnhancer.enhanceQueryWithProjectSortParameters(queryBuilder, request); |
400 | 439 |
|
440 |
FormattedSearchResult formattedSearchResult = null; |
|
441 |
try { |
|
401 | 442 |
formattedSearchResult = searchService.search(queryBuilder.toString(),sTransformer, format, locale, page, size); |
402 |
writer.append(formattedSearchResult.getFormattedResult()); |
|
403 | 443 |
|
404 |
} catch (Exception e) {
|
|
405 |
logger.error("Fail to execute search.", e);
|
|
406 |
createXmlErrorPage(writer, e);
|
|
444 |
} catch (SearchServiceException e) {
|
|
445 |
throw new Exception("Fail to execute search");
|
|
446 |
}
|
|
407 | 447 |
|
408 |
} finally { |
|
409 |
if (writer != null) { |
|
410 |
IOUtils.closeQuietly(writer); |
|
411 |
} |
|
412 |
} |
|
448 |
PrintWriter writer = response.getWriter(); |
|
449 |
writer.append(formattedSearchResult.getFormattedResult()); |
|
450 |
writer.close(); |
|
413 | 451 |
} |
414 | 452 |
|
453 |
|
|
454 |
@ExceptionHandler(IllegalArgumentException.class) |
|
455 |
public @ResponseBody ResponseEntity<Error> invalidInput(HttpServletRequest request, HttpServletResponse httpServletResponse, Exception ex) { |
|
456 |
Error response = new Error(); |
|
457 |
response.setStatus("error"); |
|
458 |
response.setCode("400"); |
|
459 |
response.setMessage("400 - Illegal argument exception."); |
|
460 |
response.setException(ex.getMessage()); |
|
461 |
|
|
462 |
String format = (request.getParameter("format") == null)? "xml": request.getParameter("format").toLowerCase(); |
|
463 |
|
|
464 |
return new ResponseEntity<Error>(response, HttpStatus.BAD_REQUEST); |
|
465 |
} |
|
466 |
|
|
415 | 467 |
private String finalFormat(String format) { |
416 | 468 |
if (format.equals("tsv")){ |
417 | 469 |
return "project_tsv"; |
... | ... | |
468 | 520 |
|
469 | 521 |
//TODO: check if needed |
470 | 522 |
private String defineTransformer(String model, String format) { |
471 |
if (model!=null && !model.trim().isEmpty()) {
|
|
523 |
if (model != null && !model.trim().isEmpty() && format != null && (format.equals("json")||format.equals("xml"))) {
|
|
472 | 524 |
if (model.equals("openaire")) { |
473 | 525 |
return null; |
474 | 526 |
|
... | ... | |
510 | 562 |
return null; |
511 | 563 |
} |
512 | 564 |
|
565 |
@Deprecated |
|
513 | 566 |
private void createXmlErrorPage(PrintWriter writer, Exception e) { |
514 | 567 |
writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
515 | 568 |
writer.append("<error>"); |
... | ... | |
648 | 701 |
} |
649 | 702 |
} |
650 | 703 |
|
704 |
|
|
705 |
/* TODO: enable if we decide to use ACCEPT Header as a priority to format parameter |
|
706 |
private String getFormat(HttpServletRequest request) { |
|
707 |
if (request.getParameter("format") == null && request.getHeader("Accept")!=null) { |
|
708 |
if (request.getHeader("Accept").equals(MediaType.APPLICATION_XML_VALUE) || |
|
709 |
request.getHeader("Accept").contains(MediaType.APPLICATION_XML_VALUE)){ |
|
710 |
return "xml"; |
|
711 |
} |
|
712 |
|
|
713 |
if (request.getHeader("Accept").equals(MediaType.APPLICATION_JSON_VALUE)){ |
|
714 |
return "json"; |
|
715 |
} |
|
716 |
} |
|
717 |
|
|
718 |
if (request.getParameter("format") == null && (request.getHeader("Accept")== null || |
|
719 |
request.getHeader("Accept").equals(MediaType.ALL_VALUE))) { |
|
720 |
return "xml"; |
|
721 |
} |
|
722 |
|
|
723 |
return request.getParameter("format"); |
|
724 |
} */ |
|
725 |
|
|
651 | 726 |
/*public void test(HttpServletRequest request, HttpServletResponse response) throws IOException, SolrServerException { |
652 | 727 |
|
653 | 728 |
long time = System.currentTimeMillis(); |
Also available in: Unified diff
Latest for solr7 + deletedByInferencePublications method added #4216