From c2db737fa3fe4a9b140718d2fecc50744a32c16c Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Sat, 5 Jun 2010 18:25:32 +0000 Subject: [PATCH] Handlers refactored; AbstractGetHandler implemented; logic implemented in HandlersFactory; WebContext (+designs) architecture implemented --- IISMainHandler/Extensions.cs | 4 ++ IISMainHandler/HandlersFactory.cs | 8 ++-- IISMainHandler/IISMainHandler.csproj | 7 ++++ IISMainHandler/ISpecificHandler.cs | 4 +- IISMainHandler/MainHandler.cs | 8 ++-- IISMainHandler/TemplateEngine.cs | 15 +++++++ IISMainHandler/WebContext.cs | 40 +++++++++++++++++++ IISMainHandler/designs/Classic.cs | 9 +++++ IISMainHandler/designs/IDesign.cs | 9 +++++ IISMainHandler/designs/Lite.cs | 9 +++++ IISMainHandler/handlers/AbstractGetHandler.cs | 20 ++++++++++ IISMainHandler/handlers/DebugHandler.cs | 19 ++++----- IISMainHandler/handlers/RootHandler.cs | 24 +++++++++++ IISMainHandler/handlers/WrongUrlHandler.cs | 4 +- 14 files changed, 157 insertions(+), 23 deletions(-) create mode 100644 IISMainHandler/TemplateEngine.cs create mode 100644 IISMainHandler/WebContext.cs create mode 100644 IISMainHandler/designs/Classic.cs create mode 100644 IISMainHandler/designs/IDesign.cs create mode 100644 IISMainHandler/designs/Lite.cs create mode 100644 IISMainHandler/handlers/AbstractGetHandler.cs create mode 100644 IISMainHandler/handlers/RootHandler.cs diff --git a/IISMainHandler/Extensions.cs b/IISMainHandler/Extensions.cs index bd7a178..bbee1f5 100644 --- a/IISMainHandler/Extensions.cs +++ b/IISMainHandler/Extensions.cs @@ -13,5 +13,9 @@ namespace FLocal.IISHandler { response.Write((char)0x0a); } + public static string[] Split(this string str, string separator, StringSplitOptions options) { + return str.Split(new string[] { separator }, options); + } + } } diff --git a/IISMainHandler/HandlersFactory.cs b/IISMainHandler/HandlersFactory.cs index 681d7b9..f96b229 100644 --- a/IISMainHandler/HandlersFactory.cs +++ b/IISMainHandler/HandlersFactory.cs @@ -7,9 +7,11 @@ using System.Web; namespace FLocal.IISHandler { class HandlersFactory { - public static ISpecificHandler getHandler(HttpContext context) { - //return new handlers.DebugHandler(context); - return new handlers.WrongUrlHandler(); + public static ISpecificHandler getHandler(WebContext context) { + string[] requestParts = context.httprequest.Path.Split("/", StringSplitOptions.RemoveEmptyEntries); + if(requestParts.Length < 1) return new handlers.RootHandler(); + return new handlers.DebugHandler(requestParts[0]); + //return new handlers.WrongUrlHandler(); } } diff --git a/IISMainHandler/IISMainHandler.csproj b/IISMainHandler/IISMainHandler.csproj index 5d66cf7..1e58e47 100644 --- a/IISMainHandler/IISMainHandler.csproj +++ b/IISMainHandler/IISMainHandler.csproj @@ -46,13 +46,20 @@ + + + + + + + diff --git a/IISMainHandler/ISpecificHandler.cs b/IISMainHandler/ISpecificHandler.cs index 6d31272..3621466 100644 --- a/IISMainHandler/ISpecificHandler.cs +++ b/IISMainHandler/ISpecificHandler.cs @@ -5,9 +5,9 @@ using System.Text; using System.Web; namespace FLocal.IISHandler { - interface ISpecificHandler : IDisposable { + interface ISpecificHandler { - void Handle(); + void Handle(WebContext context); } } diff --git a/IISMainHandler/MainHandler.cs b/IISMainHandler/MainHandler.cs index 94e3f21..8b67d97 100644 --- a/IISMainHandler/MainHandler.cs +++ b/IISMainHandler/MainHandler.cs @@ -11,10 +11,10 @@ namespace FLocal.IISHandler { get { return true; } } - public void ProcessRequest(HttpContext context) { - using(ISpecificHandler handler = HandlersFactory.getHandler(context)) { - handler.Handle(); - } + public void ProcessRequest(HttpContext httpcontext) { + WebContext context = new WebContext(httpcontext); + ISpecificHandler handler = HandlersFactory.getHandler(context); + handler.Handle(context); } } diff --git a/IISMainHandler/TemplateEngine.cs b/IISMainHandler/TemplateEngine.cs new file mode 100644 index 0000000..a3c9bae --- /dev/null +++ b/IISMainHandler/TemplateEngine.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Xml.Linq; + +namespace FLocal.IISHandler { + class TemplateEngine { + + public static string Compile(string pathToTemplate, XDocument data) { + throw new NotImplementedException(); + } + + } +} diff --git a/IISMainHandler/WebContext.cs b/IISMainHandler/WebContext.cs new file mode 100644 index 0000000..1c6bf1b --- /dev/null +++ b/IISMainHandler/WebContext.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Web; + +namespace FLocal.IISHandler { + class WebContext { + + public readonly HttpContext httpcontext; + + public HttpRequest httprequest { + get { + return this.httpcontext.Request; + } + } + + public HttpResponse httpresponse { + get { + return this.httpcontext.Response; + } + } + + public designs.IDesign design { + get { + throw new NotImplementedException(); + } + } + + public WebContext(HttpContext httpcontext) { + this.httpcontext = httpcontext; + } + + public string Transform(string templateName, System.Xml.Linq.XDocument data) { + //TODO: this should work according to design! + return TemplateEngine.Compile(templateName, data); + } + + } +} diff --git a/IISMainHandler/designs/Classic.cs b/IISMainHandler/designs/Classic.cs new file mode 100644 index 0000000..de807d6 --- /dev/null +++ b/IISMainHandler/designs/Classic.cs @@ -0,0 +1,9 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace FLocal.IISHandler.designs { + class Classic : IDesign { + } +} diff --git a/IISMainHandler/designs/IDesign.cs b/IISMainHandler/designs/IDesign.cs new file mode 100644 index 0000000..642a512 --- /dev/null +++ b/IISMainHandler/designs/IDesign.cs @@ -0,0 +1,9 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace FLocal.IISHandler.designs { + interface IDesign { + } +} diff --git a/IISMainHandler/designs/Lite.cs b/IISMainHandler/designs/Lite.cs new file mode 100644 index 0000000..77232e3 --- /dev/null +++ b/IISMainHandler/designs/Lite.cs @@ -0,0 +1,9 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace FLocal.IISHandler.designs { + class Lite : IDesign { + } +} diff --git a/IISMainHandler/handlers/AbstractGetHandler.cs b/IISMainHandler/handlers/AbstractGetHandler.cs new file mode 100644 index 0000000..cac7e6d --- /dev/null +++ b/IISMainHandler/handlers/AbstractGetHandler.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace FLocal.IISHandler.handlers { + abstract class AbstractGetHandler : ISpecificHandler { + + abstract protected string templateName { + get; + } + + abstract protected System.Xml.Linq.XDocument getData(WebContext context); + + public void Handle(WebContext context) { + context.httpresponse.Write(context.Transform(this.templateName, this.getData(context))); + } + + } +} diff --git a/IISMainHandler/handlers/DebugHandler.cs b/IISMainHandler/handlers/DebugHandler.cs index 6bcd30b..081aae5 100644 --- a/IISMainHandler/handlers/DebugHandler.cs +++ b/IISMainHandler/handlers/DebugHandler.cs @@ -7,20 +7,17 @@ using System.Web; namespace FLocal.IISHandler.handlers { class DebugHandler : ISpecificHandler { - private HttpContext context; + private string type; - public DebugHandler(HttpContext context) { - this.context = context; + public DebugHandler(string type) { + this.type = type; } - public void Handle() { - context.Response.ContentType = "text/plain"; - context.Response.WriteLine("Path: " + context.Request.Path); - context.Response.WriteLine("PathInfo: " + context.Request.PathInfo); - } - - public void Dispose() { - this.context = null; + public void Handle(WebContext context) { + context.httpresponse.ContentType = "text/plain"; + context.httpresponse.WriteLine("Page: " + this.type); + context.httpresponse.WriteLine("Path: " + context.httprequest.Path); + context.httpresponse.WriteLine("PathInfo: " + context.httprequest.PathInfo); } } diff --git a/IISMainHandler/handlers/RootHandler.cs b/IISMainHandler/handlers/RootHandler.cs new file mode 100644 index 0000000..c451cc8 --- /dev/null +++ b/IISMainHandler/handlers/RootHandler.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Web; +using System.Xml.Linq; + +namespace FLocal.IISHandler.handlers { + + class RootHandler : AbstractGetHandler { + + override protected string templateName { + get { + throw new NotImplementedException(); + } + } + + override protected XDocument getData(WebContext context) { + throw new NotImplementedException(); + } + + } + +} \ No newline at end of file diff --git a/IISMainHandler/handlers/WrongUrlHandler.cs b/IISMainHandler/handlers/WrongUrlHandler.cs index f2393bc..df5529d 100644 --- a/IISMainHandler/handlers/WrongUrlHandler.cs +++ b/IISMainHandler/handlers/WrongUrlHandler.cs @@ -7,11 +7,9 @@ using System.Web; namespace FLocal.IISHandler.handlers { class WrongUrlHandler : ISpecificHandler { - public void Handle() { + public void Handle(WebContext context) { throw new HttpException(404, "page not found"); } - public void Dispose() { } - } }