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

📄 cpheaderhandler.java

📁 中国联通短信通信协议
💻 JAVA
字号:
package com.wireless.sms.unsoap.axis;

import java.util.Iterator;

import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;

import org.apache.axis.AxisFault;
import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.commons.configuration.DOM4JConfiguration;

import com.wireless.sms.gwif.SmsGWIF;
import com.wireless.sms.gwif.smsagent.global.LoggerConstant;
import com.wireless.sms.unsoap.SMSSender;

public class CPHeaderHandler extends BasicHandler {
	static final long serialVersionUID = 1L;
	private static boolean hasInitFlag = false;

    //NotifySOAPHeader
	public static String spRevId = "";//SP反向调用名,可选 
	public static String spRevpassword = "";//SP反向调用密码,可选 
	public static String spId = "";//SP
	public static String san = "";//分析判断应该就是文档中的SPNumber
	public static String serviceId = "";//业务ID
	public static String transactionId = "";//业务流水号,用于组合业务。可选
	public static String linkId = "";//事务关联ID,用于点播业务的事务关联,由平台产生。格式为:MMDDHHMMSS+10

	// 在执行 service 之前,会被处理的 handler
	// invoke() 会被自动呼叫,而且 SOAP 信息可以经由 msgContext 取得
	public void invoke(MessageContext msgContext) throws AxisFault {
//		initSystemOnce(msgContext);
		System.out.println("CPHeaderHandler...");
		boolean processedHeader = false;
		try {
			// 取得 Request 的 SOAP 信息
			Message msg = msgContext.getRequestMessage();
			SOAPEnvelope envelope = msg.getSOAPEnvelope();
			SOAPHeader header = envelope.getHeader();
			Iterator it = header.examineAllHeaderElements();
			SOAPHeaderElement hel;

			while (it.hasNext()) {
				hel = (SOAPHeaderElement) it.next();
				String headerName = hel.getNodeName();
//				System.out.println("headerName = "+headerName);
				if (headerName.equals("cuc:NotifySOAPHeader")) {
					// 对于 mustUnderstand 设为 true 的 Header,必须
					// 利用下列的方式把它设为"已经处理了",否则 service
					// 会回传 Did not understand "MustUnderstand"
					hel.setProcessed(true);
					parseHeader(hel);
					processedHeader = true;
				}
			}
		} catch (SOAPException e) {
			throw new AxisFault("无法处理 SOAP Header.", e);
		}

		if (!processedHeader)
			throw new AxisFault("接收 SOAP Header 失败");
//		writeLog();
	}

	/**第一次访问请求要进行初始化*/
	protected void initSystemOnce(MessageContext msgContext) {
		if(hasInitFlag)
			return;
		String config_fileName = (String)this.getOption("config");
		String log4j_fileName = (String)this.getOption("log4j");
		String path = (String)this.getOption("path");
		System.out.println("config_fileName = " + config_fileName + "\n" +
				"log4j_fileName = " + log4j_fileName + "\n" +
						"path = " + path);
		
		if(config_fileName == null || log4j_fileName == null)
			throw new Error("配置文件没有找到参数config或者log4j",
					new Throwable("配置参数config或者log4j在server-config.wsdd文件对应的服务中没有设置,检查此项!"));
		
		java.io.File config_file;
		java.io.File log4j_file;
		//config_file
		if(config_fileName.startsWith(path))
			config_file = new java.io.File(config_fileName);
		else
			config_file = new java.io.File(path + config_fileName);
		//log4j_file
		if(log4j_fileName.startsWith(path))
			log4j_file = new java.io.File(log4j_fileName);
		else
			log4j_file = new java.io.File(path + log4j_fileName);
		//confirm
		if(!config_file.exists() || !log4j_file.exists())
			throw new Error("配置文件config=" + config_file +
					"或者log4j=" + log4j_file +
					"没有找到",
					new Throwable("配置文件config或者log4j在服务中配置的路径下没有找到该文件,检查此项!"));
		//初始化log4j配置
		try {
			LoggerConstant.init(log4j_file.getPath().replaceAll("\\\\", "/"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		//初始化全局配置文件config.xml
		try {
			com.wireless.sms.unsoap.SMSReceiver.configuration = new DOM4JConfiguration(config_file);
			com.wireless.sms.unsoap.SMSReceiver.initConstant(path);
			
		    new SmsGWIF().start();
		    //启动mt发送
		    SMSSender.getInstance().start();
		} catch (Exception e) {
			e.printStackTrace();
		}
		hasInitFlag = true;
	}

	protected void parseHeader(SOAPHeaderElement hel) throws AxisFault {
		org.dom4j.Element notifySOAPHeader;
		try {
//			System.out.println("Header.getAsString = "+hel.getAsString());
			org.dom4j.Document doc = new org.dom4j.io.SAXReader().read(
					new java.io.StringBufferInputStream(hel.getAsString()));
			notifySOAPHeader = doc.getRootElement();
		} catch (Exception e1) {
			e1.printStackTrace();
			return;
		}
		if(notifySOAPHeader == null)
			return;
//		parse soapHeader
		spRevId = notifySOAPHeader.elementTextTrim("spRevId");
		spRevpassword = notifySOAPHeader.elementTextTrim("spRevpassword");
		spId = notifySOAPHeader.elementTextTrim("spId");
		san = notifySOAPHeader.elementTextTrim("SAN");
		serviceId = notifySOAPHeader.elementTextTrim("serviceId");
		transactionId = notifySOAPHeader.elementTextTrim("transactionId");
		linkId = notifySOAPHeader.elementTextTrim("linkId");	
	}
	
	public static void writeLog(){
		System.out.println("spRevId = "+spRevId);
		System.out.println("spRevpassword = "+spRevpassword);
		System.out.println("spId = "+spId);
		System.out.println("san = "+san);
		System.out.println("serviceId = "+serviceId);
		System.out.println("transactionId = "+transactionId);
		System.out.println("linkId = "+linkId);
	}
	
	public static void main(String[] args){
		String fileName = "D:\\Program Files\\Apache Software Foundation/Tomcat 4.1/webapps/axis/WEB-INF/sdun8009/log4j.xml";
		java.io.File file = new java.io.File(fileName);
		if(file.exists()){
			System.out.println(file.getAbsolutePath()+ "\n" +
					"" + file.getName() + "\n" +
							"" + file.getPath() + "\n" +
									"replaceAll : " + fileName.replaceAll("\\\\", "/"));
		}
			
	}
}


⌨️ 快捷键说明

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