예제 #1
0
def entry(entry_id):
    """
    GET a representation of the container in the appropriate (content negotiated) format as identified by
    the supplied id
    Args:
    - entry_id:   The ID of the container as supplied in the request URL
    Returns a representation of the container: SSS will return either the Atom Entry identical to the one supplied
    as a deposit receipt or the pure RDF/XML Statement depending on the Accept header
    """
    # authenticate
    try:
        auth = basic_auth()
        ss = SwordServer(config, auth)

        # first thing we need to do is check that there is an object to return, because otherwise we may throw a
        # 415 Unsupported Media Type without looking first to see if there is even any media to content negotiate for
        # which would be weird from a client perspective
        if not ss.container_exists(entry_id):
            abort(404)

        # get the content negotiation headers
        accept_header = request.headers.get("Accept")
        accept_packaging_header = request.headers.get("Accept-Packaging")

        # do the negotiation
        default_accept_parameters, acceptable = config.get_container_formats()
        cn = ContentNegotiator(default_accept_parameters, acceptable)
        accept_parameters = cn.negotiate(accept=accept_header)
        app.logger.info("Container requested in format: " +
                        str(accept_parameters))

        # did we successfully negotiate a content type?
        if accept_parameters is None:
            raise SwordError(error_uri=Errors.content, status=415, empty=True)

        # now actually get hold of the representation of the container and send it to the client
        cont = ss.get_container(entry_id, accept_parameters)

        resp = make_response(cont)
        if cont is not None:
            resp.mimetype = accept_parameters.content_type.mimetype()

        return resp

    except SwordError as e:
        return raise_error(e)
def entry(entry_id):
    """
    GET a representation of the container in the appropriate (content negotiated) format as identified by
    the supplied id
    Args:
    - entry_id:   The ID of the container as supplied in the request URL
    Returns a representation of the container: SSS will return either the Atom Entry identical to the one supplied
    as a deposit receipt or the pure RDF/XML Statement depending on the Accept header
    """
    # authenticate
    try:
        auth = basic_auth()
        ss = SwordServer(config, auth)

        # first thing we need to do is check that there is an object to return, because otherwise we may throw a
        # 415 Unsupported Media Type without looking first to see if there is even any media to content negotiate for
        # which would be weird from a client perspective
        if not ss.container_exists(entry_id):
            abort(404)

        # get the content negotiation headers
        accept_header = request.headers.get("Accept")
        accept_packaging_header = request.headers.get("Accept-Packaging")

        # do the negotiation
        default_accept_parameters, acceptable = config.get_container_formats()
        cn = ContentNegotiator(default_accept_parameters, acceptable)
        accept_parameters = cn.negotiate(accept=accept_header)
        app.logger.info("Container requested in format: " + str(accept_parameters))

        # did we successfully negotiate a content type?
        if accept_parameters is None:
            raise SwordError(error_uri=Errors.content, status=415, empty=True)

        # now actually get hold of the representation of the container and send it to the client
        cont = ss.get_container(entry_id, accept_parameters)

        resp = make_response(cont)
        if cont is not None:
            resp.mimetype = accept_parameters.content_type.mimetype()

        return resp

    except SwordError as e:
        return raise_error(e)
예제 #3
0
def content(entry_id):
    """
    GET the media resource content in the requested format (web request will include content negotiation via
    Accept header)
    Args:
    - entry_id:   the ID of the object in the store
    Returns the content in the requested format
    """

    auth = basic_auth()
    ss = SwordServer(config, auth)

    # first thing we need to do is check that there is an object to return, because otherwise we may throw a
    # 406 Not Acceptable without looking first to see if there is even any media to content negotiate for
    # which would be weird from a client perspective
    if not ss.media_resource_exists(entry_id):
        abort(404)

    # get the content negotiation headers
    accept_header = request.headers.get("Accept")
    accept_packaging_header = request.headers.get("Accept-Packaging")

    # do the negotiation
    default_accept_parameters, acceptable = config.get_media_resource_formats()
    cn = ContentNegotiator(default_accept_parameters, acceptable)
    accept_parameters = cn.negotiate(accept=accept_header,
                                     accept_packaging=accept_packaging_header)

    try:
        # can get hold of the media resource
        media_resource = ss.get_media_resource(entry_id, accept_parameters)
    except SwordError as e:
        return raise_error(e)

    # either send the client a redirect, or stream the content out
    if media_resource.redirect:
        return redirect(media_resource.url)
    else:
        resp = send_file(media_resource.filepath,
                         mimetype=media_resource.content_type)
        resp.headers["Packaging"] = media_resource.packaging
        return resp
def content(entry_id):
    """
    GET the media resource content in the requested format (web request will include content negotiation via
    Accept header)
    Args:
    - entry_id:   the ID of the object in the store
    Returns the content in the requested format
    """

    auth = basic_auth()
    ss = SwordServer(config, auth)

    # first thing we need to do is check that there is an object to return, because otherwise we may throw a
    # 406 Not Acceptable without looking first to see if there is even any media to content negotiate for
    # which would be weird from a client perspective
    if not ss.media_resource_exists(entry_id):
        abort(404)

    # get the content negotiation headers
    accept_header = request.headers.get("Accept")
    accept_packaging_header = request.headers.get("Accept-Packaging")

    # do the negotiation
    default_accept_parameters, acceptable = config.get_media_resource_formats()
    cn = ContentNegotiator(default_accept_parameters, acceptable)
    accept_parameters = cn.negotiate(accept=accept_header, accept_packaging=accept_packaging_header)

    try:
        # can get hold of the media resource
        media_resource = ss.get_media_resource(entry_id, accept_parameters)
    except SwordError as e:
        return raise_error(e)

    # either send the client a redirect, or stream the content out
    if media_resource.redirect:
        return redirect(media_resource.url)
    else:
        resp = send_file(media_resource.filepath, mimetype=media_resource.content_type)
        resp.headers["Packaging"] = media_resource.packaging
        return resp