martes, 7 de diciembre de 2010

AXL conexion a callmanager

amigos hay les dejo una forma de conectarse por medio de axl a un call manager, esto utilizando consultas sql directas, a mi me hizo un buen batallar asi es de que si les sirve con gusto lo comparto, yo voy al grano ese es el codigo, calenlo y listo oviamente asumo que ya en el servidor de call manager estan todas las configuraciones necesarias, solo dejo el codigo espero que les sirva como a mi



el codigo aparte de conectarse y extraer los datos lo guarda en un archivo xml, y ese archivo xml yo lo manipulaba con etl, si la gente me lo pide subire un video de como se hace solo pidanmelo por correo o con algun comentario saludos...


package axljavaclient;
import java.io.*;
import java.net.*;
import javax.net.ssl.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
 *
 * @author juan
 */
public class AXLJavaClient {
        public static void main(String[] args) {
        byte[] bArray = null; // buffer for reading response from
        Socket socket = null; // socket to AXL server
        OutputStream out = null; // output stream to server
        InputStream in = null; // input stream from server
        String sAXLSOAPRequest = "";
        // HTTPS header and SOAP payload
        String sAXLRequest = null; // will hold only the SOAP payload
        //username=CCMAdministrator and password=cisco_cisco
        String authorization = "administrator" + ":" + "mipassword";
        // base64 encoding of the username and password
        authorization = new sun.misc.BASE64Encoder().encode(authorization.getBytes());
        // Form the http header
        sAXLSOAPRequest = "POST /axl/ HTTP/1.0\r\n";
                sAXLSOAPRequest += "Host:IPDELSERVER:8443\r\n";
                sAXLSOAPRequest += "Authorization: Basic " + authorization + "\r\n";
                sAXLSOAPRequest += "Accept: text/*\r\n";
                sAXLSOAPRequest += "Content-type: text/xml\r\n";
                sAXLSOAPRequest += "SOAPAction: \"CUCM:DB ver=7.0\"\r\n";
                sAXLSOAPRequest += "Content-length: ";
                sAXLRequest = "
                sAXLRequest += " xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"";
                sAXLRequest += " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
                sAXLRequest += " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> ";
                sAXLRequest += " ";
                sAXLRequest += " ";
                sAXLRequest += " ";
                sAXLRequest += "select  pkid,department,firstname,lastname,telephonenumber from enduser" ;
//                sAXLRequest += "select pkid,description from device" ;
                sAXLRequest += "
";
                sAXLRequest += "
";
                sAXLRequest += "
";
                sAXLRequest += "
";
              
        // finish the HTTPS Header
        sAXLSOAPRequest += sAXLRequest.length();
        sAXLSOAPRequest += "\r\n\r\n";
        // now add the SOAP payload to the HTTPS header, which completes the AXL
        // SOAP request
        sAXLSOAPRequest += sAXLRequest;
        try {
            AXLJavaClient axl = new AXLJavaClient();
            // Implement the certificate-related stuffs required for sending request via https
            X509TrustManager xtm = axl.new MyTrustManager();
            TrustManager[] mytm = { xtm };
            SSLContext ctx = SSLContext.getInstance("SSL");
            ctx.init(null, mytm, null);
            SSLSocketFactory sslFact = (SSLSocketFactory) ctx.getSocketFactory();
            socket = (SSLSocket) sslFact.createSocket("IPDELSERVER", Integer.parseInt("8443"));
            in = socket.getInputStream();
            // send the request to the server
            // read the response from the server
            StringBuffer sb = new StringBuffer(2048);
            bArray = new byte[2048];
            int ch = 0;
            int sum = 0;
            out = socket.getOutputStream();
            out.write(sAXLSOAPRequest.getBytes());
            while ((ch = in.read(bArray)) != -1) {
                sum += ch;
                sb.append(new String(bArray, 0, ch));
            }
            socket.close();
            String XML=sb.substring(sb.lastIndexOf(""), sb.lastIndexOf("")+9);
                       //**********************ESCRIBIMOS EN UN ARCHIVO***********
                        FileWriter fichero = null;
                        PrintWriter pw = null;
                        try
                        {
                           fichero = new FileWriter("/home/juan/Escritorio/axl.xml");
                           pw = new PrintWriter(fichero);
                           pw.println(XML);
                         } catch (Exception e) {
                            e.printStackTrace();
                         } finally {
                           try {
                                // Nuevamente aprovechamos el finally para
                                // asegurarnos que se cierra el fichero.
                               if (null != fichero)
                                  fichero.close();
                            } catch (Exception e2) {
                                  e2.printStackTrace();
                              }
                         }
                        //**********************TERMINA LA ESCRITURA DEL ARCHIVO
                         System.out.println("SE HA GENERADO EL ARCHIVO XML");

        } catch (UnknownHostException e) {
            System.err.println("Error connecting to host: " + e.getMessage());
            return;
           } catch (IOException ioe) {
            System.err.println("Error sending/receiving from server: " + ioe.getMessage());
            // close the socket
             } catch (Exception ea) {
             System.err.println("Unknown exception " + ea.getMessage());
             return;
             }
             finally{
                   try {
                       if (socket != null)
                      socket.close();
                   } catch (Exception exc) {
                    exc.printStackTrace();
                    System.err.println("Error closing connection to server: "+ exc.getMessage());
                     }
             }

        }
        public class MyTrustManager implements X509TrustManager {
        MyTrustManager() {

            // create/load keystore
        }
        public void checkClientTrusted(X509Certificate chain[], String authType)

                throws CertificateException {
        }
        public void checkServerTrusted(X509Certificate chain[], String authType)

                throws CertificateException {
        }
        public X509Certificate[] getAcceptedIssuers() {
            return null;
                }
       }
}

No hay comentarios:

Publicar un comentario