Skip to content

Commit

Permalink
validate timestamp at correct time
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorHarbo committed Dec 10, 2024
1 parent 5dd331a commit cad754e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Date;

import static dk.kb.netarchivesuite.solrwayback.memento.TimeMap.getTimeMap;
import static dk.kb.netarchivesuite.solrwayback.util.DateUtils.validateTimestamp;

/**
* <h2>Memento framework</h2>
Expand Down Expand Up @@ -154,28 +155,16 @@ public Response getResolvedTimeGate(@Context UriInfo uriInfo, @Context HttpServl
@Path("/{timestamp:\\d+}/{url:.+}")
public Response getResolvedTimeGateFromPathTimestamp(@Context UriInfo uriInfo, @Context HttpServletRequest httpRequest, @PathParam("url") String url, @PathParam("timestamp")
String timestamp) throws Exception {
/*if (!StringUtils.isNumeric(timestamp)){
log.debug("No timestamp is available. The whole URL is part of a memento.");
// Combine URL prefix with url as no timestamp is present and the variable timestamp probably contains https/
StringJoiner joiner = new StringJoiner("/");
joiner.add(timestamp).add(url);
// When this happens we need a new timestamp.
String currentTime = DateUtils.formatDate(new Date(System.currentTimeMillis()));
URI uri = PathResolver.mementoAPIResolver("/memento/", uriInfo, joiner.toString());
return DatetimeNegotiation.getMemento(String.valueOf(uri), currentTime);
}*/


if (timestamp == null || timestamp.isEmpty()){
timestamp = DateUtils.formatDate(new Date(System.currentTimeMillis()));
timestamp = org.apache.http.client.utils.DateUtils.formatDate(new Date(System.currentTimeMillis()));
log.info("Requested memento without timestamp info. The newest memento is returned.");
}


URI uri = PathResolver.mementoAPIResolver("/memento/", uriInfo, url);
return DatetimeNegotiation.getMemento(String.valueOf(uri), timestamp);
// Here we need the original timestamp
URI uri = PathResolver.mementoAPIResolver("/memento/", uriInfo, url, timestamp);
// Using correctedTimestamp here
String correctDatetime = validateTimestamp(timestamp);
return DatetimeNegotiation.getMemento(String.valueOf(uri), correctDatetime);
}

/**
Expand All @@ -192,8 +181,4 @@ private String fileEndingFromAcceptHeader(String responseFormat) {
}
}





}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Locale;
import java.util.TimeZone;

import dk.kb.netarchivesuite.solrwayback.service.exception.InternalServiceException;
import org.apache.solr.common.util.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -262,4 +263,25 @@ public static List<Pair<LocalDate, LocalDate>> calculatePeriods(LocalDate start,
return listPeriods;
}


/**
* Validate that a wayback date string is in fact 14 digits long
* @param timestamp consisting of 0 to 14 digits.
* @return a 14 digits long wayback date.
* @throws InternalServiceException when the timestamp string is longer than 14 digits.
*/
public static String validateTimestamp(String timestamp) throws InternalServiceException {
if (timestamp.length() > 14){
throw new InternalServiceException("Wayback date has been defined wrong. Please only use 14 digits.");
}

StringBuilder timestampBuilder = new StringBuilder(timestamp);
while (timestampBuilder.length() < 14){
timestampBuilder.append("0");
}

timestamp = timestampBuilder.toString();
log.info("Timestamp is now: '{}'", timestamp);
return timestamp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import dk.kb.netarchivesuite.solrwayback.properties.PropertiesLoader;
import dk.kb.netarchivesuite.solrwayback.service.SolrWaybackResource;
import dk.kb.netarchivesuite.solrwayback.service.dto.IndexDoc;
import dk.kb.netarchivesuite.solrwayback.service.exception.InternalServiceException;
import dk.kb.netarchivesuite.solrwayback.service.exception.NotFoundServiceException;
import dk.kb.netarchivesuite.solrwayback.service.exception.SolrWaybackServiceException;
import dk.kb.netarchivesuite.solrwayback.solr.NetarchiveSolrClient;
import org.archive.wayback.util.url.AggressiveUrlCanonicalizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -30,14 +30,40 @@ public class PathResolver {
* @return a normalised uri ready for lookup in SolrWayback.
*/
public static URI mementoAPIResolver(String basePath, //should be: "/memento/" or "/timemap/"
UriInfo uriInfo, String path ) throws URISyntaxException {
UriInfo uriInfo, String path) throws URISyntaxException {

log.debug("{} called with data:{}", basePath, path);
String fullUrl = uriInfo.getRequestUri().toString();

int dataStart = fullUrl.indexOf(basePath);
String url = fullUrl.substring(dataStart + basePath.length());

return normalizeUri(url);
}

/**
*
* Resolves the URL of an original resource to fetch mementos for through SolrWayback.
* The URL is given as a path parameter, so some resolving and normalising has to be done.
* @param basePath contains the last part of the path, before the URI-R begins.
* @param uriInfo contains information of the full URI requested from browser.
* @param path which represents the URL-R
* @param datetime
* @return a normalised uri ready for lookup in SolrWayback.
*/
public static URI mementoAPIResolver(String basePath, //should be: "/memento/" or "/timemap/"
UriInfo uriInfo, String path, String datetime) throws URISyntaxException, InternalServiceException {

log.debug("{} called with data:{}", basePath, path);
String fullUrl = uriInfo.getRequestUri().toString();

int dataStart = fullUrl.indexOf(basePath);
String url = fullUrl.substring(dataStart + basePath.length() + datetime.length() + 1);

return normalizeUri(url);
}

private static URI normalizeUri(String url) throws URISyntaxException {
url = checkForSingleSlash(url);
String newUrl = replaceEncoding(url);
newUrl = checkForNoHttpButPresentWWW(newUrl);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dk.kb.netarchivesuite.solrwayback.util;

import dk.kb.netarchivesuite.solrwayback.service.exception.InternalServiceException;
import org.junit.Test;
import org.mockito.Mockito;

Expand All @@ -23,6 +24,17 @@ public void testMementoResolvingHttpsAndSingleSlashc() throws URISyntaxException
assertEquals("http://kb.dk/", result.toString());
}

@Test
public void testMementoResolvingWaybackDate() throws URISyntaxException, InternalServiceException {
UriInfo uriInfo = Mockito.mock(UriInfo.class);
Mockito.when(uriInfo.getRequestUri())
.thenReturn(URI.create("http://localhost:8080/services/memento/2013/https:/kb.dk/"));

URI result = PathResolver.mementoAPIResolver("/memento/", uriInfo, "https://kb.dk/", "2013");

assertEquals("http://kb.dk/", result.toString());
}

@Test
public void testMementoResolvingWWW() throws URISyntaxException {
UriInfo uriInfo = Mockito.mock(UriInfo.class);
Expand Down

0 comments on commit cad754e

Please sign in to comment.