Commit 2e1f2db5 authored by Maxym Borodenko's avatar Maxym Borodenko

sitemap-INSTCODE.xml

parent beafa94b
/**
* Copyright 2014 Global Crop Diversity Trust
/*
* Copyright 2019 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -12,7 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
*/
package org.genesys2.server.mvc;
......@@ -20,18 +20,25 @@ import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.google.common.collect.Sets;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.worker.AccessionProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
......@@ -44,6 +51,8 @@ import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class SitemapXMLController {
private static final Logger LOG = LoggerFactory.getLogger(SitemapXMLController.class);
@Value("${frontend.url}")
private String frontendUrl;
......@@ -56,6 +65,9 @@ public class SitemapXMLController {
@Autowired
private CropService cropService;
@Autowired
private AccessionProcessor accessionProcessor;
private static class SitemapPage {
String url;
String freq = "always";
......@@ -132,12 +144,44 @@ public class SitemapXMLController {
String sitemapWiewsXml(HttpServletResponse response) {
response.setContentType("text/xml");
final StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sb.append("<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");
for (FaoInstitute institute : instituteService.listActive(new PageRequest(0, Integer.MAX_VALUE))) {
sb.append(" <sitemap>");
sb.append(" <loc>").append(frontendUrl).append("/sitemap-").append(institute.getCode().toUpperCase()).append(".xml").append("</loc>");
sb.append(" </sitemap>");
}
sb.append("</sitemapindex>");
return sb.toString();
}
@RequestMapping(value = "/sitemap-{instCode}.xml", method = RequestMethod.GET, produces = { MediaType.TEXT_XML_VALUE })
public @ResponseBody
String sitemapWiewAndAccessionsXml(@PathVariable(value = "instCode") String instCode, HttpServletResponse response) {
FaoInstitute faoInstitute = instituteService.getInstitute(instCode);
if (faoInstitute == null) {
throw new NotFoundElement("No Institute with specified code.");
}
response.setContentType("text/xml");
final StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sb.append("<urlset xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\" xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");
for (FaoInstitute institute : instituteService.listActive(new PageRequest(0, Integer.MAX_VALUE))) {
writePage(response, sb, new SitemapPage("/wiews/" + institute.getCode().toUpperCase()));
writePage(response, sb, new SitemapPage("/wiews/" + faoInstitute.getCode()));
// Create JSON filter
AccessionFilter instituteFilter = new AccessionFilter();
instituteFilter.holder().code = Sets.newHashSet(faoInstitute.getCode());
try {
accessionProcessor.process(instituteFilter, (accession) -> {
writePage(response, sb, new SitemapPage("/a/" + accession.getUuid()));
return accession;
});
} catch (Exception e) {
LOG.warn(e.getMessage());
}
sb.append("</urlset>");
......
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