Commit b42b497f authored by Jean-Baptiste Nizet's avatar Jean-Baptiste Nizet
Browse files

refactor: use multiple sitemaps for sites too

parent c6bc74b2
......@@ -74,16 +74,19 @@ public class SiteController {
);
}
@GetMapping(value = "/sitemap.txt")
@GetMapping(value = "/sitemap-{index}.txt")
@ResponseBody
public ResponseEntity<StreamingResponseBody> sitemap() {
public ResponseEntity<StreamingResponseBody> sitemap(@PathVariable("index") int index) {
if (index < 0 || index >= Sitemaps.BUCKET_COUNT) {
throw new NotFoundException("no sitemap for this index");
}
StreamingResponseBody body = out -> {
Iterator<LocationSitemapVO> iterator = locationRepository.scrollAllForSitemap(1000);
Sitemaps.generateSitemap(
"/sites/sitemap.txt",
"/sites/sitemap-" + index + ".txt",
out,
iterator,
vo -> true,
vo -> Math.floorMod(vo.getLocationDbId().hashCode(), Sitemaps.BUCKET_COUNT) == index,
vo -> "/sites/" + vo.getLocationDbId()
);
};
......
......@@ -33,7 +33,9 @@ public class SitemapIndexController {
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n");
appendSiteMap(builder, "/sites/sitemap.txt");
for (int i = 0; i < Sitemaps.BUCKET_COUNT; i++) {
appendSiteMap(builder, "/sites/sitemap-" + i + ".txt");
}
for (int i = 0; i < Sitemaps.BUCKET_COUNT; i++) {
appendSiteMap(builder, "/germplasms/sitemap-" + i + ".txt");
}
......
......@@ -14,12 +14,14 @@ import java.util.List;
import fr.inra.urgi.faidare.config.FaidareProperties;
import fr.inra.urgi.faidare.domain.data.LocationSitemapVO;
import fr.inra.urgi.faidare.domain.data.LocationVO;
import fr.inra.urgi.faidare.domain.data.study.StudySitemapVO;
import fr.inra.urgi.faidare.domain.datadiscovery.data.DataSource;
import fr.inra.urgi.faidare.domain.response.PaginatedList;
import fr.inra.urgi.faidare.domain.xref.XRefDocumentSearchCriteria;
import fr.inra.urgi.faidare.domain.xref.XRefDocumentVO;
import fr.inra.urgi.faidare.repository.es.LocationRepository;
import fr.inra.urgi.faidare.repository.es.XRefDocumentRepository;
import fr.inra.urgi.faidare.utils.Sitemaps;
import fr.inra.urgi.faidare.web.Fixtures;
import fr.inra.urgi.faidare.web.thymeleaf.CoordinatesDialect;
import fr.inra.urgi.faidare.web.thymeleaf.FaidareDialect;
......@@ -85,10 +87,28 @@ public class SiteControllerTest {
void shouldGenerateSitemap() throws Exception {
List<LocationSitemapVO> sites = Arrays.asList(
new LocationSitemapVO("site1"),
new LocationSitemapVO("site2")
new LocationSitemapVO("site4"),
new LocationSitemapVO("site53"),
new LocationSitemapVO("site68")
);
when(mockLocationRepository.scrollAllForSitemap(anyInt())).thenReturn(sites.iterator());
MvcResult mvcResult = mockMvc.perform(get("/faidare/sites/sitemap.txt")
// the hashCode algorithm is specified in the javadoc, so it's guaranteed to be
// the same everywhere
// uncomment the following line to see which sitemap index each study has
// sites.forEach(site -> System.out.println(site.getLocationDbId() + " = " + Math.floorMod(site.getLocationDbId().hashCode(), Sitemaps.BUCKET_COUNT)));
when(mockLocationRepository.scrollAllForSitemap(anyInt())).thenAnswer(invocation -> sites.iterator());
testSitemap(2, "http://localhost/faidare/sites/site1\nhttp://localhost/faidare/sites/site53\n");
testSitemap(5, "http://localhost/faidare/sites/site4\nhttp://localhost/faidare/sites/site68\n");
testSitemap(7, "");
mockMvc.perform(get("/faidare/sites/sitemap-17.txt")
.contextPath("/faidare"))
.andExpect(status().isNotFound());
}
private void testSitemap(int index, String expectedContent) throws Exception {
MvcResult mvcResult = mockMvc.perform(get("/faidare/sites/sitemap-" + index + ".txt")
.contextPath("/faidare"))
.andExpect(request().asyncStarted())
.andReturn();
......@@ -96,6 +116,7 @@ public class SiteControllerTest {
this.mockMvc.perform(asyncDispatch(mvcResult))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
.andExpect(content().string("http://localhost/faidare/sites/site1\nhttp://localhost/faidare/sites/site2\n"));
.andExpect(content().string(expectedContent));
}
}
......@@ -25,10 +25,11 @@ class SitemapIndexControllerTest {
mockMvc.perform(get("/faidare/sitemap.xml").contextPath("/faidare"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.TEXT_XML))
.andExpect(xpath("/sitemapindex/sitemap[1]/loc").string("http://localhost/faidare/sites/sitemap.txt"))
.andExpect(xpath("/sitemapindex/sitemap[2]/loc").string("http://localhost/faidare/germplasms/sitemap-0.txt"))
.andExpect(xpath("/sitemapindex/sitemap[3]/loc").string("http://localhost/faidare/germplasms/sitemap-1.txt"))
.andExpect(xpath("/sitemapindex/sitemap[" + (Sitemaps.BUCKET_COUNT + 2) + "]/loc").string("http://localhost/faidare/studies/sitemap-0.txt"))
.andExpect(xpath("/sitemapindex/sitemap[" + (Sitemaps.BUCKET_COUNT + 3) + "]/loc").string("http://localhost/faidare/studies/sitemap-1.txt"));
.andExpect(xpath("/sitemapindex/sitemap[1]/loc").string("http://localhost/faidare/sites/sitemap-0.txt"))
.andExpect(xpath("/sitemapindex/sitemap[2]/loc").string("http://localhost/faidare/sites/sitemap-1.txt"))
.andExpect(xpath("/sitemapindex/sitemap[" + (Sitemaps.BUCKET_COUNT + 1) + "]/loc").string("http://localhost/faidare/germplasms/sitemap-0.txt"))
.andExpect(xpath("/sitemapindex/sitemap[" + (Sitemaps.BUCKET_COUNT + 2) + "]/loc").string("http://localhost/faidare/germplasms/sitemap-1.txt"))
.andExpect(xpath("/sitemapindex/sitemap[" + (Sitemaps.BUCKET_COUNT * 2 + 1) + "]/loc").string("http://localhost/faidare/studies/sitemap-0.txt"))
.andExpect(xpath("/sitemapindex/sitemap[" + (Sitemaps.BUCKET_COUNT * 2 + 2) + "]/loc").string("http://localhost/faidare/studies/sitemap-1.txt"));
}
}
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