webserve

package Freesky.PCM.Common.Soap.WS;

import java.io.File;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.xerces.xni.QName;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import Freesky.PCM.Common.Soap.Interface.IImageBP;
import Freesky.PCM.Common.Soap.Interface.IImageWS;

public class ImageWS implements IImageWS {

private IImageBP iImageBP;

public IImageBP getiImageBP() {
return iImageBP;
}

public void setiImageBP(IImageBP iImageBP) {
this.iImageBP = iImageBP;
}

public String IfService(String ifUser, String ifPass, String ifCode,
String ifEvent, String info) throws Exception {
List<String> list = null;
Map<String , String> map=null;
Map<String, String> maps = new HashMap<String, String>();
/*if (ifUser.equalsIgnoreCase("PCMUSER")) {
maps.put("flag", "1");
maps.put("info", "成功");
if (ifPass.equalsIgnoreCase("USERPCM")) {
maps.put("flag", "1");
maps.put("info", "成功");
if (ifCode.equalsIgnoreCase("SXPCM")) {
maps.put("flag", "1");
maps.put("info", "成功");
if (ifEvent.equalsIgnoreCase("UPDATESTATE")) {
maps.put("flag", "1");
maps.put("info", "成功");

} else {
maps.put("flag", "0");
maps.put("info", "接口标识错误");
}
} else {
maps.put("flag", "0");
maps.put("info", "系统标示符错误");
}
} else {
maps.put("flag", "0");
maps.put("info", "密码错误");
}
} */
if(ifCode.equalsIgnoreCase("IF_ImageState")){
maps.put("flag", "1");
maps.put("info", "成功");
}else {
maps.put("flag", "0");
maps.put("info", "接口标识有误");
}
if (maps.get("flag").equalsIgnoreCase("1")) {
map = parseXMLS(info);
maps = iImageBP.changePCMState(map);
}
return createXMLS(maps);
}

private List<String> parseXML(String info) throws Exception {
List<String> list = new ArrayList<String>();
String pcm_code = "";
Document document = DocumentHelper.parseText(info);
Element root = document.getRootElement();
Element result = root.element("images");
Iterator iter = result.elementIterator("image");
while (iter.hasNext()) {
Element image = (Element) iter.next();
pcm_code = image.element("pcm_code").getText().trim();
list.add(pcm_code);
}
return list;
}

private Map<String , String> parseXMLS(String info) throws Exception {
List<String> list = new ArrayList<String>();
Map<String , String> map=new HashMap<String, String>();
String pcm_code = "";
String bar_detail = "";
Document document = DocumentHelper.parseText(info);
Element root = document.getRootElement();
Element result = root.element("images");
Iterator iter = result.elementIterator("image");
while (iter.hasNext()) {
Element image = (Element) iter.next();
pcm_code = image.element("bar_code").getText().trim();
bar_detail=image.element("bar_detail").getText().trim();
// list.add(pcm_code);
map.put(pcm_code, bar_detail);
}
return map;
}

private String createXML(Map<String, String> map) throws Exception {
Document document = DocumentHelper.createDocument();
document.setXMLEncoding("GBK");
Element root = document.addElement("ifResponse");
Element ifResult = root.addElement("ifResult");
if (map.get("flag").equalsIgnoreCase("1")) {
Element flag = root.addElement("flag");
flag.setText(map.get("flag"));
flag.addAttribute("Note", "标志");
Element info = root.addElement("info");
info.setText(map.get("info"));
info.addAttribute("Note", "信息");
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
String key = iter.next().toString();
if (map.get(key).equalsIgnoreCase("0")
|| map.get(key).equalsIgnoreCase("1")
&& !key.equalsIgnoreCase("flag")) {
Element result = ifResult.addElement("result");
result.addAttribute("Note", "结果");
Element pcm_result = result.addElement("pcm_result");
pcm_result.setText(map.get(key));
pcm_result.addAttribute("Note", "接口处理结果");
Element pcm_code = result.addElement("pcm_code");
pcm_code.setText(key);
pcm_code.addAttribute("Note", "报账单号");
Element pcm_time = result.addElement("pcm_time");
pcm_time.setText(new Date().toLocaleString());
pcm_time.addAttribute("Note", "接收时间");
}
}

} else {
Element flag = root.addElement("flag");
flag.setText(map.get("flag"));
flag.addAttribute("Note", "标志");
Element info = root.addElement("info");
info.setText(map.get("info"));
info.addAttribute("Note", "信息");
}
return document.asXML();
}

private String createXMLS(Map<String, String> map) throws Exception {
Document document = DocumentHelper.createDocument();
document.setXMLEncoding("UTF-8");
Element root = document.addElement("ifResponse");
Element returnInfos = root.addElement("returnInfos");
if (map.get("flag").equalsIgnoreCase("3")||map.get("flag").equalsIgnoreCase("0")) {
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
String key = iter.next().toString().trim();
if (map.get(key).equalsIgnoreCase("0")
|| map.get(key).equalsIgnoreCase("1")) {
Element result = returnInfos.addElement("result");
// result.addAttribute("Note", "结果");
Element ifResult = result.addElement("ifResult");
ifResult.setText(map.get(key));
// ifResult.addAttribute("Note", "接口处理结果");
Element infoId = result.addElement("infoId");
infoId.setText(map.get(key+"bar_detail"));
// infoId.addAttribute("Note", "发送记录Id");
Element receive_user = result.addElement("receive_user");
receive_user.setText("PCMUSER");
// receive_user.addAttribute("Note", "接收人");
Element receive_time = result.addElement("receive_time");
receive_time.setText(new Date().toLocaleString());
// receive_time.addAttribute("Note", "接收时间");
}
}

} else {
Element result = returnInfos.addElement("result");
// result.addAttribute("Note", "结果");
Element ifResult = result.addElement("info");
ifResult.setText("21");
// ifResult.addAttribute("Note", "接口处理结果");
Element infoId = result.addElement("info");
infoId.setText("");
// infoId.addAttribute("Note", "发送记录Id");
Element receive_user = result.addElement("info");
receive_user.setText("");
// receive_user.addAttribute("Note", "接收人");
Element receive_time = result.addElement("info");
receive_time.setText("");
// receive_time.addAttribute("Note", "接收时间");
}
System.out.println("outout  "+document.asXML());
return document.asXML();
}

public static void main(String args[]) {
SAXReader reader = new SAXReader();
ImageWS ws = null;
Map<String, String> map = null;
Document document = null;
try {
document = reader.read(new File("c:\\images.xml"));
System.out.println(document.asXML());
// ws = new ImageWS();
// ws.parseXMLS(document.asXML());
System.out.println(new Date().toLocaleString());
// map = new HashMap<String, String>();
// map.put("123123123", "1");
// map.put("123123122", "1");
// map.put("123123121", "1");
// map.put("flag", "1");
// map.put("info", "信息");
// System.out.println(ws.createXML(map));
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String url = "http://169.254.8.81:8080/pcm/services/imageWS";
Service ser = new Service();
try {
Call call = (Call) ser.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName(new javax.xml.namespace.QName(url,
"IfService"));
String result = (String) call.invoke(new Object[] { "PCMUSER",
"USERPCM", "IF_ImageState", "IF_ImageState", document.asXML() });
System.out.println(result);
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

你可能感兴趣的:(apache,xml,SOAP)