Example #1
0
def query_exception_handler(request):
    if EXCEPTION_HANDLERS is None:
        _setup_handlers()

    try:
        decoder = get_decoder(request)
        handlers = sorted([
            handler() for handler in EXCEPTION_HANDLERS
            if handler_supports_service(handler, decoder.service)
        ],
                          key=lambda h: max(h.versions),
                          reverse=True)

        # try to get the correctly versioned exception handler
        if decoder.version:
            for handler in handlers:
                if decoder.version in handler.versions:
                    return handler
        else:
            # return the exception handler with the highest version,
            # if one is available
            return handlers[0]
    except:
        # swallow any exception here, because we *really* need a handler
        # to correctly show the exception.
        pass

    # last resort fallback is a plain OWS exception handler
    return OWS20ExceptionHandler()
Example #2
0
    def query_exception_handler(self, request):
        try:
            decoder = get_decoder(request)
            handlers = self.exception_handlers
            handlers = sorted(
                filter(
                    partial(handler_supports_service, service=decoder.service),
                    self.exception_handlers
                ),
                key=lambda h: max(h.versions), reverse=True
            )

            # try to get the correctly versioned exception handler
            if decoder.version:
                for handler in handlers:
                    if decoder.version in handler.versions:
                        return handler
            else:
                # return the exception handler with the highest version,
                # if one is available
                return handlers[0]
        except:
            # swallow any exception here, because we *really* need a handler
            # to correctly show the exception.
            pass

        # last resort fallback is a plain OWS exception handler
        return OWS20ExceptionHandler()
Example #3
0
    def query_service_handler(self, request):
        """ Tries to find the correct service handler
        """

        decoder = get_decoder(request)
        if request.method == "GET":
            handlers = self.get_service_handlers 
        elif request.method == "POST":
            handlers = self.post_service_handlers 
        else:
            handlers = self.service_handlers

        version = decoder.version
        if version is None:
            accepted_versions = decoder.acceptversions
            handlers = filter_handlers(
                handlers, decoder.service, accepted_versions, decoder.request
            )
            return self.version_negotiation(handlers, accepted_versions)

        # check that the service is supported
        handlers = filter(
            partial(handler_supports_service, service=decoder.service), handlers
        )
        if not handlers:
            raise ServiceNotSupportedException(decoder.service)

        # check that the required version is enabled
        handlers_ = filter(
            lambda h: decoder.version in h.versions, handlers
        )
        if not handlers_:
            # old style version negotiation shall always return capabilities
            if decoder.request == "GETCAPABILITIES":
                handlers = [sorted(
                    filter(
                        lambda h: decoder.request == h.request.upper(), handlers
                    ), key=lambda h: max(h.versions), reverse=True
                )[0]]
            else:
                raise VersionNotSupportedException(decoder.service, decoder.version)
        else:
            handlers = handlers_

        # check that the required operation is supported and sort by the highest
        # version supported in descending manner
        handlers = sorted(
            filter(
                lambda h: decoder.request == h.request.upper(), handlers
            ), key=lambda h: max(h.versions), reverse=True
        )

        if not handlers:
            operation = decoder.request
            raise OperationNotSupportedException(
                "Operation '%s' is not supported." % operation, operation
            )

        # return the handler with the highest version
        return handlers[0]
Example #4
0
    def query_exception_handler(self, request):
        try:
            decoder = get_decoder(request)
            handlers = self.exception_handlers
            handlers = sorted(filter(
                partial(handler_supports_service, service=decoder.service),
                self.exception_handlers),
                              key=lambda h: max(h.versions),
                              reverse=True)

            # try to get the correctly versioned exception handler
            if decoder.version:
                for handler in handlers:
                    if decoder.version in handler.versions:
                        return handler
            else:
                # return the exception handler with the highest version,
                # if one is available
                return handlers[0]
        except:
            # swallow any exception here, because we *really* need a handler
            # to correctly show the exception.
            pass

        # last resort fallback is a plain OWS exception handler
        return OWS20ExceptionHandler()
Example #5
0
    def _decide(self, request):

        decoder = get_decoder(request)
        userAttributes = self._getAssertedAttributes(request)
        resourceAttributes = self._getResourceAttributes(request)

        result = self.client.authorize(userAttributes, resourceAttributes,
                                       decoder.request.lower())
        return result
Example #6
0
    def _decide(self, request):

        decoder = get_decoder(request)
        userAttributes     = self._getAssertedAttributes(request)
        resourceAttributes = self._getResourceAttributes(request)

        result = self.client.authorize(
            userAttributes, resourceAttributes, decoder.request.lower()
        )
        return result
Example #7
0
    def _getResourceAttributes(self, request):
        httpHeader = request.META
        attributes = {}

        if self.serviceID == 'default' :
            attributes[attrib_resource] = httpHeader['SERVER_NAME']
        else :
            attributes[attrib_resource] = self.serviceID

        decoder = get_decoder(request)
        attributes['serviceType'] = decoder.service.lower()
        attributes['serverName'] = httpHeader['SERVER_NAME']

        return attributes
Example #8
0
    def _getResourceAttributes(self, request):
        httpHeader = request.META
        attributes = {}

        if self.serviceID == 'default':
            attributes[attrib_resource] = httpHeader['SERVER_NAME']
        else:
            attributes[attrib_resource] = self.serviceID

        decoder = get_decoder(request)
        attributes['serviceType'] = decoder.service.lower()
        attributes['serverName'] = httpHeader['SERVER_NAME']

        return attributes
Example #9
0
    def query_service_handler(self, request):
        """ Tries to find the correct service handler for a given request. The
        request ``method`` can either be "POST" (in which case the request body
        is parsed as XML) or "GET" (in which case the request is parsed
        as "KVP").

        If necessary a version negotiation is conducted, following OWS
        guidelines.

        :param request: a :class:`Django HttpRequest <django.http.HttpRequest>`
                        object
        :returns: the request handler component for the given request
        :raises ServiceNotSupportedException: if the service is not supported
                                              by any component
        :raises VersionNotSupportedException: if the specified version is not
                                              supported
        :raises OperationNotSupportedException: if the specified request
                                                operation is not supported
        """

        decoder = get_decoder(request)


        if request.method == "GET":
            handlers = self.get_service_handlers
        elif request.method == "POST":
            handlers = self.post_service_handlers
        elif request.method == "OPTIONS":
            return OptionsRequestHandler()
        else:
            raise HTTPMethodNotAllowedError(
                "The %s HTTP method is not allowed!" % request.method,
                ALLOWED_HTTP_METHODS
            )
            #handlers = self.service_handlers

        version = decoder.version
        if version is None:
            accepted_versions = decoder.acceptversions
            handlers = filter_handlers(
                handlers, decoder.service, accepted_versions, decoder.request
            )
            return self.version_negotiation(handlers, accepted_versions)

        # check that the service is supported
        handlers = filter(
            partial(handler_supports_service, service=decoder.service), handlers
        )
        if not handlers:
            raise ServiceNotSupportedException(decoder.service)

        # check that the required version is enabled
        handlers_ = filter(
            lambda h: decoder.version in h.versions, handlers
        )
        if not handlers_:
            # old style version negotiation shall always return capabilities
            if decoder.request == "GETCAPABILITIES":
                handlers = [sorted(
                    filter(
                        lambda h: decoder.request == h.request.upper(), handlers
                    ), key=lambda h: max(h.versions), reverse=True
                )[0]]
            else:
                raise VersionNotSupportedException(
                    decoder.service, decoder.version
                )
        else:
            handlers = handlers_

        # check that the required operation is supported and sort by the highest
        # version supported in descending manner
        handlers = sorted(
            filter(
                lambda h: decoder.request == h.request.upper(), handlers
            ), key=lambda h: max(h.versions), reverse=True
        )

        if not handlers:
            operation = decoder.request
            raise OperationNotSupportedException(
                "Operation '%s' is not supported." % operation, operation
            )

        # return the handler with the highest version
        logger.debug("Handling '%s' request for '%s' service version '%s'." %
                     (handlers[0].request, handlers[0].service,
                      handlers[0].versions[0]))
        return handlers[0]
Example #10
0
def query_service_handler(request):
    """ Tries to find the correct service handler for a given request. The
    request ``method`` can either be "POST" (in which case the request body
    is parsed as XML) or "GET" (in which case the request is parsed
    as "KVP").

    If necessary a version negotiation is conducted, following OWS
    guidelines.

    :param request: a :class:`Django HttpRequest <django.http.HttpRequest>`
                    object
    :returns: the request handler component for the given request
    :raises ServiceNotSupportedException: if the service is not supported
                                          by any component
    :raises VersionNotSupportedException: if the specified version is not
                                          supported
    :raises OperationNotSupportedException: if the specified request
                                            operation is not supported
    """
    if SERVICE_HANDLERS is None:
        _setup_handlers()

    decoder = get_decoder(request)

    if request.method == "GET":
        handlers = GET_SERVICE_HANDLERS
    elif request.method == "POST":
        handlers = POST_SERVICE_HANDLERS
    elif request.method == "OPTIONS":
        return OptionsRequestHandler()
    else:
        raise HTTPMethodNotAllowedError(
            "The %s HTTP method is not allowed!" % request.method,
            ALLOWED_HTTP_METHODS)

    version = decoder.version
    if version is None:
        accepted_versions = decoder.acceptversions
        handlers = filter_handlers(handlers, decoder.service,
                                   accepted_versions, decoder.request)
        return version_negotiation(handlers, accepted_versions)()

    # check that the service is supported
    handlers = [
        handler for handler in handlers
        if handler_supports_service(handler, decoder.service)
    ]
    if not handlers:
        raise ServiceNotSupportedException(decoder.service)

    # check that the required version is enabled
    handlers_ = [
        handler for handler in handlers if decoder.version in handler.versions
    ]

    if not handlers_:
        # old style version negotiation shall always return capabilities
        if decoder.request == "GETCAPABILITIES":
            handlers = [
                sorted(filter(lambda h: decoder.request == h.request.upper(),
                              handlers),
                       key=lambda h: max(h.versions),
                       reverse=True)[0]
            ]
        else:
            raise VersionNotSupportedException(decoder.service,
                                               decoder.version)
    else:
        handlers = handlers_

    # check that the required operation is supported and sort by the highest
    # version supported in descending manner
    handlers = sorted(filter(lambda h: decoder.request == h.request.upper(),
                             handlers),
                      key=lambda h: max(h.versions),
                      reverse=True)

    if not handlers:
        operation = decoder.request
        raise OperationNotSupportedException(
            "Operation '%s' is not supported." % operation, operation)

    # return the handler with the highest version
    logger.debug(
        "Handling '%s' request for '%s' service version '%s'." %
        (handlers[0].request, handlers[0].service, handlers[0].versions[0]))
    return handlers[0]()