Commit 6fc77f5e authored by Laurent Wouters's avatar Laurent Wouters
Browse files

[fix] Fixing problem with resources with multiple alternative URIs

parent 9098079d783f
......@@ -261,7 +261,7 @@ export function fetchObservableAt(target: string): Promise<ObservableContent> {
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status < 200 && xmlHttp.status >= 300)
if (xmlHttp.status < 200 || xmlHttp.status >= 300)
return reject("HTTP error: " + xmlHttp.status);
let contentType = xmlHttp.getResponseHeader("Content-Type");
let index = contentType.indexOf(";");
......@@ -470,7 +470,7 @@ function doFetchDocumentLink(link: DocumentSourceLinked): Promise<RawContent> {
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status < 200 && xmlHttp.status >= 300) {
if (xmlHttp.status < 200 || xmlHttp.status >= 300) {
reject("HTTP error: " + xmlHttp.status);
}
let ct = xmlHttp.getResponseHeader("Content-Type");
......@@ -747,7 +747,7 @@ export function tryNegotiateData(
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status < 200 && xmlHttp.status >= 300)
if (xmlHttp.status < 200 || xmlHttp.status >= 300)
return reject("HTTP error: " + xmlHttp.status);
let contentType = xmlHttp.getResponseHeader("Content-Type");
let index = contentType.indexOf(";");
......
......@@ -148,6 +148,57 @@ export function sameResources(first: Resource, second: Resource): boolean {
return false;
}
/**
* Probes the different URIs for a resource and remove the dead ones
* Returns a promise for clean version of the resource
* @param resource The resource
*/
export function probeResource(resource: Resource): Promise<Resource> {
return new Promise<Resource>(
(resolve: (result: Resource) => void, reject: (reason: any) => void) => {
let remaining: String[] = [];
let count = resource.uris.length;
let onFinished = function() {
count -= 1;
if (count == 0) {
let main =
remaining.indexOf(resource.uri) >= 0 ? resource.uri : remaining[0];
resolve({ uri: main, uris: remaining } as Resource);
}
};
resource.uris.forEach((uri: string) =>
probeResourceUri(uri)
.then((uri: string) => {
remaining.push(uri);
onFinished();
})
.catch(onFinished)
);
}
);
}
/**
* Probes an URI for a resource to check whether it is valid
* @param uri An URI
*/
function probeResourceUri(uri: string): Promise<String> {
return new Promise<String>(
(resolve: (result: String) => void, reject: (reason: any) => void) => {
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status < 200 || xmlHttp.status >= 300)
return reject("HTTP error: " + xmlHttp.status);
resolve(uri);
}
};
xmlHttp.open("HEAD", uri, true);
xmlHttp.send();
}
);
}
/**
* Represents the rendering of a resource
*/
......
......@@ -24,7 +24,8 @@ import {
Language,
ViewRenderer,
RenderingContext,
Resource
Resource,
probeResource
} from "./application";
/**
......@@ -287,6 +288,10 @@ export class MetaClass {
* The class's meta properties
*/
public properties: MetaProperty[] = [];
/**
* Whether to probe the associated resource when loading
*/
public probeEntity: boolean = false;
/**
* Loads an instance of this metaclass from the specified entity
......@@ -303,6 +308,21 @@ export class MetaClass {
result[property.name] = property.load(entity, loader);
});
}
/**
* Loads an instance of this metaclass from the specified entity
* @param entity An entity
* @param result The resulting object
* @param loader The loader
*/
public load(entity: RdfEntity, result: Loadable, loader: Loader): void {
if (this.probeEntity) {
probeResource(result.__resource).then((cleanResource: Resource) => {
result.__resource = cleanResource;
});
}
this.loadProperties(entity, result, loader);
}
}
/**
......@@ -378,7 +398,7 @@ class LoaderImpl implements Loader {
public close(): void {
let i = 0;
while (i < this.items.length) {
this.items[i].metaClass.loadProperties(
this.items[i].metaClass.load(
this.items[i].entity,
this.items[i].object,
this
......
......@@ -223,6 +223,7 @@ export interface Talk extends rdfMeta.Loadable {
* The metaclass for simple links
*/
const LINK_METACLASS = new rdfMeta.MetaClass();
LINK_METACLASS.probeEntity = true;
/**
* The metaclass for Person
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment