#
# Copyright (c) 2005-2007 rPath, Inc.
#
# All Rights Reserved
#
import simplejson
import sys
import xmlrpclib

from mod_python import apache

from mint import config
from mint import mint_error
from mint import server
from mint import maintenance
from mint.logerror import logWebErrorAndEmail
from mint.web.webhandler import getHttpAuth

from conary.lib import coveragehook
from conary.repository import errors

def rpcHandler(req, db, cfg, pathInfo = None):
    maintenance.enforceMaintenanceMode(cfg)
    isJSONrpc = isXMLrpc = allowPrivate = False

    # only handle POSTs
    if req.method.upper() != 'POST':
        return apache.HTTP_METHOD_NOT_ALLOWED

    if "allowPrivate" in req.get_options():
        allowPrivate = req.get_options()['allowPrivate']
    elif "xmlrpc-private" in req.uri.split("/")[1]:
        allowPrivate = True

    if req.headers_in['Content-Type'].startswith('text/xml'):
        isXMLrpc = True
    elif req.headers_in['Content-Type'].startswith('application/x-json'):
        isJSONrpc = True
    else:
        return apache.HTTP_BAD_REQUEST

    authToken = getHttpAuth(req)

    if type(authToken) is list:
        authToken = authToken[0:2] # throw away entitlement
    # instantiate a MintServer
    srvr = server.MintServer(cfg, allowPrivate=allowPrivate, req=req, db=db)

    # switch on XML/JSON here
    if isXMLrpc:
        (args, method) = xmlrpclib.loads(req.read())
    if isJSONrpc:
        (method, args) = simplejson.loads(req.read())

    # coax parameters into something MintServer likes
    params = [method, authToken, args]

    # go for it; return 403 if permission is denied
    try:
        # result is (isError, returnValues)
        result = srvr.callWrapper(*params)
    except (errors.InsufficientPermission, mint_error.PermissionDenied):
        return apache.HTTP_FORBIDDEN

    # create a response
    if isXMLrpc:
        resp = xmlrpclib.dumps((result,), methodresponse=1)
        req.content_type = "text/xml"
    elif isJSONrpc:
        resp = simplejson.dumps(result[1])
        req.content_type = "application/x-json"

    # write repsonse
    try:
        req.write(resp)
    except IOError, err:
        # Client went away
        req.log_error('Error writing to client: %s' % err)
    return apache.OK

def handler(req):
    coveragehook.install()
    cfg = config.getConfig(req.filename)

    try:
        return rpcHandler(req, None, cfg)
    except:
        e_type, e_value, e_tb = sys.exc_info()
        logWebErrorAndEmail(req, cfg, e_type, e_value, e_tb, 'XMLRPC handler')
        del e_tb
        return apache.HTTP_INTERNAL_SERVER_ERROR

@@ -23,9 +23,9 @@
 -200
-7
+9
  rPa
@@ -59,16 +59,31 @@
 erved%0A#%0A
+import logging%0A
 import s
@@ -257,16 +257,47 @@
 tenance%0A
+from mint.lib import mintutils%0A
 from min
@@ -453,16 +453,16 @@
 errors%0A%0A
-
 def rpcH
@@ -500,16 +500,95 @@
  None):%0A
+    mintutils.setupLogging(consoleLevel=logging.INFO, consoleFormat='apache')%0A%0A
     main

Enable logging for XMLRPC requests.