I want to get a few blog posts out of the way as I have left off blogging until I had a suitable Syntax Highlighter plug-in for Windows Live Writer.

This morning whilst I am literally inside VS 2010 developing, the computer froze, restarted and then informed me that it could not find a valid boot sector…joy!  Once this happened following my other computer blowing up and the laptop being on the fritz I thought I would finally take the plunge and get on with using Linux, more than that I want to use Linux and Windows i.e. Duel Boot. 

Before I started however I needed to backup the files that I had on the hard drive which has just gone west, this hard drive is a PATA and all the external enclosures I have are for SATA.  So one small purchase was an external enclosure for this which only set me back 17.00GBP, bonus.  I have another of these PATA hard drive which I had previously tried to install CENT OS onto and it went wrong, period; so with this new hardware I was easily able to wire up and format, bang back into the desktop and boot from CD. 

Installing Ubuntu next to Windows 7

IMPORTANT : Install Windows first.  I have been told this in person and also read about it afterwards, please read up about the WHY? but either way, it is better with Windows first.

  1. Install Windows
  2. Go to Computer Management inside Administrative Tools
  3. Right click on your main volume which windows is installed on
  4. Select, Shrink Volume.

After the process calculates how much space it can reduce the volume by, you can select the size of the reduction and in turn giving the amount of space you will be left with on the new partition. 

Once complete, restart your computer.

NEXT:

  1. Insert your Ubuntu disk which you download and burned the ISO to disk
  2. Close any dialogue it raises for the Auto Run and then restart your machine again
  3. This should now boot into Ubuntu automatically assuming you allow booting from CD/DVD in the bios.
  4. Select the second option which is to install Ubuntu and then follow through the steps until you get to the partition manager.

The top line should inform you that it has found WIndows 7 already on your computer, and beneath that it gives you an option to install Ubuntu side by side to this.  This is the option you want and after selection continue and confirm all the changes to be written to disk.  Continue through the confirmation dialogues until completion.

On Restart

After the above is complete you get a really useful boot screen where you can select to boot Ubuntu or Windows.  I am well impressed with this and cannot believe I have not done this sooner. 

I am a total newcomer to the Linux scene, but so far so good.  One small thing which I would point out is the apparent lack of a desktop blog publishing tool with equal capabilities and extensibility as Windows Live Writer.  But hey, reboot, load windows and blog there… for now lol!!

Cheers for now,

Andrew


Monday, January 25, 2010 7:28:30 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer

Due to a home dev project I am currently involved in with another contributor, called byteface, it has lead me to using the Google AppEngine and specifically the Python flavour as opposed to the Java flavour.  I chose Python as I am enjoying further exposure to different programming languages.  The Django framework is nearly entirely supported on the Google AppEngine with some modules omitted due to either security considerations or conflict.  Both the DJango framework and the Google AppEngine have their own ORM, so this is one of the module omissions where you have to use the Google ORM, (wonder if they ever thought of doing GHibernate ;-)).

imageAnother point to make also is that you can choose to use the Google http handler functions or DJango’s.  I have gone with Django’s so I can get a complete experience of the framework and utilise all of the powerful utilities and features.

In this post I want to show how you can emulate the project structure of a default ASP.NET MVC 1.0 application, i.e. File –> New –> ASP.NET MVC Application inside Visual Studio.  The image on the right is an example of one freshly created, all I have done is remove any references to Account, as for the purposes of this example I just want to emulate the core things like structure and behaviour.

The following are considerations which I need to think about in order to emulate Microsoft’s ASP.NET implementation of MVC including:

  • Master page templates
    • i.e. Site.Master
  • Static content
  • Routing
  • Controller Actions
    • Including HTTP Verb restrictions
  • General layout

Off the top of my head one thing which will need to be an add in is the routing, as this is due to one of the rules/principles of python:

Explicit is better than implicit

So basically in the context of routing, we would have to define all of our routes as opposed to what we can do in ASP.NET MVC and have a route to match the {controller}/{action}/{id} url pattern.  From a little research I did, Ruby on Rails also has this out of the box.  So I am not sure if I will be able to manage this part of the project in this post, so when I find a robust way of doing it inside Django I will again post something about it.  It may be the case I stick with the explicit route and conform to the Zen of Python(>>> import this).

Google App Engine SDK Console

If you have not done already, you will need to download the SDK and create the application.  I am using the GUI for this, so download and create an application.  The folder structure should have been created for you.  In the case of this example I named my application narbley and in return in created the following folder structure inside my app engine source path:

  • narbley
    • narbley

So it created a folder with the same name inside this folder with the relevant application files for Google App Engine. Because I want to use the Django framework, I deleted the nested narbley folder as I will create this using the django-admin utility.

To create the base django app files I ran the following cmd from inside my application folder:

django-admin-py startproject narbley

What is narbley? Simply a code name for this project I am working on with a fellow contributor.  It is a codename for many reasons including, we are not 100% sure on what we are building yet, that and it is very difficult to find a name for a Google AppEngine … App which is not taken, so we tried to be abstract, and failing that we simply kept adding a letter to the end and trying it, hence narbley was created.

Also you will need to follow this short how to, to use the Django Framework completely over the Google one, http://code.google.com/appengine/articles/django.html

The Home Controller

From what I have seen, in Python and Django the views are actually a front controller.  I would not class the function which handles the request the view, in the context of DJango I would  regard the templates as the views, the controller as the front controller with the different actions on and the model, the model.

For the http verb restriction you find in the ASP.NET MVC 1.0 implementation, I did some searching about and actually came back to finding that one has already been written in DJango and its usage should very familiar to you.

from django.template import Context, loader
from django.http import HttpResponse
from django.views.decorators.http import require_http_methods
12
@require_http_methods(["GET"])
def index(request):
    t = loader.get_template('home/index.html')
    c = Context({
        'message': "Welcome to the Django Framework on the Google App Engine"
    })
    return HttpResponse(t.render(c))

@require_http_methods(["GET"])
def about(request):
    t = loader.get_template('home/about.html')
    c = Context({
        'message': "Narbley is the codename"
    })
    return HttpResponse(t.render(c))

Great so that is the index and the about page sorted.  One thing I will point out here is a point with regards to how python handles namespaces/file structure/packages or how ever you want to call the equivalent.  I have created a folder called controllers and inside I have a file called homecontroller.py.  I also need to add the file __init__.py .  Unfortunately as of this moment I cannot tell you why, only that I know it requires it, and subsequently I think for any other folder nesting.

Master/Content Page Templates

These are self explanatory and their implementation quite similar, with a simple change to the import and the syntax of course.  From my experience to date, the DJango templates require the .html extension whether they are content or master.  You need to add a reference to any folders which will be deemed as template folders inside the settings.py file, i.e.:

ROOT_PATH = os.path.dirname(__file__)
TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    ROOT_PATH + '/views',
)

Inline with the project files from a default ASP.NET MVC Project I have simply copied and changed the files to use the DJango templating syntax, notice how:

  • The content placeholders are defined
  • The master page is defined and referenced

The master page (views/shared/master.html)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>{% block title %}{% endblock %}</title>
    <link href="/content/site.css" rel="stylesheet" type="text/css" />
    {% block css_includes %}{% endblock %}
    {% block script_includes %}{% endblock %}
</head>

<body>
    <div class="page">

        <div id="header">
            <div id="title">
                <h1>My MVC Application</h1>
            </div>
            
            <div id="menucontainer">
            
                <ul id="menu">              
                    <li><a href="/home/">Home</a></li>
                    <li><a href="/home/about/">About</a></li>
                </ul>
            
            </div>
	<br style="clear:both"/>
        </div>

        <div id="main">
            {% block content %}{% endblock %}

            <div id="footer">
		{% block footer %}{% endblock %}
            </div>
        </div>
    </div>
</body>
</html>

The homepage (views/home/index.html)

I am only attaching the code for the homepage as the about page is very similar.

{% extends "shared/master.html" %}

{% block title %}DJango Example{% endblock %}
{% block css_includes %}{% endblock %}
{% block script_includes %}{% endblock %}


{% block content %}
<p>{{ message }}.</p>
{% endblock %}

{% block footer %}
Footer here
{% endblock %}

image Apart from that there is just the default application files required by Python/DJango and Google App Engine.  I am currently looking into:

  • Routing
  • Extension less Urls
  • JSON support libraries in Python
  • The Message Queue of the Google App Engine

The resulting project structure for the base project is shown on the right.  I opened the website inside Visual Studio to see the structure, not for editing lol.

I should have deleted it but I have not, the http_methods file is not used, I forgot to delete.

Cheers,

Andrew


Sunday, December 20, 2009 4:09:25 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer

Files

Please download the example solution for this post from the following url.

http://lab.andrewrea.co.uk/ResourcesExampleMvc.rar

Summary

Ok, I have been writing this one as I went, whilst thinking and deving so my opinion on this has changed from when I started writing this to what I ended up with and I have to say I am quite chuffed with the outcome as it yields some other possibilities which I want to now blog about, but to cut a long story/blog post short I have made a Http Handler which accepts some parameters so it can locate a resource file, enumerate through its properties and output them as JavaScript variables to the Response Output Stream. This allows me to expose resources the application is using to client script so I am not duplicating any of them and making maintenance and additions much easier.

I have also wrapped in a bit of token based security, although I do kind of ensure that what is attempted to be enumerated is a resource file by passing the type into a ResourceManager I have added the token based security regardless.  Another good option would be to encrypt the url like the common .axd resource handler does, but either way I have used a token based approach using HMAC and SHA1.

Example usage:

<%= Html.ClientResourceLink<ResourcesExampleMvc.Core.Resources.Global>("jsloc.axd","fr") %>

Example output:

<script type="text/javascript" src="/jsloc.axd?typeName=ResourcesExampleMvc.Core.Resources.Web.Controllers.Home.Home&culture=fr&token=2E935FA9EF23865A9A57B437EC9A7CCE62A7712B&timestamp=1260744495"></script> 

The handler at work:

var cache_date = "13 December 2009 23:57";
var GlobalString1 = "French Global String 1";

image

Finally I have made a HtmlHelper which will generate the relevant link / links I need.  So the blog that I started writing, dev’d and changed my mind…

here goes…

I have been looking at different examples on the web relating to client side localisation.  The two main points I see are:

  1. Add secondary resources inside JavaScript files (This leads to duplication of resources)
  2. Make a call to the Resource Manager inside delimiters inside the mark-up. (This is fine as long as you are not using separate JavaScript Files)

I might be wrong, but from what I can see on the Resource Manager, there is only a set way of getting access to a resource, and that is by its name, so if I want to get several related items, I need to know the keys of each.  As I am writing this I am now starting to think if what I originally wanted to do is as efficient as I first thought.

Basically I thought that inside each Resource file that is defined, each key could be prefixed with some kind of grouping information, e.g. HomeController , so I could have the following keys:

  • HomeController_WelcomeText
  • HomeController_Button1
  • HomeController_Button2

With this I then thought of making a way to query the resources, so I could apply a prefix and then get in return all matched keys.  I would do this by reflection and looping over each property, storing its name and value.

So to summarise I was stuck in the way of thinking one resource file and differentiating based on the string key.  DOH!, thinking about it now, I think the best and most clean solution is to use a separate resource file per each localisable entity i.e. a Form, or Controller etc…  If you think about it also, this will make things much more organised when you get to a point where you have thousands of resources.  Having them logically separated in line with the entity which will be localised makes good sense.  Not only that it also make the task I was thinking about much easier.

The Idea

If you think about ASP.NET MVC for example, and the following. You will always have resources which are common or global and then you will have resources which are specific to a certain part of the project, e.g. Home Controller.

I want to be able to output any localisation that I need so that I can consume with JavaScript without any unnecessary AJAX calls.  More so I only want to output the required keys from the global resources and the resources specific to the area which it is currently being executed, i.e. Home Controller.

The format which I am thinking for the output of the client side localisation is simply a included JavaScript file with the contents simply declared variables which match the name of those inside the resource files i.e.

var Global_Resource_Key_1 = "Hello World";
var Global_Resource_Key_2 = "Hello Galaxy";
var Local_Resource_Key_1 = "Local Number 1";
var Local_Resource_Key_2 = "Local Number 2";

Going back to what I said above, these will be output by a method using reflection to iterate through each of its properties to then generate the required output.  Once the iteration has complete it would be wise to store the resulting collection in Cache or Application object.  I am thinking that the generation of the script will be using a HttpHandler, allowing for the variables to be dynamic inside the mark-up and script  declaration.

Thinking about it more, this is exactly why we are given the special .NET folders of :

  • App_GlobalResources
  • App_LocalResources

These are great, but, I need to have the resource files inside another assembly so that they can be referenced both from the web and also internally from the calling assembly.  So inside a test project I have done the following to setup:

image

  • Create a MVC Application
    • Delete the Account View and Controllers
    • Move the Controllers, Global.asax.cs and Models to the Class Library project
  • Create a separate Class Library Project
    • Create a folder for resources
    • Create a Global Resources and also a Resource file per Controller with the relevant folder structure
    • Created a Configuration folder and class to handle the secret key for hmac’ing and cache timeout
    • Created an Extension Method folder and HtmlHelper class which will be a shortcut for the deveoper to use which will generate the link
    • Created a HttpHandlers folder and the actual handler which I will use to generate and cache the relevant properties

Also, I have omitted any DI/IOC for the purposes of this example. I will go through each of the Class Library project sections separately.

Create a folder for resources & Create a Global Resources and also a Resource file per Controller with the relevant folder structure

Having a separate folder in the resource means I can consume these from the web application but also from any models which are inside the assembly or business data for example where I may state the resource for validation attributes like those used in the DataAnnotations or the MVAB (Microsoft Validation Application Block).

I have but the global resource file directly inside this folder and then created sub folders to reflect different parts of the application which the one being in this instance, Web and then even further by controller.  I think grouping the resources by Controller for the web is a logical step, and along with a global resource file, you are pretty much covered.

image

When you build the solution, .NET will logically group your assemblies by culture, so have many resource files still means they will compile down into one assembly, which is great.

Created a Configuration folder and class to handle the secret key for hmac’ing and cache timeout

I could quite as easily have used the AppSettings but I thought that it would be good to give this attempt its own configuration section, which I could extend and keep encapsulated in the future.  The two things which I am using this section for at the moment is to store the key I will use to generate the HMAC hashes and also the sliding timeout for the cache of the resources.  The secret key can be anything you want, but I needed it in a centralised place so i can ensure the same one is used to generate and also compare.  There is not much to the Configuration Section accept a couple of required attributes and the syntax for retrieving the value declared in the config file.

<clientResourceConfiguration 
	resourceSlidingTimeout="20" 
	resourceHmacKey="470F0BE4675941baBEFBC1134CC1FEAF28C47C15D71543b8A9F57360CFFCD33B"/>

The secret key / resourceHmacKey here is simply two GUIDs stripped of the curlies and dashes and concatenated together.  To reference this configuration inside the code, I have placed a static property on the MvcApplication class inside the Global.asax.cs file.  Seemed like a logical place to put it, and of course made it a singleton.

    public class MvcApplication : System.Web.HttpApplication
    {
        private static ClientResourceConfiguration _clientResourceConfiguration = null;

        public static ClientResourceConfiguration ClientResourceConfiguration
        {
            get
            {
                if (_clientResourceConfiguration == null)
                {
                    _clientResourceConfiguration = (ClientResourceConfiguration)ConfigurationManager.GetSection("clientResourceConfiguration");
                }
                return _clientResourceConfiguration;
            }
        }

...

Created an Extension Method folder and HtmlHelper class which will be a shortcut for the developer to use which will generate the link

This is simply to make it easier for the developer to create the link.  You can simply add the type you want to parse, the name of the handler which is mapped in the config file and the culture.  In this implementation I have designed it to expect the two letter culture name and then go on to resolve the specific culture.  I know it would have been easier to just specify the specific culture but I dev’d this with a work related problem I had and wanted to simulate the environment in which I have to work with.

I have created an overloaded method so that if I need to I can force clear the cache for a specific handler.  As I will show you further down I also output the date it was cached, again simply for diagnostic purposes.

The token here is simply so i can be sure that the resource file which is being requested has been authorized by the server, as it is the server which is the only entity that has the secret key and can create such links.  I have included a timestamp which makes the HMAC hash different each time.  The validation of this token will only occur if the requested resource is not in the cache, as I make the assumption that is if it is in the cache, then it has to have been generated for a valid reason by the server.

Oh and there is a small method in there I found on Brad Abrams site which simply gives me back the number of seconds since 1970, which acts as a timestamp.

    public static class HtmlHelpers
    {
        public static string ClientResourceLink<T>(this HtmlHelper helper, string handler, string twoLetterCultureName)
        {
            return ClientResourceLink<T>(helper, handler, twoLetterCultureName, true);
        }

        public static string ClientResourceLink<T>(this HtmlHelper helper, string handler, string twoLetterCultureName, bool cache)
        {
            var typeName = typeof(T).FullName;
            var timeStamp = GetTimeStamp().ToString();
            var valueToHash = String.Concat(typeName, twoLetterCultureName, timeStamp);
            var token = CryptoHelper.Hmac(valueToHash, MvcApplication.ClientResourceConfiguration.ResourceHmacKey, HashType.SHA1);
            var url = String.Format("~/{0}?typeName={1}&culture={2}&token={3}&timestamp={4}",
                handler,
                typeName,
                twoLetterCultureName,
                token,
                timeStamp);
            if (!cache)
            {
                url += "&cache=ncache";
            }
            return String.Format("<script type=\"text/javascript\" src=\"{0}\"></script>",
                new UrlHelper(helper.ViewContext.RequestContext).Content(url));
        }

        /// <summary>
        /// From Brad Abrams : http://blogs.msdn.com/brada/archive/2004/03/20/93332.aspx
        /// </summary>
        /// <returns></returns>
        private static int GetTimeStamp()
        {
            TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
            int timestamp = (int)t.TotalSeconds;
            return timestamp;
        }
    }

The CryptoHelper class is one which “I made earlier,” and which I blogged about here http://www.andrewrea.co.uk/2009/10/05/ACryptographyHelperClassForHashingAndForKHMACKeyedHashMessageAuthenticationCode.aspx.  It is simply a helper method wrapping around some types and methods inside the System.Security.Cryptography namespace.  You will see some example usages in the summary above.

Created a HttpHandlers folder and the actual handler which I will use to generate and cache the relevant properties

This is basically the crooks of the solution and it is the handler.  I have used the .axd extension simply because it is already recognised and is ignored my the MVC route handler. 

Below is the entry I have used to configure the Http Handler for GET only and an example path to map it to

<add verb="GET" path="/jsloc.axd" validate="false" type="ResourcesExampleMvc.Core.HttpHandlers.ClientSideResourceHttpHandler,ResourcesExampleMvc.Core" />

It is in this class where I handle:

  • The parameters passed in
  • The validation of the token
  • The cache of the resources for the client
  • The generation of the resources

I simply set the Response.ContentType to text/javascript and then write out the information through a StreamWriter.  The first variable I add is the CacheDate and then followed by the resources themselves, as they appear inside the resource file.  A point to mention here is if you have defined any of the keys in the resource files with spaces in they will be replaced with underscores.

Major Point : You must set the scope of your Resource File, which ever one you want the Handler to parse as Public, this is due to me put Binding Flags on the reflection as Public and Static.  I suppose I could have added Internal, but not sure, so I will leave for now as Public.

    public class ClientSideResourceHttpHandler : IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string typeName = context.Request.QueryString["typeName"];
            string twoLetterCultureName = context.Request.QueryString["culture"];
            string token = context.Request.QueryString["token"];
            string timestamp = context.Request.QueryString["timestamp"];
            string nocache = context.Request.QueryString["nocache"];
            var timeout = MvcApplication.ClientResourceConfiguration.ResourceSlidingTimeout;

            string key = GetKey(typeName, twoLetterCultureName);

            if (context.Cache[key] == null || !String.IsNullOrEmpty(nocache))
            {
                var type = Type.GetType(typeName);
                var resourceManager = new ResourceManager(type);

                if (!ValidateToken(typeName, twoLetterCultureName, timestamp, token))
                    throw new SecurityException("Invalid token submitted for client resource");

                var list = new List<KeyValuePair<string, string>>();
                var properties = type.GetProperties(
                    System.Reflection.BindingFlags.Public |
                    System.Reflection.BindingFlags.Static);

                foreach (var property in properties)
                {
                    if (property.PropertyType == typeof(string))
                    {

                        list.Add(
                            new KeyValuePair<string, string>(property.Name,
                                resourceManager.GetString(property.Name.Replace("_", " "),
                                GetCulture(twoLetterCultureName))
                                )
                        );
                    }
                }

                context.Cache.Insert(key, list, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(timeout));
            }

            context.Response.ContentType = "text/javascript";
            WriteOutClientResources(context.Response.OutputStream, (List<KeyValuePair<string, string>>)context.Cache[key]);
            context.Response.End();
        }

        #endregion

        private void WriteOutClientResources(Stream outputStream, List<KeyValuePair<string, string>> values)
        {
            using (var sw = new StreamWriter(outputStream))
            {
                sw.WriteLine(String.Format("var cache_date = \"{0}\";", DateTime.Now.ToString("f")));
                foreach (var item in values)
                {
                    sw.WriteLine(String.Format("var {0} = \"{1}\";", item.Key, item.Value));
                }
                sw.Flush();
            }
        }

        private bool ValidateToken(string typeName, string twoLetterCultureName, string timestamp, string token)
        {
            var valueToHmac = String.Concat(typeName, twoLetterCultureName, timestamp);
            var valueToCompare = CryptoHelper.Hmac(valueToHmac, MvcApplication.ClientResourceConfiguration.ResourceHmacKey, HashType.SHA1);
            return valueToCompare.Equals(token);
        }

        private string GetKey(string controllerName, string twoLetterCultureName)
        {
            return String.Format("{0}!{1}", controllerName, twoLetterCultureName);
        }

        private CultureInfo GetCulture(string twoLetterCultureName)
        {
            switch (twoLetterCultureName)
            {
                case "fr":
                    return CultureInfo.CreateSpecificCulture("fr-FR");
                default:
                    return CultureInfo.CreateSpecificCulture("en-GB");
            }
        }
    }

If you did not want the overhead of a Handler, or you do not want the actual dynamic script reference, then there is nothing stopping you in cutting this write down, and making a HtmlHelper which simply parsing a type and outputs the JavaScript variables directly to the requesting resource, so instead of an include script tag, it would output a script block directly in the dom.  Personally I just like the script tag and the visual reduction in code in the view source, I am unsure of any performance gain if any. 

So that is basically it, this is something I am definitely going to test drive and among other things, use it for other purposes.  One idea I had was to use this to generate Client Side objects based on say the models. I will do this for the next post I hope.



.NET | C# | JavaScript
Sunday, December 13, 2009 11:53:19 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer