ClientWebServiceImpl

/***********************************************************************
Copyright (c) 2007-2010, AgileSC,Inc.China
All rights reserved.
************************************************************************/
package com.asc.das.webservice.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.sql.Clob;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.codehaus.xfire.transport.http.XFireServletController;
import org.codehaus.xfire.util.Base64;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.hibernate.Hibernate;


import com.asc.dg.DataGatherManager;
import com.asc.dg.frame.sys.IFileManager;

import com.asc.das.orgmanager.bean.BaseDataProp;
import com.asc.das.orgmanager.bean.BaseDataType;
import com.asc.das.orgmanager.bean.BaseOrg;
import com.asc.das.orgmanager.bean.BaseOrgParam;
import com.asc.das.orgmanager.dao.BaseDataPropDao;
import com.asc.das.orgmanager.dao.BaseDataTypeDao;
import com.asc.das.orgmanager.dao.BaseOrgDao;
import com.asc.das.orgmanager.dao.BaseOrgParamDao;
import com.asc.das.sysmanager.bean.SysClientMonitor;
import com.asc.das.sysmanager.bean.SysSoftUpdateInfo;
import com.asc.das.sysmanager.bean.SysSoftVersion;
import com.asc.das.sysmanager.bean.SysUploadrec;
import com.asc.das.sysmanager.dao.SysClientMonitorDao;
import com.asc.das.sysmanager.dao.SysSoftUpdateInfoDao;
import com.asc.das.sysmanager.dao.SysSoftVersionDao;
import com.asc.das.sysmanager.dao.SysUploadrecDao;
import com.asc.das.sysmanager.service.MDGLoginService;
import com.asc.das.sysmanager.service.SoftUpgradeService;
import com.asc.das.webservice.IClientWebService;
import com.asc.das.webservice.MsgInfo;
import com.asc.sys.sysparam.dao.SysParamDao;
import com.asc.sys.user.bean.SysUser;

/**
* 对客户端的WebService接口
*
* @author Kevin A125
* @version v1.0.0 创建时间 2010-2-3 上午10:24:00
*/
public class ClientWebServiceImpl implements IClientWebService {

private static String LOGIN_FAIL_MSG = "login failed.";
private MDGLoginService MDGLoginService;
private SysUser currentUser;
private SysUploadrecDao sysUploadrecaDao;
private DataGatherManager dgManager;
private BaseOrgParamDao baseOrgParamDao;
private BaseDataTypeDao baseDataTypeDao;
private SysSoftUpdateInfoDao sysSoftUpdateInfoDao;
private SysSoftVersionDao softWareVersionDao;
private SoftUpgradeService softUpgradeService;
private SysParamDao sysParamDao;
private SysClientMonitorDao sysClientMonitorDao;

public void setMDGLoginService(MDGLoginService loginService) {
MDGLoginService = loginService;
}

public void setSysSoftUpdateInfoDao(
SysSoftUpdateInfoDao sysSoftUpdateInfoDao) {
this.sysSoftUpdateInfoDao = sysSoftUpdateInfoDao;
}

public void setSysUploadrecaDao(SysUploadrecDao sysUploadrecaDao) {
this.sysUploadrecaDao = sysUploadrecaDao;
}

public void setDgManager(DataGatherManager dgManager) {
this.dgManager = dgManager;
}

public void setBaseOrgParamDao(BaseOrgParamDao baseOrgParamDao) {
this.baseOrgParamDao = baseOrgParamDao;
}

public void setBaseDataTypeDao(BaseDataTypeDao baseDataTypeDao) {
this.baseDataTypeDao = baseDataTypeDao;
}

public void setSoftWareVersionDao(SysSoftVersionDao softWareVersionDao) {
this.softWareVersionDao = softWareVersionDao;
}

public void setSoftUpgradeService(SoftUpgradeService softUpgradeService) {
this.softUpgradeService = softUpgradeService;
}

public void setSysParamDao(SysParamDao sysParamDao) {
this.sysParamDao = sysParamDao;
}

public void setSysClientMonitorDao(SysClientMonitorDao sysClientMonitorDao) {
this.sysClientMonitorDao = sysClientMonitorDao;
}

public String callFunction(String params) {

MsgInfo inputMsg = null;
try {
inputMsg = new MsgInfo(params);
String username = inputMsg.getUserName();
String password = inputMsg.getPassword();

// 先登录
if (!login(username, password)) {

return failMsg(LOGIN_FAIL_MSG).toString();
}

// 判别功能,执行
if (IClientWebService.FUNC_LOGIN.equals(inputMsg.getFunction())) {

return doLogin(inputMsg).toString();
} else if (IClientWebService.FUNC_GET_PARAM.equals(inputMsg
.getFunction())) {

return doGetParam(inputMsg).toString();
} else if (IClientWebService.FUNC_GET_DATATYPEINFO.equals(inputMsg
.getFunction())) {

return doGetDataTypeInfo(inputMsg).toString();
} else if (IClientWebService.FUNC_UPLOAD_FILE.equals(inputMsg
.getFunction())) {

return doUploadFile(inputMsg).toString();
} else if (IClientWebService.FUNC_UPLOAD_NOTIFY.equals(inputMsg
.getFunction())) {

return doUploadNotify(inputMsg).toString();
} else if (IClientWebService.FUNC_UPLOAD_CONFIG.equals(inputMsg
.getFunction())) {

return doSetParam(inputMsg).toString();
} else if (IClientWebService.FUNC_LISTEN_CLIENT.equals(inputMsg
.getFunction())) {

return doGetClient(inputMsg).toString();
} else if (IClientWebService.FUNC_UPGRADE_FINISH.equals(inputMsg
.getFunction())) {

return doUpgradeFinish(inputMsg).toString();
} else {

return failMsg("the called function not exist.").toString();
}
}
catch (Exception e) {
inputMsg = null;
e.printStackTrace();
}
finally{
inputMsg = null;
}
return null;
}

/**
* 1,监听客户端是否还在运行,和客户端现在是不是需要升级 2,错误日志的上传
*
* @param inputMsg
* @return
*/
private MsgInfo doGetClient(MsgInfo inputMsg) {

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
MsgInfo outMsg = new MsgInfo("");
Date date = new Date();
String version = "";
List<String[]> paramList = inputMsg.getParamList();
for (int i = 0; i < paramList.size(); i++) {
String[] tempArray = paramList.get(i);
if ("version".equals(tempArray[0])) {
version = tempArray[1];
}

}

//记录客户端监控消息
try{
SysClientMonitor scm = new SysClientMonitor();
scm.setBaseOrg(this.currentUser.getBaseOrg());
scm.setSysUser(this.currentUser);
scm.setMonitorTime(new Date());
sysClientMonitorDao.save(scm);
}catch (Exception e){
e.printStackTrace();
}

//客户端监控
String address = XFireServletController.getRequest().getHeader("X-Forwarded-For");
if (address == null || isIpAddress(address)) {
address = XFireServletController.getRequest().getRemoteAddr();
}

BaseOrgParam orgParam = baseOrgParamDao.get(this.currentUser.getBaseOrg().getOrgId());
orgParam.setLastrefreshtime(date);
orgParam.setClientIpAddr(address);
orgParam.setClientVersion(version);
baseOrgParamDao.update(orgParam);

//获取升级信息
try {
SysSoftVersion sysSoftVersion =softUpgradeService.upgradeSofrVersion(orgParam.getOrgId(), version);
if(null!=sysSoftVersion){
String softFtp=sysParamDao.getSysUpgradeSoftFtp();
String fTPaddress="";
String username="";
String password="";
if(null!=softFtp&&!("".equals(softFtp))){
String []arrayTemp=softFtp.split(";");

if(null!=arrayTemp&&!("".equals(arrayTemp))){
String temp="";

for(int i=0;i<arrayTemp.length;i++){
temp=arrayTemp[i];
if(null!=temp&&!("".equals(temp))){
if(temp.startsWith("fTPaddress=")){
fTPaddress=temp.split("fTPaddress=")[1];
}
if(temp.startsWith("username=")){
username=temp.split("username=")[1];
}
if(temp.startsWith("password=")){
password=temp.split("password=")[1];
}
}

}
}
}
List<String[]> outparamList = new ArrayList<String[]>();
outparamList.add(new String[] { "FTPaddress", fTPaddress});
outparamList.add(new String[] { "username", username});
outparamList.add(new String[] { "password", password });
outMsg.setOtherReturnParam(outparamList);
outMsg.setResult(true);
}else{
outMsg.setResult(false);
return outMsg;
}
} catch (Exception e) {

e.printStackTrace();
return failMsg("get upgradeinfo fail.");
}
return outMsg;
}

/**
* 进行系统登录,每个服务都要先登录,才能使用
*
* @param username
* @param password
* @return
*/
private boolean login(String username, String password) {

SysUser user = MDGLoginService.checkAuthUser(username, password);
if (user != null) {
this.currentUser = user;
return true;
}
return false;
}

/**
* 返回失败消息
*
* @return
*/
private MsgInfo failMsg(String sMsg) {

MsgInfo msg = new MsgInfo("");
msg.setResult(false);
msg.setErrMsg(sMsg);
return msg;
}

private MsgInfo succSimpleMsg() {

MsgInfo msg = new MsgInfo("");
msg.setResult(true);
return msg;
}

/**
* 系统登录服务
*
* @param msg
* 消息对象
* @return
*/
private MsgInfo doLogin(MsgInfo msg) {

MsgInfo outMsg = new MsgInfo("");
outMsg.setResult(true);
List<String[]> paramList = new ArrayList<String[]>();
paramList.add(new String[] { "orgcode",
this.currentUser.getBaseOrg().getAscCode() });
paramList.add(new String[] { "orgname",
this.currentUser.getBaseOrg().getOrgName() });
outMsg.setOtherReturnParam(paramList);
return outMsg;
}

/**
* 获取系统配置服务
*
* @param msg
* @return
*/
private MsgInfo doGetParam(MsgInfo msg) {

MsgInfo outMsg = new MsgInfo("");


String dgParam = "";
try {

BaseOrgParam param = baseOrgParamDao.get(this.currentUser.getBaseOrg().getOrgId());
//在客户端未设置一对多时,默认使用配置文件中的第一个作为配置
// List<BaseCollectParam> collectParams = param.getParams();
// collectParams.get(0).getDgParams();
Clob clob = param.getDgParams();
dgParam = clob.getSubString(1, (int) clob.length());
} catch (Exception e) {
e.printStackTrace();
return failMsg("get param from database fail.");
}
if(dgParam == null){
dgParam="";
}
Document doc = null;
try {
doc = DocumentHelper.parseText(dgParam);
doc.setXMLEncoding("GBK");
} catch (Exception e) {

e.printStackTrace();
return failMsg("set xml encoding fail.");
}

// outMsg.setResult(true);
// outMsg.setData(new String(Base64.decode(dgParam)));
//System.out.println(doc.asXML());
outMsg.sMsg.append(doc.asXML());
return outMsg;
}

/**
* 设置系统配置
*
* @param msg
* @return
*/
private MsgInfo doSetParam(MsgInfo msg) {

MsgInfo outMsg = new MsgInfo("");
String data = null;
try {

List<String[]> paramList = msg.getParamList();
for (int i = 0; i < paramList.size(); i++) {

String[] tempArray = paramList.get(i);
if ("data".equals(tempArray[0])) {
data = new String(Base64.decode(tempArray[1]));
}
}
} catch (Exception e) {

e.printStackTrace();
return failMsg("parse uploadconfig pamameter fail.");
}

BaseOrgParam param = baseOrgParamDao.get(this.currentUser.getBaseOrg().getOrgId());

Clob xmlParam = Hibernate.createClob(data);

//在客户端未设置一对多时,默认使用配置文件中的第一个作为配置
// BaseCollectParam collectParam = param.getParams().get(0);
// collectParam.setDgParams(xmlParam);
param.setDgParams(xmlParam);
baseOrgParamDao.saveOrUpdate(param);

return outMsg;
}

/**
* 获取数据映射配置服务
*
* @param msg
* @return
*/
private MsgInfo doGetDataTypeInfo(MsgInfo msg) {

MsgInfo outMsg = new MsgInfo("");

Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("datatypes");

String sql = " select to_char(c.ct_id), to_char(a.datatype_id), a.datatype_name, a.datatype_code, b.prefix,"
+ " to_char(d.prop_id), d.prop_name, d.prop_code, to_char(d.isrequred), to_char(d.key_prop), to_char(d.timestamp_prop)"
+ " from base_datatype a, base_org b, base_collect_param c, base_dataprop d"
+ " where a.org_id = b.org_id and b.org_id = c.ct_id and a.datatype_id = d.datatype_id "
+ " and c.org_id = " + this.currentUser.getBaseOrg().getOrgId()
+ " order by c.ct_id, a.datatype_id, d.prop_id";

List<Object[]> list = baseDataTypeDao.findBySql(sql);
if (list != null && list.size() > 0) {

Object[] ObjArr = null;
Element eDataType = null;
Element ePropsType = null;
String currDataTypeId = "-1";
String lastDataTypeId = "-1";
String idProp = "";
String timeProp = "";
for(int i = 0; i < list.size(); i++){

ObjArr = list.get(i);
currDataTypeId = (String)ObjArr[1];

if(!currDataTypeId.equals(lastDataTypeId)){

if(!"-1".equals(lastDataTypeId)){
Element eId = eDataType.addElement("idprop");
eId.setText(idProp);
Element eStamp = eDataType.addElement("timestampprop");
eStamp.setText(timeProp);
}

eDataType = root.addElement("datatype");
eDataType.addAttribute("name", (String)ObjArr[3]);
eDataType.addAttribute("displayname", (String)ObjArr[2]);
eDataType.addAttribute("code", (String)ObjArr[4]);

ePropsType = eDataType.addElement("props");
}

Element ePropType = null;

ePropType = ePropsType.addElement("prop");
ePropType.addAttribute("name", (String)ObjArr[7]);
ePropType.addAttribute("displayname", (String)ObjArr[6]);
if("1".equals((String)ObjArr[8])){

ePropType.addAttribute("required", "true");
}

if("1".equals((String)ObjArr[9])){

idProp = (String)ObjArr[7];
}

if("1".equals((String)ObjArr[10])){

timeProp = (String)ObjArr[7];
}

lastDataTypeId = currDataTypeId;
}

Element eId = eDataType.addElement("idprop");
eId.setText(idProp);
Element eStamp = eDataType.addElement("timestampprop");
eStamp.setText(timeProp);
}
doc.setXMLEncoding("GBK");
// outMsg.setResult(true);
// outMsg.setData(doc.asXML());
//System.out.println(doc.asXML());
outMsg.sMsg.append(doc.asXML());
return outMsg;
}

/**
* 上传文件服务
*
* @param msg
* @return
*/
private MsgInfo doUploadFile(MsgInfo msg) {

String data = null;
String filename = null;
String filesize = null;
String iscrypt = null;
String iszip = null;
String appname = null;
String orgcode = null;
String md5 = null;

try {

List<String[]> paramList = msg.getParamList();
for (int i = 0; i < paramList.size(); i++) {

String[] tempArray = paramList.get(i);
if ("data".equals(tempArray[0])) {
data = tempArray[1];
}
if ("filename".equals(tempArray[0])) {
filename = tempArray[1];
}
if ("filesize".equals(tempArray[0])) {
filesize = tempArray[1];
}
if ("iscrypt".equals(tempArray[0])) {
iscrypt = tempArray[1];
}
if ("iszip".equals(tempArray[0])) {
iszip = tempArray[1];
}
if ("appname".equals(tempArray[0])) {
appname = tempArray[1];
}
if ("orgcode".equals(tempArray[0])) {
orgcode = tempArray[1];
}
if ("md5".equals(tempArray[0])) {
md5 = tempArray[1];
}
}
} catch (Exception e) {

e.printStackTrace();
return failMsg("parse uploadfile pamameter fail.");
}

// 判断文件大小是否正确
if (filesize != null && !"".equals(filesize)) {

if (Integer.parseInt(filesize) != Base64.decode(data).length) {

return failMsg("the size of file is different from the parameter filesize");
}
}

long orgid = this.currentUser.getBaseOrg().getOrgId();

byte[] unzipTemp = null;
byte[] decryptTemp = null;
IFileManager fileManager = dgManager.getFileManager();
try {

// 解密
if (iscrypt.equals(IClientWebService.ENCRYPT)) {
SecretKeySpec skeySpec = new SecretKeySpec("AgilesClientLons"
.getBytes(), "AES");
// System.out.println(skeySpec.getEncoded().length);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
decryptTemp = cipher.doFinal(Base64.decode(data));
} else {
decryptTemp = Base64.decode(data);
}

// 解压
if (iszip.equals(IClientWebService.ZIP)) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipInputStream zis = new ZipInputStream(
new ByteArrayInputStream(decryptTemp));
ZipEntry zip = zis.getNextEntry();
byte[] buff = new byte[1024];
String name = null;
while (zip != null) {
name = zip.getName();
int len = 0;
while ((len = zis.read(buff)) != -1) {
baos.write(buff, 0, len);
}
zis.read(buff, 0, zis.available());
fileManager.addWebServiceData(orgid, name, baos
.toByteArray(), appname, true);
zip = zis.getNextEntry();
}
} else {
unzipTemp = decryptTemp;
fileManager.addWebServiceData(orgid, filename, unzipTemp,
appname, true);
}
} catch (Exception e) {
e.printStackTrace();
return failMsg("upload file fail.");
}
return succSimpleMsg();
}

/**
* 上传日志服务
*
* @param msg
* @return
* @throws ParseException
*/
private MsgInfo doUploadNotify(MsgInfo msg) {

String action = null;
String actionresult = null;
String filename = null;
String md5 = null;
String uploadtype = null;
String localtime = null;
String filesize = null;

SimpleDateFormat Formator = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
List<SysUploadrec> recList = null;
SysUploadrec rec = null;
BaseOrg baseOrg = null;
try {

List<String[]> paramList = msg.getParamList();
for (int i = 0; i < paramList.size(); i++) {

String[] tempArray = paramList.get(i);
if ("action".equals(tempArray[0])) {
action = tempArray[1];
}
if ("actionresult".equals(tempArray[0])) {
actionresult = tempArray[1];
}
if ("filename".equals(tempArray[0])) {
filename = tempArray[1];
}
if ("md5".equals(tempArray[0])) {
md5 = tempArray[1];
}
if ("uploadtype".equals(tempArray[0])) {
uploadtype = tempArray[1];
}
if ("localtime".equals(tempArray[0])) {
localtime = tempArray[1];
}
if ("filesize".equals(tempArray[0])) {
filesize = tempArray[1];
}
}
} catch (Exception e) {

e.printStackTrace();
return failMsg("parse uploadnotify pamameter fail.");
}

try {

if (IClientWebService.ACTION_BEGIN.equals(action)) {

rec = new SysUploadrec();
rec.setSellOrg(this.currentUser.getBaseOrg());
rec.setSysUser(this.currentUser);
rec.setUploaddate(new Date());
rec.setFilename(filename);
rec.setChannel(uploadtype);
rec.setMd5(md5);
rec.setBegintime(new Date());
rec.setLocalbegintime(Formator.parse(localtime));
rec.setUploadstate(Long.parseLong(actionresult));
rec.setUploadtimes(0L);
sysUploadrecaDao.save(rec);
} else {

recList = sysUploadrecaDao.find(
"from SysUploadrec o where o.sellOrg = ? and o.sysUser=? and o.md5 = ? order by o.uploaddate desc",
new Object[] { this.currentUser.getBaseOrg(), this.currentUser, md5 });

if (recList != null && recList.size() > 0) {
rec = recList.get(0);
rec.setFilesize(Long.parseLong(filesize));
rec.setLocalendtime(Formator.parse(localtime));
rec.setUploadstate(Long.parseLong(actionresult));
if(IClientWebService.ACTION_RETRY.endsWith(action)){
rec.setUploadtimes(rec.getUploadtimes() + 1);
}
if (IClientWebService.ACTION_END.endsWith(action)) {
rec.setEndtime(new Date());
}
sysUploadrecaDao.update(rec);
}
}
} catch (Exception e) {
e.printStackTrace();
return failMsg("Add upload log to database fail.");
}
return succSimpleMsg();
}

/**
* 升级状态通知
*
* @param msg
* @return
* @throws ParseException
*/
private MsgInfo doUpgradeFinish(MsgInfo msg) {

String success = null;

try {

List<String[]> paramList = msg.getParamList();
for (int i = 0; i < paramList.size(); i++) {

String[] tempArray = paramList.get(i);
if ("success".equals(tempArray[0])) {
success = tempArray[1];
}
}
} catch (Exception e) {

e.printStackTrace();
return failMsg("parse upgradefinish pamameter fail.");
}

try{
SysSoftVersion softVersion = softWareVersionDao.getNewVersion();
if(softVersion != null){
SysSoftUpdateInfo sysSoftUpdateInfo = sysSoftUpdateInfoDao.getByVersionAndOrg(
this.currentUser.getBaseOrg().getOrgId(), softVersion.getId());
if(null!=sysSoftUpdateInfo){
if(RESULT_SUCCESS.equals(success)){
sysSoftUpdateInfo.setUpgradeSuccTime(new Date());
sysSoftUpdateInfo.setStatus(2L);
sysSoftUpdateInfoDao.update(sysSoftUpdateInfo);
BaseOrgParam orgParam = baseOrgParamDao.get(this.currentUser.getBaseOrg().getOrgId());
orgParam.setClientVersion(softVersion.getVersion());
baseOrgParamDao.update(orgParam);
}
sysSoftUpdateInfoDao.update(sysSoftUpdateInfo);
}
}

} catch (Exception e) {

e.printStackTrace();
return failMsg("notify upgradefinish fail.");
}
return succSimpleMsg();
}

public static Boolean isIpAddress(String s){
String regex = "(((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
return m.matches();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值