def retrieveResourceFromStorage(uri, only_public=False, is_partial=False): """ Return the details about a stored resource (partial or otherwise). The resource is identified via its URI. @param uri: Identifies a resource via its URI. @type uri: string @param only_public: Flag indicating whether we only want to see the public information about this resource. @type only_public: boolean @param is_partial: Indicate whether we are loading a partial resource. @type is_partial: boolean @return: Dictionary or None if not found. @rtype: dict """ # Need to take the resource URI prefix off to get the resource_name. if is_partial: resource_name = uri[len(settings.PREFIX_SPECIALIZED)+1:] else: resource_name = uri[len(settings.PREFIX_RESOURCE)+1:] obj = None try: obj = STORAGE_OBJECT.loadResourceFromStorage(resource_name, is_partial) if not obj: raise Exception("Unknown resource: " + resource_name) if type(obj) is not dict or 'public' not in obj: obj = None raise Exception("Missing top-level element 'public' or malformed resource.") public_obj = obj['public'] # Do some sanity checking on the resource. Needs to contain # a few key elements at least. for mandatory_key in [ 'uri', 'desc', 'name' ]: if mandatory_key not in public_obj: public_obj = None raise Exception("Mandatory key '%s' missing in stored resource '%s'" % \ (mandatory_key, resource_name)) if only_public: obj = public_obj except Exception, e: log("Malformed storage for resource '%s': %s" % (resource_name, str(e)), facility=LOGF_RESOURCES)
def retrieveResourceFromStorage(uri, only_public=False): """ Return the details about a stored resource. The resource is identified via its URI. @param uri: Identifies a resource via its URI. @type uri: string @param only_public: Flag indicating whether we only want to see the public information about this resource. @type only_public: boolean @return: Dictionary or None if not found. @rtype: dict """ # Need to take the resource URI prefix off to get the resource_name. resource_name = uri[len(settings.PREFIX_RESOURCE) + 1:] obj = None try: obj = STORAGE_OBJECT.loadResourceFromStorage(resource_name) if not obj: raise Exception("Unknown resource: " + resource_name) if type(obj) is not dict or 'public' not in obj: obj = None raise Exception( "Missing top-level element 'public' or malformed resource.") public_obj = obj['public'] # Do some sanity checking on the resource. Needs to contain # a few key elements at least. for mandatory_key in ['uri', 'desc', 'name']: if mandatory_key not in public_obj: public_obj = None raise Exception("Mandatory key '%s' missing in stored resource '%s'" % \ (mandatory_key, resource_name)) if only_public: obj = public_obj except Exception, e: log("Malformed storage for resource '%s': %s" % (resource_name, str(e)), facility=LOGF_RESOURCES)
def form(self, method, input, component_name, message="", specialized=False): """ Display a resource creation form for a specified component. @param method: The HTTP request method. @type method: string @param input: Any data that came in the body of the request. @type input: string @param component_name: Name of the component for which to create the resource. @type component_name: string @param message: An error message to be displayed above the form. @type message: string @return: The output data of this service. @rtype: Result """ input_params = dict() input_rctp = dict() if input and HttpMethod.POST: flag, msg, input = self.__create(input, component_name, specialized) if not flag: message = msg else: return Result.created(msg['uri'], msg) if input: if type(input) is dict: # We receive a dict of values if the 'create' method discovered an # error. In that case, the values should be used to pre-populate # the fields when the form is re-displayed (with the error messsage # on top). input_rctp = input.get('resource_creation_params', dict()) # Resource creation time parameters input_params = input.get('params', dict()) # Other parameters if specialized: # Need to read the definition of the partial resource and get the # component name from there. specialized_code_name = component_name specialized_def = STORAGE_OBJECT.loadResourceFromStorage(specialized_code_name, True) component_uri = specialized_def['private']['code_uri'] elems = component_uri.split("/") component_name = elems[len(elems)-1] # Take the parameter map from the component comp = restx.components.make_component(component_name) if not comp: return Result.notFound("Cannot find component '%s'" % component_name) header = settings.HTML_HEADER # Assemble the form elements for the parameters params = dict() params.update(comp.getParams()) # In case this is a Java component, we get a Python dict this way if specialized: fname = specialized_def['public']['name'] fdesc = specialized_def['public']['desc'] # Remove all parameters that have been specified in the specialized component resource # definition already spec_params = specialized_def['private'].get('params') if spec_params: for name in spec_params: if name in params: del params[name] else: fname = comp.getName() fdesc = comp.getDesc() param_fields_html = "" if params: param_field_names = params.keys() param_field_names.sort() for pname in param_field_names: pdef = params[pname] if not pdef.required: opt_str = "<br>optional, default: %s" % pdef.getDefaultVal() else: opt_str = "" values = input_params.get(pname) if type(values) is not list and pdef.isList(): if values is None: values = [] else: values = [ values ] param_fields_html += \ """<tr> <td valign=top id="%s_name">%s<br><small>(%s%s)</small></td> <td valign=top>%s</td> </tr>""" % (pname, pname, pdef.desc, opt_str, pdef.html_type("params__"+pname, values)) if message: msg = "<b><i><font color=red>%s</font></i></b><br><p>" % message else: msg = "" body = """ <h3>Resource creation form for: %s</h3> <p><i>"%s"</i></p> <hr> Please enter the resource configuration...<br><p> %s <form id="resource_form" name="input" action="%s" method="POST"> <table>""" % (fname, fdesc, msg, "%s%s/form/%s%s" % (settings.DOCUMENT_ROOT, self.getMyResourceUri(), component_name if not specialized else specialized_code_name, "?specialized=y" if specialized else "")) # Gather any initial values of the resource creation time form fields suggested_name_value = input_rctp.get("suggested_name", "") if suggested_name_value: suggested_name_value = 'value="%s" ' % suggested_name_value desc_value = input_rctp.get("desc", "") if desc_value: desc_value = 'value="%s" ' % desc_value specialized_value = "checked " if input_rctp.get("specialized") in [ "on", "ON" ] else " " if not specialized: body += """ <tr> <td id="Make_this_a_specialized_component_name">Make this a specialized component:</td> <td><input type="checkbox" %s id="resource_creation_params__specialized" name="resource_creation_params__specialized" /><label for=resource_creation_params__specialized><small>Can only be used as basis for other resources</small></label></td> </tr> """ % specialized_value body += """ <tr> <td id="Resource_name_name">Resource name:</td> <td><input type="text" %sname="resource_creation_params__suggested_name" id="resource_creation_params__suggested_name" /></td> </tr> <tr> <td id="Description_name">Description:<br><small>(optional)</small></td> <td><input type="text" %sname="resource_creation_params__desc" id="resource_creation_params__desc" /></td> </tr> %s <tr><td colspan=2 align=center><input id="submit_button" type="submit" value="Submit" /></tr> </table> </form>""" % (suggested_name_value, desc_value, param_fields_html) footer = settings.HTML_FOOTER return Result.ok(header + body + footer).addHeader("Content-type", "text/html; charset=UTF-8")