Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Marmotta 655 memento compliance issues #24

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions platform/marmotta-versioning-kiwi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,61 @@
<groupId>org.apache.marmotta</groupId>
<artifactId>kiwi-versioning</artifactId>
</dependency>
<!-- testing -->
<dependency>
<groupId>org.apache.marmotta</groupId>
<artifactId>marmotta-core</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>

<!-- webjars -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,15 @@ public void write(Resource original, RepositoryResult<Version> versions, OutputS
//write data to map
Map<String, Object> data = new HashMap<String, Object>();

data.put("original",original.toString());
data.put("original", MementoUtils.originalURI(original.toString(),configurationService.getBaseUri()));

List<Map<String,String>> vs = new ArrayList<Map<String,String>>();

while (versions.hasNext()) {
Version v = versions.next();
Map<String,String> m = new HashMap<String,String>();
m.put("date",v.getCommitTime().toString());
m.put("formatedDate", MementoUtils.MEMENTO_DATE_FORMAT.format(v.getCommitTime()));
m.put("uri",MementoUtils.resourceURI(original.toString(), v.getCommitTime(), configurationService.getBaseUri()).toString());
m.put("tstamp", TSTAMP.format(v.getCommitTime()));
vs.add(m);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void write(Resource original, RepositoryResult<Version> versions, OutputS

//write original resource
w.append("<");
w.append(original.toString());
w.append(MementoUtils.originalURI(original.toString(), configurationService.getBaseUri()).toString());
w.append(">;rel=\"original\",");
w.newLine();

Expand Down Expand Up @@ -108,7 +108,7 @@ public void write(Resource original, RepositoryResult<Version> versions, OutputS

//add datetime
w.append("\"; datetime=\"");
w.append(v.getCommitTime().toString());
w.append(MementoUtils.MEMENTO_DATE_FORMAT.format(v.getCommitTime()));
w.append("\",");

w.newLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
package org.apache.marmotta.platform.versioning.model;

import org.apache.marmotta.kiwi.versioning.model.Version;
import org.apache.marmotta.platform.core.api.config.ConfigurationService;
import org.apache.marmotta.platform.versioning.exception.MementoException;
import org.apache.marmotta.platform.versioning.utils.MementoUtils;
import org.openrdf.model.Resource;

import javax.inject.Inject;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -60,7 +63,11 @@ public Set<String> buildLinks(String baseURI) throws MementoException {
links.add(buildLink(prefix,original.toString(),current.getCommitTime(),"memento"));

//add link to original
links.add("<"+original.toString()+">;rel=\"original\"");
try {
links.add("<"+MementoUtils.originalURI(original.toString(),baseURI).toString()+">;rel=\"original\"");
} catch (UnsupportedEncodingException e) {
throw new MementoException(e);
}

//add next and previous if they exist
if( next != null ) links.add(buildLink(prefix,original.toString(),next.getCommitTime(),"next memento"));
Expand All @@ -70,7 +77,7 @@ public Set<String> buildLinks(String baseURI) throws MementoException {
}

private String buildLink( String prefix, String resource, Date date, String rel ) {
return "<" + prefix + MementoUtils.MEMENTO_DATE_FORMAT.format(date) + "/" + resource +
return "<" + prefix + MementoUtils.MEMENTO_DATE_FORMAT_FOR_URIS.format(date) + "/" + resource +
">;datetime=\"" + MementoUtils.MEMENTO_DATE_FORMAT.format(date) + "\";rel=\"" + rel +"\"";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ public class VersionSerializerServiceImpl implements VersionSerializerService {

/**
* returns an adequate serializer for a mimetype
* @param type a list of mimetype (from Accept header)
* @param types a list of mimetype (from Accept header)
* @return a serializer
* @throws IOException if there is no serializer for mimetype
*/
@Override
public VersionSerializer getSerializer(List<ContentType> type) throws IOException {
for(VersionSerializer serializer : serializers) {
if(MarmottaHttpUtils.bestContentType(serializer.getContentTypes(),type) != null) return serializer;
}
throw new IOException("Cannot find serializer for " + type);
public VersionSerializer getSerializer(List<ContentType> types) throws IOException {
for(ContentType type : types) {
for(VersionSerializer serializer : serializers) {
for(ContentType stype : serializer.getContentTypes()) {
if(stype.matches(type)) return serializer;
}
}
} //TODO there is not fuzzy match e.g. text/*
throw new IOException("Cannot find serializer for " + types);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@
*/
package org.apache.marmotta.platform.versioning.utils;

import org.apache.marmotta.platform.core.api.config.ConfigurationService;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
* ...
Expand All @@ -36,8 +42,29 @@ public class MementoUtils {

/**
* is used for date format used in memento resource uris
* TODO should be HTTP Date format specified by RFC 1123 and in the GMT timezone like "Mon, 19 Sep 2016 23:47:12 GMT"
*/
public static final DateFormat MEMENTO_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
public static final DateFormat MEMENTO_DATE_FORMAT;

static {
MEMENTO_DATE_FORMAT = new SimpleDateFormat(
"EEE, dd MMM yyyy HH:mm:ss z", Locale.US); //TODO which locale should be used?
MEMENTO_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
}

public static final DateFormat MEMENTO_DATE_FORMAT_FOR_URIS = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");

public static URI originalURI(String resource, String baseURI) throws UnsupportedEncodingException {

if(resource.startsWith(baseURI)) {
return URI.create(resource);
} else {
return URI.create(
baseURI +
ConfigurationService.RESOURCE_PATH + "?uri=" +
URLEncoder.encode(resource, "UTF-8"));
}
}

/**
* builds a memento permalink
Expand All @@ -50,7 +77,7 @@ public static URI resourceURI(String resource, Date date, String baseURI) {
baseURI +
MEMENTO_WEBSERVICE + "/" +
MEMENTO_RESOURCE + "/" +
MEMENTO_DATE_FORMAT.format(date) + "/" +
MEMENTO_DATE_FORMAT_FOR_URIS.format(date) + "/" +
resource);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,18 @@ public Response timgateService(@PathParam("resource") String resource_string, @H
try {
//check preconditions
Preconditions.checkNotNull(resource_string,"Resource URI may not null");
Preconditions.checkNotNull(date_string, "Accept-Datetime Header may not be null");

final RepositoryConnection conn = sesameService.getConnection();
try {
Date date = DateUtils.parseDate(date_string);

//if date_string is not set, get NOW, else parse date_string
Date date = null;

if(date_string == null) {
date = new Date();
} else {
date = DateUtils.parseDate(date_string);
}

URI resource = conn.getValueFactory().createURI(resource_string);

Expand All @@ -125,10 +132,9 @@ public Response timgateService(@PathParam("resource") String resource_string, @H

//return permalink
return Response
.status(301)
.status(302)
.location(MementoUtils.resourceURI(resource_string, versions.getCurrent().getCommitTime(), configurationService.getBaseUri()))
.header(VARY, "negotiate, accept-datetime, accept")
.header("Memento-Datetime", versions.getCurrent().getCommitTime().toString())
.header(LINK, Joiner.on(", ").join(links))
.build();

Expand Down Expand Up @@ -173,7 +179,7 @@ public Response resourceService(@PathParam("date")String date_string,
RepositoryConnection conn = sesameService.getConnection();

try {
final Date date = MementoUtils.MEMENTO_DATE_FORMAT.parse(date_string);
final Date date = MementoUtils.MEMENTO_DATE_FORMAT_FOR_URIS.parse(date_string);

final URI resource = conn.getValueFactory().createURI(resource_string);

Expand Down Expand Up @@ -282,7 +288,7 @@ public void write(OutputStream output) throws IOException, WebApplicationExcepti
Set<String> links = new HashSet<String>();
links.add("<" + MementoUtils.timegateURI(resource_string, configurationService.getBaseUri()) + ">;rel=timegate");

links.add("<" + resource_string + ">;rel=original");
links.add("<" + MementoUtils.originalURI(resource_string, configurationService.getBaseUri()) + ">;rel=original");

//create response
return Response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
var target = "#timeknots",
v = [
<#list versions as version>
{'name':"${version.date}", 'date':new Date("${version.tstamp}"), 'uri':"${version.uri}"},
{'name':"${version.formatedDate}", 'date':new Date("${version.tstamp}"), 'uri':"${version.uri}"},
</#list>
{'name':"now", 'date':new Date(),'lineWidth':1, 'uri':"${SERVER_URL}resource?uri=${original?url}"}
];
Expand Down Expand Up @@ -88,7 +88,7 @@
</tr>
<#list versions as version>
<tr>
<td><a target="_blank" href="${version.uri}" class="ldcache">${version.date}</a></td>
<td><a target="_blank" href="${version.uri}" class="ldcache">${version.formatedDate}</a></td>
</tr>
</#list>
</table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ <h1><a name="Memento"></a>Memento Support<a class="section_anchor" href="#Mement
</p>
<p>
Several tools exist for accessing Memento
archives, e.g. the <a href="https://addons.mozilla.org/de/firefox/addon/mementofox/">MementoFox</a> browser
archives, e.g. the <a href="http://bit.ly/memento-for-chrome">Memento For Chrome</a> browser
extension.
</p>

Expand Down
Loading