Tuesday, 21 February 2012

Serving the Mule Tools XSD using the Apache Rewrite Module and PHP

After I created the NamespaceHandler and XSD for the Integration Cocktail Mule Tools module, I wanted to make the XSD available at the URL http://www.integrationcocktail.org/schema/mule/tools/3.2/mule-tools.xsd so that users of the module will have code completion using any half decent XML editor. I bought the domain and was going to use a simple LAMP server to provide the XSD. It was straightforward enough if I wanted to simply put the XSD file, however I know myself, I will forget to update the XSD file after I will do any form of updates to the file. So why not provide the schema directly from the git repository? So, after years not touching any form of PHP I was going to hack a bit with it.

First I added a .htaccess file to rewrite all requests for .xsd files which do not exist to my php script:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} .*\.xsd
RewriteRule ^(.*)$ myproxy.php?url=$1
view raw gistfile1.txt hosted with ❤ by GitHub


After that all I had to do is create a simple script to proxy. I used PHP cURL library to do the requests and voila, simple enough:

<?php
$requestUrl = $_GET['url'];
switch($requestUrl){
case "schema/mule/tools/3.2/mule-tools.xsd":{
$targetUrl = "https://raw.github.com/stephenfenech/integrationcocktail-blog/master/mule/mule-tools/src/main/resources/META-INF/mule-tools.xsd";
break;
}
default : $targetUrl = "https://raw.github.com/stephenfenech/integrationcocktail-blog/master/mule/mule-tools/src/main/resources/META-INF/mule-tools.xsd";
}
$ch= curl_init($targetUrl);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xsd = curl_exec($ch);
header("Content-Type: text/xml");
echo $xsd;
curl_close($ch);
?>
view raw gistfile1.aw hosted with ❤ by GitHub


Anybody out there who is creating custom mule modules and as they should, to encourage use, creates a custom namespace, please, do make the XSD available for your fellow developers so that you do not have to stay adding it to the IDE.

No comments:

Post a Comment