Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraSitemap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package org.fcrepo.api; | ||
|
||
import com.google.common.base.Function; | ||
import com.google.common.collect.FluentIterable; | ||
import org.fcrepo.AbstractResource; | ||
import org.fcrepo.jaxb.responses.sitemap.SitemapEntry; | ||
import org.fcrepo.jaxb.responses.sitemap.SitemapIndex; | ||
import org.fcrepo.jaxb.responses.sitemap.SitemapUrlSet; | ||
import org.fcrepo.jaxb.search.FieldSearchResult; | ||
import org.fcrepo.provider.VelocityViewer; | ||
import org.fcrepo.services.ObjectService; | ||
import org.fcrepo.services.RepositoryService; | ||
import org.fcrepo.utils.FedoraJcrTypes; | ||
import org.slf4j.Logger; | ||
|
||
import javax.inject.Inject; | ||
import javax.jcr.LoginException; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.Session; | ||
import javax.jcr.Value; | ||
import javax.jcr.query.Query; | ||
import javax.jcr.query.QueryManager; | ||
import javax.jcr.query.QueryResult; | ||
import javax.jcr.query.Row; | ||
import javax.jcr.query.RowIterator; | ||
import javax.jcr.query.qom.Source; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.core.Response; | ||
import javax.xml.bind.annotation.XmlElement; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.ArrayList; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import static java.lang.Integer.parseInt; | ||
import static javax.jcr.query.Query.JCR_SQL2; | ||
import static org.fcrepo.utils.FedoraJcrTypes.*; | ||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
@Path("/sitemap") | ||
public class FedoraSitemap extends AbstractResource { | ||
|
||
private static final Logger logger = getLogger(FedoraSitemap.class); | ||
|
||
@Inject | ||
ObjectService objectService; | ||
|
||
@GET | ||
public SitemapIndex getSitemapIndex() throws RepositoryException { | ||
final Session session = repo.login(); | ||
try { | ||
final long count = objectService.getRepositoryObjectCount(session) / 50000; | ||
|
||
SitemapIndex sitemapIndex = new SitemapIndex(); | ||
|
||
for(int i = 0; i <= count; i++ ) { | ||
sitemapIndex.appendSitemapEntry(new SitemapEntry(uriInfo.getBaseUriBuilder().path(FedoraSitemap.class).path(FedoraSitemap.class, "getSitemap").build(i+1))); | ||
} | ||
|
||
return sitemapIndex; | ||
} finally { | ||
session.logout(); | ||
} | ||
} | ||
|
||
@GET | ||
@Path("/{page}") | ||
public SitemapUrlSet getSitemap(@PathParam("page") final String page) throws RepositoryException { | ||
final Session session = repo.login(); | ||
try { | ||
final SitemapUrlSet sitemapUrlSet = new SitemapUrlSet(); | ||
|
||
RowIterator rows = getSitemapEntries(session, parseInt(page)); | ||
|
||
while(rows.hasNext()) { | ||
Row r = rows.nextRow(); | ||
|
||
sitemapUrlSet.appendSitemapEntry(getSitemapEntry(r)); | ||
} | ||
|
||
return sitemapUrlSet; | ||
} finally { | ||
session.logout(); | ||
} | ||
} | ||
|
||
private RowIterator getSitemapEntries(Session session, int page) throws RepositoryException { | ||
QueryManager queryManager = session.getWorkspace().getQueryManager(); | ||
|
||
//TODO expand to more fields | ||
String sqlExpression = "SELECT [jcr:name],[jcr:lastModified] FROM [" + FEDORA_OBJECT + "]"; | ||
Query query = queryManager.createQuery(sqlExpression, JCR_SQL2); | ||
|
||
query.setOffset(page*50000); | ||
query.setLimit(50000); | ||
|
||
|
||
QueryResult queryResults = query.execute(); | ||
|
||
final RowIterator rows = queryResults.getRows(); | ||
|
||
return rows; | ||
} | ||
|
||
private SitemapEntry getSitemapEntry(Row r) throws RepositoryException { | ||
return new SitemapEntry(uriInfo.getBaseUriBuilder().path(FedoraObjects.class).path(FedoraObjects.class, "getObject").build(r.getNode().getName()), r.getValue("jcr:lastModified").getDate()); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
fcrepo-http-api/src/test/java/org/fcrepo/api/FedoraSitemapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.fcrepo.api; | ||
|
||
import org.fcrepo.jaxb.responses.sitemap.SitemapIndex; | ||
import org.fcrepo.services.DatastreamService; | ||
import org.fcrepo.services.ObjectService; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.modeshape.jcr.api.Repository; | ||
|
||
import javax.jcr.LoginException; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.Session; | ||
|
||
import static org.fcrepo.api.TestHelpers.getUriInfoImpl; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class FedoraSitemapTest { | ||
|
||
FedoraSitemap testObj; | ||
|
||
ObjectService mockObjects; | ||
|
||
|
||
Repository mockRepo; | ||
|
||
Session mockSession; | ||
|
||
@Before | ||
public void setUp() throws LoginException, RepositoryException { | ||
mockObjects = mock(ObjectService.class); | ||
testObj = new FedoraSitemap(); | ||
testObj.objectService = mockObjects; | ||
mockRepo = mock(Repository.class); | ||
mockSession = mock(Session.class); | ||
when(mockRepo.login()).thenReturn(mockSession); | ||
testObj.setRepository(mockRepo); | ||
testObj.setUriInfo(getUriInfoImpl()); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
|
||
} | ||
|
||
@Test | ||
public void testGetSitemapIndex() throws Exception { | ||
when(mockObjects.getRepositoryObjectCount(mockSession)).thenReturn(49999L); | ||
|
||
final SitemapIndex sitemapIndex = testObj.getSitemapIndex(); | ||
|
||
assertEquals(1, sitemapIndex.getSitemapEntries().size()); | ||
} | ||
|
||
@Test | ||
public void testGetSitemapIndexMultiplePages() throws Exception { | ||
when(mockObjects.getRepositoryObjectCount(mockSession)).thenReturn(50001L); | ||
|
||
final SitemapIndex sitemapIndex = testObj.getSitemapIndex(); | ||
|
||
assertEquals(2, sitemapIndex.getSitemapEntries().size()); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
fcrepo-http-api/src/test/java/org/fcrepo/integration/api/FedoraSitemapIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.fcrepo.integration.api; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.fcrepo.api.FedoraSitemap; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class FedoraSitemapIT extends AbstractResourceIT { | ||
@Test | ||
public void testGetSitemapIndex() throws Exception { | ||
|
||
getStatus(postObjMethod("new")); | ||
final HttpGet httpGet = new HttpGet(serverAddress + "sitemap"); | ||
|
||
assertEquals(200, getStatus(httpGet)); | ||
|
||
logger.info(IOUtils.toString(execute(httpGet).getEntity().getContent())); | ||
|
||
} | ||
|
||
@Test | ||
public void testGetSitemap() throws Exception { | ||
|
||
|
||
getStatus(postObjMethod("test:1")); | ||
getStatus(postObjMethod("new")); | ||
|
||
final HttpGet httpGet = new HttpGet(serverAddress + "sitemap/1"); | ||
|
||
assertEquals(200, getStatus(httpGet)); | ||
|
||
logger.info(IOUtils.toString(execute(httpGet).getEntity().getContent())); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
fcrepo-http-commons/src/main/java/org/fcrepo/jaxb/responses/sitemap/SitemapEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.fcrepo.jaxb.responses.sitemap; | ||
|
||
import javax.jcr.RepositoryException; | ||
import javax.jcr.query.Row; | ||
import javax.xml.bind.annotation.XmlElement; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
import java.net.URI; | ||
import java.util.Calendar; | ||
|
||
@XmlRootElement(name = "url", namespace = "http://www.sitemaps.org/schemas/sitemap/0.9") | ||
public class SitemapEntry { | ||
@XmlElement | ||
private final URI loc; | ||
|
||
@XmlElement | ||
private final Calendar lastmod; | ||
|
||
@XmlElement | ||
private final String changefreq = "monthly"; | ||
|
||
@XmlElement | ||
private final double priority = 0.8; | ||
|
||
public SitemapEntry() { | ||
loc = null; | ||
lastmod = null; | ||
} | ||
|
||
|
||
public SitemapEntry(URI loc) throws RepositoryException { | ||
this.loc = loc; | ||
this.lastmod = Calendar.getInstance(); | ||
} | ||
|
||
public SitemapEntry(URI loc, Calendar lastmod) throws RepositoryException { | ||
this.loc = loc; | ||
this.lastmod = lastmod; | ||
} | ||
|
||
|
||
} |
25 changes: 25 additions & 0 deletions
25
fcrepo-http-commons/src/main/java/org/fcrepo/jaxb/responses/sitemap/SitemapIndex.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.fcrepo.jaxb.responses.sitemap; | ||
|
||
|
||
import javax.xml.bind.annotation.XmlElement; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@XmlRootElement(name = "sitemapindex", namespace = "http://www.sitemaps.org/schemas/sitemap/0.9") | ||
public class SitemapIndex { | ||
|
||
private List<SitemapEntry> entries = new ArrayList<SitemapEntry>(); | ||
public SitemapIndex() { | ||
|
||
} | ||
|
||
public void appendSitemapEntry(SitemapEntry e) { | ||
entries.add(e); | ||
} | ||
|
||
@XmlElement(name = "sitemap") | ||
public List<SitemapEntry> getSitemapEntries() { | ||
return entries; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
fcrepo-http-commons/src/main/java/org/fcrepo/jaxb/responses/sitemap/SitemapUrlSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.fcrepo.jaxb.responses.sitemap; | ||
|
||
|
||
import javax.xml.bind.annotation.XmlElement; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@XmlRootElement(name = "urlset", namespace = "http://www.sitemaps.org/schemas/sitemap/0.9") | ||
public class SitemapUrlSet { | ||
|
||
|
||
@XmlElement(name = "url") | ||
private List<SitemapEntry> sitemapEntries = new ArrayList<SitemapEntry>(); | ||
|
||
public SitemapUrlSet() { | ||
|
||
} | ||
|
||
public void appendSitemapEntry(SitemapEntry e) { | ||
sitemapEntries.add(e); | ||
} | ||
public List<SitemapEntry> getSitemapEntries() { | ||
return sitemapEntries; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters