Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
758 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { BodyParser } from './BodyParser'; | ||
import { HttpRequest } from '../../server/HttpRequest'; | ||
import { Readable } from 'stream'; | ||
import { readableToString } from '../../util/Util'; | ||
import { SparqlUpdatePatch } from './SparqlUpdatePatch'; | ||
import { translate } from 'sparqlalgebrajs'; | ||
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError'; | ||
import { UnsupportedMediaTypeHttpError } from '../../util/errors/UnsupportedMediaTypeHttpError'; | ||
|
||
/** | ||
* {@link BodyParser} that supports `application/sparql-update` content. | ||
* Will convert the incoming update string to algebra in a {@link SparqlUpdatePatch}. | ||
* Simple since metadata parsing is not yet implemented. | ||
*/ | ||
export class SimpleSparqlUpdateBodyParser extends BodyParser { | ||
public async canHandle(input: HttpRequest): Promise<void> { | ||
const contentType = input.headers['content-type']; | ||
|
||
if (!contentType || contentType !== 'application/sparql-update') { | ||
throw new UnsupportedMediaTypeHttpError('This parser only supports SPARQL UPDATE data.'); | ||
} | ||
} | ||
|
||
public async handle(input: HttpRequest): Promise<SparqlUpdatePatch> { | ||
try { | ||
const sparql = await readableToString(input); | ||
const algebra = translate(sparql, { quads: true }); | ||
|
||
// Prevent body from being requested again | ||
return { | ||
algebra, | ||
dataType: 'sparql-algebra', | ||
raw: sparql, | ||
get data(): Readable { | ||
throw new Error('Body already parsed'); | ||
}, | ||
metadata: { | ||
raw: [], | ||
profiles: [], | ||
contentType: 'application/sparql-update', | ||
}, | ||
}; | ||
} catch (error) { | ||
throw new UnsupportedHttpError(error); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Patch } from './Patch'; | ||
import { Update } from 'sparqlalgebrajs/lib/algebra'; | ||
|
||
/** | ||
* A specific type of {@link Patch} corresponding to a SPARQL update. | ||
*/ | ||
export interface SparqlUpdatePatch extends Patch { | ||
/** | ||
* Algebra corresponding to the SPARQL update. | ||
*/ | ||
algebra: Update; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Operation } from './Operation'; | ||
import { OperationHandler } from './OperationHandler'; | ||
import { Patch } from '../http/Patch'; | ||
import { ResourceStore } from '../../storage/ResourceStore'; | ||
import { ResponseDescription } from './ResponseDescription'; | ||
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError'; | ||
|
||
export class SimplePatchOperationHandler extends OperationHandler { | ||
private readonly store: ResourceStore; | ||
|
||
public constructor(store: ResourceStore) { | ||
super(); | ||
this.store = store; | ||
} | ||
|
||
public async canHandle(input: Operation): Promise<void> { | ||
if (input.method !== 'PATCH') { | ||
throw new UnsupportedHttpError('This handler only supports PATCH operations.'); | ||
} | ||
} | ||
|
||
public async handle(input: Operation): Promise<ResponseDescription> { | ||
await this.store.modifyResource(input.target, input.body as Patch); | ||
return { identifier: input.target }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Conditions } from './Conditions'; | ||
import { Patch } from '../ldp/http/Patch'; | ||
import { PatchHandler } from './patch/PatchHandler'; | ||
import { Representation } from '../ldp/representation/Representation'; | ||
import { RepresentationPreferences } from '../ldp/representation/RepresentationPreferences'; | ||
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier'; | ||
import { ResourceStore } from './ResourceStore'; | ||
|
||
/** | ||
* {@link ResourceStore} using decorator pattern for the `modifyResource` function. | ||
* If the original store supports the {@link Patch}, behaviour will be identical, | ||
* otherwise one of the {@link PatchHandler}s supporting the given Patch will be called instead. | ||
*/ | ||
export class PatchingStore implements ResourceStore { | ||
private readonly source: ResourceStore; | ||
private readonly patcher: PatchHandler; | ||
|
||
public constructor(source: ResourceStore, patcher: PatchHandler) { | ||
this.source = source; | ||
this.patcher = patcher; | ||
} | ||
|
||
public async addResource(container: ResourceIdentifier, representation: Representation, conditions?: Conditions): Promise<ResourceIdentifier> { | ||
return this.source.addResource(container, representation, conditions); | ||
} | ||
|
||
public async deleteResource(identifier: ResourceIdentifier, conditions?: Conditions): Promise<void> { | ||
return this.source.deleteResource(identifier, conditions); | ||
} | ||
|
||
public async getRepresentation(identifier: ResourceIdentifier, preferences: RepresentationPreferences, conditions?: Conditions): Promise<Representation> { | ||
return this.source.getRepresentation(identifier, preferences, conditions); | ||
} | ||
|
||
public async setRepresentation(identifier: ResourceIdentifier, representation: Representation, conditions?: Conditions): Promise<void> { | ||
return this.source.setRepresentation(identifier, representation, conditions); | ||
} | ||
|
||
public async modifyResource(identifier: ResourceIdentifier, patch: Patch, conditions?: Conditions): Promise<void> { | ||
try { | ||
return await this.source.modifyResource(identifier, patch, conditions); | ||
} catch (error) { | ||
return this.patcher.handleSafe({ identifier, patch }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { AsyncHandler } from '../../util/AsyncHandler'; | ||
import { Patch } from '../../ldp/http/Patch'; | ||
import { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier'; | ||
|
||
export abstract class PatchHandler extends AsyncHandler<{identifier: ResourceIdentifier; patch: Patch}> {} |
Oops, something went wrong.