Commit 22651767 authored by Matija Obreza's avatar Matija Obreza
Browse files

Added support for Open Search (opensearch.org)

parent 280d16e0
......@@ -16,28 +16,63 @@
package org.genesys2.server.servlet.controller;
import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.service.ElasticService;
import org.genesys2.server.service.impl.SearchException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@Scope("request")
// @Scope("request")
public class SearchController {
public static final Log LOG = LogFactory.getLog(SearchController.class);
@Autowired
ElasticService searchService;
@Value("${base.url}")
private String baseUrl;
@RequestMapping(value = "/acn/opensearch/desc", method = RequestMethod.GET)
public String openSearchDesc(ModelMap model) {
model.addAttribute("baseUrl", baseUrl);
return "/search/opensearch-desc";
}
@RequestMapping(value = "/acn/opensearch", produces = MediaType.APPLICATION_XML_VALUE)
public String openSearch(ModelMap model, @RequestParam(required = false, value = "q") String searchQuery,
@RequestParam(value = "page", required = false, defaultValue = "1") int page) throws IOException {
model.addAttribute("baseUrl", baseUrl);
model.addAttribute("q", searchQuery);
if (!StringUtils.isBlank(searchQuery)) {
try {
final Page<?> x = searchService.search(searchQuery, new PageRequest(page - 1, 50));
model.addAttribute("pagedData", x);
LOG.info("Searching for: " + searchQuery + " returns " + x.getNumberOfElements());
} catch (SearchException e) {
LOG.info("Searching for: " + searchQuery + " failed with error " + e.getMessage());
LOG.error(e.getMessage(), e);
model.addAttribute("error", e);
}
}
return "/search/opensearch";
}
@RequestMapping("/acn/search")
public String findAccession(ModelMap model, @RequestParam(required = false, value = "q") String searchQuery,
@RequestParam(value = "page", required = false, defaultValue = "1") int page) {
......
......@@ -17,6 +17,9 @@
<!-- Links -->
<link rel="shortcut icon" href="<c:url value="/html/images/genesys.png" />" />
<!-- opensearch.org -->
<link rel="search" hreflang="${pageContext.response.locale.language}" type="application/opensearchdescription+xml" href="<c:url value="/acn/opensearch/desc" />" title="<spring:message code="search.input.placeholder" />" />
<!-- l10n -->
<link rel="alternate" hreflang="en" href="<c:url value="/en${pageContext.request.getAttribute('org.genesys2.server.servlet.filter.LocaleURLFilter.INTERNALURL')}" />" />
......
......@@ -23,6 +23,9 @@
<!-- Links -->
<link rel="shortcut icon" href="<c:url value="/html/images/genesys.png" />" />
<!-- opensearch.org -->
<link rel="search" hreflang="${pageContext.response.locale.language}" type="application/opensearchdescription+xml" href="<c:url value="/acn/opensearch/desc" />" title="<spring:message code="search.input.placeholder" />" />
<!-- l10n -->
<link rel="alternate" hreflang="en" href="<c:url value="/en${pageContext.request.getAttribute('org.genesys2.server.servlet.filter.LocaleURLFilter.INTERNALURL')}" />" />
......
<%@ page contentType="application/opensearchdescription+xml" pageEncoding="UTF-8" language="java" session="false"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%@ taglib prefix="security" uri="http://www.springframework.org/security/tags"%><%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%><%@ taglib
prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%><?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
<ShortName><spring:message code="page.home.title" /></ShortName>
<LongName>Search global accession database Genesys PGR</LongName>
<Description></Description>
<Image height="16" width="16" type="image/x-icon"><c:out value="${baseUrl}" />/favicon.ico</Image>
<Tags>accession, PGR, genesys</Tags>
<Contact>helpdesk@genesys-pgr.org</Contact>
<Query role="example" searchTerms="IRGC 1001" />
<Url type="application/rss+xml" template="<c:out value="${baseUrl}" /><c:url value="/acn/opensearch" />?q={searchTerms}&amp;page={startPage?}"/>
<Url type="text/html" template="<c:out value="${baseUrl}" /><c:url value="/acn/search" />?q={searchTerms}&amp;page={startPage?}"/>
<Attribution>See <c:out value="${baseUrl}" /><c:url value="/content/terms" /></Attribution>
<SyndicationRight>limited</SyndicationRight>
<AdultContent>false</AdultContent>
<Language>${pageContext.response.locale.language}</Language>
<OutputEncoding>UTF-8</OutputEncoding>
<InputEncoding>UTF-8</InputEncoding>
<moz:SearchForm><c:out value="${baseUrl}" /><c:url value="/acn/search" /></moz:SearchForm>
</OpenSearchDescription>
<%@ page contentType="text/xml;charset=UTF-8" pageEncoding="UTF-8" language="java" session="false"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%@ taglib prefix="security" uri="http://www.springframework.org/security/tags"%><%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%><%@ taglib
prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%><?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title><spring:message code="search.page.title" />: <c:out value="${q}" /></title>
<link><c:url value="/acn/search"><c:param name="q" value="${q}" /><c:param name="page" value="${pagedData.number + 1}" /></c:url></link>
<description>Search results for "New York history" at Example.com</description>
<opensearch:totalResults><c:out value="${pagedData.totalElements}" /></opensearch:totalResults>
<opensearch:startPage><c:out value="${pagedData.number + 1}" /></opensearch:startPage>
<opensearch:itemsPerPage><c:out value="${pagedData.size}" /></opensearch:itemsPerPage>
<atom:link rel="search" type="application/opensearchdescription+xml" href="<c:url value="/acn/opensearch/desc" />" />
<opensearch:Query role="request" searchTerms="<c:out value="${a}" />" startPage="<c:out value="${pagedData.number + 1} "/>" />
<c:if test="${error ne null}">
<div class="alert alert-warning">
<spring:message code="search.search-query-failed" arguments="${error.message}" />
</div>
</c:if>
<c:if test="${pagedData eq null}">
<div class="alert alert-warning">
<spring:message code="search.search-query-missing" />
</div>
</c:if>
<c:forEach items="${pagedData.content}" var="accession" varStatus="status">
<item>
<title><c:out value="${accession.acceNumb}" /></title>
<link><c:url value="/acn/id/${accession.id}" /></link>
<description>
<spring:message code="accession.sampleStatus.${accession.sampStat}" />
<c:out value="${accession.taxonomy.sciName}" />
<c:out value="${accession.institute.code}" />
</description>
</item>
</c:forEach>
</channel>
</rss>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment