rest_dbus: add plug-in to check content-type

Add a plug-in which ensures a route's content-type header matches the
expected type for that route.

Change-Id: I3376a0e35aae22747e90d7c46680639dbfc1404d
Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
diff --git a/module/obmc/wsgi/apps/rest_dbus.py b/module/obmc/wsgi/apps/rest_dbus.py
index 47dacaa..7ef4afd 100644
--- a/module/obmc/wsgi/apps/rest_dbus.py
+++ b/module/obmc/wsgi/apps/rest_dbus.py
@@ -755,6 +755,33 @@
         response_body['body'] = self.to_jsonp(response_body['body'])
 
 
+class ContentCheckerPlugin(object):
+    ''' Ensures that a route is associated with the expected content-type
+        header. '''
+    name = 'content_checker'
+    api = 2
+
+    class Checker:
+        def __init__(self, type, callback):
+            self.expected_type = type
+            self.callback = callback
+            self.error_str = "Expecting content type '%s', got '%s'"
+
+        def __call__(self, *a, **kw):
+            if self.expected_type and \
+                    self.expected_type != request.content_type:
+                abort(415, self.error_str % (self.expected_type,
+                      request.content_type))
+
+            return self.callback(*a, **kw)
+
+    def apply(self, callback, route):
+        content_type = getattr(
+            route.get_undecorated_callback(), '_content_type', None)
+
+        return self.Checker(content_type, callback)
+
+
 class App(Bottle):
     def __init__(self):
         super(App, self).__init__(autojson=False)
@@ -772,6 +799,7 @@
         json_kw = {'indent': 2, 'sort_keys': True}
         self.install(AuthorizationPlugin())
         self.install(CorsPlugin(self))
+        self.install(ContentCheckerPlugin())
         self.install(JsonpPlugin(self, **json_kw))
         self.install(JsonErrorsPlugin(self, **json_kw))
         self.install(JsonApiResponsePlugin(self))