def convert_from_xml(payload, param_name, cid, is_required, is_complex, default_value, path_prefix, use_text): try: elem = path('{}.{}'.format(path_prefix, param_name), is_required).get_from(payload) except ParsingException, e: msg = 'Caught an exception while parsing, payload:[<![CDATA[{}]]>], e:[{}]'.format( etree.tostring(payload), format_exc(e)) raise ParsingException(cid, msg)
def get_params(self, request_params, path_prefix='', default_value=ZATO_NO_DEFAULT_VALUE, use_text=True, is_required=True): """ Gets all requested parameters from a message. Will raise ParsingException if any is missing. """ params = {} if not isinstance(self.payload, basestring): for param in request_params: if isinstance(param, ForceType): param_name = param.name else: param_name = param if self.is_xml: try: elem = path('{}.{}'.format(path_prefix, param_name), is_required).get_from(self.payload) except ParsingException, e: msg = 'Caught an exception while parsing, payload:[<![CDATA[{}]]>], e:[{}]'.format( etree.tostring(self.payload), format_exc(e)) raise ParsingException(self.cid, msg) if isinstance(param, List): value = elem #.getchildren() else: if elem is not None: if use_text: value = elem.text # We are interested in the text the elem contains .. else: return elem # .. or in the elem itself. else: value = default_value else: value = self.payload.get(param_name) # Use a default value if an element is empty and we're allowed to # substitute its (empty) value with the default one. if default_value != ZATO_NO_DEFAULT_VALUE and value is None: value = default_value else: if value is not None: if not isinstance(param, List): value = unicode(value) try: if not isinstance(param, AsIs): params[param_name] = self.convert( param, param_name, value, self.has_simple_io_config, self.is_xml) else: params[param_name] = value except Exception, e: msg = 'Caught an exception, param:[{}], param_name:[{}], value:[{}], self.has_simple_io_config:[{}], e:[{}]'.format( param, param_name, value, self.has_simple_io_config, format_exc(e)) self.logger.error(msg) raise Exception(msg)
def get_params(self, request_params, path_prefix='', default_value=NO_DEFAULT_VALUE, use_text=True, is_required=True): """ Gets all requested parameters from a message. Will raise ParsingException if any is missing. """ params = {} if not isinstance(self.payload, basestring): for param in request_params: if isinstance(param, ForceType): param_name = param.name else: param_name = param if self.is_xml: try: elem = path('{}.{}'.format(path_prefix, param_name), is_required).get_from(self.payload) except ParsingException, e: msg = 'Caught an exception while parsing, payload:[<![CDATA[{}]]>], e:[{}]'.format( etree.tostring(self.payload), format_exc(e)) raise ParsingException(self.cid, msg) if isinstance(param, List): value = elem else: if elem is not None: if use_text: value = elem.text # We are interested in the text the elem contains .. else: return elem # .. or in the elem itself. else: value = default_value else: value = self.payload.get(param_name, NOT_GIVEN) # Use a default value if an element is empty and we're allowed to # substitute its (empty) value with the default one. if value == NOT_GIVEN: if default_value != NO_DEFAULT_VALUE: value = default_value else: if is_required and not self.channel_params.get(param_name): msg = 'Required input element:[{}] not found, value:[{}]'.format(param, value) raise ParsingException(self.cid, msg) else: if value is not None and not isinstance(param, List): value = unicode(value) try: if not isinstance(param, AsIs): params[param_name] = self.convert(param, param_name, value, self.has_simple_io_config, self.is_xml) else: params[param_name] = value except Exception, e: msg = 'Caught an exception, param:[{}], param_name:[{}], value:[{}], self.has_simple_io_config:[{}], e:[{}]'.format( param, param_name, value, self.has_simple_io_config, format_exc(e)) self.logger.error(msg) raise Exception(msg)
def convert_from_xml(payload, param_name, cid, is_required, is_complex, default_value, path_prefix, use_text): try: elem = path('{}.{}'.format(path_prefix, param_name), is_required).get_from(payload) except ParsingException: msg = 'Caught an exception while parsing, payload:`<![CDATA[{}]]>`, e:`{}`'.format( etree.tostring(payload), format_exc()) raise ParsingException(cid, msg) if is_complex: value = elem else: if elem is not None: if use_text: value = elem.text # We are interested in the text the elem contains .. else: return elem # .. or in the elem itself. else: value = default_value return value
def index(req): """ Lists SQL connection pools and dispatches the management requests (create, edit and change_password). """ zato_servers = Server.objects.all().order_by("name") pools = [] config_pub_key = "" server_id = None choose_server_form = ChooseClusterForm(zato_servers, req.GET) edit_form = SQLConnectionPoolForm(prefix="edit") # Build a list of SQL connection pools for a given Zato server. server_id = req.GET.get("server") if server_id and req.method == "GET": # We have a server to pick the connection pools from, try to # invoke it now. server = Server.objects.get(id=server_id) _ignored, zato_message, soap_response = invoke_admin_service(server.address, "zato:pool.sql.get-list") # Config pub key is always needed. config_pub_key = zato_path("envelope.config_pub_key", True).get_from(zato_message) if zato_path("data.pool_list.pool").get_from(zato_message) is not None: for pool in zato_message.data.pool_list.pool: original_pool_name = unicode(pool.pool_name) pool_name = unicode(pool.pool_name) engine = unicode(pool.engine) engine_friendly = engine_friendly_name[str(engine)] user = unicode(pool.user) host = unicode(pool.host) db_name = unicode(pool.db_name) pool_size = pool.pool_size if path("pool.extra.item").get_from(pool) is not None: logger.log(TRACE1, "Found 'extra.item' in the response, pool_name=[%s]" % pool_name) extra = [] for extra_elem in pool.extra.item: extra.append(unicode(extra_elem)) extra = "\n".join(extra) else: logger.log(TRACE1, "No 'extra.item' found in the response, pool_name=[%s]" % pool_name) extra = "" pool = SQLConnectionPool(uuid4().hex, original_pool_name, pool_name, engine, engine_friendly, user, host, db_name, pool_size, extra) pools.append(pool) else: logger.info("No pools found, soap_response=[%s]" % soap_response) if req.method == "POST": action = req.POST.get("zato_action") if not action: msg = "Missing 'zato_action' parameter in req.POST" logger.error(msg) return HttpResponseServerError(msg) if not server_id: msg = "Parameter 'server' is missing in GET data." if action != "change_password": msg += " Action [%s], req.POST=[%s], req.GET=[%s]" % (action, pprint(req.POST), pprint(req.GET)) logger.error(msg) return HttpResponseServerError(msg) server = Server.objects.get(id=server_id) handler = globals().get("_" + action) if not handler: msg = "No handler found for action [%s]." % action if action != "change_password": msg += " req.POST=[%s], req.GET=[%s]" % (pprint(req.POST), pprint(req.GET)) logger.error(msg) return HttpResponseServerError(msg) # Finally, invoke the action handler. try: response = handler(server.address, req.POST) response = response if response else "" return HttpResponse(response) except Exception, e: msg = "Could not invoke action [%s], e=[%s]. " % (action, format_exc()) if action != "change_password": msg += " req.POST=[%s], req.GET=[%s]" % (pprint(req.POST), pprint(req.GET)) logger.error(msg) return HttpResponseServerError(msg)