# -*- coding: utf-8 -*-


import sys
import cgi
import time
import base64
import httplib
import ConfigParser
import os
import re
import commands
import subprocess
import codecs
import json

from xml.dom import minidom
from xml.dom.minidom import Document
import ctypes

class AbstractDialog:
    
    def process(self, request):
        raise NotImplementedError('must be redeclared')
    
    def req_answer(self, content, code=httplib.OK):
        response = {}
        response["code"] = code
        #response["Content-Type"] = "application/Json"#"text/xml"
        #response['content-type']="application/json;charset=UTF-8"
        response['content-type']="text/json"

        response["data"] = json.dumps(content)#.toxml("utf-8")
        
        return response

    def req_answerText(self, content, code=httplib.OK):
        response = {}
        response["code"] = code
        response["Content-Type"] = "text/plain"
        response["data"] = content
        
        return response

    def req_unauthorized(self):
        response = {}
        response["code"] = httplib.UNAUTHORIZED
        response["Content-Type"] = "text/plain"
        response["data"] = "Unauthorized"
        
        return response

    def req_forbidden(self):
        response = {}
        response["code"] = httplib.FORBIDDEN
        response["Content-Type"] = "text/plain"
        response["data"] = "Forbidden"
        
        return response

    def req_not_found(self):
        response = {}
        response["code"] = httplib.NOT_FOUND
        response["Content-Type"] = "text/plain"
        response["data"] = "Not Found"
        
        return response

class ExtendDialog(AbstractDialog):
    """
    响应并处理请求部分
    """
    def __init__(self, role_instance=None):
        self.role_instance = role_instance

    #Modified by gongyuwang
    def process(self, request):
        """
        处理请求
        :param request: 请求
        :return:请求响应
        """
        path = request["path"]
        
        if request["method"] == "GET":
            """
            处理GET请求
            """
            print "ExtendDialog do_GET "+path
            # Logger.debug("ExtendDialog do_GET "+path)
            
            #获取应用列表的http请求
            if path == "/applications":#请求的URL的除ip和端口部分外的部分
                print "deal with /applications"
                return self.req_applications(request)#实际处理函数
            
            return None
        
        elif request["method"] == "POST":
            """
            处理POST请求
            """
            # Logger.debug("ExtendDialog do_POST "+path)
            #来自center的创建会话的POST请求
            if path == "/session/create":#请求的URL的除ip和端口部分外的部分
                return self.req_session_create(request)  #实际处理函数
            
            return None
        
        return None
   
    
    def req_applications(self, request):
        doc={}
        doc["applications"]=None
        appNode={'application':{"id":"8858"}}
        doc["applications"]=appNode

        return self.req_answer(doc)
    
       
        
    def req_session_create(self, request):
        print type(request["data"])
        print type(json.loads(request["data"]))
        doc={}
        doc['session']=None

        return self.req_answer(doc)
    
   
if __name__ == "__main__":
    print "hello" + time.ctime()
        
