java通过http协议与Freeswitch对接实现动态添加用户

FreeSwitch 支持将话单写入远程的 HTTP 服务器,在哪里你可以使用任何喜欢的编程语言处理话单以及写入任何可能的数据库。有三个模块可以实现他:mod_xml_cdr、mod_json_cdr 以及 mod_format_cdr。前两者分别产生 xml 和 json 格式,最后一个模块则可配置产生 XML 或 JSON 格式。
使用 mod_xml_curl为例:

第一步: 在C:\Program
Files\FreeSWITCH\conf\autoload_configs\modules.conf.xml中释放<load module="mod_xml_curl"/> 的注释。
第二步:② C:\Program
Files\FreeSWITCH\conf\autoload_configs\xml_curl.conf.xml 中添加

 <binding name="directory"> 
      <param name="gateway-url" value="http://localhost:8080/fsapi/" bindings="directory"/> 
    </binding> 

中的value指向你的 HTTP 服务器地址。

第三步:创建并开启HTTP服务

//连接FS服务
connectFSAndSubscibeFSEvent();
ExecutorService  pool = Executors.newCachedThreadPool();
server = HttpServer.create(new InetSocketAddress(8080),10);
server.createContext("/fsapi/", new DirectoryHandler());
//可以通过设置一个线程组由线程来决定执行的过程
server.setExecutor(pool);
server.start();
System.out.println("server started:");

第四步:号码验证

    /** * @author cyq *号码注册认证 */
    class DirectoryHandler implements HttpHandler{

        @Override
        public void handle(HttpExchange exc) throws IOException {

            //String uri=exc.getRequestURI().toString();
            //获得输入流
            BufferedReader reader=new BufferedReader(new InputStreamReader(exc.getRequestBody()));
            //用于存储请求信息 请求参数
            String valueString = null;
            StringBuilder sbf=new StringBuilder();
            while ((valueString=reader.readLine())!=null){
                sbf.append(valueString);
            } 
            String param=sbf.toString();
            //System.out.println(param);
            String[] str_= param.split("&");
            @SuppressWarnings("unused")
            String section=null;
            String req_key = null;
            String req_user = null;
            String req_domain = null;
            @SuppressWarnings("unused")
            String req_ip = null;
            String req_callout = null;
            String req_pswd = "1234";
            for(int i=0;i<str_.length;i++){
                String[] str2_ = str_[i].split("=");
                switch (str2_[0]) {

                case "section":
                    section=str2_[1];
                    break;
                case "key":
                    req_key=str2_[1];
                    break;
                case "user":
                    req_user=str2_[1];
                    req_callout=str2_[1];
                    break;
                case "domain":
                    req_domain=str2_[1];
                    break;
                case "ip":
                    req_ip=str2_[1];
                    break;

                default:
                    break;
                }
            }
    //此处查询数据库
DirectoryEntity directorys=directoryManageServerBusiness.selectExtensions(req_user,req_domain);
req_pswd = directorys.get(0).getExtensionPswd();
                responseMessage=
                        "<document type='freeswitch/xml'>"+
                                "<section name='directory'>" +
                                " <domain name='"+req_domain+"'>"+
                                " <params>"+
                                " <param name='dial-string' " +
                                " value='{presence_id=${dialed_user}@${dialed_domain}}${sofia_contact(${dialed_user}@${dialed_domain})}'/>" +
                                " </params>"+
                                " <groups>"+
                                " <group name='default'>" +
                                " <users>"+
                                " <user id='"+req_user+"'>"+
                                " <params>"+
                                " <param name='password' value='"+req_pswd+"'/>"+
                                " <param name='vm-password' value='"+req_pswd+"'/>"+
                                " </params>" +
                                " <variables>" +
                                " <variable name='toll_allow' value='domestic,international,local'/>"+
                                " <variable name='accountcode' value='"+req_user+"'/>" +
                                " <variable name='user_context' value='default'/>" +
                                " <variable name='effective_caller_id_name' value='"+req_user+"'/>" +
                                " <variable name='effective_caller_id_number' value='"+req_user+"'/>" +
                                " <variable name='directory-visible' value='true'/>"+
                                " <variable name='directory-exten-visible' value='true'/>"+
                                " <variable name='limit_max' value='1'/>"+
                                " <variable name='outbound_caller_id_name' value='"+req_callout+"'/>"+
                                " <variable name='outbound_caller_id_number' value='"+req_callout+"'/>"+
                                " <variable name='callgroup' value='techsupport'/>"+
                                " </variables>"+
                                " </user>"+
                                " </users>"+
                                " </group>"+
                                " </groups>" +
                                " </domain>" +
                                " </section>" +
                                "</document>";
            }else{
                responseMessage=
                        " <document type='freeswitch/xml'>" +
                                " <section name='directory'>" +
                                " </section>" +
                                " </document>";
            }
            //回应信息
            exc.sendResponseHeaders(HttpURLConnection.HTTP_OK, responseMessage.getBytes().length);
            OutputStream out = exc.getResponseBody(); 
            out.write(responseMessage.getBytes());
            out.flush();
            exc.close();
        }

    }

你可能感兴趣的:(java,用户,http协议,Directory,freeswitch)