Skip to content

Commit

Permalink
make async, report error if staged file exists #4261
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Oct 2, 2018
1 parent 3b9bbf1 commit 11f6fca
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
2 changes: 1 addition & 1 deletion doc/sphinx-guides/source/installation/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ This will create or update a file in the following location unless you have cust

``/usr/local/glassfish4/glassfish/domains/domain1/docroot/sitemap/sitemap.xml``

On an installation of Dataverse with many datasets, the creation or updating of the sitemap can take a while. You can check Glassfish's server.log file for "BEGIN updateSiteMap" and "END updateSiteMap" lines to know when the process is complete.
On an installation of Dataverse with many datasets, the creation or updating of the sitemap can take a while. You can check Glassfish's server.log file for "BEGIN updateSiteMap" and "END updateSiteMap" lines to know when the process started and stopped and any errors in between.

https://demo.dataverse.org/sitemap.xml is the sitemap URL for the Dataverse Demo site and yours should be similar. Submit your sitemap URL to Google by following `Google's "submit a sitemap" instructions`_ or similar instructions for other search engines.

Expand Down
15 changes: 10 additions & 5 deletions src/main/java/edu/harvard/iq/dataverse/api/SiteMap.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package edu.harvard.iq.dataverse.api;

import edu.harvard.iq.dataverse.sitemap.SiteMapServiceBean;
import edu.harvard.iq.dataverse.sitemap.SiteMapUtil;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
Expand All @@ -12,15 +14,18 @@
@Path("admin/sitemap")
public class SiteMap extends AbstractApiBean {

@EJB
SiteMapServiceBean siteMapSvc;

@POST
@Produces(MediaType.APPLICATION_JSON)
public Response updateSiteMap() {
try {
SiteMapUtil.updateSiteMap(dataverseSvc.findAll(), datasetSvc.findAll());
return ok("Sitemap updated.");
} catch (Exception ex) {
return error(Response.Status.BAD_REQUEST, "Sitemap could not be updated. Exception: " + ex.getLocalizedMessage());
boolean stageFileExists = SiteMapUtil.stageFileExists();
if (stageFileExists) {
return error(Response.Status.BAD_REQUEST, "Sitemap cannot be updated because staged file exists.");
}
siteMapSvc.updateSiteMap(dataverseSvc.findAll(), datasetSvc.findAll());
return ok("Sitemap update has begun. Check logs for status.");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package edu.harvard.iq.dataverse.sitemap;

import edu.harvard.iq.dataverse.Dataset;
import edu.harvard.iq.dataverse.Dataverse;
import java.util.List;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;

@Stateless
public class SiteMapServiceBean {

@Asynchronous
public void updateSiteMap(List<Dataverse> dataverses, List<Dataset> datasets) {
SiteMapUtil.updateSiteMap(dataverses, datasets);
}

}
37 changes: 27 additions & 10 deletions src/main/java/edu/harvard/iq/dataverse/sitemap/SiteMapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,9 @@ public static void updateSiteMap(List<Dataverse> dataverses, List<Dataset> datas

logger.info("BEGIN updateSiteMap");

String sitemapPath = "/tmp";
// i.e. /usr/local/glassfish4/glassfish/domains/domain1
String domainRoot = System.getProperty("com.sun.aas.instanceRoot");
if (domainRoot != null) {
// Note that we write to a directory called "sitemap" but we serve just "/sitemap.xml" using PrettyFaces.
sitemapPath = domainRoot + File.separator + "docroot" + File.separator + "sitemap";
}
String stagedSitemapPathAndFileString = sitemapPath + File.separator + SITEMAP_FILENAME_STAGED;
String finalSitemapPathAndFileString = sitemapPath + File.separator + SITEMAP_FILENAME_FINAL;
String sitemapPathString = getSitemapPathString();
String stagedSitemapPathAndFileString = sitemapPathString + File.separator + SITEMAP_FILENAME_STAGED;
String finalSitemapPathAndFileString = sitemapPathString + File.separator + SITEMAP_FILENAME_FINAL;

Path stagedPath = Paths.get(stagedSitemapPathAndFileString);
if (Files.exists(stagedPath)) {
Expand Down Expand Up @@ -140,7 +134,7 @@ public static void updateSiteMap(List<Dataverse> dataverses, List<Dataset> datas
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(document);
File directory = new File(sitemapPath);
File directory = new File(sitemapPathString);
if (!directory.exists()) {
directory.mkdir();
}
Expand Down Expand Up @@ -205,4 +199,27 @@ private static String getLastModDate(DvObjectContainer dvObjectContainer) {
// This format allows you to omit the time portion, if desired, and use YYYY-MM-DD."
return new SimpleDateFormat("yyyy-MM-dd").format(dvObjectContainer.getModificationTime());
}

public static boolean stageFileExists() {
String sitemapPathString = getSitemapPathString();
String stagedSitemapPathAndFileString = sitemapPathString + File.separator + SITEMAP_FILENAME_STAGED;
Path stagedPath = Paths.get(stagedSitemapPathAndFileString);
if (Files.exists(stagedPath)) {
logger.warning("Unable to update sitemap! The staged file from a previous run already existed. Delete " + stagedSitemapPathAndFileString + " and try again.");
return true;
}
return false;
}

private static String getSitemapPathString() {
String sitemapPathString = "/tmp";
// i.e. /usr/local/glassfish4/glassfish/domains/domain1
String domainRoot = System.getProperty("com.sun.aas.instanceRoot");
if (domainRoot != null) {
// Note that we write to a directory called "sitemap" but we serve just "/sitemap.xml" using PrettyFaces.
sitemapPathString = domainRoot + File.separator + "docroot" + File.separator + "sitemap";
}
return sitemapPathString;

}
}

0 comments on commit 11f6fca

Please sign in to comment.