添加相关工具类信息

main
ic_excellent 2022-12-08 15:07:18 +08:00
parent 01014bda04
commit df1e8bb8c1
14 changed files with 1427 additions and 23 deletions

View File

@ -0,0 +1,28 @@
package weaver.xiao.commons.config.entity;
import lombok.Data;
import weaver.xiao.commons.utils.annotation.SqlFieldMapping;
import java.io.InputStream;
/**
* <h1></h1>
*
* @author XiaoBokang
* @create 2022/8/8 10:24
*/
@Data
public class WeaverFile {
@SqlFieldMapping("imagefilename")
String fileName;
@SqlFieldMapping("imagefileid")
String imageFileId;
@SqlFieldMapping("docid")
String docId;
InputStream inputStream;
}

View File

@ -1239,6 +1239,45 @@ public class DealWithMapping extends ToolUtil {
}
}
/**
* <h2></h2>
* @param responseMappingList
* @param requestRes
* @return
*/
public Map<String, Map<String, Object>> dealResponse(List<ResponseMapping> responseMappingList, Map<String, Object> requestRes) {
this.writeDebuggerLog("回写信息转换处理 responseMappingList==>" + responseMappingList + " requestRes==>" + requestRes);
Map<String, Map<String, Object>> tableUpdateMap = new HashMap<>();
if (responseMappingList != null && !responseMappingList.isEmpty()) {
Map<String, Object> mainUpdateMap = new HashMap<>();
List<ResponseMapping> detailResponseList = new ArrayList<>();
for (ResponseMapping responseMapping : responseMappingList) {
if (responseMapping.getMainOrDetail() == 0) {
String resVal = Util.null2String(requestRes.get(responseMapping.getResponseFieldName()));
mainUpdateMap.put(responseMapping.getWorkflowFieldName(), resVal);
} else {
detailResponseList.add(responseMapping);
}
}
tableUpdateMap.put(mainTable, mainUpdateMap);
Map<String, List<ResponseMapping>> groupMap = detailResponseList.stream().collect(Collectors.groupingBy(ResponseMapping::getDetailTableId));
Set<Map.Entry<String, List<ResponseMapping>>> entries = groupMap.entrySet();
for (Map.Entry<String, List<ResponseMapping>> entry : entries) {
Map<String, Object> detailUpdateMap = new HashMap<>();
String detailId = entry.getKey();
List<ResponseMapping> responseMappings = entry.getValue();
String detailTableName = mainTable + "_dt" + detailId;
for (ResponseMapping responseMapping : responseMappings) {
String resVal = Util.null2String(requestRes.get(responseMapping.getResponseFieldName()));
detailUpdateMap.put(responseMapping.getWorkflowFieldName(), resVal);
}
tableUpdateMap.put(detailTableName, detailUpdateMap);
}
}
this.writeDebuggerLog("回写信息tableUpdateMap==> " + tableUpdateMap);
return tableUpdateMap;
}
/**
*
*

View File

@ -0,0 +1,25 @@
package weaver.xiao.commons.core;
import weaver.hrm.User;
import weaver.workflow.request.RequestManager;
/**
* @ClassName BaseActionHandleFunction
* @Author
* @Date 2022/11/25 12:17
* @Description <h1></h1>
**/
@FunctionalInterface
public interface BaseActionHandleFunction {
/**
* <h2>action</h2>
*
* @param requestId ID
* @param billTable
* @param workflowId ID
* @param user
* @param requestManager
*/
void handle(String requestId, String billTable, int workflowId,
User user, RequestManager requestManager) throws Exception;
}

View File

@ -0,0 +1,276 @@
package weaver.xiao.commons.core;
import org.apache.log4j.Logger;
import weaver.conn.RecordSetTrans;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.*;
import weaver.workflow.request.RequestManager;
import weaver.workflow.workflow.WorkflowBillComInfo;
import weaver.workflow.workflow.WorkflowComInfo;
import weaver.xiao.commons.utils.LogUtil;
import weaver.xiao.commons.utils.VerifyUtil;
import java.util.*;
/**
* @ClassName BaseAction
* @Author
* @Date 2022/11/25 12:09
* @Description <h1>Action</h1>
**/
public abstract class CusBaseAction implements Action {
/** 全局日志对象 */
protected final Logger log = LogUtil.getLogger();
/** 全局requestInfo对象 */
protected RequestInfo requestInfo;
/** 全局数据处理事务类 */
protected RecordSetTrans recordSetTrans = new RecordSetTrans();
/** 流程提交方式处理方法集合 */
private final Map<String, BaseActionHandleFunction> actionHandleMethod = new HashMap<>();
/**
* <h2></h2>
*/
private void initHandleMethod() {
//提交
actionHandleMethod.put(ActionRunType.SUBMIT, this::doSubmit);
//退回
actionHandleMethod.put(ActionRunType.REJECT, this::doReject);
//撤回
actionHandleMethod.put(ActionRunType.WITHDRAW, this::doWithdraw);
//强制收回
actionHandleMethod.put(ActionRunType.DRAW_BACK, this::doDrawBack);
}
@Override
public final String execute(RequestInfo requestInfo) {
this.requestInfo = requestInfo;
RequestManager requestManager = requestInfo.getRequestManager();
String billTable = requestManager.getBillTableName();
String requestId = requestInfo.getRequestid();
User user = requestInfo.getRequestManager().getUser();
int workflowId = requestManager.getWorkflowid();
//操作类型 submit - 提交 reject - 退回 等
String src = requestManager.getSrc();
if ("".equals(billTable)) {
WorkflowComInfo workflowComInfo = new WorkflowComInfo();
String formId = workflowComInfo.getFormId(String.valueOf(workflowId));
WorkflowBillComInfo workflowBillComInfo = new WorkflowBillComInfo();
billTable = workflowBillComInfo.getTablename(formId);
}
try {
VerifyUtil.requireVerify(this);
if (VerifyUtil.isBlank(src)) {
src = "submit";
}
//初始化默认的流程处理方法
initHandleMethod();
//提供自定义注册处理方法
registerHandler(actionHandleMethod);
//获取流程对应的处理方法
BaseActionHandleFunction cusBaseActionHandleFunction = actionHandleMethod.get(src);
//默认没有直接成功不做拦截
if (null == cusBaseActionHandleFunction) {
return Action.SUCCESS;
}
cusBaseActionHandleFunction.handle(requestId, billTable, workflowId, user, requestManager);
}catch (Exception e) {
e.printStackTrace();
log.info("action执行异常 ==>"+LogUtil.getExceptionStr(e));
String message = doExceptionDeal(e,requestId,workflowId,user,requestManager);
requestInfo.getRequestManager().setMessageid("11111C " + requestId + " 22222");
requestInfo.getRequestManager().setMessagecontent(message);
return Action.FAILURE_AND_CONTINUE;
}
return Action.SUCCESS;
}
/**
* <h2></h2>
*
* @param actionHandleMethod map
*/
public void registerHandler(Map<String, BaseActionHandleFunction> actionHandleMethod) {
}
/**
* <h2>action </h2>
* <p>
* log
* Util.actionFailException(requestManager,"error msg"); action
* </p>
*
* @param requestId ID
* @param billTable
* @param workflowId ID
* @param user
* @param requestManager
*/
public abstract void doSubmit(String requestId, String billTable, int workflowId,
User user, RequestManager requestManager) throws Exception;
/**
* <h2>action 退</h2>
* <p>
* log
* Util.actionFailException(requestManager,"error msg"); action
* </p>
*
* @param requestId ID
* @param billTable
* @param workflowId ID
* @param user
* @param requestManager
*/
public void doReject(String requestId, String billTable, int workflowId,
User user, RequestManager requestManager) throws Exception{
}
/**
* <h2>action </h2>
* <p>
* log
* Util.actionFailException(requestManager,"error msg"); action
* </p>
*
* @param requestId ID
* @param billTable
* @param workflowId ID
* @param user
* @param requestManager
*/
public void doWithdraw(String requestId, String billTable, int workflowId,
User user, RequestManager requestManager) throws Exception{
}
/**
* <h2>action </h2>
* <p>
* log
* Util.actionFailException(requestManager,"error msg"); action
* </p>
*
* @param requestId ID
* @param billTable
* @param workflowId ID
* @param user
* @param requestManager
*/
public void doDrawBack(String requestId, String billTable, int workflowId,
User user, RequestManager requestManager) throws Exception{
}
/**
* <h2>action </h2>
* @param e
* @param requestId id
* @param workflowId id
* @param user
* @param requestManager
* @return
*/
public abstract String doExceptionDeal(Exception e,String requestId,int workflowId,User user,RequestManager requestManager);
/**
* <h2></h2>
*
* @return
*/
protected Map<String, Object> getMainTableValue() {
//获取主表数据
Property[] propertyArr = requestInfo.getMainTableInfo().getProperty();
if (null == propertyArr) {
return Collections.emptyMap();
}
Map<String, Object> mainTable = new HashMap<>((int) Math.ceil(propertyArr.length * 1.4));
for (Property property : propertyArr) {
String fieldName = property.getName();
String value = property.getValue();
mainTable.put(fieldName, value);
}
return mainTable;
}
/**
* <h2></h2>
*
* @return
*/
protected Map<String, List<Map<String, String>>> getDetailTableValue() {
DetailTable[] detailTableArr = requestInfo.getDetailTableInfo().getDetailTable();
Map<String, List<Map<String, String>>> detailDataList = new HashMap<>((int) Math.ceil(detailTableArr.length * 1.4));
for (DetailTable detailTable : detailTableArr) {
List<Map<String, String>> detailData = getDetailValue(detailTable);
detailDataList.put(detailTable.getId(), detailData);
}
return detailDataList;
}
/**
* <h2></h2>
*
* @param detailNo
* @return
*/
protected List<Map<String, String>> getDetailTableValueByDetailNo(int detailNo) {
DetailTable detailTable = requestInfo.getDetailTableInfo().getDetailTable(detailNo);
return getDetailValue(detailTable);
}
/**
* <h2></h2>
*
* @param detailTable
* @return
*/
private List<Map<String, String>> getDetailValue(DetailTable detailTable) {
Row[] rowArr = detailTable.getRow();
List<Map<String, String>> detailData = new ArrayList<>(rowArr.length);
for (Row row : rowArr) {
Cell[] cellArr = row.getCell();
Map<String, String> rowData = new HashMap<>((int) Math.ceil(cellArr.length * (1 + 0.4)));
rowData.put("id", row.getId());
for (Cell cell : cellArr) {
String fieldName = cell.getName();
String value = cell.getValue();
rowData.put(fieldName, value);
}
detailData.add(rowData);
}
return detailData;
}
public static final class ActionRunType {
/**
* 退
*/
public static final String REJECT = "reject";
/**
*
*/
public static final String WITHDRAW = "withdraw";
/**
*
*/
public static final String DRAW_BACK = "drawBack";
/**
*
*/
public static final String SUBMIT = "submit";
}
}

View File

@ -0,0 +1,21 @@
package weaver.xiao.commons.exception;
/**
* @author XiaoBokang
* @create 2022/7/28 14:47
*/
public class VerifyException extends RuntimeException{
public VerifyException(String message){
super(message);
}
public VerifyException(String message, Throwable cause) {
super(message, cause);
}
@Override
public synchronized Throwable fillInStackTrace() {
return super.fillInStackTrace();
}
}

View File

@ -0,0 +1,292 @@
package weaver.xiao.commons.utils;
import aiyh.utils.excention.CustomerException;
import org.apache.log4j.Logger;
import weaver.docs.docs.DocImageManager;
import weaver.docs.webservices.DocInfo;
import weaver.docs.webservices.DocServiceImpl;
import weaver.file.ImageFileManager;
import weaver.general.IOUtils;
import weaver.hrm.User;
import weaver.xiao.commons.config.entity.WeaverFile;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
/**
* @author XiaoBokang
* @create 2022/7/28 16:05
*/
public class DocImageFileUtil {
private static final Logger logger = LogUtil.getLogger();
public static final String DOC = "doc";
public static final String XLS = "xls";
public static final String PPTX = "pptx";
public static final String PPT = "ppt";
public static final String WPS = "wps";
public static final String DOCX = "docx";
public static final String XLSX = "xlsx";
private static final int BUFFER_SIZE = 2 * 1024;
/**
* <h2>zip</h2>
* @param srcFile zip
* @param destDirPath
* @throws RuntimeException
*/
public static void unZip(File srcFile, String destDirPath) throws RuntimeException {
long start = System.currentTimeMillis();
// 判断源文件是否存在
if (!srcFile.exists()) {
throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
}
// 开始解压
ZipFile zipFile = null;
try {
zipFile = new ZipFile(srcFile, Charset.forName("GBK"));
Enumeration<?> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
System.out.println("解压" + entry.getName());
// 如果是文件夹,就创建个文件夹
if (entry.isDirectory()) {
String dirPath = destDirPath + "/" + entry.getName();
File dir = new File(dirPath);
dir.mkdirs();
} else {
// 如果是文件就先创建一个文件然后用io流把内容copy过去
File targetFile = new File(destDirPath + "/" + entry.getName());
// 保证这个文件的父文件夹必须存在
if(!targetFile.getParentFile().exists()){
targetFile.getParentFile().mkdirs();
}
targetFile.createNewFile();
// 将压缩文件内容写入到这个文件中
InputStream is = zipFile.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(targetFile);
int len;
byte[] buf = new byte[BUFFER_SIZE];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
// 关流顺序,先打开的后关闭
fos.close();
is.close();
}
}
} catch (Exception e) {
logger.info("解压文件出现异常 ==>"+LogUtil.getExceptionStr(e));
throw new RuntimeException("unzip error from ZipUtils", e);
} finally {
if(zipFile != null){
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* <h2>ZIP</h2>
* @param srcFiles
* @param out
* @throws RuntimeException
*/
public static void toZip(List<File> srcFiles , OutputStream out)throws Exception {
long start = System.currentTimeMillis();
ZipOutputStream zos = null ;
try {
zos = new ZipOutputStream(out);
for (File srcFile : srcFiles) {
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int len;
FileInputStream in = new FileInputStream(srcFile);
while ((len = in.read(buf)) != -1){
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) +" ms");
} catch (Exception e) {
logger.info("压缩文件出现异常 ==>"+LogUtil.getExceptionStr(e));
throw new RuntimeException("zip error from ZipUtils",e);
}finally{
if(zos != null){
try {
zos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
*
* @param fileList
* @param dirPath
* @param fileName
*/
public static void toZipFileName(List<WeaverFile> fileList,String dirPath,String fileName) throws IOException {
String filePath = dirPath + File.separator + fileName;
File file = new File(filePath);
if(!file.exists()){
file.createNewFile();
}
OutputStream outputStream = Files.newOutputStream(file.toPath());
toZipByWeaverFile(fileList,outputStream);
}
/**
*
* @param fileList
* @param outputStream
*/
public static void toZipByWeaverFile(List<WeaverFile> fileList, OutputStream outputStream){
ZipOutputStream zos = null ;
try {
zos = new ZipOutputStream(outputStream);
for (WeaverFile weaverFile : fileList) {
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(weaverFile.getFileName()));
int len;
InputStream inputStream = weaverFile.getInputStream();
while ((len = inputStream.read(buf)) != -1){
zos.write(buf, 0, len);
}
inputStream.close();
zos.closeEntry();
}
zos.flush();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("zip error from ZipUtils",e);
}finally{
if(zos != null){
try {
zos.finish();
zos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* <h2></h2>
* @param content
* @param fileName
* @return id
*/
public static int createFileByInputSteam(InputStream content, String fileName) {
ImageFileManager imageFileManager = new ImageFileManager();
int imgFileId = -1;
try {
ImageFileManager.class.getMethod("saveImageFileByInputStream", InputStream.class, String.class);
imgFileId = imageFileManager.saveImageFileByInputStream(content, fileName);
} catch (NoSuchMethodException e) {
imageFileManager.setImagFileName(fileName);
try {
imageFileManager.setData(IOUtils.toBytes(content));
} catch (Exception ex) {
throw new CustomerException("创建文件失败,文件流转换失败", e);
}
imgFileId = imageFileManager.saveImageFile();
} catch (Exception e) {
throw new CustomerException("创建文件失败");
}
return imgFileId;
}
/**
* <h2></h2>
*
* @param fileName
* @param secCategory
* @param imageFileId ID
* @param userId ID
* @return ID
* @throws Exception
*/
public static int createDocByImageFileId(String fileName, int secCategory, int imageFileId, int userId) throws Exception {
DocInfo docInfo = new DocInfo();
docInfo.setImagefileId(imageFileId);
docInfo.setSeccategory(secCategory);
docInfo.setDocSubject(fileName);
docInfo.setDoccontent("");
DocServiceImpl docService = new DocServiceImpl();
int docId = docService.createDocByUser(docInfo, new User(userId));
DocImageManager imgManger = new DocImageManager();
imgManger.resetParameter();
imgManger.setDocid(docId);
imgManger.setImagefileid(imageFileId);
imgManger.setImagefilename(fileName);
imgManger.setIsextfile("1");
String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
if (DOC.equalsIgnoreCase(ext)) {
imgManger.setDocfiletype("3");
} else if (XLS.equalsIgnoreCase(ext)) {
imgManger.setDocfiletype("4");
} else if (PPT.equalsIgnoreCase(ext)) {
imgManger.setDocfiletype("5");
} else if (WPS.equalsIgnoreCase(ext)) {
imgManger.setDocfiletype("6");
} else if (DOCX.equalsIgnoreCase(ext)) {
imgManger.setDocfiletype("7");
} else if (XLSX.equalsIgnoreCase(ext)) {
imgManger.setDocfiletype("8");
} else if (PPTX.equalsIgnoreCase(ext)) {
imgManger.setDocfiletype("9");
} else {
imgManger.setDocfiletype("2");
}
imgManger.AddDocImageInfo();
return docId;
}
/**
*
* @param tempFilePath
*/
public static void deleteTempFile(String tempFilePath){
logger.info("=========删除临时文件:"+tempFilePath+"================");
boolean deleteFlag = true;
int n = 0;
while (deleteFlag) {
try {
n++;
Files.delete(Paths.get(tempFilePath));
deleteFlag = false;
} catch (Exception e) {
//设置线程沉睡500毫秒等待jvm进行垃圾回收将持有临时文件的流对象回收确保临时文件能删除
try {
Thread.sleep(500);
} catch (InterruptedException interruptedException) {
deleteFlag = false;
}
if (n > 5) {
deleteFlag = false;
}
}
}
}
}

View File

@ -0,0 +1,98 @@
package weaver.xiao.commons.utils;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
/**
* @author XiaoBokang
* @create 2021/12/7 14:19
*/
public class HttpManager {
/**
*
*/
private static final int CONNECT_TIMEOUT = 1000 * 60 * 3;
private static final int CONNECTION_REQUEST_TIMEOUT = 1000 * 60 * 3;
private static final int SOCKET_TIMEOUT = 10000 * 60 * 3;
private static final int MAX_TOTAL = 500;
private static final int MAX_PRE_ROUTE = 500;
/**
*
*/
static RequestConfig requestConfig = RequestConfig.custom()
//网络请求的超时时间
.setConnectTimeout(CONNECT_TIMEOUT)
//连接池去获取连接的超时时间
.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)
//设置socket超时时间
.setSocketTimeout(SOCKET_TIMEOUT)
.build();
static PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager();
static {
// 配置最大的连接数
manager.setMaxTotal(MAX_TOTAL);
// 每个路由最大连接数路由是根据host来管理的大小不好控制
manager.setDefaultMaxPerRoute(MAX_PRE_ROUTE);
}
/**
* urlhttp / https
* @param url
* @return
*/
public static CloseableHttpClient getHttpConnection(String url, CredentialsProvider credentialsProvider){
if(url.trim().toUpperCase().startsWith("https".toUpperCase())){
SSLContext sslContext;
SSLConnectionSocketFactory sslsf = null;
try {
sslContext = new SSLContextBuilder().loadTrustMaterial(null, (x509Certificates, s) -> {
// 绕过所有验证
return true;
}).build();
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
sslsf = new SSLConnectionSocketFactory(sslContext,hostnameVerifier);
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
e.printStackTrace();
}
HttpClientBuilder httpClientBuilder = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.setConnectionManager(manager)
.setConnectionManagerShared(true)
.setDefaultRequestConfig(requestConfig);
if(credentialsProvider != null){
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
return httpClientBuilder
.build();
}else{
HttpClientBuilder httpClientBuilder = HttpClients.custom()
.setConnectionManager(manager)
.setDefaultRequestConfig(requestConfig)
.setConnectionManagerShared(true);
if(credentialsProvider != null){
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
return httpClientBuilder
.build();
}
}
}

View File

@ -10,9 +10,9 @@ import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import weaver.wechat.request.HttpManager;
import weaver.xiao.commons.exception.RequestException;
import weaver.zwl.common.ToolUtil;
import weaver.wechat.request.HttpManager;
import javax.ws.rs.core.MediaType;
import java.io.IOException;

View File

@ -0,0 +1,221 @@
package weaver.xiao.commons.utils;
import com.jcraft.jsch.*;
import org.apache.log4j.Logger;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;
/**
* @author XiaoBokang
* @create 2022/8/10 12:07
*/
public class SFTPClientUtil {
private final Logger logger = LogUtil.getLogger();
/**
* sftp
* @param host
* @param port
* @param username
* @param password
* @return
*/
public ChannelSftp connect(String host, int port, String username, String password) {
ChannelSftp sftp = null;
Channel channel=null;
Session sshSession=null;
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
sshSession = jsch.getSession(username, host, port);
if(logger.isInfoEnabled()){
logger.info("***************** Session created. **********************");
logger.info("***************** stf host is "+host+"port is "+port+" username is "+username+" password is "+password+"**********************");
}
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
if(logger.isInfoEnabled()){
logger.info("***************** Session connected. **********************");
logger.info("***************** Opening Channel. **********************");
}
channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
if(logger.isInfoEnabled()){
logger.info("***************** Connected to " + host + ". **********************");
}
} catch (Throwable e) {
if (channel!=null) {
try {
channel.disconnect();
}catch(Throwable e1) {
}
}
if (sshSession!=null) {
try {
sshSession.disconnect();
}catch(Throwable e1) {
}
}
logger.error(e.getMessage(), e);
}
return sftp;
}
/**
*
* @param sftp
*/
public void disconnect(String host, ChannelSftp sftp){
// 关闭连接
try {
if (null != sftp) {
sftp.disconnect();
if(logger.isInfoEnabled()){
logger.info("***************** Closing Channel. **********************");
}
if (null != sftp.getSession()) {
sftp.getSession().disconnect();
if(logger.isInfoEnabled()){
logger.info("***************** Session disconnect. **********************");
}
}
}
if (logger.isInfoEnabled()) {
logger.info("**************** Disconnect to " + host + ". *******************");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @param directory
* @param fileName
* @param sftp
* @return
*/
public boolean isExist(String directory, String fileName, ChannelSftp sftp) {
try {
Vector<ChannelSftp.LsEntry> v = listFiles(directory, sftp);
Iterator<ChannelSftp.LsEntry> it = v.iterator();
while (it.hasNext()) {
ChannelSftp.LsEntry entry = it.next();
String flName = entry.getFilename();
if (flName.equals(fileName)) {
return true;
}
}
} catch (SftpException e) {
e.printStackTrace();
}
return false;
}
/**
*
* @param directory
* @param uploadFile
* @param sftp
*/
public boolean upload(String directory, String uploadFile, ChannelSftp sftp) {
boolean successFlg = true;
try {
sftp.cd(directory);
File file = new File(uploadFile);
sftp.put(Files.newInputStream(file.toPath()), file.getName());
if(logger.isInfoEnabled()){
logger.info("***************** Finished **********************");
}
} catch (Exception e) {
successFlg = false;
logger.info(LogUtil.getExceptionStr(e));
}
return successFlg;
}
/**
*
* @param directory
* @param downloadFile
* @param saveFile
* @param sftp
*/
public boolean download(String directory, String downloadFile, String saveFile, ChannelSftp sftp) {
boolean successFlg = true;
try {
sftp.cd(directory);
File file = new File(saveFile);
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
sftp.get(downloadFile, new FileOutputStream(file));
if(logger.isInfoEnabled()){
logger.info("***************** Finished **********************");
}
} catch (Exception e) {
successFlg = false;
logger.info(LogUtil.getExceptionStr(e));
}
return successFlg;
}
/**
*
* @param directory
* @param deleteFile
* @param sftp
*/
public void delete(String directory, String deleteFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
sftp.rm(deleteFile);
if(logger.isInfoEnabled()){
logger.info("***************** Finished **********************");
}
if (null != sftp) {
sftp.disconnect();
if (null != sftp.getSession()) {
sftp.getSession().disconnect();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @param directory
* @param sftp
* @return
* @throws SftpException
*/
@SuppressWarnings("unchecked")
public Vector<ChannelSftp.LsEntry> listFiles(String directory, ChannelSftp sftp) throws SftpException {
return sftp.ls(directory);
}
/**
*
*/
public void makeDirs(String localSavePath) {
File localFile = new File(localSavePath);
if (!localFile.exists()) {
localFile.mkdirs();
}
}
}

View File

@ -8,7 +8,6 @@ import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.xiao.commons.utils.annotation.SqlFieldMapping;
import java.lang.reflect.Field;
import java.util.*;
@ -59,6 +58,7 @@ public class SqlUtil {
case 2:{
valueText = this.getFieldVal(fieldType,value);
}break;
default:break;
}
field.set(res,valueText);
}
@ -78,18 +78,51 @@ public class SqlUtil {
*/
public Object getFieldVal(Class<?> fieldType,String tempValue){
Object fieldVal = null;
if(fieldType.equals(int.class)){
if(fieldType.equals(Integer.class)){
fieldVal = Util.getIntValue(tempValue);
}else if(fieldType.equals(String.class)){
fieldVal = Util.null2String(tempValue);
}else if(fieldType.equals(boolean.class)){
}else if(fieldType.equals(Boolean.class)){
fieldVal = Boolean.parseBoolean(tempValue);
}else if(fieldType.equals(Double.class)){
fieldVal = Util.getDoubleValue(tempValue,0.0);
}else {
fieldVal = "";
}
return fieldVal;
}
/**
*
* @param tableName
* @param param
* @return
*/
public RecordSet getTableMessage(String tableName, Map<String,Object> param){
List<Object> paramList = new ArrayList<>();
RecordSet recordSet = new RecordSet();
Set<Map.Entry<String, Object>> entries = param.entrySet();
StringBuilder whereBuilder = new StringBuilder();
whereBuilder.append("select * from ").append(tableName);
for (Map.Entry<String, Object> entry : entries) {
if("cusSql".equals(entry.getKey())){
whereBuilder.append(" and ").append(entry.getValue());
continue;
}
whereBuilder.append(" and ")
.append(entry.getKey())
.append(" = ? ");
paramList.add(entry.getValue());
}
String whereStr = whereBuilder.toString();
if(!"".equals(whereStr)){
whereStr = whereStr.replaceFirst(" and"," where");
}
logger.info("执行查询的sql>>>" + whereStr + " paramList>>>" + paramList);
recordSet.executeQuery(whereStr,paramList);
return recordSet;
}
/**
*
* @param tableName
@ -120,6 +153,10 @@ public class SqlUtil {
updateBuilder.append(tableName).append(" set ");
Set<Map.Entry<String, Object>> updateEntries = updateParam.entrySet();
for (Map.Entry<String, Object> updateEntry : updateEntries) {
if("cusSql".equals(updateEntry.getKey())){
updateBuilder.append(updateEntry.getValue());
continue;
}
updateBuilder.append(updateEntry.getKey())
.append(" = ?,");
paramList.add(updateEntry.getValue());
@ -127,6 +164,10 @@ public class SqlUtil {
StringBuilder whereBuilder = new StringBuilder();
Set<Map.Entry<String, Object>> whereEntries = whereParam.entrySet();
for (Map.Entry<String, Object> whereEntry : whereEntries) {
if("cusSql".equals(whereEntry.getKey())){
whereBuilder.append(" and ").append(whereEntry.getValue());
continue;
}
whereBuilder.append(" and ")
.append(whereEntry.getKey())
.append(" = ? ");
@ -257,4 +298,22 @@ public class SqlUtil {
return modeId;
}
/**
*
* @param input
* @return
*/
public String toDBC(String input) {
char c[] = input.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == '\u3000') {
c[i] = ' ';
} else if (c[i] > '\uFF00' && c[i] < '\uFF5F') {
c[i] = (char) (c[i] - 65248);
}
}
String returnString = new String(c);
return returnString;
}
}

View File

@ -0,0 +1,328 @@
package weaver.xiao.commons.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;
import weaver.xiao.commons.exception.ValueDealException;
import weaver.xiao.commons.exception.VerifyException;
import weaver.xiao.commons.utils.annotation.JsonParse;
import weaver.xiao.commons.utils.annotation.RequiredVerify;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
/**
* @author XiaoBokang
* @create 2022/7/28 12:38
*/
public class VerifyUtil {
private static final Logger logger = LogUtil.getLogger();
public static <T> void requireVerify(T t) throws IllegalAccessException {
Class<?> clazz = t.getClass();
Field[] declaredFields = clazz.getDeclaredFields();
for (Field declaredField : declaredFields) {
declaredField.setAccessible(true);
boolean annotationPresent = declaredField.isAnnotationPresent(RequiredVerify.class);
if (annotationPresent) {
Object value = declaredField.get(t);
Class<?> type = declaredField.getType();
boolean valueIsNull = isNull(type, value);
RequiredVerify annotation = declaredField.getAnnotation(RequiredVerify.class);
if (valueIsNull) {
throw new VerifyException(annotation.value());
}
}
}
}
public static <T> T xmlStrToObject(Class<T> clazz, String xmlStr) throws Exception {
T t = null;
JAXBContext context = JAXBContext.newInstance(clazz);
// 进行将Xml转成对象的核心接口
Unmarshaller unmarshaller = context.createUnmarshaller();
StringReader sr = new StringReader(xmlStr);
t = (T) unmarshaller.unmarshal(sr);
dealJsonStr(clazz, t);
return t;
}
public static <T> T xmlStrToObject(Class<T> clazz, InputStream inputStream) throws Exception {
T t = null;
JAXBContext context = JAXBContext.newInstance(clazz);
// 进行将Xml转成对象的核心接口
Unmarshaller unmarshaller = context.createUnmarshaller();
t = (T) unmarshaller.unmarshal(inputStream);
dealJsonStr(clazz, t);
return t;
}
private static <T> T dealJsonStr(Class<T> clazz, T t) throws Exception {
Field[] declaredFields = clazz.getDeclaredFields();
for (Field declaredField : declaredFields) {
declaredField.setAccessible(true);
boolean annotationPresent = declaredField.isAnnotationPresent(JsonParse.class);
if (annotationPresent) {
JsonParse annotation = declaredField.getAnnotation(JsonParse.class);
String sourceFieldName = annotation.value();
Field sourceField = clazz.getDeclaredField(sourceFieldName);
String sourceMethodName = "get" + sourceFieldName.substring(0, 1).toUpperCase() + sourceFieldName.substring(1);
Method declaredMethod = clazz.getDeclaredMethod(sourceMethodName);
Object sourceValue = declaredMethod.invoke(t);
Class<?> sourceType = sourceField.getType();
if (isNotNull(sourceType, sourceValue)) {
ObjectMapper objectMapper = new ObjectMapper();
String targetFieldName = declaredField.getName();
Class<?> targetType = declaredField.getType();
String targetMethodName = "set" + targetFieldName.substring(0, 1).toUpperCase() + targetFieldName.substring(1);
Method targetMethod = clazz.getDeclaredMethod(targetMethodName, targetType);
if (targetType.equals(List.class)) {
Type genericType = declaredField.getGenericType();
ParameterizedType pt = (ParameterizedType) genericType;
//得到泛型里的class类型对象
Class<?> genericClazz = (Class<?>) pt.getActualTypeArguments()[0];
CollectionType listType = objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, genericClazz);
List<Object> list = objectMapper.readValue(sourceValue.toString(), listType);
targetMethod.invoke(t, list);
} else {
Object o = objectMapper.readValue(sourceValue.toString(), targetType);
targetMethod.invoke(t, o);
}
}
}
}
return t;
}
public static <T> String beanToXml(T t, String encoding) {
String xmlStr = "";
try {
//1、创建上下文
JAXBContext context = JAXBContext.newInstance(t.getClass());
StringWriter writer = new StringWriter();
//2、创建Marshaller 这个是用于转换成xml的
Marshaller marshaller = context.createMarshaller();
//3、设置marshaller的属性
//JAXB_FORMATTED_OUTPUT格式化
//JAXB_ENCODING:编码格式
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
//4、将实体类读取到writer中
marshaller.marshal(t, writer);
xmlStr = writer.toString();
} catch (Exception e) {
logger.info("java bean 转换 xml异常 ==>" + LogUtil.getExceptionStr(e));
}
return xmlStr;
}
public static String mapToXml(Map<String, Object> map) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>").append("\n").append("<root>").append("\n");
String xmlBody = mapDeal(map);
stringBuilder.append(xmlBody).append("</root>");
return stringBuilder.toString();
}
public static String mapDeal(Map<String, Object> map) {
StringBuilder stringBuilder = new StringBuilder();
Set<Map.Entry<String, Object>> entries = map.entrySet();
for (Map.Entry<String, Object> entry : entries) {
String key = entry.getKey();
Object value = entry.getValue();
stringBuilder.append("\t").append("<").append(key).append(">");
if (value instanceof Map) {
String s = mapDeal((Map<String, Object>) value);
stringBuilder.append(s);
} else if (value instanceof List) {
String nodeName = "node";
String s = listDeal((List<Object>) value, nodeName);
stringBuilder.append(s);
} else {
stringBuilder.append(value);
}
stringBuilder.append("</").append(key).append(">").append("\n");
}
return stringBuilder.toString();
}
public static String listDeal(List<Object> list, String nodeName) {
StringBuilder stringBuilder = new StringBuilder();
for (Object o : list) {
stringBuilder.append("\t").append("<").append(nodeName).append(">");
if (o instanceof Map) {
String s = mapToXml((Map<String, Object>) o);
stringBuilder.append(s);
} else if (o instanceof List) {
throw new ValueDealException("不支持嵌套list");
} else {
stringBuilder.append(o);
}
stringBuilder.append("</").append(nodeName).append(">").append("\n");
}
return stringBuilder.toString();
}
public static Document convertStringToDocument(String xmlStr) {
SAXBuilder sb = new SAXBuilder();
try {
return sb.build(new InputSource(new StringReader(xmlStr)));
} catch (Exception e) {
logger.info("xml字符串解析异常 ==>" + LogUtil.getExceptionStr(e));
}
return null;
}
public static Document convertStringToDocument(InputStream inputStream) {
SAXBuilder sb = new SAXBuilder();
try {
return sb.build(inputStream);
} catch (Exception e) {
logger.info("xml字符串解析异常 ==>" + LogUtil.getExceptionStr(e));
}
return null;
}
public static boolean isNull(Class<?> type, Object value) {
if (type.equals(String.class)) {
return isBlank(value);
} else {
return value == null;
}
}
public static boolean isNotNull(Class<?> type, Object value) {
return !isNull(type, value);
}
public static boolean isNotBlank(Object value) {
return !isBlank(value);
}
public static boolean isBlank(Object value) {
if (value == null) {
return true;
}
String cs = String.valueOf(value);
int strLen;
if ((strLen = cs.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
}
/**
* <h2>线</h2>
* @param sourceMap
* @return map
*/
public static Map<String, Object> humpToUnderlineMap(Map<String, Object> sourceMap) {
return paramConversion(sourceMap, 2);
}
/**
* <h2>线</h2>
* @param sourceMap
* @return map
*/
public static Map<String, Object> underlineToHumpMap(Map<String, Object> sourceMap) {
return paramConversion(sourceMap, 1);
}
/**
* <h2></h2>
* @param sourceMap
* @param conversionType 1线2线
* @return map
*/
public static Map<String, Object> paramConversion(Map<String, Object> sourceMap, int conversionType) {
if (sourceMap == null || sourceMap.isEmpty()) {
return sourceMap;
}
Map<String, Object> targetMap = new HashMap<>();
Set<Map.Entry<String, Object>> entries = sourceMap.entrySet();
for (Map.Entry<String, Object> entry : entries) {
String key = entry.getKey();
Object value = entry.getValue();
if (conversionType == 1) {
key = toCamelCase(key);
} else {
key = toUnderlineCase(key);
}
targetMap.put(key, value);
}
return targetMap;
}
/**
* <h2>线</h2>
* @param sourceString
* @return
*/
public static String toUnderlineCase(String sourceString) {
if (VerifyUtil.isBlank(sourceString)) {
return sourceString;
}
StringBuffer stringBuffer = new StringBuffer();
char[] chars = sourceString.toCharArray();
for (char aChar : chars) {
if (aChar >= 65 && aChar <= 90) {
stringBuffer.append("_").append(aChar += 32);
} else {
stringBuffer.append(aChar);
}
}
return stringBuffer.toString();
}
/**
* <h2>线</h2>
* @param sourceString
* @return
*/
public static String toCamelCase(String sourceString) {
if (VerifyUtil.isBlank(sourceString)) {
return sourceString;
}
StringBuffer stringBuffer = new StringBuffer();
char[] chars = sourceString.toCharArray();
boolean isConversion = false;
for (char aChar : chars) {
//判断是否为'_'
if (aChar == 95) {
isConversion = true;
continue;
}
if (isConversion) {
stringBuffer.append(aChar -= 32);
isConversion = false;
} else {
stringBuffer.append(aChar);
}
}
return stringBuffer.toString();
}
}

View File

@ -0,0 +1,19 @@
package weaver.xiao.commons.utils.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* <h1>JSON</h1>
*
* @author XiaoBokang
* @create 2022/8/5 15:13
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonParse {
String value();
}

View File

@ -0,0 +1,17 @@
package weaver.xiao.commons.utils.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author XiaoBokang
* @create 2022/7/28 12:11
*/
@Target({ElementType.FIELD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiredVerify {
String value();
}

View File

@ -224,24 +224,5 @@ public class TestOrganization extends BaseTest {
System.out.println(JSON.toJSONString(orgChartTree));
}
@Test
public void testDownload() throws IOException {
HttpUtils httpUtils = new HttpUtils();
// String url = "http://e-sign-dev-yace-web-alb-1694614450.cn-north-1.elb.amazonaws.com.cn:8070/contract/download?appId=100000&signType=SHA256&sign=OEI4RDA5RjMwRTE3Qzk1ODI0RkM3OENCQTA3QzZEQUFBQTgwRjM5MDdDMkIzQTIxNDFERDkyNUI5RDVFRjc4Qg==&timestamp=2022-12-07%2014:32:08&bizContent=JTdCJTIyZG9jTm8lMjIlM0ElMjJjMGY5YzhjYTk5OTQ0MzZjYThjY2Y0MWVmMTZkMDUzYSUyMiU3RA==";
String url = "https://apigwaws-lite.porsche-cloudservice.com/env-101/econtract/econtract/contract/api/v1/contract/download";
String heard = "{\"apikey\":\"TulQxnZSRKeHoQfmeZzOUzGn6KpTDkDK\",\"appId\":\"100001\",\"bizContent\":\"JTdCJTIyY29udHJhY3RObyUyMiUzQSUyMjAwZDBkNmFlYzFkOTQzNGE4YjFlZjc5YzZjODcwNWFmJTIyJTdE\",\"sign\":\"M0I2NjhGRjgwNDY2RjM4MjRBMTg4RUQ0QUU0MkEzOUVEQTAxNTZCRTgwRDE3NzQ3RkQxMzZGOEE1M0FCODRGOA==\",\"signType\":\"SHA256\",\"Content-Type\":\"application/json\",\"timestamp\":\"2022-12-07 17:38:07\"} ";
Map<String, String> map = JSONObject.parseObject(heard, Map.class);
FaRequestUtils.downContract(new HashMap<String, Object>() {{
put("contractNo", "00d0d6aec1d9434a8b1ef79c6c8705af");
}}, (map1, response) -> {
getFileName(response);
Header[] allHeaders = response.getAllHeaders();
for (Header allHeader : allHeaders) {
System.out.println(allHeader);
}
return null;
}, url);
}
}