Implement Resource.FallBackResource

master
Harald Wolff 2019-09-02 09:59:09 +02:00
parent 943e889cf9
commit cef4e263b5
4 changed files with 24 additions and 15 deletions

View File

@ -40,7 +40,10 @@ namespace ln.http.resources
public override Resource GetResource(string name)
{
return this.resources[name];
if (this.resources.ContainsKey(name))
return this.resources[name];
return GetFallBackResource();
}
public override IEnumerable<Resource> GetResources()
@ -54,10 +57,6 @@ namespace ln.http.resources
}
public virtual Resource CreateResource(string name)
{
throw new NotImplementedException();

View File

@ -126,7 +126,7 @@ namespace ln.http.resources
return resource;
}
throw new KeyNotFoundException();
return GetFallBackResource();
}
public override IEnumerable<Resource> GetResources()

View File

@ -10,6 +10,8 @@ namespace ln.http.resources
public Resource Container { get; protected set; }
public Resource DefaultResource { get; set; }
public Resource FallBackResource { get; set; }
public virtual bool HandlesDispatching => false;
public Resource(String name)
@ -66,7 +68,8 @@ namespace ln.http.resources
foreach (Resource r in GetResources())
if (r.Name.Equals(name))
return r;
throw new KeyNotFoundException();
return GetFallBackResource();
}
public abstract IEnumerable<Resource> GetResources();
@ -75,6 +78,17 @@ namespace ln.http.resources
return this.GetResources().Where((x) => resourceType.IsInstanceOfType(x));
}
public virtual Resource GetFallBackResource()
{
if (FallBackResource != null)
return FallBackResource;
if (Container != null)
return Container.GetFallBackResource();
throw new KeyNotFoundException();
}
public virtual HttpResponse GetResponse(HttpRequest httpRequest, Queue<string> pathStack)
{
return GetResponse(httpRequest);

View File

@ -14,22 +14,18 @@ namespace ln.http.resources
public override HttpResponse GetResponse(HttpRequest httpRequest)
{
/* TODO: Implement Authentication here... */
Resource currentResource = RootResource;
Queue<String> pathStack = new Queue<String>(httpRequest.URI.AbsolutePath.Split(new String[] { "/" }, StringSplitOptions.RemoveEmptyEntries));
while ((pathStack.Count > 0) && (!currentResource.HandlesDispatching))
{
String next = pathStack.Dequeue();
if (!currentResource.Contains(next))
throw new KeyNotFoundException();
{
currentResource = currentResource.GetFallBackResource();
break;
}
currentResource = currentResource.GetResource(next);
/* TODO: Implement Authorization hook here... */
}
return currentResource.GetResponse(httpRequest,pathStack);