138 lines
3.7 KiB
C#
138 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.Remoting.Contexts;
|
|
using System.Linq;
|
|
|
|
namespace ln.http.resources
|
|
{
|
|
public abstract class Resource : IHTTPResource
|
|
{
|
|
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)
|
|
{
|
|
Name = name;
|
|
}
|
|
public Resource(Resource container,String name)
|
|
:this(name)
|
|
{
|
|
if (container != null)
|
|
container.AddResource(this);
|
|
|
|
Container = container;
|
|
}
|
|
|
|
|
|
string name;
|
|
public String Name
|
|
{
|
|
get => this.name;
|
|
set
|
|
{
|
|
if ((this.name != null) && this.Name.Equals(value))
|
|
return;
|
|
|
|
if (String.Empty.Equals(value) && Container != null)
|
|
throw new ArgumentOutOfRangeException("Name must not be empty");
|
|
|
|
if ((Container != null) && Container.Contains(value))
|
|
throw new ArgumentOutOfRangeException("Container already has resource with this name");
|
|
|
|
if (Container != null)
|
|
Container.RemoveResource(this);
|
|
|
|
this.name = value;
|
|
|
|
if (Container != null)
|
|
Container.AddResource(this);
|
|
}
|
|
}
|
|
|
|
|
|
public abstract bool Contains(String name);
|
|
public virtual bool Contains(Resource resource)
|
|
{
|
|
return (resource != null) && (resource.Container == this) && (GetResource(resource.Name) == resource);
|
|
}
|
|
|
|
public abstract void AddResource(Resource resource);
|
|
public abstract void RemoveResource(Resource resource);
|
|
|
|
public virtual Resource GetResource(string name)
|
|
{
|
|
foreach (Resource r in GetResources())
|
|
if (r.Name.Equals(name))
|
|
return r;
|
|
|
|
return GetFallBackResource();
|
|
}
|
|
public abstract IEnumerable<Resource> GetResources();
|
|
|
|
public virtual IEnumerable<Resource> GetResources(Type resourceType)
|
|
{
|
|
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);
|
|
}
|
|
public abstract HttpResponse GetResponse(HttpRequest httpRequest);
|
|
|
|
private List<Resource> ResourcePathList
|
|
{
|
|
get
|
|
{
|
|
List<Resource> l = null;
|
|
if (Container != null)
|
|
l = Container.ResourcePathList;
|
|
else
|
|
l = new List<Resource>();
|
|
|
|
l.Add(this);
|
|
return l;
|
|
}
|
|
}
|
|
public Resource[] ResourcePath
|
|
{
|
|
get
|
|
{
|
|
return ResourcePathList.ToArray();
|
|
}
|
|
}
|
|
public String[] Path
|
|
{
|
|
get
|
|
{
|
|
return ResourcePathList.Select((x) => x.Name).ToArray();
|
|
}
|
|
}
|
|
|
|
public Resource Root
|
|
{
|
|
get
|
|
{
|
|
if (Container != null)
|
|
return Container.Root;
|
|
return this;
|
|
}
|
|
}
|
|
}
|
|
}
|