package cn.tisson.apms.eleInvoice.client;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.namespace.QName;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPFactory;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.SOAPHeaderElement;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import cn.tisson.apms.eleInvoice.entity.PreEleInvoice;
import cn.tisson.apms.eleInvoice.util.EsbData;
import cn.tisson.apms.eleInvoice.util.Param;
import net.sf.json.JSONObject;
/**
* Servlet implementation class EleInvoiceServlet
*/
public class EleInvoiceServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public EleInvoiceServlet() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//response.getWriter().append("Served at: ").append(request.getContextPath());\
System.out.println("doGet");
}
/* (non-Javadoc)
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("*****************doPost*********************");
request.setCharacterEncoding("UTF-8");
response.setContentType("application/json;charset=GBK");
try {
InputStream in=request.getInputStream();
BufferedReader inbf=new BufferedReader(new InputStreamReader(in));
StringBuffer msg = new StringBuffer("");
String temp;
while ((temp = inbf.readLine()) != null) {
msg.append(temp);
}
inbf.close();
String msgString = msg.toString();
System.out.println(msgString);
String sendData = "";
if(msgString != ""){
JSONObject jo = JSONObject.fromObject(msgString);
System.out.println("转换数据为:"+jo.get("operationType"));
PreEleInvoice eLeInvoice = new PreEleInvoice();
eLeInvoice.setOperationType(jo.getString("operationType"));
eLeInvoice.setPayID(jo.getString("payID"));
eLeInvoice.setJoinNum(jo.getString("joinNum"));
eLeInvoice.setStorageDate(jo.getString("storageDate"));
if(jo.getString("operationType").equals("2")){
System.out.println("开具发票");
eLeInvoice.setInvoiceValue(jo.getString("invoiceValue"));
eLeInvoice.setCustomerName(jo.getString("customerName"));
eLeInvoice.setPushPhoneNum(jo.getString("pushPhoneNum"));
eLeInvoice.setPushEmail(jo.getString("pushEmail"));
eLeInvoice.setCityIdentifying(jo.getString("cityIdentifying"));
}
sendData = getXml(eLeInvoice);
System.out.println(sendData);
}
String result = callWebService(sendData);
System.out.println("ESB返回:"+result);
Document document = DocumentHelper.parseText(result);
OutputStreamWriter out = new OutputStreamWriter(response.getOutputStream());
BufferedWriter bw = new BufferedWriter(out);
JSONObject json = new JSONObject();
String userName = "admin" ;
json.put("userName", userName);
bw.write("nothing but a alert");
bw.append(json.toString());
//bw.append("返回*********");
bw.flush();
bw.close();
}
catch (Exception e) {
// TODO: handle exception
}finally {
}
doGet(request, response);
}
//json2String
public static String createJsonString(String key, Object value)
{
JSONObject jsonObject = new JSONObject();
jsonObject.put(key, value);
return jsonObject.toString();
}
//调用webservice服务
public static String callWebService(String sendData){
String endPoint = "http://132.121.152.21:9002/ESBCBS/ProxySvc/ESBCBSWSSERV?wsdl";
String result ="";
try {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endPoint);
call.addHeader(setSoapHeader());
call.setOperation(setOperationDesc());
call.setEncodingStyle(null);
call.setProperty(org.apache.axis.client.Call.SEND_TYPE_ATTR,Boolean.FALSE);
call.setProperty(org.apache.axis.AxisEngine.PROP_DOMULTIREFS,Boolean.FALSE);
call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
call.setOperationName(new QName("mboss-esb", "call"));
Data data = new Data();
data.setSendData(sendData);
result = (String) call.invoke(new Object[]{data});
} catch (Exception e) {
System.out.println("call方法异常");
e.printStackTrace();
}
return result;
}
//设置soapheader
public static SOAPHeaderElement setSoapHeader() throws Exception {
SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement(
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
"Security");
soapHeaderElement.setActor(null);
soapHeaderElement.setPrefix("soap");
soapHeaderElement.setMustUnderstand(true);
soapHeaderElement.setPrefix("wsse");
soapHeaderElement.setNamespaceURI("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
SOAPElement UsernameToken = soapHeaderElement.addChildElement("UsernameToken");
UsernameToken.setAttribute("xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
UsernameToken.setPrefix("wsse");
Name Id = SOAPFactory.newInstance().createName("wsu:Id");
UsernameToken.addAttribute(Id, "unt_yt8oqZry2uqoLc3d");
Name Type = SOAPFactory.newInstance().createName("Type");
UsernameToken.addChildElement("Username").setValue("TESTDZDB");
UsernameToken.addChildElement("Password").addAttribute(Type,"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText").setValue("2a5f4092");
return soapHeaderElement;
}
public static org.apache.axis.description.OperationDesc setOperationDesc()
throws Exception {
org.apache.axis.description.OperationDesc oper;
oper = new org.apache.axis.description.OperationDesc();
oper.setName("call");
org.apache.axis.description.ParameterDesc param1 = new org.apache.axis.description.ParameterDesc(
new javax.xml.namespace.QName("mboss/esb", "data"),
org.apache.axis.description.ParameterDesc.IN,
new javax.xml.namespace.QName("mboss/esb", ">data"),
Data.class, false, false);
oper.addParameter(param1);
oper.setReturnType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
oper.setReturnClass(java.lang.String.class);
oper.setReturnQName(new javax.xml.namespace.QName("mboss/esb","recvData"));
oper.setStyle(org.apache.axis.constants.Style.WRAPPED);
oper.setUse(org.apache.axis.constants.Use.LITERAL);
return oper;
}
//打包webservice请求数据
public static String getXml(PreEleInvoice preEleInvoice) {
EsbData data = new EsbData("EXPREELECINV");
Param param = new Param("101");
param.addCol("1", "1093001", "操作类型", preEleInvoice.getOperationType());
param.addCol("2", "1093002", "缴费流水号", preEleInvoice.getPayID());
param.addCol("3", "1093003", "接入号码", preEleInvoice.getJoinNum());
param.addCol("6", "1093006", "存入日期", preEleInvoice.getStorageDate());
if(preEleInvoice.getOperationType().equals("2")){
param.addCol("4", "1093004", "发票金额", preEleInvoice.getInvoiceValue());
param.addCol("5", "1093005", "购方名称", preEleInvoice.getCustomerName());
param.addCol("7", "1093007", "推送手机号码", preEleInvoice.getPushPhoneNum());
param.addCol("8", "1093008", "推送电子邮箱", preEleInvoice.getPushEmail());
param.addCol("9", "1093009", "地市标识", preEleInvoice.getCityIdentifying());
}
param.endRow();
data.addParam(param);
return data.getXmlData();
}
public static void main(String[] args) {
System.out.println(new Date(System.currentTimeMillis()));
System.out.println(System.currentTimeMillis());
//1472715668552
System.out.println(new Date(System.currentTimeMillis()+60*1000));
// WARN [http-apr-8080-exec-8] org.apache.axis.utils.JavaUtils - Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
// ESB返回:BEA-382612请检查控制信息中FuncCode与TargetCode输入是否正确!( ESBCBS/BizSvc/CBS_GZ_ )
// doGet
}
}