Skip to content

Commit

Permalink
Merge pull request #42 from futures/sitemap
Browse files Browse the repository at this point in the history
Basic sitemap support
  • Loading branch information
cbeer committed Mar 27, 2013
2 parents 8419612 + a627c98 commit 2e11835
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 0 deletions.
115 changes: 115 additions & 0 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraSitemap.java
@@ -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());
}


}
Expand Up @@ -24,6 +24,7 @@
<bean class="org.fcrepo.api.legacy.FedoraIdentifiers"/>
<bean class="org.fcrepo.api.legacy.FedoraNamespaces"/>
<bean class="org.fcrepo.api.legacy.FedoraObjects"/>
<bean class="org.fcrepo.api.FedoraSitemap"/>
<bean class="org.fcrepo.foxml.FedoraOXML"/>
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
Expand Down
@@ -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());
}
}
@@ -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()));

}
}
1 change: 1 addition & 0 deletions fcrepo-http-api/src/test/resources/spring-test/rest.xml
Expand Up @@ -27,6 +27,7 @@
<bean class="org.fcrepo.api.FedoraNamespaces"/>
<bean class="org.fcrepo.api.FedoraObjects"/>
<bean class="org.fcrepo.api.FedoraFieldSearch"/>
<bean class="org.fcrepo.api.FedoraSitemap"/>
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="json" value="application/json"/>
Expand Down
@@ -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;
}


}
@@ -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;
}
}
@@ -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;
}
}
1 change: 1 addition & 0 deletions fcrepo-webapp/src/main/resources/spring/rest.xml
Expand Up @@ -52,6 +52,7 @@
<bean class="org.fcrepo.api.FedoraFieldSearch"/>
<bean class="org.fcrepo.generator.DublinCoreGenerator"/>
<bean class="org.fcrepo.generator.RdfGenerator"/>
<bean class="org.fcrepo.api.FedoraSitemap"/>
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="json" value="application/json"/>
Expand Down

0 comments on commit 2e11835

Please sign in to comment.