refactor: Remove base URls with ContainerManager when possible

This commit is contained in:
Joachim Van Herwegen 2020-12-09 16:08:59 +01:00
parent f0db9e501f
commit 1bb5de97ab
10 changed files with 12 additions and 28 deletions

View File

@ -26,9 +26,6 @@
"DataAccessorBasedStore:_accessor": { "DataAccessorBasedStore:_accessor": {
"@id": "urn:solid-server:default:FileDataAccessor" "@id": "urn:solid-server:default:FileDataAccessor"
}, },
"DataAccessorBasedStore:_base": {
"@id": "urn:solid-server:default:variable:baseUrl"
},
"DataAccessorBasedStore:_identifierStrategy": { "DataAccessorBasedStore:_identifierStrategy": {
"@id": "urn:solid-server:default:IdentifierStrategy" "@id": "urn:solid-server:default:IdentifierStrategy"
} }

View File

@ -14,9 +14,6 @@
"DataAccessorBasedStore:_accessor": { "DataAccessorBasedStore:_accessor": {
"@id": "urn:solid-server:default:MemoryDataAccessor" "@id": "urn:solid-server:default:MemoryDataAccessor"
}, },
"DataAccessorBasedStore:_base": {
"@id": "urn:solid-server:default:variable:baseUrl"
},
"DataAccessorBasedStore:_identifierStrategy": { "DataAccessorBasedStore:_identifierStrategy": {
"@id": "urn:solid-server:default:IdentifierStrategy" "@id": "urn:solid-server:default:IdentifierStrategy"
} }

View File

@ -7,9 +7,6 @@
"SparqlDataAccessor:_endpoint": { "SparqlDataAccessor:_endpoint": {
"@id": "urn:solid-server:default:variable:sparqlEndpoint" "@id": "urn:solid-server:default:variable:sparqlEndpoint"
}, },
"SparqlDataAccessor:_base": {
"@id": "urn:solid-server:default:variable:baseUrl"
},
"SparqlDataAccessor:_identifierStrategy": { "SparqlDataAccessor:_identifierStrategy": {
"@id": "urn:solid-server:default:IdentifierStrategy" "@id": "urn:solid-server:default:IdentifierStrategy"
} }
@ -21,9 +18,6 @@
"DataAccessorBasedStore:_accessor": { "DataAccessorBasedStore:_accessor": {
"@id": "urn:solid-server:default:SparqlDataAccessor" "@id": "urn:solid-server:default:SparqlDataAccessor"
}, },
"DataAccessorBasedStore:_base": {
"@id": "urn:solid-server:default:variable:baseUrl"
},
"DataAccessorBasedStore:_identifierStrategy": { "DataAccessorBasedStore:_identifierStrategy": {
"@id": "urn:solid-server:default:IdentifierStrategy" "@id": "urn:solid-server:default:IdentifierStrategy"
} }

View File

@ -50,12 +50,10 @@ import type { ResourceStore } from './ResourceStore';
*/ */
export class DataAccessorBasedStore implements ResourceStore { export class DataAccessorBasedStore implements ResourceStore {
private readonly accessor: DataAccessor; private readonly accessor: DataAccessor;
private readonly base: string;
private readonly identifierStrategy: IdentifierStrategy; private readonly identifierStrategy: IdentifierStrategy;
public constructor(accessor: DataAccessor, base: string, identifierStrategy: IdentifierStrategy) { public constructor(accessor: DataAccessor, identifierStrategy: IdentifierStrategy) {
this.accessor = accessor; this.accessor = accessor;
this.base = ensureTrailingSlash(base);
this.identifierStrategy = identifierStrategy; this.identifierStrategy = identifierStrategy;
} }
@ -149,7 +147,7 @@ export class DataAccessorBasedStore implements ResourceStore {
public async deleteResource(identifier: ResourceIdentifier): Promise<void> { public async deleteResource(identifier: ResourceIdentifier): Promise<void> {
this.validateIdentifier(identifier); this.validateIdentifier(identifier);
if (ensureTrailingSlash(identifier.path) === this.base) { if (this.identifierStrategy.isRootContainer(identifier)) {
throw new MethodNotAllowedHttpError('Cannot delete root container.'); throw new MethodNotAllowedHttpError('Cannot delete root container.');
} }
const metadata = await this.accessor.getMetadata(identifier); const metadata = await this.accessor.getMetadata(identifier);
@ -163,7 +161,7 @@ export class DataAccessorBasedStore implements ResourceStore {
* Verify if the given identifier matches the stored base. * Verify if the given identifier matches the stored base.
*/ */
protected validateIdentifier(identifier: ResourceIdentifier): void { protected validateIdentifier(identifier: ResourceIdentifier): void {
if (!identifier.path.startsWith(this.base)) { if (!this.identifierStrategy.supportsIdentifier(identifier)) {
throw new NotFoundHttpError(); throw new NotFoundHttpError();
} }
} }

View File

@ -26,7 +26,7 @@ import { UnsupportedMediaTypeHttpError } from '../../util/errors/UnsupportedMedi
import { guardStream } from '../../util/GuardedStream'; import { guardStream } from '../../util/GuardedStream';
import type { Guarded } from '../../util/GuardedStream'; import type { Guarded } from '../../util/GuardedStream';
import type { IdentifierStrategy } from '../../util/identifiers/IdentifierStrategy'; import type { IdentifierStrategy } from '../../util/identifiers/IdentifierStrategy';
import { ensureTrailingSlash, isContainerIdentifier } from '../../util/PathUtil'; import { isContainerIdentifier } from '../../util/PathUtil';
import { generateResourceQuads } from '../../util/ResourceUtil'; import { generateResourceQuads } from '../../util/ResourceUtil';
import { CONTENT_TYPE, LDP } from '../../util/UriConstants'; import { CONTENT_TYPE, LDP } from '../../util/UriConstants';
import { toNamedNode } from '../../util/UriUtil'; import { toNamedNode } from '../../util/UriUtil';
@ -49,14 +49,12 @@ const { defaultGraph, namedNode, quad, variable } = DataFactory;
export class SparqlDataAccessor implements DataAccessor { export class SparqlDataAccessor implements DataAccessor {
protected readonly logger = getLoggerFor(this); protected readonly logger = getLoggerFor(this);
private readonly endpoint: string; private readonly endpoint: string;
private readonly base: string;
private readonly identifierStrategy: IdentifierStrategy; private readonly identifierStrategy: IdentifierStrategy;
private readonly fetcher: SparqlEndpointFetcher; private readonly fetcher: SparqlEndpointFetcher;
private readonly generator: SparqlGenerator; private readonly generator: SparqlGenerator;
public constructor(endpoint: string, base: string, identifierStrategy: IdentifierStrategy) { public constructor(endpoint: string, identifierStrategy: IdentifierStrategy) {
this.endpoint = endpoint; this.endpoint = endpoint;
this.base = ensureTrailingSlash(base);
this.identifierStrategy = identifierStrategy; this.identifierStrategy = identifierStrategy;
this.fetcher = new SparqlEndpointFetcher(); this.fetcher = new SparqlEndpointFetcher();
this.generator = new Generator(); this.generator = new Generator();
@ -93,7 +91,7 @@ export class SparqlDataAccessor implements DataAccessor {
const quads = await arrayifyStream(stream); const quads = await arrayifyStream(stream);
// Root container will not have metadata if there are no containment triples // Root container will not have metadata if there are no containment triples
if (quads.length === 0 && identifier.path !== this.base) { if (quads.length === 0 && !this.identifierStrategy.isRootContainer(identifier)) {
throw new NotFoundHttpError(); throw new NotFoundHttpError();
} }
@ -103,7 +101,7 @@ export class SparqlDataAccessor implements DataAccessor {
} }
// Need to generate type metadata for the root container since it's not stored // Need to generate type metadata for the root container since it's not stored
if (identifier.path === this.base) { if (this.identifierStrategy.isRootContainer(identifier)) {
metadata.addQuads(generateResourceQuads(name, true)); metadata.addQuads(generateResourceQuads(name, true));
} }

View File

@ -61,7 +61,7 @@ export const getRootFilePath = (subfolder: string): string => join(__dirname, '.
* @returns The data accessor based store. * @returns The data accessor based store.
*/ */
export const getDataAccessorStore = (base: string, dataAccessor: DataAccessor): DataAccessorBasedStore => export const getDataAccessorStore = (base: string, dataAccessor: DataAccessor): DataAccessorBasedStore =>
new DataAccessorBasedStore(dataAccessor, base, new SingleRootIdentifierStrategy(base)); new DataAccessorBasedStore(dataAccessor, new SingleRootIdentifierStrategy(base));
/** /**
* Gives an in memory resource store based on (default) base url. * Gives an in memory resource store based on (default) base url.

View File

@ -26,7 +26,7 @@ describe('A LockingResourceStore', (): void => {
const base = 'http://test.com/'; const base = 'http://test.com/';
path = `${base}path`; path = `${base}path`;
source = new DataAccessorBasedStore(new InMemoryDataAccessor(base), base, new SingleRootIdentifierStrategy(base)); source = new DataAccessorBasedStore(new InMemoryDataAccessor(base), new SingleRootIdentifierStrategy(base));
locker = new SingleThreadedResourceLocker(); locker = new SingleThreadedResourceLocker();
expiringLocker = new WrappedExpiringResourceLocker(locker, 1000); expiringLocker = new WrappedExpiringResourceLocker(locker, 1000);

View File

@ -8,7 +8,7 @@ import { describeIf, FileTestHelper } from '../util/TestHelpers';
describeIf('docker', 'a server with a SPARQL endpoint as storage', (): void => { describeIf('docker', 'a server with a SPARQL endpoint as storage', (): void => {
describe('without acl', (): void => { describe('without acl', (): void => {
const config = new DataAccessorBasedConfig(BASE, const config = new DataAccessorBasedConfig(BASE,
new SparqlDataAccessor('http://localhost:4000/sparql', BASE, new SingleRootIdentifierStrategy(BASE)), new SparqlDataAccessor('http://localhost:4000/sparql', new SingleRootIdentifierStrategy(BASE)),
INTERNAL_QUADS); INTERNAL_QUADS);
const handler = config.getHttpHandler(); const handler = config.getHttpHandler();
const fileHelper = new FileTestHelper(handler, new URL(BASE)); const fileHelper = new FileTestHelper(handler, new URL(BASE));

View File

@ -80,7 +80,7 @@ describe('A DataAccessorBasedStore', (): void => {
beforeEach(async(): Promise<void> => { beforeEach(async(): Promise<void> => {
accessor = new SimpleDataAccessor(); accessor = new SimpleDataAccessor();
store = new DataAccessorBasedStore(accessor, root, identifierStrategy); store = new DataAccessorBasedStore(accessor, identifierStrategy);
containerMetadata = new RepresentationMetadata( containerMetadata = new RepresentationMetadata(
{ [RDF.type]: [ DataFactory.namedNode(LDP.Container), DataFactory.namedNode(LDP.BasicContainer) ]}, { [RDF.type]: [ DataFactory.namedNode(LDP.Container), DataFactory.namedNode(LDP.BasicContainer) ]},

View File

@ -66,7 +66,7 @@ describe('A SparqlDataAccessor', (): void => {
})); }));
// This needs to be last so the fetcher can be mocked first // This needs to be last so the fetcher can be mocked first
accessor = new SparqlDataAccessor(endpoint, base, identifierStrategy); accessor = new SparqlDataAccessor(endpoint, identifierStrategy);
}); });
it('can only handle quad data.', async(): Promise<void> => { it('can only handle quad data.', async(): Promise<void> => {