⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 axlrequest.java

📁 Example of using Cisco Call Manager AXL service
💻 JAVA
字号:
/**
 * 
 */
package org.caixapenedes.updlocation.core.axl;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.caixapenedes.updlocation.core.UpdLocation;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;


/**
 * @author dbea
 *
 */
public abstract class AXLRequest {
	private String axlUserName = null;
	private String axlPassword = null;
	private Integer axlPort = null;
	private String axlHost = null;
	private String ccmVersion = "6.0";
	private static DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	
	public AXLRequest ( String axlUserName, String axlPassword,
			String axlPort,	String axlHost ){
		super();
		this.axlUserName = axlUserName;
		this.axlPassword = axlPassword;
		this.axlPort     = Integer.parseInt( axlPort );
		this.axlHost     = axlHost;
	}
	


	public String sendRequest() {
		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

		
		String authorization = axlUserName + ":" + axlPassword;
//		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:" + axlHost + ":" + axlPort + "\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=" + ccmVersion  + "\"\r\n";
		sAXLSOAPRequest += "Content-length: ";
		
//		Build the SOAP payload
		sAXLRequest = getRequest();

//		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 = new MyTrustManager();
			TrustManager[] mytm = { xtm };
			SSLContext ctx = SSLContext.getInstance("SSL");
			ctx.init(null, mytm, null);
			
			SSLSocketFactory sslFact = ctx.getSocketFactory();
			
			socket = sslFact.createSocket( axlHost , axlPort );
			
			//socket = sslFact.createSocket( "http://" + axlHost + "/CCMApi/AXL/V1/soapisapi.dll", 80 );
			

			
			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();
			socket = null;
//			output the response to the standard output
			return sb.toString();
		} catch (Exception ea) {
			UpdLocation.logger.error("Error ", ea );
		}
		finally{
			try {
				if (socket != null)
					socket.close();
			} catch (Exception exc) {
				UpdLocation.logger.error("Error ", exc );
			}
		}
		return null;
	}


	protected abstract String getRequest();

	public String getAxlHost() {
		return axlHost;
	}



	public void setAxlHost(String axlHost) {
		this.axlHost = axlHost;
	}



	public String getAxlPassword() {
		return axlPassword;
	}



	public void setAxlPassword(String axlPassword) {
		this.axlPassword = axlPassword;
	}



	public Integer getAxlPort() {
		return axlPort;
	}



	public void setAxlPort(String axlPort) {
		this.axlPort = Integer.parseInt( axlPort );
	}



	public String getAxlUserName() {
		return axlUserName;
	}



	public void setAxlUserName(String axlUserName) {
		this.axlUserName = axlUserName;
	}



	public String getCcmVersion() {
		return ccmVersion;
	}



	public void setCcmVersion(String ccmVersion) {
		this.ccmVersion = ccmVersion;
	}
	
	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;
		}
	}
	
	public static String getAXLNodeValue( String response, String userId, String NodeName ) {
		int iniEnv =  response.indexOf( AXLManager.INIENV );
		if ( iniEnv != -1){
			String aux = response.substring( iniEnv,  response.length() );
			DocumentBuilder db;
			try {
				db = dbf.newDocumentBuilder();
				InputStream is = new ByteArrayInputStream(aux.getBytes());
				Document xmlDoc = db.parse(is);
				NodeList returnnl = xmlDoc.getElementsByTagName(NodeName);
				if (( returnnl != null )&&( returnnl.item(0) != null ))
					return returnnl.item(0).getFirstChild().getNodeValue().toString();
			} catch (Exception e) {
				UpdLocation.logger.error("Error ", e );
			}
		}
		return null;
	}
	
	public static String getAXLAttributeValue( String response, String userId, String nodeName, String attributeName ) {
		int iniEnv =  response.indexOf( AXLManager.INIENV );
		if ( iniEnv != -1){
			String aux = response.substring( iniEnv,  response.length() );
			DocumentBuilder db;
			try {
				db = dbf.newDocumentBuilder();
				InputStream is = new ByteArrayInputStream(aux.getBytes());
				Document xmlDoc = db.parse(is);
				NodeList returnnl = xmlDoc.getElementsByTagName( nodeName );
				if ( returnnl != null ){
					NamedNodeMap nodeMap = returnnl.item(0).getAttributes();
					aux = nodeMap.getNamedItem(attributeName).getNodeValue();
					return aux;
				}
			} catch (Exception e) {
				UpdLocation.logger.error("Error ", e );
			}
		}
		return null;
	}


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -