package cn.linkey.rulelib.KSSXLC;
import java.util.*;
import cn.linkey.dao.*;
import cn.linkey.util.*;
import cn.linkey.doc.*;
import cn.linkey.factory.*;
import cn.linkey.wf.*;
import cn.linkey.rule.LinkeyRule;
import cn.linkey.org.LinkeyUser;
import cn.linkey.dao.Rdb;
import cn.linkey.factory.BeanCtx;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;
/**
* @Package: rule
* @ClassName: R_KSSXLC_B005_transmitTar
* @Description: <p>Description: 执行传包(如果是制品库的话,执行的业务规则)</p>
* @author:
* @since: 2019/8/25 17:42
* @version: 1.0
* @Copyright: 2019 . All rights reserved.
*/
final public class R_KSSXLC_B005_transmitTar implements LinkeyRule {
// 编码格式。发送编码格式统一用UTF-8
private static final String ENCODING = "UTF-8";
// 设置连接超时时间,单位毫秒。
private static final int CONNECT_TIMEOUT = 6000;
// 请求获取数据的超时时间(即响应时间),单位毫秒。
private static final int SOCKET_TIMEOUT = 6000;
//制品库域名:http://22.5.104.1:8081/artifactory
private String domainName;
//认证的用户名
private String userName;
//认证的密码
private String password;
//执行传包的“源地址”
private String fromPath;
//执行传包的目标地址
private String targetPath;
//制品库域名
private String domainNamePre;
//实例id
private String instanceId;
//包名
private String packageName;
//配置文件名
private String proName;
//部署方式
private String deployMethod;
//启动时间
private String startTime;
//会签意见
private String remarkTextArea;
//是否自动传包
private String isAutoDeploy;
//系统标识
private String sysDocTag;
//执行传包返回结果
private String passPackageResult = "";
//包名数组(因为有可能是多个)
private String[] packageNames;
//tar包名称
private String fileName;
//doc
private Document doc;
/**
* @Title: <p>run</p>
* @Description: <p>执行传包(如果是制品库的话,执行的业务规则)</p>
* @param params
* @return: java.lang.String
* @author:
* @Date: 2019/8/25 18:10
* @throws
* @version: 1.0
* @Copyright: . All rights reserved.
*/
@Override
public String run(HashMap<String, Object> params) throws Exception {
/**
* 1.数据初始化
*/
String logPre = "[执行传包(如果是制品库的话,执行的业务规则)]<R_KSSXLC_B005_transmitTar>";
boolean res = init();
/**
* 2.返回值准备
*/
String returnValue;
/**
* 3.参数校验
*/
if (!res) {
BeanCtx.out(logPre + "数据初始化失败,直接返回!");
returnValue = "{\"success\":\"false\",\"message\":\"数据初始化失败,直接返回!" + "\"}";
BeanCtx.p(returnValue);
return "";
}
if (StringUtils.isAnyBlank(fromPath, targetPath)) {
BeanCtx.out(logPre + "入参有空值,直接返回!fromPath = " + fromPath + ", targetPath = " + targetPath);
returnValue = "{\"success\":\"false\",\"message\":\"传包作业失败!入参有空值,直接返回!" + "\"}";
BeanCtx.p(returnValue);
return "";
}
/**
* 4.执行传包
*/
for (int i = 0; i < packageNames.length; i++) {
fileName = packageNames[i];
BeanCtx.out("[+]------------fileName-->"+fileName);
boolean success = copy(fromPath, targetPath,fileName);
if(success){
passPackageResult +="<br>源路径:"+ fromPath + ",目标路径:"+targetPath+",版本包名称:"+fileName + ",执行结果:传包成功";
}else{
doc.s("pass_package_state",0);
doc.s("pass_package_result",passPackageResult+"<br>源路径:"+ fromPath + ",目标路径:"+targetPath+"," +
"版本包名称:"+fileName + ",执行结果:传包失败,源文件不存在");
doc.save();
returnValue = "{\"success\":\"false\",\"message\":\"传包作业失败,源文件不存在"+"\"}";
BeanCtx.p(returnValue);
return "";
}
}
/**
* 5.数据返回
*/
returnValue = "{\"success\":\"true\",\"message\":\"传包作业成功\"}";
doc.s("pass_package_state",1);
doc.s("pass_package_result",passPackageResult);
doc.save();
BeanCtx.p(returnValue);
BeanCtx.out(logPre + "出参:returnValue= " + returnValue);
return "";
}
/**
* @Title: <p>init</p>
* @Description: <p>数据初始化</p>
* @param
* @return: boolean
* @author:
* @Date: 2019/9/18 14:07
* @throws
* @version: 1.0
* @Copyright: . All rights reserved.
*/
public boolean init(){
String logPre = "[执行传包(如果是制品库的话,执行的业务规则)]<R_KSSXLC_B005_transmitTar>(init)";
try {
//实例id
instanceId = BeanCtx.g("instanceId");
BeanCtx.out(logPre + " instanceId=" + instanceId);
//包名
packageName = BeanCtx.g("packageName");
packageNames = packageName.split(",");
BeanCtx.out(logPre + " packageName=" + packageName);
BeanCtx.out(logPre + " packageNames=" + Arrays.toString(packageNames));
//配置文件名
proName = BeanCtx.g("proName");
String[] proNames = proName.split(",");
BeanCtx.out(logPre + " proName=" + proName);
//String fromPath = "IPDO-JFROG-DEV/sprint-3-deploy-20190812/160/IPDO_20190812104010.tar";
//String targetPath = "JFrog-test/sprint-3-deploy-20190812/20190813/IPDO_2019081204010.tar";
//版本包目标路径
targetPath = BeanCtx.g("targetPath");
BeanCtx.out(logPre + " targetPath=" + targetPath);
//版本包源路径
fromPath = BeanCtx.g("fromPath");
BeanCtx.out(logPre + " fromPath=" + fromPath);
//部署方式
deployMethod = BeanCtx.g("deploy_method");
BeanCtx.out(logPre + " deployMethod=" + deployMethod);
//启动时间
startTime = BeanCtx.g("start_time");
BeanCtx.out(logPre + " startTime=" + startTime);
//会签意见
remarkTextArea = BeanCtx.g("remarkTextarea");
BeanCtx.out(logPre + " remarkTextArea=" + remarkTextArea);
//是否自动传包
isAutoDeploy = BeanCtx.g("is_auto_deploy");
BeanCtx.out(logPre + " isAutoDeploy=" + isAutoDeploy);
//系统标识
sysDocTag = BeanCtx.g("sysDocTag");
BeanCtx.out(logPre + " sysDocTag=" + sysDocTag);
//执行传包返回结果
String sql = "select * from bpm_maindata where WF_OrUnid = '" + instanceId + "' ";
doc = Rdb.getDocumentBySql(sql);
if ("true".equals(isAutoDeploy) || "DCSC".equals(sysDocTag) || "DCLN".equals(sysDocTag) || "DCDP"
.equals(sysDocTag) || "DCPM".equals(sysDocTag) || "DCPY".equals(sysDocTag)) {
doc.s("version_pkg_name1", packageName);
} else {
doc.s("version_pkg_name2", packageName);
}
doc.s("version_pkg_source_path", fromPath);
doc.s("version_pkg_target_path", targetPath);
doc.s("deploy_method", deployMethod);
doc.s("start_time", startTime);
doc.s("remarkTextarea", remarkTextArea);
doc.save();
userName = getInterfaceValueByInterfaceKey("transfer.productLibUserName");
BeanCtx.out(logPre + " userName=" + userName);
password = getInterfaceValueByInterfaceKey("transfer.productLibUserPwd");
BeanCtx.out(logPre + " password=" + password);
domainNamePre = getInterfaceValueByInterfaceKey("transfer.productLibIP");
BeanCtx.out(logPre + " domainNamePre=" + domainNamePre);
domainName = domainNamePre + "/artifactory";
BeanCtx.out(logPre + " domainName=" + domainName);
return true;
} catch (Exception e) {
BeanCtx.out(logPre + "init方法发生异常: " + e);
return false;
}
}
/**
* @Title: <p>run</p>
* @Description: <p>根据关键字查询interface_config表对应的属性值</p>
* @param interfaceKey
* @return: java.lang.String
* @author:
* @Date: 2019/9/4 17:50
* @throws
* @version: 1.0
* @Copyright: . All rights reserved.
*/
public String getInterfaceValueByInterfaceKey(String interfaceKey) throws Exception {
/**
* 1.入参日志:获取系统唯一id
*/
String logPre = "[根据关键字查询制品库的测试ip与端口]<getInterfaceValueByInterfaceKey>";
BeanCtx.out(logPre + "入参:interfaceKey = " + interfaceKey);
/**
* 2.参数校验
*/
if (StringUtils.isEmpty(interfaceKey)) {
BeanCtx.out(logPre + "入参为空,直接返回!interfaceKey = " + interfaceKey);
return "";
}
/**
* 3.查询流程数据:根据关键字查询interface_config表对应的属性值
*/
String sql = "select interface_value from pms.interface_config where interface_key = '" + interfaceKey + "' limit 1; ";
BeanCtx.out(logPre + " 执行的sql = " + sql);
String interfaceValue = Rdb.getValueBySql(sql);
/**
* 4.出参日志
*/
BeanCtx.out(logPre + "出参: interfaceValue = " + interfaceValue);
/**
* 5.数据返回
*/
return interfaceValue;
}
/**
* @Title: <p>copy</p>
* @Description: <p>执行复制操作</p>
* @param originPath war原路径
* @param destinationPath war目标路径
* @return: boolean
* @author:
* @Date: 2019/8/25 17:50
* @throws
* @version: 1.0
* @Copyright: . All rights reserved.
*/
public boolean copy(String originPath, String destinationPath,String packageName) {
/**
* 1.入参日志
*/
String logPre = "[执行复制操作]<R_KSSXLC_B005_transmitTar>(copy)";
BeanCtx.out(logPre + "入参:originPath = " + originPath + ", destinationPath = " + destinationPath);
/**
* 2.参数校验
*/
if (StringUtils.isAnyBlank(originPath, destinationPath)) {
BeanCtx.out(logPre + "入参有空值,直接返回!originPath = " + originPath + "destinationPath = " + destinationPath);
return false;
}
/**
* 3.拼接制品复制转发URL
*/
String requestUrl = domainName + "/api/copy/" + originPath + packageName + "?to=/" +
destinationPath + packageName;
BeanCtx.out(logPre + "制品复制转发URL = " + requestUrl);
/**
* 4.封装授权信息
*/
Map<String, String> headermap = new HashMap<String, String>();
String authInfo = "Basic " + Base64.encodeBase64URLSafeString((userName + ":" + password).getBytes()) +
"=";
headermap.put("Authorization", authInfo);
Map<String, String> map = new HashMap<String, String>();
BeanCtx.out(logPre + "封装授权信息 = " + headermap.entrySet().toString());
/**
* 5.执行请求接口
*/
int httpClientResult = doPost(requestUrl, headermap, map);
/**
* 6.返回值校验并返回
*/
if (HttpStatus.SC_INTERNAL_SERVER_ERROR == httpClientResult) {
BeanCtx.out("复制失败");
return false;
} else {
BeanCtx.out("复制成功");
return true;
}
}
/**
* @Title: <p>doPost</p>
* @Description: <p>发送post请求;带请求头和请求参数</p>
* @param url 请求地址
* @param headers 请求头集合
* @param params 请求参数集合
* @return: int
* @author:
* @Date: 2019/8/25 17:56
* @throws
* @version: 1.0
* @Copyright: . All rights reserved.
*/
public int doPost(String url, Map<String, String> headers, Map<String, String> params) {
/**
* 1.入参日志
*/
String logPre = "[发送post请求;带请求头和请求参数]<R_KSSXLC_B005_transmitTar>(doPost)";
BeanCtx.out(logPre + "入参:url = " + url + ",headers = " + headers.entrySet().toString() + ",params = " +
params.entrySet().toString());
/**
* 2.参数校验
*/
if (StringUtils.isBlank(url)) {
BeanCtx.out(logPre + "url为空值,直接返回!url = " + url);
return HttpStatus.SC_INTERNAL_SERVER_ERROR;
}
/**
* 3.创建httpClient对象和http对象
*/
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
/**
* setConnectTimeout:设置连接超时时间,单位毫秒。
* setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection
* 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
* setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
*/
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout
(SOCKET_TIMEOUT).build();
httpPost.setConfig(requestConfig);
packageHeader(headers, httpPost);
/**
* 4.创建httpResponse对象
*/
CloseableHttpResponse httpResponse = null;
try {
/**
* 5.封装请求参数
*/
packageParam(params, httpPost);
/**
* 6.执行请求并获得响应结果
*/
return getHttpClientResult(httpResponse, httpClient, httpPost);
} catch (Exception e) {
/**
* 7.异常捕捉
*/
BeanCtx.out("执行请求并获得响应结果发生异常:" + e);
} finally {
/**
* 8.释放资源
*/
try {
release(httpResponse, httpClient);
} catch (IOException e) {
BeanCtx.out("释放资源发生异常:" + e);
}
}
return HttpStatus.SC_INTERNAL_SERVER_ERROR;
}
/**
* @Title: <p>packageHeader</p>
* @Description: <p>封装请求头</p>
* @param params
* @param httpMethod
* @return: void
* @author:
* @Date: 2019/8/25 18:00
* @throws
* @version: 1.0
* @Copyright: . All rights reserved.
*/
public void packageHeader(Map<String, String> params, HttpRequestBase httpMethod) {
/**
* 1.入参日志
*/
String logPre = "[封装请求头]<R_KSSXLC_B005_transmitTar>(packageHeader)";
BeanCtx.out(logPre + "入参:params = " + params.entrySet().toString() + ", httpMethod = " + httpMethod.toString());
/**
* 2.参数校验
*/
if (null == params) {
BeanCtx.out(logPre + "请求头为空值,直接返回!params = " + params);
return;
}
/**
* 3.开始封装请求头
*/
if (params != null) {
Set<Map.Entry<String, String>> entrySet = params.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
// 设置到请求头到HttpRequestBase对象中
httpMethod.setHeader(entry.getKey(), entry.getValue());
}
}
}
/**
* @Title: <p>packageParam</p>
* @Description: <p>封装请求参数</p>
* @param params
* @param httpMethod
* @return: void
* @author:
* @Date: 2019/8/25 18:03
* @throws
* @version: 1.0
* @Copyright: . All rights reserved.
*/
public void packageParam(Map<String, String> params, HttpEntityEnclosingRequestBase httpMethod)
throws UnsupportedEncodingException {
/**
* 1.入参日志
*/
String logPre = "[封装请求参数]<R_KSSXLC_B005_transmitTar>(packageParam)";
BeanCtx.out(logPre + "入参:params = " + params.entrySet().toString() + ", httpMethod = " + httpMethod.toString());
/**
* 2.参数校验
*/
if (null == params) {
BeanCtx.out(logPre + "请求参数为空值,直接返回!params = " + params);
return;
}
/**
* 3.封装请求参数
*/
if (params != null) {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
Set<Map.Entry<String, String>> entrySet = params.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
// 设置到请求的http对象中
httpMethod.setEntity(new UrlEncodedFormEntity(nvps, ENCODING));
}
}
/**
* @Title: <p>getHttpClientResult</p>
* @Description: <p>获得响应结果</p>
* @param httpResponse
* @param httpClient
* @param httpMethod
* @return: int
* @author:
* @Date: 2019/8/25 18:04
* @throws
* @version: 1.0
* @Copyright: . All rights reserved.
*/
public int getHttpClientResult(CloseableHttpResponse httpResponse,
CloseableHttpClient httpClient, HttpRequestBase httpMethod) throws Exception {
/**
* 1.入参日志
*/
String logPre = "[获得响应结果]<R_KSSXLC_B005_transmitTar>(getHttpClientResult)";
BeanCtx.out(logPre + "入参:httpResponse = " + httpResponse + ", httpClient = " + httpClient + ", httpMethod = "
+ httpMethod);
/**
* 2.执行请求
*/
httpResponse = httpClient.execute(httpMethod);
BeanCtx.out("执行请求结果是:httpResponse=" + httpResponse.toString());
/**
* 3.获取返回结果
*/
if (httpResponse != null && httpResponse.getStatusLine() != null) {
String content = "";
if (httpResponse.getEntity() != null) {
content = EntityUtils.toString(httpResponse.getEntity(), ENCODING);
}
BeanCtx.out("执行请求结果是:content=" + content.toString());
/**
* 4.返回值校验
*/
if (200 != httpResponse.getStatusLine().getStatusCode()) {
return HttpStatus.SC_INTERNAL_SERVER_ERROR;
} else {
return HttpStatus.SC_OK;
}
}
return HttpStatus.SC_INTERNAL_SERVER_ERROR;
}
/**
* @Title: <p>release</p>
* @Description: <p>释放资源</p>
* @param httpResponse
* @param httpClient
* @return: void
* @author:
* @Date: 2019/8/25 18:06
* @throws
* @version: 1.0
* @Copyright: . All rights reserved.
*/
public void release(CloseableHttpResponse httpResponse, CloseableHttpClient httpClient) throws IOException {
// 释放资源
if (httpResponse != null) {
httpResponse.close();
}
if (httpClient != null) {
httpClient.close();
}
}
}
调用http接口执行文件复制
猜你喜欢
转载自blog.csdn.net/u012045045/article/details/102524875
今日推荐
周排行