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
30 changed files
with
691 additions
and
323 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
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
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,42 @@ | ||
import { Conditions } from './Conditions'; | ||
import { Patch } from '../ldp/http/Patch'; | ||
import { Representation } from '../ldp/representation/Representation'; | ||
import { RepresentationPreferences } from '../ldp/representation/RepresentationPreferences'; | ||
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier'; | ||
import { ResourceStore } from './ResourceStore'; | ||
|
||
/** | ||
* Store that calls the corresponding functions of the source Store. | ||
* Can be extended by stores that do not want to override all functions | ||
* by implementing a decorator pattern. | ||
*/ | ||
export class PassthroughStore implements ResourceStore { | ||
protected readonly source: ResourceStore; | ||
|
||
public constructor(source: ResourceStore) { | ||
this.source = source; | ||
} | ||
|
||
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 modifyResource(identifier: ResourceIdentifier, patch: Patch, conditions?: Conditions): Promise<void> { | ||
return this.source.modifyResource(identifier, patch, conditions); | ||
} | ||
|
||
public async setRepresentation(identifier: ResourceIdentifier, representation: Representation, | ||
conditions?: Conditions): Promise<void> { | ||
return this.source.setRepresentation(identifier, representation, conditions); | ||
} | ||
} |
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Conditions } from './Conditions'; | ||
import { matchingMediaType } from '../util/Util'; | ||
import { PassthroughStore } from './PassthroughStore'; | ||
import { Representation } from '../ldp/representation/Representation'; | ||
import { RepresentationConverter } from './conversion/RepresentationConverter'; | ||
import { RepresentationPreferences } from '../ldp/representation/RepresentationPreferences'; | ||
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier'; | ||
import { ResourceStore } from './ResourceStore'; | ||
|
||
/** | ||
* Store that overrides the `getRepresentation` function. | ||
* Tries to convert the {@link Representation} it got from the source store | ||
* so it matches one of the given type preferences. | ||
* | ||
* In the future this class should take the preferences of the request into account. | ||
* Even if there is a match with the output from the store, | ||
* if there is a low weight for that type conversions might still be preferred. | ||
*/ | ||
export class RepresentationConvertingStore extends PassthroughStore { | ||
private readonly converter: RepresentationConverter; | ||
|
||
public constructor(source: ResourceStore, converter: RepresentationConverter) { | ||
super(source); | ||
this.converter = converter; | ||
} | ||
|
||
public async getRepresentation(identifier: ResourceIdentifier, preferences: RepresentationPreferences, | ||
conditions?: Conditions): Promise<Representation> { | ||
const representation = await super.getRepresentation(identifier, preferences, conditions); | ||
if (this.matchesPreferences(representation, preferences)) { | ||
return representation; | ||
} | ||
return this.converter.handleSafe({ identifier, representation, preferences }); | ||
} | ||
|
||
private matchesPreferences(representation: Representation, preferences: RepresentationPreferences): boolean { | ||
if (!preferences.type) { | ||
return true; | ||
} | ||
return Boolean( | ||
representation.metadata.contentType && | ||
preferences.type.some((type): boolean => | ||
type.weight > 0 && | ||
matchingMediaType(type.value, representation.metadata.contentType!)), | ||
); | ||
} | ||
} |
Oops, something went wrong.