def set_language(self):
		locale = self.getHeader('Accept-Language')
		locale = locale.replace('-', '_')
		MODULE.process('Setting language to %r' % (locale,))
		if not change_locale(locale):
			locale = 'C'
		self.site.handler.set_language(locale)
	def __umcp_respond(self, response):
		MODULE.process('Responding to %r; status=%r' % (response.id, response.status))
		request = self.requests.pop(response.id)
		request.setResponseCode(response.status)
		request.setHeader('X-UMC-Message', json.dumps(response.message or ''))

		if response.mimetype != 'application/json':
			request.write(response.body)
		else:
			request.write(json.dumps(response.result))
		request.finish()
	def __load_module(self):
		modname = self.__module
		self.__module = None
		MODULE.info('Importing module %r' % (modname,))
		try:
			self.__import(modname)
		except ImportError as exc:
			MODULE.error('Failed to import module %s: %s\n%s' % (modname, exc, format_exc()))
			self.reload_server()  # TODO: should we check module existance in umc-server
			raise

		self.handler = self.__module.Instance()
	def render(self, request):
		request.setHeader('Content-Type', 'application/json')

		handler = request.site.handler
		method = request.getHeader('X-UMC-Method')
		umcptype, command = self.get_command(request.path)
		umcprequest = request.get_umcp_request(umcptype, command)

		MODULE.info('Executing %s' % command)
		try:
			func = getattr(handler, method)
		except AttributeError:
			MODULE.info('Method %s of command %s does not exists' % (method, command))
			request.setResponseCode(500)
			return ''

		func(umcprequest)
		return NOT_DONE_YET
	def render(self, resource):
		message = None
		result = ''
		method = self.getHeader('X-UMC-Method')
		_ = self.getSession(Translation)._  # TODO: this runs in the module process so it can be a global
		try:
			self.site.initialize(self)
			return Request.render(self, resource)
		except UMC_OptionSanitizeError as exc:
			self.setResponseCode(409)  # Conflict  # HTTP FIXME
			message = exc.message
			result = exc.body
		except (UMC_OptionTypeError, UMC_OptionMissing, UMC_CommandError) as exc:
			self.setResponseCode(400)
			message = {
				UMC_OptionTypeError: _('An option passed to %s has the wrong type: %s') % (method, exc),
				UMC_OptionMissing: _('One or more options to %s are missing: %s') % (method, exc),
				UMC_CommandError: _('The command has failed: %s') % (exc, )
			}.get(exc.__class__)  # TODO: subclasses?
		except BaseException as exc:
			self.setResponseCode(MODULE_ERR_COMMAND_FAILED)  # HTTP FIXME
			message = _("Execution of command '%(command)s' has failed:\n\n%(text)s") % {
				'command': self.path,
				'text': unicode(traceback.format_exc())
			}

		if isinstance(message, unicode):
			message = message.encode('UTF-8')
		MODULE.process(message)

		message = json.dumps(message)
		result = json.dumps(result)

		self.setHeader('X-UMC-Message', message)
		self.setHeader('Content-Length', b'%d' % len(result))
		self.write(result)
		self.finish()
	def render(self, request):
		session = request.getSession()
		acls = ACLs(session)
		moduleManager = request.site.moduleManager

		command = '/'.join(request.prepath[1:])

		module_name = acls.get_module_providing(moduleManager, command)
		if not module_name:
			MODULE.warn('No module provides %s' % (command))
			request.setResponseCode(BAD_REQUEST_FORBIDDEN)
			return

		MODULE.info('Checking ACLs for %s (%s)' % (command, module_name))
		if not acls.is_command_allowed(request, command):
			MODULE.warn('Command %s is not allowed' % (command))
			request.setResponseCode(BAD_REQUEST_FORBIDDEN)
			return

		methodname = acls.get_method_name(moduleManager, module_name, command)
		if not methodname:
			MODULE.warn('Command %s does not exists' % (command))
			request.setResponseCode(BAD_REQUEST_NOT_FOUND)
			return

		headers = self.get_request_header(request, methodname)
		body = self.get_request_body(request)

		CORE.info('Passing new request to module %s' % (module_name,))
		process = self.get_process(session, module_name)

		urequest = process.request(request.method, request.uri, headers, body)
		urequest.addCallback(self.respond, request)
		urequest.addErrback(self.failed_request, request)

		return NOT_DONE_YET