mediatypes in exyus metadata

2007-12-04 @ 04:49#

i was able to complete the work to move all media-types definitions for exyus resource classes into a custom attribute instead of an instance collection. i also completed the code that collects media-type data for each UriPattern and stores that in memory for access throughout the run of the app. technically, i need to create a backup for the memory (a temporary disk file), but i can do that later this week.

now, defining a resource for exyus includes defining the UriPattern and the MediaTypes attributes along with the details of the resource including the content (string, static file, xml/xsl transform pair. database data, etc.), and the caching rules. here's an example that returns a simple string using an optional query string argument (?name=fred):

[UriPattern(@"/hello/(?<tail>\.xcs)(.*)?"")]
public class StartPage : GetHandler
{
    public StartPage()
    {
        this.Content = "<h1>hello $name$</h1>";
    }
}

here's a more interesting example that returns the date/time and supports three different media types:

[UriPattern(@"/time/(?<tail>\.xcs)(.*)?")]
[MediaTypes("text/html","text/xml","text/plain")]
public class timeResource : GetHandler
{
    string xml_out = "<root><date-time>{0:yyyy-MM-ddThh:mm:ss}</date-time></root>";
    string html_out = "<div><span class=\"date-time\">{0:yyyy-MM-ddThh:mm:ss}</span></div>";
    string plain_out = "{0:yyyy-MM-ddThh:mm:ss}";

    public timeResource()
    {
        this.ContentType = "text/html";
    }

    public override void Get()
    {
        Utility util = new Utility();
        string out_text = string.Empty;
        string mtype = util.GetMediaType(this);
        if (mtype != this.ContentType)
        {
            this.ContentType = mtype;
            this.Status.ContentType = mtype;
        }

        switch (mtype)
        {
            case "text/xml":
                out_text = string.Format(xml_out, DateTime.UtcNow);
                break;
            case "text/html":
                out_text = string.Format(html_out, DateTime.UtcNow);
                break;
            case "text/plain":
                out_text = string.Format(plain_out, DateTime.UtcNow);
                break;
            default:
                out_text = string.Empty;
                break;
        }

        this.Response = out_text;
    }
}

there's lots of good stuff coming out soon. i'm getting excited again!

code