Skip to content
Merged
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
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ dependencies {
compile("org.trellisldp:trellis-http:$trellisVersion")
compile("org.trellisldp:trellis-vocabulary:$trellisVersion")
compile("org.trellisldp:trellis-file:$trellisVersion")
compile("org.trellisldp:trellis-namespaces:$trellisVersion")
compile("org.trellisldp:trellis-agent:$trellisVersion")
compile("org.trellisldp:trellis-audit:$trellisVersion")
compile("org.trellisldp:trellis-event-serialization:$trellisVersion")
Expand Down
21 changes: 0 additions & 21 deletions app/src/main/java/org/trellisldp/ext/app/db/AppConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ public class AppConfiguration extends TrellisConfiguration {
@NotNull
private String binaries;

@NotNull
private String namespaces;

@NotNull
private Integer levels = 3;

Expand Down Expand Up @@ -97,24 +94,6 @@ public void setBinaries(final String config) {
this.binaries = config;
}

/**
* Set the namespaces filename.
* @param namespaces the namespaces filename
*/
@JsonProperty
public void setNamespaces(final String namespaces) {
this.namespaces = namespaces;
}

/**
* Get the namespace filename.
* @return the namespace filename
*/
@JsonProperty
public String getNamespaces() {
return namespaces;
}

/**
* Set the character length of intermediate path components for internal binary resource identifiers.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
import org.trellisldp.api.ResourceService;
import org.trellisldp.api.ServiceBundler;
import org.trellisldp.app.TrellisCache;
import org.trellisldp.ext.db.DBNamespaceService;
import org.trellisldp.ext.db.DBResourceService;
import org.trellisldp.file.FileBinaryService;
import org.trellisldp.file.FileMementoService;
import org.trellisldp.io.JenaIOService;
import org.trellisldp.namespaces.NamespacesJsonContext;
import org.trellisldp.rdfa.HtmlSerializer;

/**
Expand Down Expand Up @@ -72,7 +72,7 @@ public TrellisServiceBundler(final AppConfiguration config, final Environment en
auditService = resourceService = new DBResourceService(jdbi);
binaryService = new FileBinaryService(new DefaultIdentifierService(), config.getBinaries(),
config.getBinaryHierarchyLevels(), config.getBinaryHierarchyLength());
ioService = buildIoService(config);
ioService = buildIoService(config, jdbi);
eventService = AppUtils.getNotificationService(config.getNotifications(), environment);
}

Expand Down Expand Up @@ -111,12 +111,12 @@ public EventService getEventService() {
return eventService;
}

private static IOService buildIoService(final AppConfiguration config) {
private static IOService buildIoService(final AppConfiguration config, final Jdbi jdbi) {
final Long cacheSize = config.getJsonld().getCacheSize();
final Long hours = config.getJsonld().getCacheExpireHours();
final Cache<String, String> cache = newBuilder().maximumSize(cacheSize).expireAfterAccess(hours, HOURS).build();
final TrellisCache<String, String> profileCache = new TrellisCache<>(cache);
final NamespaceService namespaceService = new NamespacesJsonContext(config.getNamespaces());
final NamespaceService namespaceService = new DBNamespaceService(jdbi);
final RDFaWriterService htmlSerializer = new HtmlSerializer(namespaceService, config.getAssets().getTemplate(),
config.getAssets().getCss(), config.getAssets().getJs(), config.getAssets().getIcon());
return new JenaIOService(namespaceService, htmlSerializer, profileCache,
Expand Down
66 changes: 66 additions & 0 deletions db/src/main/java/org/trellisldp/ext/db/DBNamespaceService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.trellisldp.ext.db;

import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.sql.DataSource;

import org.jdbi.v3.core.Jdbi;
import org.trellisldp.api.NamespaceService;

/**
* A namespace service that stores data in a database.
*/
public class DBNamespaceService implements NamespaceService {

private final Jdbi jdbi;

/**
* Create a namespace service.
* @param ds the datasource
*/
@Inject
public DBNamespaceService(final DataSource ds) {
this(Jdbi.create(ds));
}

/**
* Create a namespace service.
* @param jdbi the Jdbi object
*/
public DBNamespaceService(final Jdbi jdbi) {
this.jdbi = jdbi;
}

@Override
public Map<String, String> getNamespaces() {
final Map<String, String> namespaces = new HashMap<>();
jdbi.useHandle(handle ->
handle.select("SELECT prefix, namespace FROM namespaces")
.map((rs, ctx) -> new SimpleImmutableEntry<>(rs.getString("prefix"), rs.getString("namespace")))
.forEach(pair -> namespaces.put(pair.getKey(), pair.getValue())));
return namespaces;
}

@Override
public Boolean setPrefix(final String prefix, final String namespace) {
jdbi.useHandle(handle ->
handle.execute("INSERT INTO namespaces (prefix, namespace) VALUES (?, ?)", prefix, namespace));
return true;
}
}
31 changes: 31 additions & 0 deletions db/src/main/resources/migrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,35 @@ databaseChangeLog:
name: object
type: STRING
file: org/trellisldp/ext/db/initial_acl${csv}.csv
- changeSet:
id: 2
author: acoburn
changes:
- createTable:
tableName: namespaces
remarks: This table keeps track of namespace prefixes.
columns:
- column:
name: prefix
type: VARCHAR(255)
remarks: A unique prefix.
constraints:
primaryKey: true
nullable: false
- column:
name: namespace
type: VARCHAR(1024)
remarks: The namespace IRI.
constraints:
nullable: false
- loadData:
tableName: namespaces
columns:
- column:
name: prefix
type: STRING
- column:
name: namespace
type: STRING
file: org/trellisldp/ext/db/initial_namespaces.csv

16 changes: 16 additions & 0 deletions db/src/main/resources/org/trellisldp/ext/db/initial_namespaces.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
prefix,namespace
acl,http://www.w3.org/ns/auth/acl#
as,https://www.w3.org/ns/activitystreams#
dc,http://purl.org/dc/terms/
dc11,http://purl.org/dc/elements/1.1/
geo,http://www.w3.org/2003/01/geo/wgs84_pos#
ldp,http://www.w3.org/ns/ldp#
memento,http://mementoweb.org/ns#
owl,http://www.w3.org/2002/07/owl#
prov,http://www.w3.org/ns/prov#
rdf,http://www.w3.org/1999/02/22-rdf-syntax-ns#
rdfs,http://www.w3.org/2000/01/rdf-schema#
schema,http://schema.org/
skos,http://www.w3.org/2004/02/skos/core#
time,http://www.w3.org/2006/time#
xsd,http://www.w3.org/2001/XMLSchema#
80 changes: 80 additions & 0 deletions db/src/test/java/org/trellisldp/ext/db/DBNamespaceServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.trellisldp.ext.db;

import static java.io.File.separator;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.condition.OS.WINDOWS;
import static org.slf4j.LoggerFactory.getLogger;

import com.opentable.db.postgres.embedded.EmbeddedPostgres;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.text.RandomStringGenerator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.slf4j.Logger;
import org.trellisldp.api.NamespaceService;
import org.trellisldp.vocabulary.LDP;

import liquibase.Contexts;
import liquibase.Liquibase;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.LiquibaseException;
import liquibase.resource.ClassLoaderResourceAccessor;

@DisabledOnOs(WINDOWS)
public class DBNamespaceServiceTest {
private static final Logger LOGGER = getLogger(DBNamespaceService.class);

private static EmbeddedPostgres pg = null;

static {
try {
pg = EmbeddedPostgres.builder()
.setDataDirectory("build" + separator + "pgdata-" + new RandomStringGenerator
.Builder().withinRange('a', 'z').build().generate(10)).start();

// Set up database migrations
try (final Connection c = pg.getPostgresDatabase().getConnection()) {
final Liquibase liquibase = new Liquibase("migrations.yml",
new ClassLoaderResourceAccessor(),
new JdbcConnection(c));
final Contexts ctx = null;
liquibase.update(ctx);
}

} catch (final IOException | SQLException | LiquibaseException ex) {
LOGGER.error("Error setting up tests", ex);
}
}

@Test
public void testNamespaceService() {
final NamespaceService svc = new DBNamespaceService(pg.getPostgresDatabase());

assertTrue(svc.getNamespaces().containsKey("ldp"));
assertEquals(LDP.getNamespace(), svc.getNamespaces().get("ldp"));
final Integer size = svc.getNamespaces().size();

assertTrue(svc.setPrefix("ex", "http://example.com/"));
assertEquals(size + 1, svc.getNamespaces().size());
assertEquals("http://example.com/", svc.getNamespaces().get("ex"));
}
}

17 changes: 0 additions & 17 deletions deployment/src/dist/data/namespaces.json

This file was deleted.

2 changes: 0 additions & 2 deletions deployment/src/dist/etc/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ binaries: ${TRELLIS_BINARIES:-/opt/trellis/data/binaries}

mementos: ${TRELLIS_MEMENTOS:-/opt/trellis/data/mementos}

namespaces: ${TRELLIS_NAMESPACES:-/opt/trellis/data/namespaces.json}

# This may refer to a static base URL for resources. If left empty, the
# base URL will reflect the Host header in the request.
baseUrl: ${TRELLIS_BASE_URL:-}
Expand Down