# $SnapHashLicense:
#
# SnapLogic - Open source data services
#
# Copyright (C) 2009, SnapLogic, Inc. All rights reserved.
#
# See http://www.snaplogic.org for more information about
# the SnapLogic project.
#
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2. See the LEGAL file
# at the top of the source tree.
#
# "SnapLogic" is a trademark of SnapLogic, Inc.
#
# $
#
# $Id: rh_front_end.py 7363 2009-04-24 17:35:55Z pamor $
from __future__ import with_statement
"""
Request Handler wrappers around the internal Repository API.
These methods provide an API translation between the repository and
the internal API used by other modules.
This is for the benefit of the request handler (thus the RH prefix),
so that the request handler does not need to know all the internals
of how the various modules (the repository in this case) want to have
message bodies and parameters interpreted.
"""
from snaplogic.server import repository
from snaplogic.server.http_request import HttpRequest
from snaplogic.server import RhResponse
from snaplogic.server import product_prefix
from snaplogic.snapi_base import keys
from snaplogic.snapi_base.resdef import create_from_dict
from snaplogic.snapi_base.exceptions import ERROR_GEN_ID_CONFLICT,ERROR_GUID_CONFLICT
from snaplogic.common import snap_http_lib
from snaplogic.common.snap_exceptions import *
def rh_init():
"""
Convenience wrapper around L{repository.init}.
This method is provided so that the server doesn't have to import the repository module directly.
"""
repository.init()
def rh_get_handler(http_req):
"""
Front-end to repository for a GET request.
@param http_req: HTTP request
@type http_req: L{HttpRequest}
@return: RhResponse object with data and code to be written to client.
@rtype: L{RhResponse}
"""
uri = http_req.path
idx = uri.find(keys.RESOURCE_PROPERTY_NAMESPACE)
if idx == -1:
# No special URI. Treat as a regular resource URI.
return rh_read_resources(http_req)
else:
# Request to get a property of a resource.
prop_name = uri[idx:]
try:
with repository.get_instance() as rep:
if prop_name == keys.RESOURCE_PROPERTY_DEPENDENCIES:
retval = rep.get_resource_dependencies(uri[:idx])
elif prop_name == keys.RESOURCE_PROPERTY_DEPENDENCIES_LOCAL:
retval = rep.get_resource_dependencies(uri[:idx], True)
else:
return RhResponse(http_req.NOT_FOUND, "Invalid resource property '%s'" % prop_name)
except SnapObjNotFoundError, e:
return RhResponse(HttpRequest.NOT_FOUND, str(e))
return RhResponse(http_req.OK, retval)
def rh_read_resources(http_req):
"""
Front-end to repository read_resources method.
@param http_req: HTTP request
@type http_req: L{HttpRequest}
@return: RhResponse object with data and code to be written to client.
@rtype: L{RhResponse}
"""
options = { 'title' : product_prefix.SNAPLOGIC_PRODUCT + ': Resource definition', 'type' : "resdef" }
method = http_req.method
uri_list = None
if method == "POST":
http_req.make_input_rp()
try:
uri_list = http_req.input.next()
except StopIteration:
return RhResponse(http_req.BAD_REQUEST, "URI list missing", None, options)
else:
uri_list = [http_req.path]
with repository.get_instance() as rep:
try:
retval = rep.read_resources(uri_list, credentials=(http_req.username, http_req.groups))
for r in retval['success']:
resdef = create_from_dict(retval[keys.SUCCESS][r][keys.RESDEF])
l = {}
not_needed_props = [ keys.SNAP_INTERNAL, keys.SNAP_GENERAL_INFO ]
for prop_name in resdef.list_property_names():
if prop_name not in not_needed_props:
obfuscate_descriptor = resdef._get_obfuscate_descriptor(prop_name)
if obfuscate_descriptor is not None:
l[prop_name] = obfuscate_descriptor
if l:
options['obfuscate_dict'] = l
except SnapValueError, e:
return RhResponse(HttpRequest.BAD_REQUEST, str(e), None, options)
# Check if there was an error indicated, specifically a 'not found'.
# This is done by returning the path as an element in the error dictionary,
# with None as the value.
if http_req.path in retval['error'] and retval['error'][http_req.path] is None:
return RhResponse(HttpRequest.NOT_FOUND, "Resource '%s' does not exist." % http_req.path, None, options)
return RhResponse(HttpRequest.OK, retval, None, options)
def rh_update_resource(http_req):
"""
Front-end to L{repository.update_resource}
@param http_req: HTTP request
@type http_req: L{HttpRequest}
@return: RhResponse object with data and code to be written to client.
@rtype: L{RhResponse}
"""
http_req.make_input_rp()
path = http_req.path
try:
resdef_info = http_req.input.next()
guid = resdef_info[keys.GUID]
gen_id = resdef_info[keys.GEN_ID]
resdef = resdef_info[keys.RESDEF]
force_flag = resdef_info.get(keys.FORCE, False)
# If capabilities contain absolute URIs, rewrite them
# with relative ones.
# This is a temporary solution for #1400.
if resdef.has_key(keys.CAPABILITIES):
capabilities = resdef[keys.CAPABILITIES]
for c in keys.CAPABILITY_URIS:
if capabilities.has_key(c):
cap_uri = capabilities[c]
if cap_uri:
(l_scheme, l_netloc, l_path, l_params, l_query, l_fragment) = snap_http_lib.parse_uri(cap_uri)
if l_netloc:
cap_uri = snap_http_lib.unparse_uri((None, None, l_path, l_params, l_query, l_fragment))
capabilities[c] = cap_uri
except StopIteration:
return RhResponse(http_req.BAD_REQUEST, "Resource definition missing")
except KeyError, ke:
return RhResponse(http_req.BAD_REQUEST, "Argument missing: %s" % ke)
if gen_id is not None and guid is None:
return RhResponse(http_req.BAD_REQUEST, "No GUID given but Generation ID set for '%s'" % path)
if gen_id is None and guid is not None:
gen_id = 0
with repository.get_instance() as rep:
if gen_id is None and guid is None:
try:
retval = rep.create_resource(path, resdef, force_flag)
except SnapObjExistsError, e:
return RhResponse(HttpRequest.CONFLICT, str(e))
except SnapObjTypeError, e:
return RhResponse(HttpRequest.BAD_REQUEST, str(e))
except SnapValueError, e:
return RhResponse(HttpRequest.BAD_REQUEST, str(e))
else:
try:
retval = rep.update_resource(path, guid, gen_id, resdef, force_flag)
except SnapObjTypeError, e:
return RhResponse(HttpRequest.BAD_REQUEST, str(e))
except SnapObjNotFoundError, e:
return RhResponse(HttpRequest.NOT_FOUND, str(e))
except SnapResDefGUIDError, e:
return RhResponse(HttpRequest.CONFLICT, str(e), ERROR_GUID_CONFLICT)
except SnapResDefGenIDError, e:
return RhResponse(HttpRequest.CONFLICT, str(e), ERROR_GEN_ID_CONFLICT)
except SnapValueError, e:
return RhResponse(HttpRequest.BAD_REQUEST, str(e))
return RhResponse(HttpRequest.OK, retval)
def rh_delete_resource(http_req):
"""
Front-end to L{repository.delete_resource}
@param http_req: HTTP request
@type http_req: L{HttpRequest}
@return: RhResponse object with data and code to be written to client.
@rtype: L{RhResponse}
"""
# XXX We use SnAPI headers for arguments here because many implementations do not support
# XXX a request body with the DELETE method.
try:
guid = http_req.snapi_headers[keys.GUID.upper()]
gen_id = http_req.snapi_headers[keys.GEN_ID.upper()]
except KeyError, ke:
return RhResponse(http_req.BAD_REQUEST, "Argument missing: %s" % ke)
# The following flag is new to SnAPI 2.1, but we will allow its absence for backwards compatibility.
try:
force_flag = bool(http_req.snapi_headers[keys.FORCE.upper()])
except KeyError:
force_flag = False
# Need to fixup the conversion to string on the header values.
if guid == 'None':
guid = None
if gen_id == 'None':
gen_id = None
else:
gen_id = int(gen_id)
try:
with repository.get_instance() as rep:
retval = rep.delete_resource(http_req.path, guid, gen_id, force_flag)
except SnapObjNotFoundError, e:
return RhResponse(HttpRequest.NOT_FOUND, str(e))
except SnapResDefGUIDError, e:
return RhResponse(HttpRequest.CONFLICT, str(e), ERROR_GUID_CONFLICT)
except SnapResDefGenIDError, e:
return RhResponse(HttpRequest.CONFLICT, str(e), ERROR_GEN_ID_CONFLICT)
except SnapValueError, e:
return RhResponse(HttpRequest.BAD_REQUEST, str(e))
return RhResponse(HttpRequest.OK, retval)
def rh_list_resources(http_req):
"""
Front-end to L{repository.list_resources}
@param http_req: HTTP request
@type http_req: L{HttpRequest}
@return: RhResponse object with data and code to be written to client.
@rtype: L{RhResponse}
"""
method = http_req.method
uri_list = None
if method == "POST":
http_req.make_input_rp()
try:
uri_list = http_req.input.next()
except StopIteration:
return RhResponse(http_req.BAD_REQUEST, "URI list missing")
with repository.get_instance() as rep:
try:
retval = rep.list_resources(uri_list=uri_list, credentials=(http_req.username, http_req.groups))
except SnapValueError, e:
return RhResponse(HttpRequest.BAD_REQUEST, str(e))
return RhResponse(HttpRequest.OK, retval, None, { 'title' : product_prefix.SNAPLOGIC_PRODUCT + ': Resource list' })
def rh_summarize_resources(http_req):
"""
Front-end to repository summarize_resources function.
@param http_req: HTTP request
@type http_req: L{HttpRequest}
@return: RhResponse object with data and code to be written to client.
@rtype: L{RhResponse}
"""
if http_req.method == "GET":
uri_list = None
details = None
elif http_req.method == "POST":
http_req.make_input_rp()
try:
args = http_req.input.next()
except StopIteration:
return RhResponse(http_req.BAD_REQUEST, "Argument list missing")
try:
uri_list = args[0]
except IndexError:
return RhResponse(http_req.BAD_REQUEST, "URI list missing")
try:
details = args[1]
except IndexError:
return RhResponse(http_req.BAD_REQUEST, "Details list missing")
with repository.get_instance() as rep:
retval = rep.summarize_resources(uri_list, details, credentials=(http_req.username, http_req.groups))
return RhResponse(HttpRequest.OK, retval, None,
{'title': product_prefix.SNAPLOGIC_PRODUCT + ': Resource summary'})
|