class SingleEntityDispatcher(object):
    """
    Dispatches requests concerning a single entity

    """

    def __init__(self, req, location, idontknow=None, idontcare=None):

        self.req = req

        self.location = location
        self.idontknow = idontknow
        self.idontcare = idontcare

        self.path_url = self.req.path_url
        self.triggered_action = None

        self.res = Response()
        self.res.content_type = str(req.accept)
        self.res.server = 'ocni-server/1.1 (linux) OCNI/1.1'

        self.req_adapter = RequestAdapter()
        self.res_adapter = ResponseAdapter()
        self.jungler = SingleEntityJungler()


    def put(self):
        """
        Create a new entity instance with a customized URL or perform a full update of the resource

        """
        #Step[1]: Detect the data type (HTTP ,JSON:OCCI or OCCI+JSON)

        jBody = self.req_adapter.convert_request_entity_content(self.req)

        if jBody is None:
            self.res.status_int = return_code['Not Acceptable']
            self.res.body = self.req.content_type + " is an unknown request content type"

        else:
            #Step[2]: create the resource with custom URL
            var, self.res.status_int = self.jungler.channel_put_single_resource(jBody, self.path_url)

            #Step[3]: Adapt the response to the required accept-type

            if self.res.status_int == return_code['OK, and location returned']:
                self.res = self.res_adapter.convert_response_entity_location_content(var, self.res)
            else:
                self.res.content_type = "text/html"
                self.res.body = var

        #Step[4]: Send back the response to the caller

        return self.res

    def get(self):
        """
        Retrieve the OCCI resource description

        """

        #Step[1]: get the resource description

        var, self.res.status_int = self.jungler.channel_get_single_resource(self.path_url)

        #Step[2]: Adapt the response to the required accept-type

        if self.res.status_int == return_code['OK']:
            self.res = self.res_adapter.convert_response_entity_content(self.res, var)

        else:
            self.res.content_type = "text/html"
            self.res.body = var

        #Step[3]: Send back the response to the caller

        return self.res

    def post(self):
        """
        Perform a partial update of a resource OCCI description or Trigger an action on a resource

        """
        #Step[1]: Identify the action name (if there is)

        if self.req.params.has_key('action'):

            self.triggered_action = self.req.params['action']

        #Step[2]: Detect the data type (HTTP ,OCCI:JSON or OCCI+JSON)

        #jBody = self.req_adapter.convert_request_entity_content(self.req)
        jBody = self.req_adapter.convert_request_entity_content_v2(self.req)

        if jBody is None:
            self.res.status_int = return_code['Not Acceptable']
            self.res.body = self.req.content_type + " is an unknown request content type"

        else:
            #Step[3a]: Partially update the resource if there was no action defined.

            if self.triggered_action is None:

                var, self.res.status_int = self.jungler.channel_post_single_resource(jBody, self.path_url)

                if self.res.status_int == return_code['OK, and location returned']:

                    #Step[4a]: Adapt the response to the required accept-type
                    self.res = self.res_adapter.convert_response_entity_location_content(var, self.res)
                else:
                    self.res.content_type = "text/html"
                    self.res.body = var

            else:
                # Step[3b]: Trigger an action on a resource

                self.res.body, self.res.status_int = self.jungler.channel_triggered_action_single(jBody, self.path_url,
                    self.triggered_action)

        return self.res


    def delete(self):
        """
        Delete a resource instance

        """

        #Step[1]: Delete a single resource

        self.res.body, self.res.status_int = self.jungler.channel_delete_single_resource(self.path_url)

        #Step[2]: return the response back to the caller

        return self.res
class MultiEntityDispatcher(object):
    """
    Dispatches requests concerning multiple entities

    """
    def __init__(self, req, location=None, idontknow=None, idontcare=None):

        self.req = req

        self.location = location

        self.idontcare = idontcare
        self.idontknow = idontknow

        self.triggered_action = None
        self.path_url = self.req.path_url

        self.res = Response()
        self.res.content_type = str(req.accept)
        self.res.server = 'ocni-server/1.1 (linux) OCNI/1.1'

        self.req_adapter = RequestAdapter()
        self.res_adapter = ResponseAdapter()
        self.jungler = MultiEntityJungler()
        self.jungler_p = PathManager()

    def post(self):
        """
        Create a new entity or trigger an action on all resources belonging to a kind or attach a mixin to a
        resource instance

        """

        #Step[1]: Detect the data type (HTTP ,OCCI:JSON or OCCI+JSON)

        jBody = self.req_adapter.convert_request_entity_content(self.req)

        if jBody is None:
            self.res.status_int = return_code['Not Acceptable']
            self.res.body = self.req.content_type + " is an unknown request content type"

        else:
            #Step[2]: Identify the action name if there is one:

            if self.req.params.has_key('action'):
                self.triggered_action = self.req.params['action']

            #Step[3a]: Dispatch a post request

            if self.triggered_action is None:

                var, self.res.status_int = self.jungler.channel_post_multi_resources(
                    jBody, self.path_url)

                #Step[4a]: Adapt response to the required Accept-Type

                if self.res.status_int == return_code[
                        'OK, and location returned']:
                    self.res_adapter.convert_response_entity_multi_location_content(
                        var, self.res)

                else:
                    self.res.content_type = "text/html"
                    self.res.body = str(var)

            #Step[3b]: Trigger an action on all resources belonging to a kind
            else:
                self.res.body, self.res.status_int = self.jungler.channel_trigger_actions(
                    jBody, self.path_url, self.triggered_action)

            return self.res

    def get(self):
        """
        Get entities belonging to a kind or a mixin

        """

        #Step[1]: Detect the body type (HTTP ,OCCI:JSON or OCCI+JSON)

        if self.req.content_type == 'text/occi' or (self.req.body != ""):

            jBody = self.req_adapter.convert_request_entity_content_v2(
                self.req)

            if jBody is None:
                self.res.status_int = return_code['Not Acceptable']
                self.res.body = self.req.content_type + " is an unknown request content type"

            else:
                #Step[2a]: Retrieve entities matching the filter provided
                var, self.res.status_int = self.jungler.channel_get_filtered_entities(
                    self.path_url, jBody)

        else:
            #Step[2b]: Retrieve all the entities
            var, self.res.status_int = self.jungler.channel_get_all_entities(
                self.path_url, "")

        #Step[3]: Adapt the response to the format defined in the Accept-Type header

        if self.res.status_int == return_code['OK']:

            self.res_adapter.convert_response_entity_multi_x_occi_location_content(
                var, self.res)

        else:
            self.res.content_type = "text/html"
            self.res.body = str(var)

        return self.res

    def put(self):
        """
        Fully update the mixin collection of entities

        """

        #Step[1]: Detect the body type (HTTP ,OCCI:JSON or OCCI+JSON)

        jBody = self.req_adapter.convert_request_category_content(self.req)

        if jBody is None:
            self.res.status_int = return_code['Not Acceptable']
            self.res.body = self.req.content_type + " is an unknown request content type"
        else:

            #Step[2]: Fully update the mixin collection of entities

            self.res.body, self.res.status_int = self.jungler.channel_put_multi(
                jBody, self.path_url)

        return self.res

    def delete(self):
        """
        Dissociates a resource instance from a mixin or delete all resource under a path

        """

        #Step[1]: Detect the body type (HTTP ,OCCI:JSON or OCCI+JSON)
        if self.req.content_type == 'text/occi' or (self.req.body != ""):

            jBody = self.req_adapter.convert_request_category_content(self.req)

            if jBody is None:
                self.res.status_int = return_code['Not Acceptable']
                self.res.body = self.req.content_type + " is an unknown request content type"

                #Step[2a]: This is a dissociate mixin request
                self.res.body, self.res.status_int = self.jungler.channel_delete_multi(
                    jBody, self.path_url)
        else:
            #Step[2b]: This is a delete on path request:
            self.jungler_p.channel_delete_on_path(self.path_url)

        return self.res
class SingleEntityDispatcher(object):
    """
    Dispatches requests concerning a single entity

    """
    def __init__(self, req, location, idontknow=None, idontcare=None):

        self.req = req

        self.location = location
        self.idontknow = idontknow
        self.idontcare = idontcare

        self.path_url = self.req.path_url
        self.triggered_action = None

        self.res = Response()
        self.res.content_type = str(req.accept)
        self.res.server = 'ocni-server/1.1 (linux) OCNI/1.1'

        self.req_adapter = RequestAdapter()
        self.res_adapter = ResponseAdapter()
        self.jungler = SingleEntityJungler()

    def put(self):
        """
        Create a new entity instance with a customized URL or perform a full update of the resource

        """
        #Step[1]: Detect the data type (HTTP ,JSON:OCCI or OCCI+JSON)

        jBody = self.req_adapter.convert_request_entity_content(self.req)

        if jBody is None:
            self.res.status_int = return_code['Not Acceptable']
            self.res.body = self.req.content_type + " is an unknown request content type"

        else:
            #Step[2]: create the resource with custom URL
            var, self.res.status_int = self.jungler.channel_put_single_resource(
                jBody, self.path_url)

            #Step[3]: Adapt the response to the required accept-type

            if self.res.status_int == return_code['OK, and location returned']:
                self.res = self.res_adapter.convert_response_entity_location_content(
                    var, self.res)
            else:
                self.res.content_type = "text/html"
                self.res.body = var

        #Step[4]: Send back the response to the caller

        return self.res

    def get(self):
        """
        Retrieve the OCCI resource description

        """

        #Step[1]: get the resource description

        var, self.res.status_int = self.jungler.channel_get_single_resource(
            self.path_url)

        #Step[2]: Adapt the response to the required accept-type

        if self.res.status_int == return_code['OK']:
            self.res = self.res_adapter.convert_response_entity_content(
                self.res, var)

        else:
            self.res.content_type = "text/html"
            self.res.body = var

        #Step[3]: Send back the response to the caller

        return self.res

    def post(self):
        """
        Perform a partial update of a resource OCCI description or Trigger an action on a resource

        """
        #Step[1]: Identify the action name (if there is)

        if self.req.params.has_key('action'):

            self.triggered_action = self.req.params['action']

        #Step[2]: Detect the data type (HTTP ,OCCI:JSON or OCCI+JSON)

        jBody = self.req_adapter.convert_request_entity_content_v2(self.req)

        if jBody is None:
            self.res.status_int = return_code['Not Acceptable']
            self.res.body = self.req.content_type + " is an unknown request content type"

        else:
            #Step[3a]: Partially update the resource if there was no action defined.

            if self.triggered_action is None:

                var, self.res.status_int = self.jungler.channel_post_single_resource(
                    jBody, self.path_url)

                if self.res.status_int == return_code[
                        'OK, and location returned']:

                    #Step[4a]: Adapt the response to the required accept-type
                    self.res = self.res_adapter.convert_response_entity_location_content(
                        var, self.res)
                else:
                    self.res.content_type = "text/html"
                    self.res.body = var

            else:
                # Step[3b]: Trigger an action on a resource

                self.res.body, self.res.status_int = self.jungler.channel_triggered_action_single(
                    jBody, self.path_url, self.triggered_action)

        return self.res

    def delete(self):
        """
        Delete a resource instance

        """

        #Step[1]: Delete a single resource

        self.res.body, self.res.status_int = self.jungler.channel_delete_single_resource(
            self.path_url)

        #Step[2]: return the response back to the caller

        return self.res
class SingleEntityDispatcher(object):
    """


    """

    def __init__(self, req, location, idontknow=None, idontcare=None):
        self.req = req

        self.location = location
        self.idontknow = idontknow
        self.idontcare = idontcare

        self.path_url = self.req.path_url
        self.triggered_action = None

        self.res = Response()
        self.res.content_type = str(req.accept)
        self.res.server = 'ocni-server/1.1 (linux) OCNI/1.1'

        self.req_adapter = RequestAdapter()
        self.res_adapter = ResponseAdapter()
        self.jungler = SingleEntityJungler()


    def put(self):
        """
        Create a new entity instance with a customized URL or perform a full update of the resource
        """
        #Step[1]: Detect the body type (HTTP ,JSON:OCCI or OCCI+JSON)

        jBody = self.req_adapter.convert_request_entity_content(self.req)

        if jBody is None:
            self.res.status_code = return_code['Not Acceptable']
            self.res.body = self.req.content_type + " is an unknown request content type"

        else:
            #Step[2]: Treat the request
            var, self.res.status_code = self.jungler.channel_put_single_resource(jBody, self.path_url)

            #Step[3]: Adapt the response to the required accept-type

            if self.res.status_code == return_code['OK, and location returned']:
                self.res = self.res_adapter.convert_response_entity_location_content(var, self.res)
            else:
                self.res.content_type = "text/html"
                self.res.body = var

        return self.res

    def get(self):
        """
        Retrieve the representation of a resource
        """
        #add the JSON to database along with other attributes

        var, self.res.status_code = self.jungler.channel_get_single_resource(self.path_url)

        if self.res.status_code == return_code['OK']:
            self.res = self.res_adapter.convert_response_entity_content(self.res, var)

        else:
            self.res.content_type = "text/html"
            self.res.body = var

        return self.res

    def post(self):
        """
        Perform a partial update of a resource
        """
        if self.req.params.has_key('action'):
            self.triggered_action = self.req.params['action']

        #Detect the body type (HTTP ,OCCI:JSON or OCCI+JSON)

        jBody = self.req_adapter.convert_request_entity_content_v2(self.req)

        if jBody is None:
            self.res.status_code = return_code['Not Acceptable']
            self.res.body = self.req.content_type + " is an unknown request content type"

        else:
            #add the JSON to database along with other attributes
            if self.triggered_action is None:
                var, self.res.status_code = self.jungler.channel_post_single_resource(jBody, self.path_url)

                if self.res.status_code == return_code['OK, and location returned']:
                    self.res = self.res_adapter.convert_response_entity_location_content(var, self.res)
                else:
                    self.res.content_type = "text/html"
                    self.res.body = var

            else:
                self.res.body, self.res.status_code = self.jungler.channel_triggered_action_single(jBody, self.path_url,
                    self.triggered_action)

        return self.res


    def delete(self):
        """
        Delete a resource instance

        """

        #Detect the body type (HTTP ,OCCI:JSON or OCCI+JSON)


        #add the JSON to database along with other attributes
        self.res.body, self.res.status_code = self.jungler.channel_delete_single_resource(self.path_url)

        return self.res
class MultiEntityDispatcher(object):
    """


    """

    def __init__(self, req, location=None, idontknow=None, idontcare=None):
        self.req = req

        self.location = location

        self.idontcare = idontcare
        self.idontknow = idontknow

        self.triggered_action = None
        self.path_url = self.req.path_url

        self.res = Response()
        self.res.content_type = str(req.accept)
        self.res.server = 'ocni-server/1.1 (linux) OCNI/1.1'

        self.req_adapter = RequestAdapter()
        self.res_adapter = ResponseAdapter()
        self.jungler = MultiEntityJungler()
        self.jungler_p = PathManager()


    def post(self):
        """
        Create a new entity instance or trigger an action on all resources belonging to a kind or attach a mixin to a
        resource instance

        """

        #Step[1]: Detect the body type (HTTP ,OCCI:JSON or OCCI+JSON)


        jBody = self.req_adapter.convert_request_entity_content(self.req)

        if jBody is None:
            self.res.status_code = return_code['Not Acceptable']
            self.res.body = self.req.content_type + " is an unknown request content type"

        else:
            #Step[2]: Treat the data and define what the request really wants:

            if self.req.params.has_key('action'):
                self.triggered_action = self.req.params['action']

            #add the JSON to database along with other attributes
            if self.triggered_action is None:
                var, self.res.status_code = self.jungler.channel_post_multi_resources(jBody, self.path_url)

                if self.res.status_code == return_code['OK, and location returned']:
                    self.res_adapter.convert_response_entity_multi_location_content(var, self.res)

                else:
                    self.res.content_type = "text/html"
                    self.res.body = str(var)

            else:
                self.res.body, self.res.status_code = self.jungler.channel_trigger_actions(jBody, self.path_url,
                    self.triggered_action)

            return self.res

    def get(self):
        """
        Gets entities belonging to a kind or a mixin

        """

        #Detect the body type (HTTP ,OCCI:JSON or OCCI+JSON)

        if  self.req.content_type == 'text/occi' or (
        self.req.body != ""):
            jBody = self.req_adapter.convert_request_entity_content_v2(self.req)

            if jBody is None:
                self.res.status_code = return_code['Not Acceptable']
                self.res.body = self.req.content_type + " is an unknown request content type"

            else:
                var, self.res.status_code = self.jungler.channel_get_filtered_entities(self.path_url, jBody)

        else:
            var, self.res.status_code = self.jungler.channel_get_all_entities(self.path_url, "")

        if self.res.status_code == return_code['OK']:
            self.res_adapter.convert_response_entity_multi_x_occi_location_content(var, self.res)

        else:
            self.res.content_type = "text/html"
            self.res.body = str(var)

        return self.res

    def put(self):
        """
        Update the mixin collection of entities

        """

        #Detect the body type (HTTP ,OCCI:JSON or OCCI+JSON)
        #Not implemented yet for text/plain and text/occi formats


        jBody = self.req_adapter.convert_request_category_content(self.req)

        if jBody is None:
            self.res.status_code = return_code['Not Acceptable']
            self.res.body = self.req.content_type + " is an unknown request content type"
        else:
            #add the JSON to database along with other attributes
            self.res.body, self.res.status_code = self.jungler.channel_put_multi(jBody, self.path_url)

        return self.res

    def delete(self):
        """
        Dissociates a resource instance from a mixin

        """

        #Detect the body type (HTTP ,OCCI:JSON or OCCI+JSON)
        #Not implemented yet for text/plain and text/occi formats

        jBody = self.req_adapter.convert_request_category_content(self.req)

        if jBody is None:
            self.res.status_code = return_code['Not Acceptable']
            self.res.body = self.req.content_type + " is an unknown request content type"
        else:
            if self.req.body is not "":
                #This is a dissociate mixin request:
                self.res.body, self.res.status_code = self.jungler.channel_delete_multi(jBody, self.path_url)
            else:
                #This is a delete on path request:
                self.jungler_p.channel_delete_on_path(self.path_url)

        return self.res
class MultiEntityDispatcher(object):
    """
    Dispatches requests concerning multiple entities

    """

    def __init__(self, req, location=None, idontknow=None, idontcare=None):

        self.req = req

        self.location = location

        self.idontcare = idontcare
        self.idontknow = idontknow

        self.triggered_action = None
        self.path_url = self.req.path_url

        self.res = Response()
        self.res.content_type = str(req.accept)
        self.res.server = 'ocni-server/1.1 (linux) OCNI/1.1'

        self.req_adapter = RequestAdapter()
        self.res_adapter = ResponseAdapter()
        self.jungler = MultiEntityJungler()
        self.jungler_p = PathManager()


    def post(self):
        """
        Create a new entity or trigger an action on all resources belonging to a kind or attach a mixin to a
        resource instance

        """

        #Step[1]: Detect the data type (HTTP ,OCCI:JSON or OCCI+JSON)


        jBody = self.req_adapter.convert_request_entity_content(self.req)

        if jBody is None:
            self.res.status_int = return_code['Not Acceptable']
            self.res.body = self.req.content_type + " is an unknown request content type"

        else:
            #Step[2]: Identify the action name if there is one:

            if self.req.params.has_key('action'):
                self.triggered_action = self.req.params['action']

            #Step[3a]: Dispatch a post request

            if self.triggered_action is None:

                var, self.res.status_int = self.jungler.channel_post_multi_resources(jBody, self.path_url)

                #Step[4a]: Adapt response to the required Accept-Type

                if self.res.status_int == return_code['OK, and location returned']:
                    self.res_adapter.convert_response_entity_multi_location_content(var, self.res)

                else:
                    self.res.content_type = "text/html"
                    self.res.body = str(var)

            #Step[3b]: Trigger an action on all resources belonging to a kind
            else:
                self.res.body, self.res.status_int = self.jungler.channel_trigger_actions(jBody, self.path_url,
                    self.triggered_action)

            return self.res

    def get(self):
        """
        Get entities belonging to a kind or a mixin

        """

        #Step[1]: Detect the body type (HTTP ,OCCI:JSON or OCCI+JSON)

        if  self.req.content_type == 'text/occi' or (
        self.req.body != ""):

            jBody = self.req_adapter.convert_request_entity_content_v2(self.req)

            if jBody is None:
                self.res.status_int = return_code['Not Acceptable']
                self.res.body = self.req.content_type + " is an unknown request content type"

            else:
                #Step[2a]: Retrieve entities matching the filter provided
                var, self.res.status_int = self.jungler.channel_get_filtered_entities(self.path_url, jBody)

        else:
            #Step[2b]: Retrieve all the entities
            var, self.res.status_int = self.jungler.channel_get_all_entities(self.path_url, "")

        #Step[3]: Adapt the response to the format defined in the Accept-Type header

        if self.res.status_int == return_code['OK']:

            self.res_adapter.convert_response_entity_multi_x_occi_location_content(var, self.res)

        else:
            self.res.content_type = "text/html"
            self.res.body = str(var)

        return self.res

    def put(self):
        """
        Fully update the mixin collection of entities

        """

        #Step[1]: Detect the body type (HTTP ,OCCI:JSON or OCCI+JSON)

        jBody = self.req_adapter.convert_request_category_content(self.req)

        if jBody is None:
            self.res.status_int = return_code['Not Acceptable']
            self.res.body = self.req.content_type + " is an unknown request content type"
        else:

            #Step[2]: Fully update the mixin collection of entities

            self.res.body, self.res.status_int = self.jungler.channel_put_multi(jBody, self.path_url)

        return self.res

    def delete(self):
        """
        Dissociates a resource instance from a mixin or delete all resource under a path

        """

        #Step[1]: Detect the body type (HTTP ,OCCI:JSON or OCCI+JSON)
        if  self.req.content_type == 'text/occi' or (
            self.req.body != ""):

            jBody = self.req_adapter.convert_request_category_content(self.req)

            if jBody is None:
                self.res.status_int = return_code['Not Acceptable']
                self.res.body = self.req.content_type + " is an unknown request content type"

                #Step[2a]: This is a dissociate mixin request
                self.res.body, self.res.status_int = self.jungler.channel_delete_multi(jBody, self.path_url)
        else:
                #Step[2b]: This is a delete on path request:
                self.jungler_p.channel_delete_on_path(self.path_url)

        return self.res