Sunday 30 December 2012

Building your own mapagent http handler in .net: An example

So I quipped previously that MgHttpRequest/MgHttpResponse makes it not only possible, but dead simple to create your own mapagent http handler in .net/Java/PHP without needing Apache or IIS.

How simple is it actually? Here's a .net one I banged out in under an hour.

If you look at the VS project, it just contains one ashx handler that does all the grunt work


And that grunt work is exactly what I described in my previous post. To recap, the handler needs to:


  • Collect the required parameters from Request.Form (for POST) or Request.QueryString (for GET)
  • Check the existence of USERNAME, PASSWORD parameter (or extract from http authentication header) or the SESSION parameter. Return a http 401 if none of these parameters are found.
  • Create a MgHttpRequest, plug in these collected parameters
  • Execute the MgHttpRequest
  • Write out the necessary response headers based on information in the MgHttpResponse
  • Write out the MgHttpResponse contents
So how can we test that this works? Start the project in Visual Studio and connect to the VS generated URL of the ashx handler with Maestro of course!


And if you see a list of folders in your site explorer like you normally do with the official mapagent, you know that your handler is now properly processing your incoming http requests from Maestro



Extra pro-tip: You can append a .fiddler to the above host name and you can see the http traffic through fiddler

Note that this sample project only implements the mapagent portion of the Web Tier, so things like the standard AJAX viewer resource preview won't work because there isn't an AJAX viewer relative to that URL. But you can use the local preview feature of Maestro that goes through the mapagent http interface (if you wanted more confirmation that the custom http handler is working as expected)


So if you're wondering like I did 5 years ago, whether this stuff is possible: Yes it is indeed!

No comments: