package com.chinamobile.datacollector.service;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.GZIPInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;
import com.chinamobile.datacollector.entity.LogInfoEntity;
import com.chinamobile.datacollector.sys.SysInit;
import com.chinamobile.datacollector.util.TransferLogInfoToLog;
import com.cmreadcommon.model.LogEntity;
import com.google.gson.Gson;
/**
* @ClassName: InteractServiceImpl
* @Description: 处理交互请求相关业务
* @author liuyao
* @date 2015年3月4日 下午3:42:54
*/
@Service
public class InteractServiceImpl implements InteractService {
static Logger logger = LogManager.getLogger(InteractServiceImpl.class.getName());
/** gzip流每次读取的缓冲区大小 */
private static final int BUFFER_SIZE = 16384;
/* (非 Javadoc)
* <p>Title: getLogInfoList</p>
* <p>Description: </p>
* @author liuyao
* @param upload
* @return
* @throws Exception
* @see com.chinamobile.datacollector.service.InteractService#getLogInfoList(java.io.File)
*/
@Override
public List<LogInfoEntity> getLogInfoList(File upload) throws Exception{
/**本地存储上传文件的路径*/
String fileName = System.currentTimeMillis()+".txt";
String filePath = ServletActionContext.getServletContext().getRealPath(File.separator)+"LogUploadFolder" + File.separator;
File path = new File(ServletActionContext.getServletContext().getRealPath(File.separator)+"LogUploadFolder" + File.separator);
if (!path.exists()) {//判断文件夹是否存在,不存在则创建
path.mkdirs();
}
File file = new File(path,fileName);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
/*
* 解压上传的压缩文件,并写入本地文件中
*/
try {
GZIPInputStream gzipIs = new GZIPInputStream(new FileInputStream(upload));
int count = 0;
byte data[] = new byte[BUFFER_SIZE];
while ((count = gzipIs.read(data, 0, BUFFER_SIZE)) != -1) {
fos.write(data, 0, count);
}
fos.close();
gzipIs.close();
} catch (IOException e) {
e.printStackTrace();
}
/**删除上传文件*/
upload.delete();
/**读取文件内容*/
String txt = null;
txt = readTxtFile(filePath + fileName);
/**解析json*/
List<LogInfoEntity> logInfoList = json(txt);
LogEntity logEntity = TransferLogInfoToLog.generateLogEntity(logInfoList.get(0));
//添加vip用户判断,如果是vip,保留日志文件到指定目录,非vip日志直接删除。
//判断安卓还是ios,修改文件名为手机号+日期
String uid = logEntity.getUserId();// ServletActionContext.getRequest().getParameter("phonenumber");
String imei = logEntity.getImei();//ServletActionContext.getRequest().getParameter("imei");
String appId = logEntity.getAppId();
int flag = 0;//为1时是DEBUG用户,为2时是VIP用户
/*
* DEBUG用户判断,优先级最高
*/
if (imei != null && !imei.equals("")
&& SysInit.appsConnections.get(appId).getDebug_imei_list() != null
&& SysInit.appsConnections.get(appId).getDebug_imei_list().contains(imei)) {
flag = 1;
} else if (uid != null && !uid.equals("")
&& SysInit.appsConnections.get(appId).getDebug_uid_list() != null
&& SysInit.appsConnections.get(appId).getDebug_uid_list().contains(uid)) {
flag = 1;
}
if(flag!=1){
/*
* VIP用户判断
*/
if (uid != null && !uid.equals("")) {
if(SysInit.appsConnections.get(appId).getVip_uid_list() != null && SysInit.appsConnections.get(appId).getVip_uid_list().contains(uid)){
flag = 2;
}
} else {
if (imei != null && !imei.equals("")) {
if(SysInit.appsConnections.get(appId).getVip_imei_list() != null && SysInit.appsConnections.get(appId).getVip_imei_list().contains(imei)){
flag = 2;
}
}
}
}
//判断是否vip日志,如果vip日志,分ios与andriod,分别存储。
/*String andriodpath="/mnt/nfs/andriod/";
String iospath="/mnt/nfs/ios/";*/
String appIdDefPath="/mnt/nfs/"+appId+"/";
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String strdate=formatter.format(currentTime);
String[] tmptime=strdate.split("-");
//File.separator
//strdate.replace("-", File.separator);
/*andriodpath=andriodpath+tmptime[0]+File.separator+tmptime[1]+File.separator+tmptime[2]+ File.separator;
iospath=iospath+tmptime[0]+File.separator+tmptime[1]+File.separator+tmptime[2]+ File.separator;*/
String appIdPath=appIdDefPath+tmptime[0]+File.separator+tmptime[1]+File.separator+tmptime[2]+ File.separator;
if(flag>0){
System.out.println("uid:"+uid);
/*System.out.println("andriodpath:"+andriodpath);
System.out.println("iospath:"+iospath);*/
System.out.println("appIdPath:"+appIdPath);
System.out.println("flag:"+flag);
/*if(logEntity.isIosLog()) {
//ios vip log
File logpath = new File(iospath);
if (!logpath.exists()) {//判断文件夹是否存在,不存在则创建
logpath.mkdirs();
//logpath.setWritable(true, false);
}
logpath.setWritable(true, false);
// File savelogfile = new File(iospath,logEntity.getUserId()+"_"+file.getName());
//
// try {
// savelogfile.createNewFile();
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// System.out.println("filename:"+savelogfile.getName());
//file.renameTo(new File(iospath,logEntity.getUserId()+"_"+file.getName()));
FileUtils.copyFile(file,new File(iospath,logEntity.getUserId()+"_"+file.getName())) ;
System.out.println("ios vip log move."+logEntity.getUserId()+"_"+file.getName());
}else{
//andriod vip log
File logpath = new File(andriodpath);
if (!logpath.exists()) {//判断文件夹是否存在,不存在则创建
logpath.mkdirs();
//logpath.setWritable(true, false);
}
logpath.setWritable(true, false);
//
// File savelogfile = new File(andriodpath,logEntity.getUserId()+"_"+file.getName());
//
// try {
// savelogfile.createNewFile();
// } catch (IOException e) {
// e.printStackTrace();
// }
// System.out.println("filename:"+savelogfile.getName());
FileUtils.copyFile(file,new File(andriodpath,logEntity.getUserId()+"_"+file.getName())) ;
//file.renameTo(new File(andriodpath,logEntity.getUserId()+"_"+file.getName()));
System.out.println("andriod vip log move."+logEntity.getUserId()+"_"+file.getName());
} */
File logpath = new File(appIdPath);
if (!logpath.exists()) {//判断文件夹是否存在,不存在则创建
logpath.mkdirs();
//logpath.setWritable(true, false);
}
logpath.setWritable(true, false);
FileUtils.copyFile(file,new File(appIdPath,logEntity.getUserId()+"_"+file.getName())) ;
System.out.println("andriod vip log move."+logEntity.getUserId()+"_"+file.getName());
}else{
/**删除本地存储文件*/
file.delete();
}
return logInfoList;
}
/**
* 读取解压后的txt文件
*
* @param filePath
* @return String
* @author zhl
*/
public String readTxtFile(String filePath) {
StringBuffer lineTxt1 = new StringBuffer();
File file = new File(filePath);
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = null;
try {
read = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8"));
} catch (FileNotFoundException e) {
}
// 考虑到编码格式
if(read!=null){
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
try {
while ((lineTxt = bufferedReader.readLine()) != null) {
lineTxt1.append(lineTxt);
}
read.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
logger.error("read interface file error,can not find the file!");
}
return lineTxt1.toString();
}
/**
* 解析json
*
* @param data
* @return List
* @Time 20150304
* @author zhl
*/
public List<LogInfoEntity> json(String data){
Gson gson = new Gson();
List<LogInfoEntity> logInfoList = new ArrayList<LogInfoEntity>();
String [] logArray = data.split("}");
for(int i = 0;i < logArray.length; i ++){
// System.out.println(logArray[i]);
//逗号类型的有问题
//requireParam:""success":false,"errorCode":-1005,"contentId":"408451224","contentName":"摆渡人","time":"2017-03-18 02:37:15","errorMessage":"网络连接已中断。"", returnCode:"2",
LogInfoEntity logInfoEntity = gson.fromJson(logArray[i]+"}", LogInfoEntity.class);
logInfoList.add(logInfoEntity);
}
return logInfoList;
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.GZIPInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;
import com.chinamobile.datacollector.entity.LogInfoEntity;
import com.chinamobile.datacollector.sys.SysInit;
import com.chinamobile.datacollector.util.TransferLogInfoToLog;
import com.cmreadcommon.model.LogEntity;
import com.google.gson.Gson;
/**
* @ClassName: InteractServiceImpl
* @Description: 处理交互请求相关业务
* @author liuyao
* @date 2015年3月4日 下午3:42:54
*/
@Service
public class InteractServiceImpl implements InteractService {
static Logger logger = LogManager.getLogger(InteractServiceImpl.class.getName());
/** gzip流每次读取的缓冲区大小 */
private static final int BUFFER_SIZE = 16384;
/* (非 Javadoc)
* <p>Title: getLogInfoList</p>
* <p>Description: </p>
* @author liuyao
* @param upload
* @return
* @throws Exception
* @see com.chinamobile.datacollector.service.InteractService#getLogInfoList(java.io.File)
*/
@Override
public List<LogInfoEntity> getLogInfoList(File upload) throws Exception{
/**本地存储上传文件的路径*/
String fileName = System.currentTimeMillis()+".txt";
String filePath = ServletActionContext.getServletContext().getRealPath(File.separator)+"LogUploadFolder" + File.separator;
File path = new File(ServletActionContext.getServletContext().getRealPath(File.separator)+"LogUploadFolder" + File.separator);
if (!path.exists()) {//判断文件夹是否存在,不存在则创建
path.mkdirs();
}
File file = new File(path,fileName);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
/*
* 解压上传的压缩文件,并写入本地文件中
*/
try {
GZIPInputStream gzipIs = new GZIPInputStream(new FileInputStream(upload));
int count = 0;
byte data[] = new byte[BUFFER_SIZE];
while ((count = gzipIs.read(data, 0, BUFFER_SIZE)) != -1) {
fos.write(data, 0, count);
}
fos.close();
gzipIs.close();
} catch (IOException e) {
e.printStackTrace();
}
/**删除上传文件*/
upload.delete();
/**读取文件内容*/
String txt = null;
txt = readTxtFile(filePath + fileName);
/**解析json*/
List<LogInfoEntity> logInfoList = json(txt);
LogEntity logEntity = TransferLogInfoToLog.generateLogEntity(logInfoList.get(0));
//添加vip用户判断,如果是vip,保留日志文件到指定目录,非vip日志直接删除。
//判断安卓还是ios,修改文件名为手机号+日期
String uid = logEntity.getUserId();// ServletActionContext.getRequest().getParameter("phonenumber");
String imei = logEntity.getImei();//ServletActionContext.getRequest().getParameter("imei");
String appId = logEntity.getAppId();
int flag = 0;//为1时是DEBUG用户,为2时是VIP用户
/*
* DEBUG用户判断,优先级最高
*/
if (imei != null && !imei.equals("")
&& SysInit.appsConnections.get(appId).getDebug_imei_list() != null
&& SysInit.appsConnections.get(appId).getDebug_imei_list().contains(imei)) {
flag = 1;
} else if (uid != null && !uid.equals("")
&& SysInit.appsConnections.get(appId).getDebug_uid_list() != null
&& SysInit.appsConnections.get(appId).getDebug_uid_list().contains(uid)) {
flag = 1;
}
if(flag!=1){
/*
* VIP用户判断
*/
if (uid != null && !uid.equals("")) {
if(SysInit.appsConnections.get(appId).getVip_uid_list() != null && SysInit.appsConnections.get(appId).getVip_uid_list().contains(uid)){
flag = 2;
}
} else {
if (imei != null && !imei.equals("")) {
if(SysInit.appsConnections.get(appId).getVip_imei_list() != null && SysInit.appsConnections.get(appId).getVip_imei_list().contains(imei)){
flag = 2;
}
}
}
}
//判断是否vip日志,如果vip日志,分ios与andriod,分别存储。
/*String andriodpath="/mnt/nfs/andriod/";
String iospath="/mnt/nfs/ios/";*/
String appIdDefPath="/mnt/nfs/"+appId+"/";
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String strdate=formatter.format(currentTime);
String[] tmptime=strdate.split("-");
//File.separator
//strdate.replace("-", File.separator);
/*andriodpath=andriodpath+tmptime[0]+File.separator+tmptime[1]+File.separator+tmptime[2]+ File.separator;
iospath=iospath+tmptime[0]+File.separator+tmptime[1]+File.separator+tmptime[2]+ File.separator;*/
String appIdPath=appIdDefPath+tmptime[0]+File.separator+tmptime[1]+File.separator+tmptime[2]+ File.separator;
if(flag>0){
System.out.println("uid:"+uid);
/*System.out.println("andriodpath:"+andriodpath);
System.out.println("iospath:"+iospath);*/
System.out.println("appIdPath:"+appIdPath);
System.out.println("flag:"+flag);
/*if(logEntity.isIosLog()) {
//ios vip log
File logpath = new File(iospath);
if (!logpath.exists()) {//判断文件夹是否存在,不存在则创建
logpath.mkdirs();
//logpath.setWritable(true, false);
}
logpath.setWritable(true, false);
// File savelogfile = new File(iospath,logEntity.getUserId()+"_"+file.getName());
//
// try {
// savelogfile.createNewFile();
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// System.out.println("filename:"+savelogfile.getName());
//file.renameTo(new File(iospath,logEntity.getUserId()+"_"+file.getName()));
FileUtils.copyFile(file,new File(iospath,logEntity.getUserId()+"_"+file.getName())) ;
System.out.println("ios vip log move."+logEntity.getUserId()+"_"+file.getName());
}else{
//andriod vip log
File logpath = new File(andriodpath);
if (!logpath.exists()) {//判断文件夹是否存在,不存在则创建
logpath.mkdirs();
//logpath.setWritable(true, false);
}
logpath.setWritable(true, false);
//
// File savelogfile = new File(andriodpath,logEntity.getUserId()+"_"+file.getName());
//
// try {
// savelogfile.createNewFile();
// } catch (IOException e) {
// e.printStackTrace();
// }
// System.out.println("filename:"+savelogfile.getName());
FileUtils.copyFile(file,new File(andriodpath,logEntity.getUserId()+"_"+file.getName())) ;
//file.renameTo(new File(andriodpath,logEntity.getUserId()+"_"+file.getName()));
System.out.println("andriod vip log move."+logEntity.getUserId()+"_"+file.getName());
} */
File logpath = new File(appIdPath);
if (!logpath.exists()) {//判断文件夹是否存在,不存在则创建
logpath.mkdirs();
//logpath.setWritable(true, false);
}
logpath.setWritable(true, false);
FileUtils.copyFile(file,new File(appIdPath,logEntity.getUserId()+"_"+file.getName())) ;
System.out.println("andriod vip log move."+logEntity.getUserId()+"_"+file.getName());
}else{
/**删除本地存储文件*/
file.delete();
}
return logInfoList;
}
/**
* 读取解压后的txt文件
*
* @param filePath
* @return String
* @author zhl
*/
public String readTxtFile(String filePath) {
StringBuffer lineTxt1 = new StringBuffer();
File file = new File(filePath);
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = null;
try {
read = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8"));
} catch (FileNotFoundException e) {
}
// 考虑到编码格式
if(read!=null){
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
try {
while ((lineTxt = bufferedReader.readLine()) != null) {
lineTxt1.append(lineTxt);
}
read.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
logger.error("read interface file error,can not find the file!");
}
return lineTxt1.toString();
}
/**
* 解析json
*
* @param data
* @return List
* @Time 20150304
* @author zhl
*/
public List<LogInfoEntity> json(String data){
Gson gson = new Gson();
List<LogInfoEntity> logInfoList = new ArrayList<LogInfoEntity>();
String [] logArray = data.split("}");
for(int i = 0;i < logArray.length; i ++){
// System.out.println(logArray[i]);
//逗号类型的有问题
//requireParam:""success":false,"errorCode":-1005,"contentId":"408451224","contentName":"摆渡人","time":"2017-03-18 02:37:15","errorMessage":"网络连接已中断。"", returnCode:"2",
LogInfoEntity logInfoEntity = gson.fromJson(logArray[i]+"}", LogInfoEntity.class);
logInfoList.add(logInfoEntity);
}
return logInfoList;
}
}