合并冲突
jingwei
ic_excellent 2023-05-16 14:54:01 +08:00
commit 3b00297fef
253 changed files with 35592 additions and 1144 deletions

View File

@ -1,9 +1,9 @@
window.Utils = { class CusUtils {
/** /**
* @author youhong.ai * @author youhong.ai
* @desc 发起请求 * @desc 发起请求
*/ */
request: function (url, type = "GET", data, isAsync = true, success = () => { request(url, type = "GET", data, isAsync = true, success = () => {
}, error = () => { }, error = () => {
}, complete = () => { }, complete = () => {
}, contentType = 'application/json', beforeSend = () => { }, contentType = 'application/json', beforeSend = () => {
@ -24,13 +24,13 @@ window.Utils = {
options.data = JSON.stringify(data) options.data = JSON.stringify(data)
} }
return $.ajax(options) return $.ajax(options)
}, }
/** /**
* @author youhong.ai * @author youhong.ai
* @desc 发起请求 * @desc 发起请求
*/ */
api: function (requestOptions = { api(requestOptions = {
url: "", url: "",
type: "GET", type: "GET",
data: "", data: "",
@ -61,13 +61,13 @@ window.Utils = {
} }
}, requestOptions) }, requestOptions)
return $.ajax(options) return $.ajax(options)
}, }
/** /**
* @author youhong.ai * @author youhong.ai
* @desc 获取react组件实例 * @desc 获取react组件实例
*/ */
findReact: function (dom, traverseUp = 0) { findReact(dom, traverseUp = 0) {
const key = Object.keys(dom).find(key => { const key = Object.keys(dom).find(key => {
return key.startsWith("__reactFiber$") // react 17+ return key.startsWith("__reactFiber$") // react 17+
|| key.startsWith("__reactInternalInstance$") || key.startsWith("__reactInternalInstance$")
@ -96,7 +96,7 @@ window.Utils = {
compFiber = GetCompFiber(compFiber); compFiber = GetCompFiber(compFiber);
} }
return compFiber.stateNode; return compFiber.stateNode;
}, }
/** /**
@ -104,7 +104,7 @@ window.Utils = {
* @param fieldName 字段名称 * @param fieldName 字段名称
* @returns {*|string} * @returns {*|string}
*/ */
convertNameToIdUtil: function (fieldName) { convertNameToIdUtil(fieldName) {
let fieldIds = []; let fieldIds = [];
if (fieldName instanceof Array) { if (fieldName instanceof Array) {
fieldName.forEach(item => { fieldName.forEach(item => {
@ -113,19 +113,32 @@ window.Utils = {
return fieldIds.join(',') return fieldIds.join(',')
} }
return Utils.convertNameObjToId(fieldName) return Utils.convertNameObjToId(fieldName)
}, }
getQueryString(name) {
let reg = new RegExp("(^|&|\/?)" + name + "=([^&]*)(&|$)", "i");
let searchStr = window.location.href
if (searchStr.startsWith('&')) {
searchStr = searchStr.substr(1)
}
let search = searchStr.match(reg)
if (search != null) {
return unescape(search[2]);
}
return null;
}
/** /**
* 将字段名称转为字段id * 将字段名称转为字段id
* @param fieldObj 字段名称对象 {string|object} * @param fieldObj 字段名称对象 {string|object}
* @returns {*} * @returns {*}
*/ */
convertNameObjToId: function (fieldObj = {fieldName: '', table: 'main'}) { convertNameObjToId(fieldObj = {fieldName: '', table: 'main'}) {
if (typeof fieldObj === 'object') { if (typeof fieldObj === 'object') {
return WfForm.convertFieldNameToId(fieldObj.fieldName, fieldObj.table) return WfForm.convertFieldNameToId(fieldObj.fieldName, fieldObj.table)
} }
return WfForm.convertFieldNameToId(fieldObj) return WfForm.convertFieldNameToId(fieldObj)
}, }
/** /**
* 根据字段名称查询字段值 * 根据字段名称查询字段值
@ -133,17 +146,18 @@ window.Utils = {
* @param rowIndex 明细行下表明细获取才传递 * @param rowIndex 明细行下表明细获取才传递
* @returns {*} 字段值 * @returns {*} 字段值
*/ */
getFiledValueByName: function (fieldName, rowIndex) { getFiledValueByName(fieldName, rowIndex) {
return WfForm.getFieldValue(Utils.convertNameObjToId(fieldName) + (rowIndex ? '_' + rowIndex : '')) return WfForm.getFieldValue(Utils.convertNameObjToId(fieldName) + (rowIndex ? '_' + rowIndex : ''))
}, }
/** /**
* 通过字段名称修改字段值 * 通过字段名称修改字段值
* @param fieldName 字段名称 * @param fieldName 字段名称
* @param value * @param value
*/ */
changeFieldValueByName: function (fieldName, value) { changeFieldValueByName(fieldName, value) {
WfForm.changeFieldValue(Utils.convertNameObjToId(fieldName), {value}) WfForm.changeFieldValue(Utils.convertNameObjToId(fieldName), {value})
} }
} }
window.Utils = new CusUtils()

View File

@ -1,3 +1,10 @@
const ecodeSDK = {}
ecodeSDK.setCom = (id, name, Com) => {
}
ecodeSDK.imp = (obj) => {
}
ecodeSDK.exp = (obj) => {
}
const WfForm = { const WfForm = {
isMobile: () => { isMobile: () => {
// true表示是eMobile、微信、钉钉等移动终端false代表PC端 // true表示是eMobile、微信、钉钉等移动终端false代表PC端

View File

@ -3,49 +3,68 @@ const yjfksj = WfForm.convertFieldNameToId('yjfksj');
// 主表比例 // 主表比例
const bl = WfForm.convertFieldNameToId('bcfkbl'); const bl = WfForm.convertFieldNameToId('bcfkbl');
// 明细要求付款日期 // 明细要求付款日期
const detail3Yqfkrq = WfForm.convertFieldNameToId('yqfkrq', "detail_3"); const detail3Yqfkrq = WfForm.convertFieldNameToId('yqfkrq',"detail_3");
// 明细本次付款比例 // 明细本次付款比例
const detailBl = WfForm.convertFieldNameToId('bcfkbl', "detail_3"); const detailBl = WfForm.convertFieldNameToId('bcfkbl',"detail_3");
// 关联订单 // 关联订单
const relationOrder = WfForm.convertFieldNameToId("gldd"); const relationOrder = WfForm.convertFieldNameToId("gldd");
// 明细4施工合同字段 // 明细4施工合同字段
const detail4SgField = WfForm.convertFieldNameToId("sght", "detail_4"); const detail4SgField = WfForm.convertFieldNameToId("sght","detail_4");
// 明细4采购合同字段 // 明细4采购合同字段
const detail4CgField = WfForm.convertFieldNameToId("cght", "detail_4"); const detail4CgField = WfForm.convertFieldNameToId("cght","detail_4");
// 明细4发票字段 // 明细4发票字段
const detail4FpField = WfForm.convertFieldNameToId("fpje", "detail_4"); const detail4FpField = WfForm.convertFieldNameToId("fpje","detail_4");
// 明细4合同发票金额和 // 明细4合同发票金额和
let detail4Map = new Map(); let detail4Map = new Map();
// 明细3施工合同字段 // 明细3施工合同字段
const detail3SgField = WfForm.convertFieldNameToId("sght", "detail_3"); const detail3SgField = WfForm.convertFieldNameToId("sght","detail_3");
// 明细3采购合同字段 // 明细3采购合同字段
const detail3CgField = WfForm.convertFieldNameToId("cght", "detail_3"); const detail3CgField = WfForm.convertFieldNameToId("cght","detail_3");
// 明细3 含税金额 // 明细3 含税金额
const detail3TaxField = WfForm.convertFieldNameToId("ddhsje", "detail_3"); const detail3TaxField = WfForm.convertFieldNameToId("ddhsje","detail_3");
// 明细3以收票金额 // 明细3以收票金额
const detail3YspField = WfForm.convertFieldNameToId("yspje", "detail_3"); const detail3YspField = WfForm.convertFieldNameToId("yspje","detail_3");
const detail3ApplyMoneyField = WfForm.convertFieldNameToId("bcfkje", "detail_3");
const detail3CgId = WfForm.convertFieldNameToId("cgddzbid", "detail_3");
const detail3Bl = WfForm.convertFieldNameToId('bcfkbl', "detail_3");
let detailBlMap = new Map();
const detail1Bl = WfForm.convertFieldNameToId('bcljfkbl', "detail_1");
const detail1CgId = WfForm.convertFieldNameToId("cgddzbid", "detail_1");
WfForm.registerCheckEvent(WfForm.OPER_ADDROW + "3", function (callback) { WfForm.registerCheckEvent(WfForm.OPER_ADDROW + "3", function (callback) {
callback(); callback();
setTimeout(() => { setTimeout(()=>{
initDeatail3Date(); initDeatail3Date();
}, 5) },5)
}); });
// 关联订单变化时将明细四的合同-金额map进行初始化 // // 关联订单变化时将明细四的合同-金额map进行初始化
// WfForm.bindFieldChangeEvent(relationOrder, function(obj,id,value){
// setTimeout(()=>{
// initDetail4Map();
// changeDetail3SpMoney();
// },2000);
// });
WfForm.bindFieldChangeEvent(relationOrder, function (obj, id, value) { WfForm.bindFieldChangeEvent(relationOrder, function (obj, id, value) {
setTimeout(() => { setTimeout(() => {
initDetail4Map(); initDetail4Map();
changeDetail3SpMoney(); changeDetail3SpMoney();
changeDetail1ApplyAMount();
}, 2000);
});
WfForm.bindDetailFieldChangeEvent(detail3ApplyMoneyField, function () {
setTimeout(() => {
changeDetail1ApplyAMount();
}, 2000); }, 2000);
}); });
WfForm.bindFieldChangeEvent(`${yjfksj},${bl}`, function(obj,id,value){
WfForm.bindFieldChangeEvent(`${yjfksj},${bl}`, function (obj, id, value) {
initDeatail3Date(); initDeatail3Date();
}); });
// 计算明细三开票金额 // 计算明细三开票金额
function changeDetail3SpMoney() { function changeDetail3SpMoney(){
let detail3RowArr = WfForm.getDetailAllRowIndexStr('detail_3').split(","); let detail3RowArr = WfForm.getDetailAllRowIndexStr('detail_3').split(",");
for (let i = 0; i < detail3RowArr.length; i++) { for (let i = 0; i < detail3RowArr.length; i++) {
let rowIndex = detail3RowArr[i]; let rowIndex = detail3RowArr[i];
@ -55,49 +74,49 @@ function changeDetail3SpMoney() {
let fpje = detail4Map.get(detail3SgFieldVal) ?? detail4Map.get(detail3CgFieldVal); let fpje = detail4Map.get(detail3SgFieldVal) ?? detail4Map.get(detail3CgFieldVal);
let mapVal = detail4Map.get(detail3SgFieldVal); let mapVal = detail4Map.get(detail3SgFieldVal);
let key = detail3SgFieldVal; let key = detail3SgFieldVal;
if (!mapVal) { if(!mapVal){
key = detail3CgFieldVal; key = detail3CgFieldVal;
} }
console.log('key ', key); console.log('key ', key);
if (fpje) { if(fpje){
console.log('fpje => ', fpje); console.log('fpje => ', fpje);
// 0.1 0.3 // 0.1 0.3
let detail3TaxVal = WfForm.getFieldValue(`${detail3TaxField}_${rowIndex}`); let detail3TaxVal = WfForm.getFieldValue(`${detail3TaxField}_${rowIndex}`);
console.log('detail3TaxVal => ', detail3TaxVal); console.log('detail3TaxVal => ', detail3TaxVal);
let tempJe = fpje - detail3TaxVal; let tempJe = fpje - detail3TaxVal;
console.log('tempJe => ', tempJe); console.log('tempJe => ', tempJe);
if (fpje > detail3TaxVal) { if(fpje > detail3TaxVal){
WfForm.changeFieldValue(`${detail3YspField}_${rowIndex}`, {value: detail3TaxVal}); WfForm.changeFieldValue(`${detail3YspField}_${rowIndex}`,{value: detail3TaxVal});
} else if (tempJe <= 0 && fpje == 0) { }else if(tempJe <= 0 && fpje == 0){
WfForm.changeFieldValue(`${detail3YspField}_${rowIndex}`, {value: 0}); WfForm.changeFieldValue(`${detail3YspField}_${rowIndex}`,{value: 0});
} else if (tempJe < 0) { }else if(tempJe < 0){
WfForm.changeFieldValue(`${detail3YspField}_${rowIndex}`, {value: fpje}); WfForm.changeFieldValue(`${detail3YspField}_${rowIndex}`,{value: fpje});
tempJe = 0; tempJe = 0;
} }
detail4Map.set(key, tempJe); detail4Map.set(key, tempJe);
console.log('detail4Map ', detail4Map) console.log('detail4Map ', detail4Map)
} else { }else{
WfForm.changeFieldValue(`${detail3YspField}_${rowIndex}`, {value: 0}); WfForm.changeFieldValue(`${detail3YspField}_${rowIndex}`,{value: 0});
} }
} }
} }
} }
function initDeatail3Date() { function initDeatail3Date(){
let dateVal = WfForm.getFieldValue(yjfksj); let dateVal = WfForm.getFieldValue(yjfksj);
let blVal = WfForm.getFieldValue(bl); let blVal = WfForm.getFieldValue(bl);
let detail3RowArr = WfForm.getDetailAllRowIndexStr('detail_3').split(","); let detail3RowArr = WfForm.getDetailAllRowIndexStr('detail_3').split(",");
for (let i = 0; i < detail3RowArr.length; i++) { for (let i = 0; i < detail3RowArr.length; i++) {
let rowIndex = detail3RowArr[i]; let rowIndex = detail3RowArr[i];
if (rowIndex !== "") { if (rowIndex !== "") {
WfForm.changeFieldValue(`${detail3Yqfkrq}_${rowIndex}`, {value: dateVal}); WfForm.changeFieldValue(`${detail3Yqfkrq}_${rowIndex}`,{value: dateVal});
WfForm.changeFieldValue(`${detailBl}_${rowIndex}`, {value: blVal}); WfForm.changeFieldValue(`${detailBl}_${rowIndex}`,{value: blVal});
} }
} }
} }
function initDetail4Map() { function initDetail4Map(){
let detail4RowArr = WfForm.getDetailAllRowIndexStr('detail_4').split(","); let detail4RowArr = WfForm.getDetailAllRowIndexStr('detail_4').split(",");
console.log('detail4RowArr ', detail4RowArr); console.log('detail4RowArr ', detail4RowArr);
for (let i = 0; i < detail4RowArr.length; i++) { for (let i = 0; i < detail4RowArr.length; i++) {
@ -110,7 +129,7 @@ function initDetail4Map() {
console.log('detail4SgFieldVal ', detail4SgFieldVal); console.log('detail4SgFieldVal ', detail4SgFieldVal);
console.log('detail4CgFieldVal ', detail4CgFieldVal); console.log('detail4CgFieldVal ', detail4CgFieldVal);
console.log('detail4FpFieldVal ', detail4FpFieldVal); console.log('detail4FpFieldVal ', detail4FpFieldVal);
if (!detail4FpFieldVal) { if(!detail4FpFieldVal){
continue; continue;
} }
key = detail4CgFieldVal ?? detail4SgFieldVal; key = detail4CgFieldVal ?? detail4SgFieldVal;
@ -123,21 +142,27 @@ function initDetail4Map() {
console.log('detail4Map ', detail4Map) console.log('detail4Map ', detail4Map)
} }
const detail3ApplyMoneyField = WfForm.convertFieldNameToId("bcfkje", "detail_3");
const detail3CgId = WfForm.convertFieldNameToId("cgddzbid", "detail_3");
const detail3Bl = WfForm.convertFieldNameToId('bcfkbl', "detail_3");
let detailBlMap = new Map();
const detail1Bl = WfForm.convertFieldNameToId('fkbl', "detail_1");
const detail1CgId = WfForm.convertFieldNameToId("cgddzbid", "detail_1");
WfForm.bindDetailFieldChangeEvent(detail3ApplyMoneyField, (id, rowIndex, value) => { WfForm.bindDetailFieldChangeEvent(detail3ApplyMoneyField, (id, rowIndex, value) => {
setTimeout(()=>{
let detail3RowArr = WfForm.getDetailAllRowIndexStr('detail_3').split(","); let detail3RowArr = WfForm.getDetailAllRowIndexStr('detail_3').split(",");
detailBlMap = new Map(); detailBlMap = new Map();
for (let i = 0; i < detail3RowArr.length; i++) { for (let i = 0; i < detail3RowArr.length; i++) {
let rowIndex = detail3RowArr[i]; let rowIndex = detail3RowArr[i];
if (rowIndex !== "") { if (rowIndex !== "") {
let bl = parseFloat(WfForm.getFieldValue(`${detail3Bl}_${rowIndex}`)); let blStr = WfForm.getFieldValue(`${detail3Bl}_${rowIndex}`);
if(blStr == ''){
console.log('111')
continue;
}
let bl = parseFloat(blStr);
console.log('bl => ' , bl);
let cg = WfForm.getFieldValue(`${detail3CgId}_${rowIndex}`); let cg = WfForm.getFieldValue(`${detail3CgId}_${rowIndex}`);
let mapVal = parseFloat(detailBlMap.get(cg) ?? 0) + bl; let mapVal = detailBlMap.get(cg);
if(!mapVal){
mapVal = 0;
}
mapVal+= bl;
detailBlMap.set(cg, mapVal); detailBlMap.set(cg, mapVal);
} }
} }
@ -151,9 +176,87 @@ WfForm.bindDetailFieldChangeEvent(detail3ApplyMoneyField, (id, rowIndex, value)
WfForm.changeFieldValue(`${detail1Bl}_${rowIndex}`, {value: val}) WfForm.changeFieldValue(`${detail1Bl}_${rowIndex}`, {value: val})
} }
} }
},300);
}); });
// 明细3合同总金额
const detail3AmountField = WfForm.convertFieldNameToId("htzje", "detail_3");
// 明细3订单号字段
const detail3OrderField = WfForm.convertFieldNameToId("ddh","detail_3");
// 明细1本次付款金额字段
const detail1ApplyAmountField = WfForm.convertFieldNameToId("bcsqfkje","detail_1");
// 明细1含税金额
const detail1AmountTaxField = WfForm.convertFieldNameToId("ddmxxj","detail_1");
// 明细1订单号
const detail1OrderField = WfForm.convertFieldNameToId("ddh","detail_1");
// 明细1预计付款日期
const detail1ApplyDate = WfForm.convertFieldNameToId("yjfkrq","detail_1");
function changeDetail1ApplyAMount(){
let detail3RowArr = WfForm.getDetailAllRowIndexStr('detail_3').split(",");
// 明细3 订单号-合同总金额map
let detail3ContractAmountMap = new Map();
let detail3ApplyAmountMap = new Map();
let detail3ApplyMoneyField = WfForm.convertFieldNameToId("bcfkje", "detail_3");
for (let i = 0; i < detail3RowArr.length; i++) {
let rowIndex = detail3RowArr[i];
if (rowIndex !== "") {
let detail3Order = WfForm.getFieldValue(`${detail3OrderField}_${rowIndex}`);
if(!detail3ContractAmountMap.has(detail3Order)){
detail3ContractAmountMap.set(detail3Order, parseFloat(WfForm.getFieldValue(`${detail3AmountField}_${rowIndex}`)));
}
let detail3Amount = parseFloat(WfForm.getFieldValue(`${detail3ApplyMoneyField}_${rowIndex}`));
let sum = detail3ApplyAmountMap.get(detail3Order) ?? 0;
detail3ApplyAmountMap.set(detail3Order, sum + detail3Amount);
}
}
console.log('detail3ContractAmountMap : ', detail3ContractAmountMap);
console.log('detail3ApplyAmountMap : ', detail3ApplyAmountMap);
let detail1RowArr = WfForm.getDetailAllRowIndexStr('detail_1').split(",");
for (let i = 0; i < detail1RowArr.length; i++) {
let rowIndex = detail1RowArr[i];
if (rowIndex !== "") {
let detail1Order =WfForm.getFieldValue(`${detail1OrderField}_${rowIndex}`);
console.log('detail1Order ', detail1Order);
// 含税金额
let detail1AmountTax = parseFloat(WfForm.getFieldValue(`${detail1AmountTaxField}_${rowIndex}`) ?? 0);
console.log('detail1AmountTax ', detail1AmountTax);
// 本次付款金额和
let applySum = parseFloat(detail3ApplyAmountMap.get(detail1Order) ?? 0);
console.log('applySum ', applySum);
// 合同总金额
let contractAmount = parseFloat(detail3ContractAmountMap.get(detail1Order) ?? 0);
console.log('contractAmount ', contractAmount);
let val = (applySum / contractAmount) * detail1AmountTax;
console.log('计算金额 : ', val)
WfForm.changeFieldValue(`${detail1ApplyAmountField}_${rowIndex}`, {value: val})
}
}
}
WfForm.bindDetailFieldChangeEvent(detail3Yqfkrq,()=>{
initDetail1ApplyDate();
});
function initDetail1ApplyDate(){
let detail3RowArr = WfForm.getDetailAllRowIndexStr('detail_3').split(",");
let detail3DateMap = new Map();
for (let i = 0; i < detail3RowArr.length; i++) {
let rowIndex = detail3RowArr[i];
if (rowIndex !== "") {
let detail3Order = WfForm.getFieldValue(`${detail3OrderField}_${rowIndex}`);
if(!detail3DateMap.has(detail3Order)){
detail3DateMap.set(detail3Order, WfForm.getFieldValue(`${detail3Yqfkrq}_${rowIndex}`));
}
}
}
console.log('detail3DateMap ', detail3DateMap);
let detail1RowArr = WfForm.getDetailAllRowIndexStr('detail_1').split(",");
for (let i = 0; i < detail1RowArr.length; i++) {
let rowIndex = detail1RowArr[i];
if (rowIndex !== "") {
let detail1Order =WfForm.getFieldValue(`${detail1OrderField}_${rowIndex}`);
WfForm.changeFieldValue(`${detail1ApplyDate}_${rowIndex}`, {value: detail3DateMap.get(detail1Order)})
}
}
}

View File

@ -0,0 +1,117 @@
// 首台销售日期
const firstSaleDateField = WfForm.convertFieldNameToId("stxsrq");
// 跟踪时间
const trackTimeField = WfForm.convertFieldNameToId("gzsj");
// 跟踪时间为三个月以内
const threeMonthIndex = 1;
// 是否提交等待节点
const submitWaitNode = WfForm.convertFieldNameToId("sftjddjd");
// 下次超时提醒日期
const timeoutRemindDateFiled = WfForm.convertFieldNameToId("cstxrq");
// 跟踪天数
const trackingDays = WfForm.getFieldValue(trackTimeField) <= 1 ? 15 : 30;
// 跟踪触发行数
const trackingLineField = WfForm.convertFieldNameToId("gzcfxs");
$(() => {
let detail2LineNum = WfForm.getDetailRowCount("detail_2");
// let firstTrack = Boolean(true);
// if (new Date(firstSaleDate) < new Date(currentDate) && dayDiff > 0) {
// firstTrack = false;
// }
// console.log('firstTrack ', firstTrack)
// 到达节点次数
const nodeNum = getNodeNum();
let trackingLine = parseInt(WfForm.getFieldValue(trackingLineField));
// 如果不是则自动添加一行明细让他自己填写
if (detail2LineNum < trackingLine && detail2LineNum < nodeNum) {
console.log('添加一行明细!');
WfForm.addDetailRow("detail_2", {});
}
if(detail2LineNum >= trackingLine){
WfForm.changeFieldValue(submitWaitNode, {value: 1});
WfForm.changeFieldValue(timeoutRemindDateFiled, {value: ''});
return;
}
initTimeoutDate();
WfForm.bindFieldChangeEvent(`${firstSaleDateField},${trackTimeField}`,()=>{
initTimeoutDate();
});
});
function getNodeNum(){
let firstSaleDate = WfForm.getFieldValue(firstSaleDateField);
console.log('首台销售日期 ', firstSaleDate);
let currentDate = getCurrentDate();
let dayDiff = getDaysDiff(firstSaleDate, currentDate);
console.log('当前天数与首台销售日期相差天数 : ', dayDiff)
return Math.floor(dayDiff / trackingDays) + 1;
}
function initTimeoutDate(){
console.log('==== initTimeoutDate begin ====')
let firstSaleDate = WfForm.getFieldValue(firstSaleDateField);
const nodeNum = getNodeNum();
console.log('到达节点次数 ', nodeNum);
console.log('跟踪天数 ', trackingDays);
let computeTimeoutDate = addDays(firstSaleDate, nodeNum * trackingDays);
console.log('计算下次超时日期 ', computeTimeoutDate);
let trackingLine = parseInt(WfForm.getFieldValue(trackingLineField));
let detail2LineNum = WfForm.getDetailRowCount("detail_2");
setTimeout(()=>{
WfForm.changeFieldValue(timeoutRemindDateFiled, {value: computeTimeoutDate});
// 判断流程提交走向
console.log('主表跟踪触发行数 : ', trackingLine)
if (nodeNum >= trackingLine) {
console.log('nodeNum >= trackingLine');
WfForm.changeFieldValue(submitWaitNode, {value: 1});
WfForm.changeFieldValue(timeoutRemindDateFiled, {value: ''});
WfForm.registerCheckEvent(WfForm.OPER_SUBMIT, function (callback) {
detail2LineNum = WfForm.getDetailRowCount("detail_2");
if (detail2LineNum < trackingLine) {
WfForm.showMessage('请填写明细表信息!');
detail2LineNum = WfForm.getDetailRowCount("detail_2");
for (let i = 0; i < trackingLine - parseInt(detail2LineNum); i++) {
WfForm.addDetailRow("detail_2", {});
}
return;
}
callback();
});
} else {
WfForm.changeFieldValue(submitWaitNode, {value: 0});
}
console.log('==== initTimeoutDate end ====')
},10);
}
function getDaysDiff(date1, date2) {
date1 = new Date(date1);
date2 = new Date(date2);
const oneDay = 24 * 60 * 60 * 1000; // 一天的毫秒数
const timeDiff = Math.abs(date1.getTime() - date2.getTime()); // 两个日期对象的毫秒数差值
// 将毫秒数差值转换为天数差值并四舍五入
return Math.round(timeDiff / oneDay);
}
function getCurrentDate() {
return parseDate(new Date());
}
function parseDate(date) {
const currentYear = date.getFullYear();
const currentMonth = date.getMonth() + 1; // getMonth()返回0~11需要加1
const currentDay = date.getDate();
return currentYear + '-' + currentMonth + '-' + currentDay;
}
function addDays(date, days) {
const newDate = new Date(date);
newDate.setDate(newDate.getDate() + days);
console.log('newDate ', newDate)
return parseDate(newDate);
}

View File

@ -681,3 +681,61 @@ $(() => {
}) })
/* ******************* 明细数据数量统计添加 end ******************* */ /* ******************* 明细数据数量统计添加 end ******************* */
/* ******************* 计算年月日 start ******************* */
$(() => {
const config = [{
// 源字段
sourceField: '',
// 目标字段
targetField: '',
// 日期加月份字段
numberField: ''
}, {
sourceField: '',
targetField: '',
numberField: ''
}]
runJs();
function runJs() {
config.forEach(item => {
bindAction(item)
})
}
function bindAction(configItem) {
let fieldId = WfForm.convertFieldNameToId(configItem.numberField)
WfForm.bindFieldChangeEvent(fieldId, (obj, id, value) => {
if ("" == value) {
WfForm.changeFieldValue(WfForm.convertFieldNameToId(configItem.targetField, {value: ""}))
return
}
let sourceValue = WfForm.getFieldValue(WfForm.convertFieldNameToId(configItem.sourceField));
let date = new Date(sourceValue)
date.setMonth(date.getMonth() + +value)
let objectDate = new Date();
let day = objectDate.getDate();
let month = objectDate.getMonth() + 1;
let year = objectDate.getFullYear();
WfForm.changeFieldValue(WfForm.convertFieldNameToId(configItem.targetField, {
value: `${year}-${fullNum(month)}-${fullNum(day)}`
}))
})
}
function fullNum(i) {
if (i <= 9) {
return '0' + i
} else {
return i
}
}
})
/* ******************* 计算年月日 end ******************* */

View File

@ -0,0 +1,25 @@
package aiyh.utils;
import aiyh.utils.tool.org.apache.commons.jexl3.*;
import java.util.Map;
/**
* <h1></h1>
*
* <p>create: 2023/3/3 23:03</p>
*
* @author youHong.ai
*/
public class ScriptUtil {
private static final JexlEngine jexl = new JexlBuilder().create();
public static Object invokeScript(String script, Map<String, Object> params) {
JexlContext jc = new MapContext();
for (Map.Entry<String, Object> entry : params.entrySet()) {
jc.set(entry.getKey(), entry.getValue());
}
JexlExpression expression = jexl.createExpression(script);
return expression.evaluate(jc);
}
}

View File

@ -3751,6 +3751,32 @@ public class Util extends weaver.general.Util {
return pathParamMap; return pathParamMap;
} }
public static <T> T getClassInstance(String classPath, Class<T> t) {
Class<?> aClass;
try {
aClass = Class.forName(classPath);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("未能找到自定义接口:" + classPath);
}
if (!t.isAssignableFrom(aClass)) {
throw new IllegalArgumentException("自定义接口:" + classPath + " 不是"
+ t.getName() + "的子类或实现类!");
}
Constructor<?> constructor;
try {
constructor = aClass.getConstructor();
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException(classPath + "没有空参构造方法,无法获取构造方法对象!");
}
T o;
try {
o = (T) constructor.newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new IllegalArgumentException("无法构造" + classPath + "对象!");
}
return o;
}
public static Object executeActionProcess(String process, RequestInfo requestInfo) { public static Object executeActionProcess(String process, RequestInfo requestInfo) {
if (StringUtils.isNullOrEmpty(process)) { if (StringUtils.isNullOrEmpty(process)) {
return null; return null;

View File

@ -32,6 +32,7 @@ import weaver.file.ImageFileManager;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import java.io.*; import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
@ -127,7 +128,11 @@ public class HttpUtils {
builder.append("&"); builder.append("&");
builder.append(entry.getKey()); builder.append(entry.getKey());
builder.append("="); builder.append("=");
builder.append(entry.getValue()); try {
builder.append(URLEncoder.encode(Util.null2String(entry.getValue()), "UTF-8"));
} catch (UnsupportedEncodingException e) {
builder.append(entry.getKey());
}
} }
return removeSeparator(builder); return removeSeparator(builder);
} }

View File

@ -0,0 +1,134 @@
package aiyh.utils.recordset;
import aiyh.utils.excention.CustomerException;
import aiyh.utils.tool.cn.hutool.core.collection.CollectionUtil;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
import java.util.Map;
/**
* <h1>sql</h1>
*
* <p>create: 2023/5/12 20:51</p>
*
* @author youHong.ai
*/
public class MapperBuilderSql {
/**
* <h2>sql</h2>
*
* @param table
* @param param
* @return sql
*/
public static String builderUpdateSql(String table, Map<String, Object> param) {
return builderUpdateSql(table, param, "");
}
/**
* <h2>sql</h2>
*
* @param table
* @param param
* @return sql
*/
public static String builderUpdateSql(String table, Map<String, Object> param, String paramPrefix) {
if (StrUtil.isBlank(table) || CollectionUtil.isEmpty(param)) {
throw new CustomerException("tableName or param can not to be null!");
}
StringBuilder sb = new StringBuilder();
sb.append("update ").append(table).append(" set ");
for (Map.Entry<String, Object> entry : param.entrySet()) {
sb.append(" ")
.append(entry.getKey())
.append(" = ")
.append("#{");
if (StrUtil.isNotBlank(paramPrefix)) {
sb.append(paramPrefix).append(".");
}
sb.append(entry.getKey())
.append("}")
.append(",");
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
/**
* <h2>sql</h2>
*
* @param table
* @param param
* @return sql
*/
public static String builderInsertSql(String table, Map<String, Object> param) {
return builderInsertSql(table, param, "");
}
public static String builderInsertSql(String table, Map<String, Object> param, String paramPrefix) {
if (StrUtil.isBlank(table) || CollectionUtil.isEmpty(param)) {
throw new CustomerException("tableName or param can not to be null!");
}
StringBuilder sb = new StringBuilder();
StringBuilder sbValue = new StringBuilder();
sb.append("insert into ").append(table).append(" (");
sbValue.append(") values ( ");
for (Map.Entry<String, Object> entry : param.entrySet()) {
sb.append(entry.getKey()).append(" ,");
sbValue.append(" #{");
if (StrUtil.isNotBlank(paramPrefix)) {
sbValue.append(paramPrefix).append(".");
}
sbValue.append(entry.getKey()).append("},");
}
sb.deleteCharAt(sb.length() - 1);
sbValue.deleteCharAt(sbValue.length() - 1);
sb.append(sbValue).append(")");
return sb.toString();
}
public static String builderWhereAnd(Map<String, Object> param, boolean containsWhere) {
return builderWhereAnd(param, "whereParam", containsWhere);
}
public static String builderWhereAnd(Map<String, Object> param) {
return builderWhereAnd(param, "whereParam", true);
}
public static String builderNoWhereAndEn(Map<String, Object> param, boolean containsWhere) {
return builderWhereAnd(param, "", containsWhere);
}
public static String builderNoWhereAndEn(Map<String, Object> param) {
return builderWhereAnd(param, "", false);
}
/**
* <h2>and</h2>
*
* @param param
* @param wherePrefix
* @param containsWhere where
* @return where
*/
public static String builderWhereAnd(Map<String, Object> param, String wherePrefix, boolean containsWhere) {
if (CollectionUtil.isEmpty(param)) {
throw new CustomerException("tableName or param can not to be null!");
}
StringBuilder sb = new StringBuilder();
if (containsWhere) {
sb.append(" where ");
}
for (Map.Entry<String, Object> entry : param.entrySet()) {
sb.append(entry.getKey()).append(" = #{");
if (StrUtil.isNotBlank(wherePrefix)) {
sb.append(wherePrefix).append(".");
}
sb.append(entry.getKey()).append("}").append(" and ");
}
sb.deleteCharAt(sb.length() - 5);
return sb.toString();
}
}

View File

@ -460,7 +460,7 @@ public class ResultMapper {
} }
if ("int".equalsIgnoreCase(columnType) || "long".equalsIgnoreCase(columnType) || "number".equalsIgnoreCase(columnType) || "MEDIUMINT".equalsIgnoreCase(columnType) || "TINYINT".equalsIgnoreCase(columnType) || "SMALLINT".equalsIgnoreCase(columnType) || "BIGINT".equalsIgnoreCase(columnType) || "INTEGER".equalsIgnoreCase(columnType)) { if ("int".equalsIgnoreCase(columnType) || "long".equalsIgnoreCase(columnType) || "number".equalsIgnoreCase(columnType) || "MEDIUMINT".equalsIgnoreCase(columnType) || "TINYINT".equalsIgnoreCase(columnType) || "SMALLINT".equalsIgnoreCase(columnType) || "BIGINT".equalsIgnoreCase(columnType) || "INTEGER".equalsIgnoreCase(columnType)) {
if (enable) { if (enable) {
((Map<? super Object, ? super Object>) o).put(Util.toCamelCase(columnName[i]), rs.getInt(i + 1)); ((Map<? super Object, ? super Object>) o).put(Util.toCamelCase(columnName[i]), rs.getInt(columnName[i]));
continue; continue;
} }
if (DBConstant.DB_TYPE_ORACLE.equals(rs.getDBType())) { if (DBConstant.DB_TYPE_ORACLE.equals(rs.getDBType())) {
@ -473,11 +473,11 @@ public class ResultMapper {
} }
if ("FLOAT".equalsIgnoreCase(columnType) || "DOUBLE".equalsIgnoreCase(columnType) || "DECIMAL".equalsIgnoreCase(columnType)) { if ("FLOAT".equalsIgnoreCase(columnType) || "DOUBLE".equalsIgnoreCase(columnType) || "DECIMAL".equalsIgnoreCase(columnType)) {
if (enable) { if (enable) {
((Map<? super Object, ? super Object>) o).put(Util.toCamelCase(columnName[i]), rs.getString(i + 1)); ((Map<? super Object, ? super Object>) o).put(Util.toCamelCase(columnName[i]), rs.getString(columnName[i]));
continue; continue;
} }
if (DBConstant.DB_TYPE_ORACLE.equals(rs.getDBType())) { if (DBConstant.DB_TYPE_ORACLE.equals(rs.getDBType())) {
((Map<? super Object, ? super Object>) o).put(Util.toCamelCase(columnName[i]), rs.getString(i + 1)); ((Map<? super Object, ? super Object>) o).put(Util.toCamelCase(columnName[i]), rs.getString(columnName[i]));
} }
((Map<? super Object, ? super Object>) o).put(columnName[i].toLowerCase(), rs.getString(i + 1)); ((Map<? super Object, ? super Object>) o).put(columnName[i].toLowerCase(), rs.getString(i + 1));
((Map<? super Object, ? super Object>) o).put(columnName[i].toUpperCase(), rs.getString(i + 1)); ((Map<? super Object, ? super Object>) o).put(columnName[i].toUpperCase(), rs.getString(i + 1));
@ -507,7 +507,7 @@ public class ResultMapper {
} }
} }
if (enable) { if (enable) {
((Map<? super Object, ? super Object>) o).put(Util.toCamelCase(columnName[i]), rs.getString(i + 1)); ((Map<? super Object, ? super Object>) o).put(Util.toCamelCase(columnName[i]), rs.getString(columnName[i]));
continue; continue;
} }
if (DBConstant.DB_TYPE_ORACLE.equals(rs.getDBType())) { if (DBConstant.DB_TYPE_ORACLE.equals(rs.getDBType())) {
@ -569,7 +569,9 @@ public class ResultMapper {
cassociationValue = paramType.get(declaredField.getType()).apply(String.valueOf(cassociationValue)); cassociationValue = paramType.get(declaredField.getType()).apply(String.valueOf(cassociationValue));
} }
try { try {
if (Objects.nonNull(cassociationValue)) {
propertyDescriptor.getWriteMethod().invoke(o, cassociationValue); propertyDescriptor.getWriteMethod().invoke(o, cassociationValue);
}
} catch (Exception e) { } catch (Exception e) {
Util.getLogger().error("实体数据写入报错:" + fieldName + " => " + value); Util.getLogger().error("实体数据写入报错:" + fieldName + " => " + value);
if (value != null) { if (value != null) {
@ -584,7 +586,9 @@ public class ResultMapper {
if (collectionMapping != null) { if (collectionMapping != null) {
Object collection = collection(rs, collectionMapping, method); Object collection = collection(rs, collectionMapping, method);
try { try {
if (Objects.nonNull(value)) {
propertyDescriptor.getWriteMethod().invoke(o, collection); propertyDescriptor.getWriteMethod().invoke(o, collection);
}
} catch (Exception e) { } catch (Exception e) {
Util.getLogger().error("实体数据写入报错:" + fieldName + " => " + value); Util.getLogger().error("实体数据写入报错:" + fieldName + " => " + value);
if (value != null) { if (value != null) {
@ -647,7 +651,6 @@ public class ResultMapper {
Association[] mappings = annotation.value(); Association[] mappings = annotation.value();
Association mapping = null; Association mapping = null;
for (Association item : mappings) { for (Association item : mappings) {
Util.getLogger().info("column: " + item.column() + " ===property: " + item.property() + " ====fieldName: " + filedName);
String property = isMap ? item.column() : item.property(); String property = isMap ? item.column() : item.property();
if (isMap ? filedName.equalsIgnoreCase(property) : filedName.equals(property)) { if (isMap ? filedName.equalsIgnoreCase(property) : filedName.equals(property)) {
mapping = item; mapping = item;
@ -660,7 +663,6 @@ public class ResultMapper {
Id id = annotation.id(); Id id = annotation.id();
String column = annotation.column(); String column = annotation.column();
String columnValue = rs.getString(column); String columnValue = rs.getString(column);
Util.getLogger().info(Arrays.toString(rs.getColumnName()));
if (Objects.isNull(columnValue) || "".equals(columnValue)) { if (Objects.isNull(columnValue) || "".equals(columnValue)) {
columnValue = rs.getString(column.toUpperCase()); columnValue = rs.getString(column.toUpperCase());
} }

View File

@ -5,18 +5,15 @@ import aiyh.utils.Util;
import aiyh.utils.mapUtil.UtilHashMap; import aiyh.utils.mapUtil.UtilHashMap;
import aiyh.utils.mapUtil.UtilLinkedHashMap; import aiyh.utils.mapUtil.UtilLinkedHashMap;
import aiyh.utils.sqlUtil.builderSql.BuilderSql; import aiyh.utils.sqlUtil.builderSql.BuilderSql;
import aiyh.utils.sqlUtil.sqlResult.impl.PrepSqlResultImpl;
import aiyh.utils.sqlUtil.sqlResult.impl.BatchSqlResultImpl; import aiyh.utils.sqlUtil.sqlResult.impl.BatchSqlResultImpl;
import aiyh.utils.sqlUtil.sqlResult.impl.PrepSqlResultImpl;
import aiyh.utils.sqlUtil.whereUtil.Where; import aiyh.utils.sqlUtil.whereUtil.Where;
import aiyh.utils.sqlUtil.whereUtil.impl.WhereImpl; import aiyh.utils.sqlUtil.whereUtil.impl.WhereImpl;
import weaver.conn.RecordSet; import weaver.conn.RecordSet;
import java.beans.BeanInfo; import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector; import java.beans.Introspector;
import java.beans.PropertyDescriptor; import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -29,7 +26,7 @@ import java.util.stream.Collectors;
public class BuilderSqlImpl implements BuilderSql { public class BuilderSqlImpl implements BuilderSql {
private String DB_TYPE; private final String DB_TYPE;
{ {
// 获取当前数据库的类型 // 获取当前数据库的类型
@ -38,6 +35,7 @@ public class BuilderSqlImpl implements BuilderSql {
/** /**
* *
*
* @param tableName * @param tableName
* @param mapConfig * @param mapConfig
* @return SQL * @return SQL
@ -66,12 +64,13 @@ public class BuilderSqlImpl implements BuilderSql {
/** /**
* SQL * SQL
*
* @param tableName * @param tableName
* @param t * @param t
* @param <T> * @param <T>
* @return SQL * @return SQL
*/ */
public <T> PrepSqlResultImpl insertSqlByEntity(String tableName, T t){ public <T> PrepSqlResultImpl insertSqlByEntity(String tableName, T t) {
List<Object> args = new ArrayList<>(); List<Object> args = new ArrayList<>();
StringBuilder sqlBuilder = new StringBuilder("insert into "); StringBuilder sqlBuilder = new StringBuilder("insert into ");
sqlBuilder.append(tableName); sqlBuilder.append(tableName);
@ -88,7 +87,7 @@ public class BuilderSqlImpl implements BuilderSql {
Object invoke = readMethod.invoke(t); Object invoke = readMethod.invoke(t);
// System.out.println(name); // System.out.println(name);
// System.out.println(invoke); // System.out.println(invoke);
if(invoke == null){ if (invoke == null) {
continue; continue;
} }
fieldBuilder.append(name); fieldBuilder.append(name);
@ -109,6 +108,7 @@ public class BuilderSqlImpl implements BuilderSql {
/** /**
* SQL * SQL
*
* @param tableName * @param tableName
* @param mapListConfig list * @param mapListConfig list
* @return SQL * @return SQL
@ -145,6 +145,7 @@ public class BuilderSqlImpl implements BuilderSql {
/** /**
* SQL * SQL
*
* @param tableName * @param tableName
* @param list * @param list
* @param <T> * @param <T>
@ -169,7 +170,7 @@ public class BuilderSqlImpl implements BuilderSql {
String name = proper.getName(); String name = proper.getName();
Method readMethod = proper.getReadMethod(); Method readMethod = proper.getReadMethod();
Object invoke = readMethod.invoke(item); Object invoke = readMethod.invoke(item);
if(invoke == null){ if (invoke == null) {
continue; continue;
} }
if (i.get() == 0) { if (i.get() == 0) {
@ -195,6 +196,7 @@ public class BuilderSqlImpl implements BuilderSql {
/** /**
* *
*
* @param tableName * @param tableName
* @param mapConfig * @param mapConfig
* @param where * @param where
@ -225,13 +227,14 @@ public class BuilderSqlImpl implements BuilderSql {
/** /**
* SQL * SQL
*
* @param tableName * @param tableName
* @param t * @param t
* @param where * @param where
* @param <T> * @param <T>
* @return SQL * @return SQL
*/ */
public <T> PrepSqlResultImpl updateSqlByEntity(String tableName, T t, Where where){ public <T> PrepSqlResultImpl updateSqlByEntity(String tableName, T t, Where where) {
this.verifyWhere(where); this.verifyWhere(where);
StringBuilder sqlBuilder = new StringBuilder("update "); StringBuilder sqlBuilder = new StringBuilder("update ");
StringBuilder fieldValue = new StringBuilder(); StringBuilder fieldValue = new StringBuilder();
@ -246,7 +249,7 @@ public class BuilderSqlImpl implements BuilderSql {
String name = proper.getName(); String name = proper.getName();
Method readMethod = proper.getReadMethod(); Method readMethod = proper.getReadMethod();
Object invoke = readMethod.invoke(t); Object invoke = readMethod.invoke(t);
if(invoke == null){ if (invoke == null) {
continue; continue;
} }
fieldValue.append(name); fieldValue.append(name);
@ -308,6 +311,7 @@ public class BuilderSqlImpl implements BuilderSql {
/** /**
* SQL * SQL
*
* @param tableName * @param tableName
* @param list * @param list
* @param whereList * @param whereList
@ -335,7 +339,7 @@ public class BuilderSqlImpl implements BuilderSql {
String name = proper.getName(); String name = proper.getName();
Method readMethod = proper.getReadMethod(); Method readMethod = proper.getReadMethod();
Object invoke = readMethod.invoke(item); Object invoke = readMethod.invoke(item);
if(invoke == null){ if (invoke == null) {
continue; continue;
} }
if (i.get() == 0) { if (i.get() == 0) {
@ -361,6 +365,7 @@ public class BuilderSqlImpl implements BuilderSql {
/** /**
* SQL * SQL
*
* @param tableName * @param tableName
* @param mapConfig * @param mapConfig
* @return SQL * @return SQL
@ -390,6 +395,7 @@ public class BuilderSqlImpl implements BuilderSql {
/** /**
* SQL * SQL
*
* @param tableName * @param tableName
* @param mapConfig * @param mapConfig
* @return SQL * @return SQL
@ -417,10 +423,9 @@ public class BuilderSqlImpl implements BuilderSql {
} }
/** /**
* SQLnull * SQLnull
*
* @param map SQLmap * @param map SQLmap
* @return UtilHashMap * @return UtilHashMap
*/ */
@ -436,6 +441,7 @@ public class BuilderSqlImpl implements BuilderSql {
/** /**
* SQL * SQL
*
* @param mapList SQL * @param mapList SQL
* @return * @return
*/ */
@ -456,6 +462,7 @@ public class BuilderSqlImpl implements BuilderSql {
/** /**
* where * where
*
* @param where where * @param where where
*/ */
private void verifyWhere(Where where) { private void verifyWhere(Where where) {
@ -463,16 +470,18 @@ public class BuilderSqlImpl implements BuilderSql {
throw new RuntimeException("where为null where is null!"); throw new RuntimeException("where为null where is null!");
} }
} }
/** /**
* where * where
*
* @param whereList where * @param whereList where
*/ */
private void verifyWhereList(List<Where> whereList) { private void verifyWhereList(List<Where> whereList) {
if (whereList == null) { if (whereList == null) {
throw new RuntimeException("whereList为null whereList is null!"); throw new RuntimeException("whereList为null whereList is null!");
} }
whereList.forEach(item->{ whereList.forEach(item -> {
if(item == null){ if (item == null) {
throw new RuntimeException("whereList中数据为null whereDate is null in whereList!"); throw new RuntimeException("whereList中数据为null whereDate is null in whereList!");
} }
}); });

View File

@ -0,0 +1,161 @@
package aiyh.utils.tool.cn.hutool.http;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
import java.nio.charset.Charset;
/**
* Content-Type
*
* @author looly
* @since 4.0.11
*/
public enum ContentType {
/**
* actiongetx-www-form-urlencodedformname1=value1&amp;name2=value2
*/
FORM_URLENCODED("application/x-www-form-urlencoded"),
/**
* Content-Disposition(boundary)
*/
MULTIPART("multipart/form-data"),
/**
* RestJSON
*/
JSON("application/json"),
/**
* RestXML
*/
XML("application/xml"),
/**
* text/plain
*/
TEXT_PLAIN("text/plain"),
/**
* Resttext/xml
*/
TEXT_XML("text/xml"),
/**
* text/html
*/
TEXT_HTML("text/html"),
/**
* application/octet-stream
*/
OCTET_STREAM("application/octet-stream");
private final String value;
/**
*
*
* @param value ContentType
*/
ContentType(String value) {
this.value = value;
}
/**
* value
*
* @return value
* @since 5.2.6
*/
public String getValue() {
return value;
}
@Override
public String toString() {
return getValue();
}
/**
* Content-Type
*
* @param charset
* @return Content-Type
*/
public String toString(Charset charset) {
return build(this.value, charset);
}
/**
* Content-Type{@code null}application/x-www-form-urlencoded
*
* @param contentType
* @return Content-Type
* @since 4.1.5
*/
public static boolean isDefault(String contentType) {
return null == contentType || isFormUrlEncode(contentType);
}
/**
* application/x-www-form-urlencoded
*
* @param contentType
* @return application/x-www-form-urlencoded
*/
public static boolean isFormUrlEncode(String contentType) {
return StrUtil.startWithIgnoreCase(contentType, FORM_URLENCODED.toString());
}
/**
* bodyContent-Type
*
* <pre>
* 1. application/json
* 1. application/xml
* </pre>
*
* @param body
* @return Content-Typenull
*/
public static ContentType get(String body) {
ContentType contentType = null;
if (StrUtil.isNotBlank(body)) {
char firstChar = body.charAt(0);
switch (firstChar) {
case '{':
case '[':
// JSON请求体
contentType = JSON;
break;
case '<':
// XML请求体
contentType = XML;
break;
default:
break;
}
}
return contentType;
}
/**
* Content-Type
*
* @param contentType Content-Type
* @param charset
* @return Content-Type
* @since 4.5.4
*/
public static String build(String contentType, Charset charset) {
return StrUtil.format("{};charset={}", contentType, charset.name());
}
/**
* Content-Type
*
* @param contentType Content-Type
* @param charset
* @return Content-Type
* @since 5.7.15
*/
public static String build(ContentType contentType, Charset charset) {
return build(contentType.getValue(), charset);
}
}

View File

@ -0,0 +1,229 @@
package aiyh.utils.tool.cn.hutool.http;
import aiyh.utils.tool.cn.hutool.core.collection.CollectionUtil;
import aiyh.utils.tool.cn.hutool.core.map.MapUtil;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
import java.util.*;
import java.util.Map.Entry;
/**
* <br>
* Http{@link HttpRequest}
*
* @author looly
*/
public enum GlobalHeaders {
INSTANCE;
/**
*
*/
final Map<String, List<String>> headers = new HashMap<>();
/**
*
*/
GlobalHeaders() {
putDefault(false);
}
/**
*
*
* @param isReset
* @return this
*/
public GlobalHeaders putDefault(boolean isReset) {
// 解决HttpURLConnection中无法自定义Host等头信息的问题
// https://stackoverflow.com/questions/9096987/how-to-overwrite-http-header-host-in-a-httpurlconnection/9098440
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
// 解决server certificate change is restricted during renegotiation问题
System.setProperty("jdk.tls.allowUnsafeServerCertChange", "true");
System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");
if (isReset) {
this.headers.clear();
}
header(aiyh.utils.tool.cn.hutool.http.Header.ACCEPT, "text/html,application/json,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", true);
header(aiyh.utils.tool.cn.hutool.http.Header.ACCEPT_ENCODING, "gzip, deflate", true);
header(aiyh.utils.tool.cn.hutool.http.Header.ACCEPT_LANGUAGE, "zh-CN,zh;q=0.8", true);
// 此Header只有在post请求中有用因此在HttpRequest的method方法中设置此头信息此处去掉
// header(Header.CONTENT_TYPE, ContentType.FORM_URLENCODED.toString(CharsetUtil.CHARSET_UTF_8), true);
header(aiyh.utils.tool.cn.hutool.http.Header.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 Hutool", true);
return this;
}
// ---------------------------------------------------------------- Headers start
/**
* name
*
* @param name Header
* @return Header
*/
public String header(String name) {
final List<String> values = headerList(name);
if (CollectionUtil.isEmpty(values)) {
return null;
}
return values.get(0);
}
/**
* name
*
* @param name Header
* @return Header
* @since 3.1.1
*/
public List<String> headerList(String name) {
if (StrUtil.isBlank(name)) {
return null;
}
return headers.get(name.trim());
}
/**
* name
*
* @param name Header
* @return Header
*/
public String header(aiyh.utils.tool.cn.hutool.http.Header name) {
if (null == name) {
return null;
}
return header(name.toString());
}
/**
* header<br>
*
*
* @param name Header
* @param value Header
* @param isOverride
* @return this
*/
synchronized public GlobalHeaders header(String name, String value, boolean isOverride) {
if (null != name && null != value) {
final List<String> values = headers.get(name.trim());
if (isOverride || CollectionUtil.isEmpty(values)) {
final ArrayList<String> valueList = new ArrayList<>();
valueList.add(value);
headers.put(name.trim(), valueList);
} else {
values.add(value.trim());
}
}
return this;
}
/**
* header<br>
*
*
* @param name Header
* @param value Header
* @param isOverride
* @return this
*/
public GlobalHeaders header(aiyh.utils.tool.cn.hutool.http.Header name, String value, boolean isOverride) {
return header(name.toString(), value, isOverride);
}
/**
* header<br>
*
*
* @param name Header
* @param value Header
* @return this
*/
public GlobalHeaders header(aiyh.utils.tool.cn.hutool.http.Header name, String value) {
return header(name.toString(), value, true);
}
/**
* header<br>
*
*
* @param name Header
* @param value Header
* @return this
*/
public GlobalHeaders header(String name, String value) {
return header(name, value, true);
}
/**
* <br>
*
*
* @param headers
* @return this
*/
public GlobalHeaders header(Map<String, List<String>> headers) {
if (MapUtil.isEmpty(headers)) {
return this;
}
String name;
for (Entry<String, List<String>> entry : headers.entrySet()) {
name = entry.getKey();
for (String value : entry.getValue()) {
this.header(name, StrUtil.nullToEmpty(value), false);
}
}
return this;
}
/**
*
*
* @param name Header
* @return this
*/
synchronized public GlobalHeaders removeHeader(String name) {
if (name != null) {
headers.remove(name.trim());
}
return this;
}
/**
*
*
* @param name Header
* @return this
*/
public GlobalHeaders removeHeader(Header name) {
return removeHeader(name.toString());
}
/**
* headers
*
* @return Headers Map
*/
public Map<String, List<String>> headers() {
return Collections.unmodifiableMap(headers);
}
/**
*
*
* @return this
* @since 5.7.13
*/
synchronized public GlobalHeaders clearHeaders() {
this.headers.clear();
return this;
}
// ---------------------------------------------------------------- Headers end
}

View File

@ -0,0 +1,94 @@
package aiyh.utils.tool.cn.hutool.http;
/**
* <br>
*
*
* @author looly
* @since 5.8.0
*/
public enum GlobalInterceptor {
INSTANCE;
private final HttpInterceptor.Chain<aiyh.utils.tool.cn.hutool.http.HttpRequest> requestInterceptors = new HttpInterceptor.Chain<>();
private final HttpInterceptor.Chain<aiyh.utils.tool.cn.hutool.http.HttpResponse> responseInterceptors = new HttpInterceptor.Chain<>();
/**
*
*
* @param interceptor
* @return this
*/
synchronized public GlobalInterceptor addRequestInterceptor(HttpInterceptor<aiyh.utils.tool.cn.hutool.http.HttpRequest> interceptor) {
this.requestInterceptors.addChain(interceptor);
return this;
}
/**
*
*
* @param interceptor
* @return this
*/
synchronized public GlobalInterceptor addResponseInterceptor(HttpInterceptor<aiyh.utils.tool.cn.hutool.http.HttpResponse> interceptor) {
this.responseInterceptors.addChain(interceptor);
return this;
}
/**
*
*
* @return this
*/
public GlobalInterceptor clear() {
clearRequest();
clearResponse();
return this;
}
/**
*
*
* @return this
*/
synchronized public GlobalInterceptor clearRequest() {
requestInterceptors.clear();
return this;
}
/**
*
*
* @return this
*/
synchronized public GlobalInterceptor clearResponse() {
responseInterceptors.clear();
return this;
}
/**
*
*
* @return {@link HttpInterceptor.Chain}
*/
HttpInterceptor.Chain<aiyh.utils.tool.cn.hutool.http.HttpRequest> getCopiedRequestInterceptor() {
final HttpInterceptor.Chain<aiyh.utils.tool.cn.hutool.http.HttpRequest> copied = new HttpInterceptor.Chain<>();
for (HttpInterceptor<HttpRequest> interceptor : this.requestInterceptors) {
copied.addChain(interceptor);
}
return copied;
}
/**
*
*
* @return {@link HttpInterceptor.Chain}
*/
HttpInterceptor.Chain<aiyh.utils.tool.cn.hutool.http.HttpResponse> getCopiedResponseInterceptor() {
final HttpInterceptor.Chain<aiyh.utils.tool.cn.hutool.http.HttpResponse> copied = new HttpInterceptor.Chain<>();
for (HttpInterceptor<HttpResponse> interceptor : this.responseInterceptors) {
copied.addChain(interceptor);
}
return copied;
}
}

View File

@ -0,0 +1,539 @@
package aiyh.utils.tool.cn.hutool.http;
import aiyh.utils.tool.cn.hutool.core.lang.Console;
import aiyh.utils.tool.cn.hutool.core.map.SafeConcurrentHashMap;
import aiyh.utils.tool.cn.hutool.core.util.CharUtil;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* HTMLXSS(Cross Site Scripting)
*
* <p>
* 线
* </p>
*
* <pre>
* String clean = new HTMLFilter().filter(input);
* </pre>
* <p>
* http://xss-html-filter.sf.net
*
* @author Joseph O'Connell
* @author Cal Hendersen
* @author Michael Semb Wever
*/
public final class HTMLFilter {
/**
* regex flag union representing /si modifiers in php
**/
private static final int REGEX_FLAGS_SI = Pattern.CASE_INSENSITIVE | Pattern.DOTALL;
private static final Pattern P_COMMENTS = Pattern.compile("<!--(.*?)-->", Pattern.DOTALL);
private static final Pattern P_COMMENT = Pattern.compile("^!--(.*)--$", REGEX_FLAGS_SI);
private static final Pattern P_TAGS = Pattern.compile("<(.*?)>", Pattern.DOTALL);
private static final Pattern P_END_TAG = Pattern.compile("^/([a-z0-9]+)", REGEX_FLAGS_SI);
private static final Pattern P_START_TAG = Pattern.compile("^([a-z0-9]+)(.*?)(/?)$", REGEX_FLAGS_SI);
private static final Pattern P_QUOTED_ATTRIBUTES = Pattern.compile("([a-z0-9]+)=([\"'])(.*?)\\2", REGEX_FLAGS_SI);
private static final Pattern P_UNQUOTED_ATTRIBUTES = Pattern.compile("([a-z0-9]+)(=)([^\"\\s']+)", REGEX_FLAGS_SI);
private static final Pattern P_PROTOCOL = Pattern.compile("^([^:]+):", REGEX_FLAGS_SI);
private static final Pattern P_ENTITY = Pattern.compile("&#(\\d+);?");
private static final Pattern P_ENTITY_UNICODE = Pattern.compile("&#x([0-9a-f]+);?");
private static final Pattern P_ENCODE = Pattern.compile("%([0-9a-f]{2});?");
private static final Pattern P_VALID_ENTITIES = Pattern.compile("&([^&;]*)(?=(;|&|$))");
private static final Pattern P_VALID_QUOTES = Pattern.compile("(>|^)([^<]+?)(<|$)", Pattern.DOTALL);
private static final Pattern P_END_ARROW = Pattern.compile("^>");
private static final Pattern P_BODY_TO_END = Pattern.compile("<([^>]*?)(?=<|$)");
private static final Pattern P_XML_CONTENT = Pattern.compile("(^|>)([^<]*?)(?=>)");
private static final Pattern P_STRAY_LEFT_ARROW = Pattern.compile("<([^>]*?)(?=<|$)");
private static final Pattern P_STRAY_RIGHT_ARROW = Pattern.compile("(^|>)([^<]*?)(?=>)");
private static final Pattern P_AMP = Pattern.compile("&");
private static final Pattern P_QUOTE = Pattern.compile("\"");
private static final Pattern P_LEFT_ARROW = Pattern.compile("<");
private static final Pattern P_RIGHT_ARROW = Pattern.compile(">");
private static final Pattern P_BOTH_ARROWS = Pattern.compile("<>");
// @xxx could grow large... maybe use sesat's ReferenceMap
private static final ConcurrentMap<String, Pattern> P_REMOVE_PAIR_BLANKS = new SafeConcurrentHashMap<>();
private static final ConcurrentMap<String, Pattern> P_REMOVE_SELF_BLANKS = new SafeConcurrentHashMap<>();
/**
* set of allowed html elements, along with allowed attributes for each element
**/
private final Map<String, List<String>> vAllowed;
/**
* counts of open tags for each (allowable) html element
**/
private final Map<String, Integer> vTagCounts = new HashMap<>();
/**
* html elements which must always be self-closing (e.g. "&lt;img /&gt;")
**/
private final String[] vSelfClosingTags;
/**
* html elements which must always have separate opening and closing tags (e.g. "&lt;b&gt;&lt;/b&gt;")
**/
private final String[] vNeedClosingTags;
/**
* set of disallowed html elements
**/
private final String[] vDisallowed;
/**
* attributes which should be checked for valid protocols
**/
private final String[] vProtocolAtts;
/**
* allowed protocols
**/
private final String[] vAllowedProtocols;
/**
* tags which should be removed if they contain no content (e.g. "&lt;b&gt;&lt;/b&gt;" or "&lt;b /&gt;")
**/
private final String[] vRemoveBlanks;
/**
* entities allowed within html markup
**/
private final String[] vAllowedEntities;
/**
* flag determining whether comments are allowed in input String.
*/
private final boolean stripComment;
private final boolean encodeQuotes;
private boolean vDebug = false;
/**
* flag determining whether to try to make tags when presented with "unbalanced" angle brackets (e.g. "&lt;b text &lt;/b&gt;" becomes "&lt;b&gt; text &lt;/g&gt;").
* If set to false, unbalanced angle brackets will be
* html escaped.
*/
private final boolean alwaysMakeTags;
/**
* Default constructor.
*/
public HTMLFilter() {
vAllowed = new HashMap<>();
final ArrayList<String> a_atts = new ArrayList<>();
a_atts.add("href");
a_atts.add("target");
vAllowed.put("a", a_atts);
final ArrayList<String> img_atts = new ArrayList<>();
img_atts.add("src");
img_atts.add("width");
img_atts.add("height");
img_atts.add("alt");
vAllowed.put("img", img_atts);
final ArrayList<String> no_atts = new ArrayList<>();
vAllowed.put("b", no_atts);
vAllowed.put("strong", no_atts);
vAllowed.put("i", no_atts);
vAllowed.put("em", no_atts);
vSelfClosingTags = new String[]{"img"};
vNeedClosingTags = new String[]{"a", "b", "strong", "i", "em"};
vDisallowed = new String[]{};
vAllowedProtocols = new String[]{"http", "mailto", "https"}; // no ftp.
vProtocolAtts = new String[]{"src", "href"};
vRemoveBlanks = new String[]{"a", "b", "strong", "i", "em"};
vAllowedEntities = new String[]{"amp", "gt", "lt", "quot"};
stripComment = true;
encodeQuotes = true;
alwaysMakeTags = true;
}
/**
* Set debug flag to true. Otherwise use default settings. See the default constructor.
*
* @param debug turn debug on with a true argument
*/
public HTMLFilter(final boolean debug) {
this();
vDebug = debug;
}
/**
* Map-parameter configurable constructor.
*
* @param conf map containing configuration. keys match field names.
*/
@SuppressWarnings("unchecked")
public HTMLFilter(final Map<String, Object> conf) {
assert conf.containsKey("vAllowed") : "configuration requires vAllowed";
assert conf.containsKey("vSelfClosingTags") : "configuration requires vSelfClosingTags";
assert conf.containsKey("vNeedClosingTags") : "configuration requires vNeedClosingTags";
assert conf.containsKey("vDisallowed") : "configuration requires vDisallowed";
assert conf.containsKey("vAllowedProtocols") : "configuration requires vAllowedProtocols";
assert conf.containsKey("vProtocolAtts") : "configuration requires vProtocolAtts";
assert conf.containsKey("vRemoveBlanks") : "configuration requires vRemoveBlanks";
assert conf.containsKey("vAllowedEntities") : "configuration requires vAllowedEntities";
vAllowed = Collections.unmodifiableMap((HashMap<String, List<String>>) conf.get("vAllowed"));
vSelfClosingTags = (String[]) conf.get("vSelfClosingTags");
vNeedClosingTags = (String[]) conf.get("vNeedClosingTags");
vDisallowed = (String[]) conf.get("vDisallowed");
vAllowedProtocols = (String[]) conf.get("vAllowedProtocols");
vProtocolAtts = (String[]) conf.get("vProtocolAtts");
vRemoveBlanks = (String[]) conf.get("vRemoveBlanks");
vAllowedEntities = (String[]) conf.get("vAllowedEntities");
stripComment = conf.containsKey("stripComment") ? (Boolean) conf.get("stripComment") : true;
encodeQuotes = conf.containsKey("encodeQuotes") ? (Boolean) conf.get("encodeQuotes") : true;
alwaysMakeTags = conf.containsKey("alwaysMakeTags") ? (Boolean) conf.get("alwaysMakeTags") : true;
}
private void reset() {
vTagCounts.clear();
}
private void debug(final String msg) {
if (vDebug) {
Console.log(msg);
}
}
// ---------------------------------------------------------------
// my versions of some PHP library functions
public static String chr(final int decimal) {
return String.valueOf((char) decimal);
}
public static String htmlSpecialChars(final String s) {
String result = s;
result = regexReplace(P_AMP, "&amp;", result);
result = regexReplace(P_QUOTE, "&quot;", result);
result = regexReplace(P_LEFT_ARROW, "&lt;", result);
result = regexReplace(P_RIGHT_ARROW, "&gt;", result);
return result;
}
// ---------------------------------------------------------------
/**
* given a user submitted input String, filter out any invalid or restricted html.
*
* @param input text (i.e. submitted by a user) than may contain html
* @return "clean" version of input, with only valid, whitelisted html elements allowed
*/
public String filter(final String input) {
reset();
String s = input;
debug("************************************************");
debug(" INPUT: " + input);
s = escapeComments(s);
debug(" escapeComments: " + s);
s = balanceHTML(s);
debug(" balanceHTML: " + s);
s = checkTags(s);
debug(" checkTags: " + s);
s = processRemoveBlanks(s);
debug("processRemoveBlanks: " + s);
s = validateEntities(s);
debug(" validateEntites: " + s);
debug("************************************************\n\n");
return s;
}
public boolean isAlwaysMakeTags() {
return alwaysMakeTags;
}
public boolean isStripComments() {
return stripComment;
}
private String escapeComments(final String s) {
final Matcher m = P_COMMENTS.matcher(s);
final StringBuffer buf = new StringBuffer();
if (m.find()) {
final String match = m.group(1); // (.*?)
m.appendReplacement(buf, Matcher.quoteReplacement("<!--" + htmlSpecialChars(match) + "-->"));
}
m.appendTail(buf);
return buf.toString();
}
private String balanceHTML(String s) {
if (alwaysMakeTags) {
//
// try and form html
//
s = regexReplace(P_END_ARROW, "", s);
s = regexReplace(P_BODY_TO_END, "<$1>", s);
s = regexReplace(P_XML_CONTENT, "$1<$2", s);
} else {
//
// escape stray brackets
//
s = regexReplace(P_STRAY_LEFT_ARROW, "&lt;$1", s);
s = regexReplace(P_STRAY_RIGHT_ARROW, "$1$2&gt;<", s);
//
// the last regexp causes '<>' entities to appear
// (we need to do a lookahead assertion so that the last bracket can
// be used in the next pass of the regexp)
//
s = regexReplace(P_BOTH_ARROWS, "", s);
}
return s;
}
private String checkTags(String s) {
Matcher m = P_TAGS.matcher(s);
final StringBuffer buf = new StringBuffer();
while (m.find()) {
String replaceStr = m.group(1);
replaceStr = processTag(replaceStr);
m.appendReplacement(buf, Matcher.quoteReplacement(replaceStr));
}
m.appendTail(buf);
// these get tallied in processTag
// (remember to reset before subsequent calls to filter method)
final StringBuilder sBuilder = new StringBuilder(buf.toString());
for (String key : vTagCounts.keySet()) {
for (int ii = 0; ii < vTagCounts.get(key); ii++) {
sBuilder.append("</").append(key).append(">");
}
}
s = sBuilder.toString();
return s;
}
private String processRemoveBlanks(final String s) {
String result = s;
for (String tag : vRemoveBlanks) {
if (!P_REMOVE_PAIR_BLANKS.containsKey(tag)) {
P_REMOVE_PAIR_BLANKS.putIfAbsent(tag, Pattern.compile("<" + tag + "(\\s[^>]*)?></" + tag + ">"));
}
result = regexReplace(P_REMOVE_PAIR_BLANKS.get(tag), "", result);
if (!P_REMOVE_SELF_BLANKS.containsKey(tag)) {
P_REMOVE_SELF_BLANKS.putIfAbsent(tag, Pattern.compile("<" + tag + "(\\s[^>]*)?/>"));
}
result = regexReplace(P_REMOVE_SELF_BLANKS.get(tag), "", result);
}
return result;
}
private static String regexReplace(final Pattern regex_pattern, final String replacement, final String s) {
Matcher m = regex_pattern.matcher(s);
return m.replaceAll(replacement);
}
private String processTag(final String s) {
// ending tags
Matcher m = P_END_TAG.matcher(s);
if (m.find()) {
final String name = m.group(1).toLowerCase();
if (allowed(name)) {
if (!inArray(name, vSelfClosingTags)) {
if (vTagCounts.containsKey(name)) {
vTagCounts.put(name, vTagCounts.get(name) - 1);
return "</" + name + ">";
}
}
}
}
// starting tags
m = P_START_TAG.matcher(s);
if (m.find()) {
final String name = m.group(1).toLowerCase();
final String body = m.group(2);
String ending = m.group(3);
// debug( "in a starting tag, name='" + name + "'; body='" + body + "'; ending='" + ending + "'" );
if (allowed(name)) {
final StringBuilder params = new StringBuilder();
final Matcher m2 = P_QUOTED_ATTRIBUTES.matcher(body);
final Matcher m3 = P_UNQUOTED_ATTRIBUTES.matcher(body);
final List<String> paramNames = new ArrayList<>();
final List<String> paramValues = new ArrayList<>();
while (m2.find()) {
paramNames.add(m2.group(1)); // ([a-z0-9]+)
paramValues.add(m2.group(3)); // (.*?)
}
while (m3.find()) {
paramNames.add(m3.group(1)); // ([a-z0-9]+)
paramValues.add(m3.group(3)); // ([^\"\\s']+)
}
String paramName, paramValue;
for (int ii = 0; ii < paramNames.size(); ii++) {
paramName = paramNames.get(ii).toLowerCase();
paramValue = paramValues.get(ii);
// debug( "paramName='" + paramName + "'" );
// debug( "paramValue='" + paramValue + "'" );
// debug( "allowed? " + vAllowed.get( name ).contains( paramName ) );
if (allowedAttribute(name, paramName)) {
if (inArray(paramName, vProtocolAtts)) {
paramValue = processParamProtocol(paramValue);
}
params.append(CharUtil.SPACE).append(paramName).append("=\"").append(paramValue).append("\"");
}
}
if (inArray(name, vSelfClosingTags)) {
ending = " /";
}
if (inArray(name, vNeedClosingTags)) {
ending = "";
}
if (ending == null || ending.length() < 1) {
if (vTagCounts.containsKey(name)) {
vTagCounts.put(name, vTagCounts.get(name) + 1);
} else {
vTagCounts.put(name, 1);
}
} else {
ending = " /";
}
return "<" + name + params + ending + ">";
} else {
return "";
}
}
// comments
m = P_COMMENT.matcher(s);
if (!stripComment && m.find()) {
return "<" + m.group() + ">";
}
return "";
}
private String processParamProtocol(String s) {
s = decodeEntities(s);
final Matcher m = P_PROTOCOL.matcher(s);
if (m.find()) {
final String protocol = m.group(1);
if (!inArray(protocol, vAllowedProtocols)) {
// bad protocol, turn into local anchor link instead
s = "#" + s.substring(protocol.length() + 1);
if (s.startsWith("#//")) {
s = "#" + s.substring(3);
}
}
}
return s;
}
private String decodeEntities(String s) {
StringBuffer buf = new StringBuffer();
Matcher m = P_ENTITY.matcher(s);
while (m.find()) {
final String match = m.group(1);
final int decimal = Integer.decode(match);
m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal)));
}
m.appendTail(buf);
s = buf.toString();
buf = new StringBuffer();
m = P_ENTITY_UNICODE.matcher(s);
while (m.find()) {
final String match = m.group(1);
final int decimal = Integer.parseInt(match, 16);
m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal)));
}
m.appendTail(buf);
s = buf.toString();
buf = new StringBuffer();
m = P_ENCODE.matcher(s);
while (m.find()) {
final String match = m.group(1);
final int decimal = Integer.parseInt(match, 16);
m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal)));
}
m.appendTail(buf);
s = buf.toString();
s = validateEntities(s);
return s;
}
private String validateEntities(final String s) {
StringBuffer buf = new StringBuffer();
// validate entities throughout the string
Matcher m = P_VALID_ENTITIES.matcher(s);
while (m.find()) {
final String one = m.group(1); // ([^&;]*)
final String two = m.group(2); // (?=(;|&|$))
m.appendReplacement(buf, Matcher.quoteReplacement(checkEntity(one, two)));
}
m.appendTail(buf);
return encodeQuotes(buf.toString());
}
private String encodeQuotes(final String s) {
if (encodeQuotes) {
StringBuffer buf = new StringBuffer();
Matcher m = P_VALID_QUOTES.matcher(s);
while (m.find()) {
final String one = m.group(1); // (>|^)
final String two = m.group(2); // ([^<]+?)
final String three = m.group(3); // (<|$)
m.appendReplacement(buf, Matcher.quoteReplacement(one + regexReplace(P_QUOTE, "&quot;", two) + three));
}
m.appendTail(buf);
return buf.toString();
} else {
return s;
}
}
private String checkEntity(final String preamble, final String term) {
return ";".equals(term) && isValidEntity(preamble) ? '&' + preamble : "&amp;" + preamble;
}
private boolean isValidEntity(final String entity) {
return inArray(entity, vAllowedEntities);
}
private static boolean inArray(final String s, final String[] array) {
for (String item : array) {
if (item != null && item.equals(s)) {
return true;
}
}
return false;
}
private boolean allowed(final String name) {
return (vAllowed.isEmpty() || vAllowed.containsKey(name)) && !inArray(name, vDisallowed);
}
private boolean allowedAttribute(final String name, final String paramName) {
return allowed(name) && (vAllowed.isEmpty() || vAllowed.get(name).contains(paramName));
}
}

View File

@ -0,0 +1,153 @@
package aiyh.utils.tool.cn.hutool.http;
/**
* Http
*
* @author Looly
*/
public enum Header {
//------------------------------------------------------------- 通用头域
/**
*
* <pre>
* Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
* </pre>
*/
AUTHORIZATION("Authorization"),
/**
*
* <pre>
* Proxy-Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
* </pre>
*/
PROXY_AUTHORIZATION("Proxy-Authorization"),
/**
* ,
*/
DATE("Date"),
/**
* /
*/
CONNECTION("Connection"),
/**
* 使MIME
*/
MIME_VERSION("MIME-Version"),
/**
* (chunked transfer encoding) ,(trailer)
*/
TRAILER("Trailer"),
/**
* ,
*/
TRANSFER_ENCODING("Transfer-Encoding"),
/**
* "升级"使
*/
UPGRADE("Upgrade"),
/**
*
*/
VIA("Via"),
/**
*
*/
CACHE_CONTROL("Cache-Control"),
/**
* Pragma:no-cacheHTTP/1.1Cache- Control:no-cache
*/
PRAGMA("Pragma"),
/**
* MIME
*/
CONTENT_TYPE("Content-Type"),
//------------------------------------------------------------- 请求头域
/**
* InteneturlHTTP/1.1400
*/
HOST("Host"),
/**
* uri退cache uriuriRefereruri
*/
REFERER("Referer"),
/**
*
*/
ORIGIN("Origin"),
/**
* HTTPwebHTTP
*/
USER_AGENT("User-Agent"),
/**
*
*/
ACCEPT("Accept"),
/**
* HTTP
*/
ACCEPT_LANGUAGE("Accept-Language"),
/**
* web
*/
ACCEPT_ENCODING("Accept-Encoding"),
/**
*
*/
ACCEPT_CHARSET("Accept-Charset"),
/**
* HTTPcookieweb
*/
COOKIE("Cookie"),
/**
*
*/
CONTENT_LENGTH("Content-Length"),
//------------------------------------------------------------- 响应头域
/**
* WWW
*/
WWW_AUTHENTICATE("WWW-Authenticate"),
/**
* Cookie
*/
SET_COOKIE("Set-Cookie"),
/**
* Content-Encoding
*/
CONTENT_ENCODING("Content-Encoding"),
/**
* Content-Disposition
*/
CONTENT_DISPOSITION("Content-Disposition"),
/**
* ETag
*/
ETAG("ETag"),
/**
* URL
*/
LOCATION("Location");
private final String value;
Header(String value) {
this.value = value;
}
/**
*
*
* @return
*/
public String getValue() {
return this.value;
}
@Override
public String toString() {
return getValue();
}
}

View File

@ -0,0 +1,212 @@
package aiyh.utils.tool.cn.hutool.http;
import aiyh.utils.tool.cn.hutool.core.util.EscapeUtil;
import aiyh.utils.tool.cn.hutool.core.util.ReUtil;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
/**
* HTML
*
* <p>
* 使HTMLHTML<br>
* 广JS使
*
* @author xiaoleilu
*/
public class HtmlUtil {
public static final String NBSP = StrUtil.HTML_NBSP;
public static final String AMP = StrUtil.HTML_AMP;
public static final String QUOTE = StrUtil.HTML_QUOTE;
public static final String APOS = StrUtil.HTML_APOS;
public static final String LT = StrUtil.HTML_LT;
public static final String GT = StrUtil.HTML_GT;
public static final String RE_HTML_MARK = "(<[^<]*?>)|(<[\\s]*?/[^<]*?>)|(<[^<]*?/[\\s]*?>)";
public static final String RE_SCRIPT = "<[\\s]*?script[^>]*?>.*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
private static final char[][] TEXT = new char[256][];
static {
// ascii码值最大的是【0x7f=127】扩展ascii码值最大的是【0xFF=255】因为ASCII码使用指定的7位或8位二进制数组合来表示128或256种可能的字符标准ASCII码也叫基础ASCII码。
for (int i = 0; i < 256; i++) {
TEXT[i] = new char[]{(char) i};
}
// special HTML characters
TEXT['\''] = "&#039;".toCharArray(); // 单引号 ('&apos;' doesn't work - it is not by the w3 specs)
TEXT['"'] = QUOTE.toCharArray(); // 双引号
TEXT['&'] = AMP.toCharArray(); // &符
TEXT['<'] = LT.toCharArray(); // 小于号
TEXT['>'] = GT.toCharArray(); // 大于号
TEXT[' '] = NBSP.toCharArray(); // 不断开空格non-breaking space缩写nbsp。ASCII值是32是用键盘输入的空格ASCII值是160不间断空格即 &nbsp所产生的空格作用是在页面换行时不被打断
}
/**
* HTML
* <ul>
* <li>' &amp;#039; (&amp;apos; doesn't work in HTML4)</li>
* <li>" &amp;quot;</li>
* <li>&amp; &amp;amp;</li>
* <li>&lt; &amp;lt;</li>
* <li>&gt; &amp;gt;</li>
* </ul>
*
* @param text
* @return
*/
public static String escape(String text) {
return encode(text);
}
/**
* HTML
*
* @param htmlStr HTML
* @return
*/
public static String unescape(String htmlStr) {
if (StrUtil.isBlank(htmlStr)) {
return htmlStr;
}
return EscapeUtil.unescapeHtml4(htmlStr);
}
// ---------------------------------------------------------------- encode text
/**
* HTML
*
* @param content
* @return
*/
public static String cleanHtmlTag(String content) {
return content.replaceAll(RE_HTML_MARK, "");
}
/**
* HTML<br>
*
*
* @param content
* @param tagNames
* @return
*/
public static String removeHtmlTag(String content, String... tagNames) {
return removeHtmlTag(content, true, tagNames);
}
/**
* HTML<br>
*
*
* @param content
* @param tagNames
* @return
*/
public static String unwrapHtmlTag(String content, String... tagNames) {
return removeHtmlTag(content, false, tagNames);
}
/**
* HTML<br>
*
*
* @param content
* @param withTagContent
* @param tagNames
* @return
*/
public static String removeHtmlTag(String content, boolean withTagContent, String... tagNames) {
String regex;
for (String tagName : tagNames) {
if (StrUtil.isBlank(tagName)) {
continue;
}
tagName = tagName.trim();
// (?i)表示其后面的表达式忽略大小写
if (withTagContent) {
// 标签及其包含内容
regex = StrUtil.format("(?i)<{}(\\s+[^>]*?)?/?>(.*?</{}>)?", tagName, tagName);
} else {
// 标签不包含内容
regex = StrUtil.format("(?i)<{}(\\s+[^>]*?)?/?>|</?{}>", tagName, tagName);
}
content = ReUtil.delAll(regex, content); // 非自闭标签小写
}
return content;
}
/**
* HTML
*
* @param content
* @param attrs
* @return
*/
public static String removeHtmlAttr(String content, String... attrs) {
String regex;
for (String attr : attrs) {
// (?i) 表示忽略大小写
// \s* 属性名前后的空白符去除
// [^>]+? 属性值,至少有一个非>的字符,>表示标签结束
// \s+(?=>) 表示属性值后跟空格加>,即末尾的属性,此时去掉空格
// (?=\s|>) 表示属性值后跟空格(属性后还有别的属性)或者跟>(最后一个属性)
regex = StrUtil.format("(?i)(\\s*{}\\s*=[^>]+?\\s+(?=>))|(\\s*{}\\s*=[^>]+?(?=\\s|>))", attr, attr);
content = content.replaceAll(regex, StrUtil.EMPTY);
}
return content;
}
/**
*
*
* @param content
* @param tagNames
* @return
*/
public static String removeAllHtmlAttr(String content, String... tagNames) {
String regex;
for (String tagName : tagNames) {
regex = StrUtil.format("(?i)<{}[^>]*?>", tagName);
content = content.replaceAll(regex, StrUtil.format("<{}>", tagName));
}
return content;
}
/**
* Encoder
*
* @param text
* @return
*/
private static String encode(String text) {
int len;
if ((text == null) || ((len = text.length()) == 0)) {
return StrUtil.EMPTY;
}
StringBuilder buffer = new StringBuilder(len + (len >> 2));
char c;
for (int i = 0; i < len; i++) {
c = text.charAt(i);
if (c < 256) {
buffer.append(TEXT[c]);
} else {
buffer.append(c);
}
}
return buffer.toString();
}
/**
* HTMLXSS
*
* @param htmlContent HTML
* @return
*/
public static String filter(String htmlContent) {
return new HTMLFilter().filter(htmlContent);
}
}

View File

@ -0,0 +1,357 @@
package aiyh.utils.tool.cn.hutool.http;
import aiyh.utils.tool.cn.hutool.core.collection.CollUtil;
import aiyh.utils.tool.cn.hutool.core.collection.CollectionUtil;
import aiyh.utils.tool.cn.hutool.core.map.CaseInsensitiveMap;
import aiyh.utils.tool.cn.hutool.core.map.MapUtil;
import aiyh.utils.tool.cn.hutool.core.util.CharsetUtil;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
import java.nio.charset.Charset;
import java.util.*;
import java.util.Map.Entry;
/**
* http
*
* @param <T> 便
* @author Looly
*/
@SuppressWarnings("unchecked")
public abstract class HttpBase<T> {
/**
* URLencodedecode
*/
protected static final Charset DEFAULT_CHARSET = CharsetUtil.CHARSET_UTF_8;
/**
* HTTP/1.0
*/
public static final String HTTP_1_0 = "HTTP/1.0";
/**
* HTTP/1.1
*/
public static final String HTTP_1_1 = "HTTP/1.1";
/**
*
*/
protected Map<String, List<String>> headers = new HashMap<>();
/**
*
*/
protected Charset charset = DEFAULT_CHARSET;
/**
* http
*/
protected String httpVersion = HTTP_1_1;
/**
*
*/
protected byte[] bodyBytes;
// ---------------------------------------------------------------- Headers start
/**
* name<br>
* RFC2616headername
*
* @param name Header
* @return Header
*/
public String header(String name) {
final List<String> values = headerList(name);
if (CollectionUtil.isEmpty(values)) {
return null;
}
return values.get(0);
}
/**
* name
*
* @param name Header
* @return Header
* @since 3.1.1
*/
public List<String> headerList(String name) {
if (StrUtil.isBlank(name)) {
return null;
}
final CaseInsensitiveMap<String, List<String>> headersIgnoreCase = new CaseInsensitiveMap<>(this.headers);
return headersIgnoreCase.get(name.trim());
}
/**
* name
*
* @param name Header
* @return Header
*/
public String header(Header name) {
if (null == name) {
return null;
}
return header(name.toString());
}
/**
* header<br>
*
*
* @param name Header
* @param value Header
* @param isOverride
* @return T
*/
public T header(String name, String value, boolean isOverride) {
if (null != name && null != value) {
final List<String> values = headers.get(name.trim());
if (isOverride || CollectionUtil.isEmpty(values)) {
final ArrayList<String> valueList = new ArrayList<>();
valueList.add(value);
headers.put(name.trim(), valueList);
} else {
values.add(value.trim());
}
}
return (T) this;
}
/**
* header<br>
*
*
* @param name Header
* @param value Header
* @param isOverride
* @return T
*/
public T header(Header name, String value, boolean isOverride) {
return header(name.toString(), value, isOverride);
}
/**
* header<br>
*
*
* @param name Header
* @param value Header
* @return T
*/
public T header(Header name, String value) {
return header(name.toString(), value, true);
}
/**
* header<br>
*
*
* @param name Header
* @param value Header
* @return T
*/
public T header(String name, String value) {
return header(name, value, true);
}
/**
*
*
* @param headers
* @param isOverride
* @return this
* @since 4.6.3
*/
public T headerMap(Map<String, String> headers, boolean isOverride) {
if (MapUtil.isEmpty(headers)) {
return (T) this;
}
for (Entry<String, String> entry : headers.entrySet()) {
this.header(entry.getKey(), StrUtil.nullToEmpty(entry.getValue()), isOverride);
}
return (T) this;
}
/**
* <br>
*
*
* @param headers
* @return this
*/
public T header(Map<String, List<String>> headers) {
return header(headers, false);
}
/**
*
*
* @param headers
* @param isOverride
* @return this
* @since 4.0.8
*/
public T header(Map<String, List<String>> headers, boolean isOverride) {
if (MapUtil.isEmpty(headers)) {
return (T) this;
}
String name;
for (Entry<String, List<String>> entry : headers.entrySet()) {
name = entry.getKey();
for (String value : entry.getValue()) {
this.header(name, StrUtil.nullToEmpty(value), isOverride);
}
}
return (T) this;
}
/**
* <br>
*
*
* @param headers
* @return this
* @since 4.0.3
*/
public T addHeaders(Map<String, String> headers) {
if (MapUtil.isEmpty(headers)) {
return (T) this;
}
for (Entry<String, String> entry : headers.entrySet()) {
this.header(entry.getKey(), StrUtil.nullToEmpty(entry.getValue()), false);
}
return (T) this;
}
/**
*
*
* @param name Header
* @return this
*/
public T removeHeader(String name) {
if (name != null) {
headers.remove(name.trim());
}
return (T) this;
}
/**
*
*
* @param name Header
* @return this
*/
public T removeHeader(Header name) {
return removeHeader(name.toString());
}
/**
* headers
*
* @return Headers Map
*/
public Map<String, List<String>> headers() {
return Collections.unmodifiableMap(headers);
}
/**
*
*
* @return this
* @since 5.7.13
*/
public T clearHeaders() {
this.headers.clear();
return (T) this;
}
// ---------------------------------------------------------------- Headers end
/**
* http
*
* @return String
*/
public String httpVersion() {
return httpVersion;
}
/**
* httpHTTPconnect:Keep-Alive
*
* @param httpVersion Http{@link HttpBase#HTTP_1_0}{@link HttpBase#HTTP_1_1}
* @return this
*/
public T httpVersion(String httpVersion) {
this.httpVersion = httpVersion;
return (T) this;
}
/**
* bodyBytes
*
* @return byte[]
*/
public byte[] bodyBytes() {
return this.bodyBytes;
}
/**
*
*
* @return
*/
public String charset() {
return charset.name();
}
/**
*
*
* @param charset
* @return T
* @see CharsetUtil
*/
public T charset(String charset) {
if (StrUtil.isNotBlank(charset)) {
charset(Charset.forName(charset));
}
return (T) this;
}
/**
*
*
* @param charset
* @return T
* @see CharsetUtil
*/
public T charset(Charset charset) {
if (null != charset) {
this.charset = charset;
}
return (T) this;
}
@Override
public String toString() {
StringBuilder sb = StrUtil.builder();
sb.append("Request Headers: ").append(StrUtil.CRLF);
for (Entry<String, List<String>> entry : this.headers.entrySet()) {
sb.append(" ").append(
entry.getKey()).append(": ").append(CollUtil.join(entry.getValue(), ","))
.append(StrUtil.CRLF);
}
sb.append("Request Body: ").append(StrUtil.CRLF);
sb.append(" ").append(StrUtil.str(this.bodyBytes, this.charset)).append(StrUtil.CRLF);
return sb.toString();
}
}

View File

@ -0,0 +1,300 @@
package aiyh.utils.tool.cn.hutool.http;
import aiyh.utils.tool.cn.hutool.core.lang.Assert;
import aiyh.utils.tool.cn.hutool.core.net.SSLUtil;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;
import java.net.InetSocketAddress;
import java.net.Proxy;
/**
* Http
*
* @author looly
* @since 5.8.0
*/
public class HttpConfig {
/**
* Http
*
* @return HttpConfig
*/
public static HttpConfig create() {
return new HttpConfig();
}
/**
*
*/
int connectionTimeout = HttpGlobalConfig.getTimeout();
/**
*
*/
int readTimeout = HttpGlobalConfig.getTimeout();
/**
*
*/
boolean isDisableCache;
/**
*
*/
int maxRedirectCount = HttpGlobalConfig.getMaxRedirectCount();
/**
*
*/
Proxy proxy;
/**
* HostnameVerifierHTTPS
*/
HostnameVerifier hostnameVerifier;
/**
* SSLSocketFactoryHTTPS
*/
SSLSocketFactory ssf;
/**
* Chuncked00Chuncked
*/
int blockSize;
/**
* EOF<br>
* HttpTransfer-Encoding: ChunkedLength0chunk<br>
* EOF
*/
boolean ignoreEOFError = HttpGlobalConfig.isIgnoreEOFError();
/**
* URLURLPathParam<br>
* HttpURL{@code true}<br>
* RFC3986{@code false}
*/
boolean decodeUrl = HttpGlobalConfig.isDecodeUrl();
/**
*
*/
final HttpInterceptor.Chain<aiyh.utils.tool.cn.hutool.http.HttpRequest> requestInterceptors = GlobalInterceptor.INSTANCE.getCopiedRequestInterceptor();
/**
*
*/
final HttpInterceptor.Chain<aiyh.utils.tool.cn.hutool.http.HttpResponse> responseInterceptors = GlobalInterceptor.INSTANCE.getCopiedResponseInterceptor();
/**
* 使
*/
boolean interceptorOnRedirect;
/**
* <br>
*
*
* <pre>
* 1.
* 2.
* </pre>
*
* @param milliseconds
* @return this
* @see #setConnectionTimeout(int)
* @see #setReadTimeout(int)
*/
public HttpConfig timeout(int milliseconds) {
setConnectionTimeout(milliseconds);
setReadTimeout(milliseconds);
return this;
}
/**
*
*
* @param milliseconds
* @return this
*/
public HttpConfig setConnectionTimeout(int milliseconds) {
this.connectionTimeout = milliseconds;
return this;
}
/**
*
*
* @param milliseconds
* @return this
*/
public HttpConfig setReadTimeout(int milliseconds) {
this.readTimeout = milliseconds;
return this;
}
/**
*
*
* @return this
*/
public HttpConfig disableCache() {
this.isDisableCache = true;
return this;
}
/**
* <br>
* 11
*
* @param maxRedirectCount
* @return this
*/
public HttpConfig setMaxRedirectCount(int maxRedirectCount) {
this.maxRedirectCount = Math.max(maxRedirectCount, 0);
return this;
}
/**
* <br>
* HTTPS
*
* @param hostnameVerifier HostnameVerifier
* @return this
*/
public HttpConfig setHostnameVerifier(HostnameVerifier hostnameVerifier) {
// 验证域
this.hostnameVerifier = hostnameVerifier;
return this;
}
/**
* Http
*
* @param host
* @param port
* @return this
*/
public HttpConfig setHttpProxy(String host, int port) {
final Proxy proxy = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(host, port));
return setProxy(proxy);
}
/**
*
*
* @param proxy {@link Proxy}
* @return this
*/
public HttpConfig setProxy(Proxy proxy) {
this.proxy = proxy;
return this;
}
/**
* SSLSocketFactory<br>
* HTTPS使SSLSocketFactory<br>
* SSLSocketFactorySSLSocketFactoryBuilder.create().build();
*
* @param ssf SSLScketFactory
* @return this
*/
public HttpConfig setSSLSocketFactory(SSLSocketFactory ssf) {
this.ssf = ssf;
return this;
}
/**
* HTTPSHTTPS使<br>
* {@link #setSSLSocketFactory(SSLSocketFactory)}
*
* <pre>
* 1. TLSv1.2
* 2. TLSv1.1
* 3. SSLv3
* ...
* </pre>
*
* @param protocol
* @return this
* @see SSLUtil#createSSLContext(String)
* @see #setSSLSocketFactory(SSLSocketFactory)
*/
public HttpConfig setSSLProtocol(String protocol) {
Assert.notBlank(protocol, "protocol must be not blank!");
setSSLSocketFactory(SSLUtil.createSSLContext(protocol).getSocketFactory());
return this;
}
/**
* <br>
* HttpUrlConnection
*
* @param blockSize bytes00Chuncked
* @return this
*/
public HttpConfig setBlockSize(int blockSize) {
this.blockSize = blockSize;
return this;
}
/**
* EOF<br>
* HttpTransfer-Encoding: ChunkedLength0chunk<br>
* EOF
*
* @param ignoreEOFError EOF
* @return this
* @since 5.7.20
*/
public HttpConfig setIgnoreEOFError(boolean ignoreEOFError) {
this.ignoreEOFError = ignoreEOFError;
return this;
}
/**
* URLURLPathParam<br>
* HttpURL{@code true}<br>
* RFC3986{@code false}
*
* @param decodeUrl URL
* @return this
*/
public HttpConfig setDecodeUrl(boolean decodeUrl) {
this.decodeUrl = decodeUrl;
return this;
}
/**
*
*
* @param interceptor
* @return this
*/
public HttpConfig addRequestInterceptor(HttpInterceptor<HttpRequest> interceptor) {
this.requestInterceptors.addChain(interceptor);
return this;
}
/**
*
*
* @param interceptor
* @return this
*/
public HttpConfig addResponseInterceptor(HttpInterceptor<HttpResponse> interceptor) {
this.responseInterceptors.addChain(interceptor);
return this;
}
/**
* 使
*
* @param interceptorOnRedirect 使
* @return this
*/
public HttpConfig setInterceptorOnRedirect(boolean interceptorOnRedirect) {
this.interceptorOnRedirect = interceptorOnRedirect;
return this;
}
}

View File

@ -0,0 +1,568 @@
package aiyh.utils.tool.cn.hutool.http;
import aiyh.utils.tool.cn.hutool.core.map.MapUtil;
import aiyh.utils.tool.cn.hutool.core.util.ObjectUtil;
import aiyh.utils.tool.cn.hutool.core.util.ReflectUtil;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
import aiyh.utils.tool.cn.hutool.core.util.URLUtil;
import aiyh.utils.tool.cn.hutool.http.ssl.DefaultSSLInfo;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* httpHttpURLConnection
*
* @author Looly
*/
public class HttpConnection {
private final URL url;
private final Proxy proxy;
private HttpURLConnection conn;
/**
* HttpConnection
*
* @param urlStr URL
* @param proxy {@code null}
* @return HttpConnection
*/
public static HttpConnection create(String urlStr, Proxy proxy) {
return create(URLUtil.toUrlForHttp(urlStr), proxy);
}
/**
* HttpConnection
*
* @param url URL
* @param proxy {@code null}
* @return HttpConnection
*/
public static HttpConnection create(URL url, Proxy proxy) {
return new HttpConnection(url, proxy);
}
// --------------------------------------------------------------- Constructor start
/**
* HttpConnection
*
* @param url URL
* @param proxy
*/
public HttpConnection(URL url, Proxy proxy) {
this.url = url;
this.proxy = proxy;
// 初始化Http连接
initConn();
}
// --------------------------------------------------------------- Constructor end
/**
*
*
* @return HttpConnection
* @since 4.4.1
*/
public HttpConnection initConn() {
try {
this.conn = openHttp();
} catch (IOException e) {
throw new HttpException(e);
}
// 默认读取响应内容
this.conn.setDoInput(true);
return this;
}
// --------------------------------------------------------------- Getters And Setters start
/**
* ,GET/POST
*
* @return , GET/POST
*/
public aiyh.utils.tool.cn.hutool.http.Method getMethod() {
return aiyh.utils.tool.cn.hutool.http.Method.valueOf(this.conn.getRequestMethod());
}
/**
*
*
* @param method
* @return
*/
public HttpConnection setMethod(aiyh.utils.tool.cn.hutool.http.Method method) {
if (aiyh.utils.tool.cn.hutool.http.Method.POST.equals(method) //
|| aiyh.utils.tool.cn.hutool.http.Method.PUT.equals(method)//
|| aiyh.utils.tool.cn.hutool.http.Method.PATCH.equals(method)//
|| aiyh.utils.tool.cn.hutool.http.Method.DELETE.equals(method)) {
this.conn.setUseCaches(false);
// 增加PATCH方法支持
if (aiyh.utils.tool.cn.hutool.http.Method.PATCH.equals(method)) {
try {
HttpGlobalConfig.allowPatch();
} catch (Exception ignore) {
// ignore
// https://github.com/dromara/hutool/issues/2832
}
}
}
// method
try {
this.conn.setRequestMethod(method.toString());
} catch (ProtocolException e) {
if (aiyh.utils.tool.cn.hutool.http.Method.PATCH.equals(method)) {
// 如果全局设置失效,此处针对单独链接重新设置
reflectSetMethod(method);
} else {
throw new HttpException(e);
}
}
return this;
}
/**
* URL
*
* @return URL
*/
public URL getUrl() {
return url;
}
/**
*
*
* @return {@link Proxy}
*/
public Proxy getProxy() {
return proxy;
}
/**
* HttpURLConnection
*
* @return HttpURLConnection
*/
public HttpURLConnection getHttpURLConnection() {
return conn;
}
// --------------------------------------------------------------- Getters And Setters end
// ---------------------------------------------------------------- Headers start
/**
* <br>
*
*
* @param header
* @param value
* @param isOverride
* @return HttpConnection
*/
public HttpConnection header(String header, String value, boolean isOverride) {
if (null != this.conn) {
if (isOverride) {
this.conn.setRequestProperty(header, value);
} else {
this.conn.addRequestProperty(header, value);
}
}
return this;
}
/**
* <br>
*
*
* @param header
* @param value
* @param isOverride
* @return HttpConnection
*/
public HttpConnection header(aiyh.utils.tool.cn.hutool.http.Header header, String value, boolean isOverride) {
return header(header.toString(), value, isOverride);
}
/**
* <br>
*
*
* @param headerMap
* @param isOverride
* @return this
*/
public HttpConnection header(Map<String, List<String>> headerMap, boolean isOverride) {
if (MapUtil.isNotEmpty(headerMap)) {
String name;
for (Entry<String, List<String>> entry : headerMap.entrySet()) {
name = entry.getKey();
for (String value : entry.getValue()) {
this.header(name, StrUtil.nullToEmpty(value), isOverride);
}
}
}
return this;
}
/**
* Http
*
* @param name Header
* @return Http
*/
public String header(String name) {
return this.conn.getHeaderField(name);
}
/**
* Http
*
* @param name Header
* @return Http
*/
public String header(aiyh.utils.tool.cn.hutool.http.Header name) {
return header(name.toString());
}
/**
* Http
*
* @return HttpMap
*/
public Map<String, List<String>> headers() {
return this.conn.getHeaderFields();
}
// ---------------------------------------------------------------- Headers end
/**
* https<br>
* httscom.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImplsunapihttp
*
* @param hostnameVerifier httpsnull
* @param ssf SSLSocketFactoryhttpsnull
* @return this
* @throws HttpException KeyManagementExceptionNoSuchAlgorithmException
*/
public HttpConnection setHttpsInfo(HostnameVerifier hostnameVerifier, SSLSocketFactory ssf) throws HttpException {
final HttpURLConnection conn = this.conn;
if (conn instanceof HttpsURLConnection) {
// Https请求
final HttpsURLConnection httpsConn = (HttpsURLConnection) conn;
// 验证域
httpsConn.setHostnameVerifier(ObjectUtil.defaultIfNull(hostnameVerifier, DefaultSSLInfo.TRUST_ANY_HOSTNAME_VERIFIER));
httpsConn.setSSLSocketFactory(ObjectUtil.defaultIfNull(ssf, DefaultSSLInfo.DEFAULT_SSF));
}
return this;
}
/**
*
*
* @return this
* @see HttpURLConnection#setUseCaches(boolean)
*/
public HttpConnection disableCache() {
this.conn.setUseCaches(false);
return this;
}
/**
*
*
* @param timeout
* @return this
*/
public HttpConnection setConnectTimeout(int timeout) {
if (timeout > 0 && null != this.conn) {
this.conn.setConnectTimeout(timeout);
}
return this;
}
/**
*
*
* @param timeout
* @return this
*/
public HttpConnection setReadTimeout(int timeout) {
if (timeout > 0 && null != this.conn) {
this.conn.setReadTimeout(timeout);
}
return this;
}
/**
*
*
* @param timeout
* @return this
*/
public HttpConnection setConnectionAndReadTimeout(int timeout) {
setConnectTimeout(timeout);
setReadTimeout(timeout);
return this;
}
/**
* Cookie
*
* @param cookie Cookie
* @return this
*/
public HttpConnection setCookie(String cookie) {
if (cookie != null) {
header(Header.COOKIE, cookie, true);
}
return this;
}
/**
* <br>
* HttpUrlConnection
*
* @param blockSize bytes00Chuncked
* @return this
*/
public HttpConnection setChunkedStreamingMode(int blockSize) {
if (blockSize > 0) {
conn.setChunkedStreamingMode(blockSize);
}
return this;
}
/**
* HTTP 30X
*
* @param isInstanceFollowRedirects
* @return this
*/
public HttpConnection setInstanceFollowRedirects(boolean isInstanceFollowRedirects) {
conn.setInstanceFollowRedirects(isInstanceFollowRedirects);
return this;
}
/**
*
*
* @return this
* @throws IOException IO
*/
public HttpConnection connect() throws IOException {
if (null != this.conn) {
this.conn.connect();
}
return this;
}
/**
*
*
* @return this
* @since 4.6.0
*/
public HttpConnection disconnectQuietly() {
try {
disconnect();
} catch (Throwable e) {
// ignore
}
return this;
}
/**
*
*
* @return this
*/
public HttpConnection disconnect() {
if (null != this.conn) {
this.conn.disconnect();
}
return this;
}
/**
* <br>
*
*
* @return
* @throws IOException IO
*/
public InputStream getInputStream() throws IOException {
if (null != this.conn) {
return this.conn.getInputStream();
}
return null;
}
/**
*
*
* @return
*/
public InputStream getErrorStream() {
if (null != this.conn) {
return this.conn.getErrorStream();
}
return null;
}
/**
*
*
* @return OutputStream
* @throws IOException IO
*/
public OutputStream getOutputStream() throws IOException {
if (null == this.conn) {
throw new IOException("HttpURLConnection has not been initialized.");
}
final aiyh.utils.tool.cn.hutool.http.Method method = getMethod();
// 当有写出需求时,自动打开之
this.conn.setDoOutput(true);
final OutputStream out = this.conn.getOutputStream();
// 解决在Rest请求中GET请求附带body导致GET请求被强制转换为POST
// 在sun.net.www.protocol.http.HttpURLConnection.getOutputStream0方法中会把GET方法
// 修改为POST而且无法调用setRequestMethod方法修改因此此处使用反射强制修改字段属性值
// https://stackoverflow.com/questions/978061/http-get-with-request-body/983458
if (method == aiyh.utils.tool.cn.hutool.http.Method.GET && method != getMethod()) {
reflectSetMethod(method);
}
return out;
}
/**
*
*
* @return
* @throws IOException IO
*/
public int responseCode() throws IOException {
if (null != this.conn) {
return this.conn.getResponseCode();
}
return 0;
}
/**
* <br>
* Http<br>
* ContentType
*
* @return
*/
public String getCharsetName() {
return HttpUtil.getCharset(conn);
}
/**
* <br>
* Http<br>
* ContentType
*
* @return {@link Charset}
* @since 3.0.9
*/
public Charset getCharset() {
Charset charset = null;
final String charsetName = getCharsetName();
if (StrUtil.isNotBlank(charsetName)) {
try {
charset = Charset.forName(charsetName);
} catch (UnsupportedCharsetException e) {
// ignore
}
}
return charset;
}
@Override
public String toString() {
StringBuilder sb = StrUtil.builder();
sb.append("Request URL: ").append(this.url).append(StrUtil.CRLF);
sb.append("Request Method: ").append(this.getMethod()).append(StrUtil.CRLF);
// sb.append("Request Headers: ").append(StrUtil.CRLF);
// for (Entry<String, List<String>> entry : this.conn.getHeaderFields().entrySet()) {
// sb.append(" ").append(entry).append(StrUtil.CRLF);
// }
return sb.toString();
}
// --------------------------------------------------------------- Private Method start
/**
* httphttps<br>
* httpscom.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImplsunapihttp
*
* @return {@link HttpURLConnection}https{@link HttpsURLConnection}
*/
private HttpURLConnection openHttp() throws IOException {
final URLConnection conn = openConnection();
if (!(conn instanceof HttpURLConnection)) {
// 防止其它协议造成的转换异常
throw new HttpException("'{}' of URL [{}] is not a http connection, make sure URL is format for http.", conn.getClass().getName(), this.url);
}
return (HttpURLConnection) conn;
}
/**
*
*
* @return {@link URLConnection}
* @throws IOException IO
*/
private URLConnection openConnection() throws IOException {
return (null == this.proxy) ? url.openConnection() : url.openConnection(this.proxy);
}
/**
* HttpURLConnection
*
* @param method
*/
private void reflectSetMethod(Method method) {
ReflectUtil.setFieldValue(this.conn, "method", method.name());
// HttpsURLConnectionImpl实现中使用了代理类需要修改被代理类的method方法
final Object delegate = ReflectUtil.getFieldValue(this.conn, "delegate");
if (null != delegate) {
ReflectUtil.setFieldValue(delegate, "method", method.name());
}
}
// --------------------------------------------------------------- Private Method end
}

View File

@ -0,0 +1,122 @@
package aiyh.utils.tool.cn.hutool.http;
import aiyh.utils.tool.cn.hutool.core.io.FastByteArrayOutputStream;
import aiyh.utils.tool.cn.hutool.core.io.StreamProgress;
import aiyh.utils.tool.cn.hutool.core.lang.Assert;
import java.io.File;
import java.io.OutputStream;
import java.nio.charset.Charset;
/**
* 使{@code GET}30x
*
* @author looly
* @since 5.6.4
*/
public class HttpDownloader {
/**
*
*
* @param url url
* @param customCharset 使{@code CharsetUtil#charset}
* @param streamPress {@link StreamProgress}
* @return
*/
public static String downloadString(String url, Charset customCharset, StreamProgress streamPress) {
final FastByteArrayOutputStream out = new FastByteArrayOutputStream();
download(url, out, true, streamPress);
return null == customCharset ? out.toString() : out.toString(customCharset);
}
/**
* 30x
*
* @param url url
* @return
*/
public static byte[] downloadBytes(String url) {
return requestDownload(url, -1).bodyBytes();
}
/**
*
*
* @param url url
* @param targetFileOrDir URL使URL
* @param timeout -1
* @param streamProgress
* @return
*/
public static long downloadFile(String url, File targetFileOrDir, int timeout, StreamProgress streamProgress) {
return requestDownload(url, timeout).writeBody(targetFileOrDir, streamProgress);
}
/**
* -<br>
* https://gitee.com/dromara/hutool/pulls/407<br>
*
*
* @param url url
* @param targetFileOrDir URL使URL
* @param tempFileSuffix ".temp"
* @param timeout -1
* @param streamProgress
* @return
* @since 5.7.12
*/
public long downloadFile(String url, File targetFileOrDir, String tempFileSuffix, int timeout, StreamProgress streamProgress) {
return requestDownload(url, timeout).writeBody(targetFileOrDir, tempFileSuffix, streamProgress);
}
/**
*
*
* @param url url
* @param targetFileOrDir URL使URL
* @param timeout -1
* @param streamProgress
* @return
*/
public static File downloadForFile(String url, File targetFileOrDir, int timeout, StreamProgress streamProgress) {
return requestDownload(url, timeout).writeBodyForFile(targetFileOrDir, streamProgress);
}
/**
*
*
* @param url url
* @param out {@link OutputStream}
* @param isCloseOut
* @param streamProgress
* @return
*/
public static long download(String url, OutputStream out, boolean isCloseOut, StreamProgress streamProgress) {
Assert.notNull(out, "[out] is null !");
return requestDownload(url, -1).writeBody(out, isCloseOut, streamProgress);
}
/**
*
*
* @param url
* @param timeout
* @return HttpResponse
* @since 5.4.1
*/
private static aiyh.utils.tool.cn.hutool.http.HttpResponse requestDownload(String url, int timeout) {
Assert.notBlank(url, "[url] is blank !");
final HttpResponse response = HttpUtil.createGet(url, true)
.timeout(timeout)
.executeAsync();
if (response.isOk()) {
return response;
}
throw new HttpException("Server response error with status code: [{}]", response.getStatus());
}
}

View File

@ -0,0 +1,36 @@
package aiyh.utils.tool.cn.hutool.http;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
/**
* HTTP
*
* @author xiaoleilu
*/
public class HttpException extends RuntimeException {
private static final long serialVersionUID = 8247610319171014183L;
public HttpException(Throwable e) {
super(e.getMessage(), e);
}
public HttpException(String message) {
super(message);
}
public HttpException(String messageTemplate, Object... params) {
super(StrUtil.format(messageTemplate, params));
}
public HttpException(String message, Throwable throwable) {
super(message, throwable);
}
public HttpException(String message, Throwable throwable, boolean enableSuppression, boolean writableStackTrace) {
super(message, throwable, enableSuppression, writableStackTrace);
}
public HttpException(Throwable throwable, String messageTemplate, Object... params) {
super(StrUtil.format(messageTemplate, params), throwable);
}
}

View File

@ -0,0 +1,214 @@
package aiyh.utils.tool.cn.hutool.http;
import aiyh.utils.tool.cn.hutool.core.util.ArrayUtil;
import aiyh.utils.tool.cn.hutool.core.util.RandomUtil;
import aiyh.utils.tool.cn.hutool.core.util.ReflectUtil;
import aiyh.utils.tool.cn.hutool.http.cookie.GlobalCookieManager;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.net.CookieManager;
import java.net.HttpURLConnection;
/**
* HTTP
*
* @author Looly
* @since 4.6.2
*/
public class HttpGlobalConfig implements Serializable {
private static final long serialVersionUID = 1L;
/**
* -1:
* timeout = 3s(3000 ms), 6s
* timeout of zero is interpreted as an infinite timeout. 0
* .
* {@link HttpURLConnection#setReadTimeout(int)} :
* {@link HttpURLConnection#setConnectTimeout(int)} :
*/
private static int timeout = -1;
private static boolean isAllowPatch = false;
private static String boundary = "--------------------Hutool_" + RandomUtil.randomString(16);
private static int maxRedirectCount = 0;
private static boolean ignoreEOFError = true;
private static boolean decodeUrl = false;
/**
*
*
* @return
*/
public static int getTimeout() {
return timeout;
}
/**
* <br>
* -1: <br>
* timeout = 3s(3000 ms), 6s<br>
* timeout of zero is interpreted as an infinite timeout. 0<br>
* .<br>
* {@link HttpURLConnection#setReadTimeout(int)} : <br>
* {@link HttpURLConnection#setConnectTimeout(int)} :
*
* @param customTimeout
*/
synchronized public static void setTimeout(int customTimeout) {
timeout = customTimeout;
}
/**
* Multipart
*
* @return Multipart
* @since 5.7.17
*/
public static String getBoundary() {
return boundary;
}
/**
* Multipart
*
* @param customBoundary Multipart
* @since 5.7.17
*/
synchronized public static void setBoundary(String customBoundary) {
boundary = customBoundary;
}
/**
* 0<br>
* 1
*
* @return
* @since 5.7.19
*/
public static int getMaxRedirectCount() {
return maxRedirectCount;
}
/**
* 0<br>
* 1
*
* @param customMaxRedirectCount
* @since 5.7.19
*/
synchronized public static void setMaxRedirectCount(int customMaxRedirectCount) {
maxRedirectCount = customMaxRedirectCount;
}
/**
* EOF<br>
* HttpTransfer-Encoding: ChunkedLength0chunk<br>
* EOF
*
* @return EOF
* @since 5.7.20
*/
public static boolean isIgnoreEOFError() {
return ignoreEOFError;
}
/**
* EOF<br>
* HttpTransfer-Encoding: ChunkedLength0chunk<br>
* EOF
*
* @param customIgnoreEOFError EOF
* @since 5.7.20
*/
synchronized public static void setIgnoreEOFError(boolean customIgnoreEOFError) {
ignoreEOFError = customIgnoreEOFError;
}
/**
* URLURLPathParam<br>
* HttpURL{@code true}<br>
* RFC3986{@code false}
*
* @return URL
* @since 5.7.22
*/
public static boolean isDecodeUrl() {
return decodeUrl;
}
/**
* URLURLPathParam<br>
* HttpURL{@code true}<br>
* RFC3986{@code false}
*
* @param customDecodeUrl URL
* @since 5.7.22
*/
synchronized public static void setDecodeUrl(boolean customDecodeUrl) {
decodeUrl = customDecodeUrl;
}
/**
* CookieCookie
*
* @return {@link CookieManager}
* @see GlobalCookieManager#getCookieManager()
* @since 4.1.0
*/
public static CookieManager getCookieManager() {
return GlobalCookieManager.getCookieManager();
}
/**
* {@link CookieManager}
*
* @param customCookieManager {@link CookieManager}
* @see GlobalCookieManager#setCookieManager(CookieManager)
* @since 4.5.14
*/
synchronized public static void setCookieManager(CookieManager customCookieManager) {
GlobalCookieManager.setCookieManager(customCookieManager);
}
/**
* Cookie
*
* @see GlobalCookieManager#setCookieManager(CookieManager)
* @since 4.1.9
*/
synchronized public static void closeCookie() {
GlobalCookieManager.setCookieManager(null);
}
/**
* METHOD<br>
* {@link HttpURLConnection}methodsPATCH<br>
* see: <a href="https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch">https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch</a>
*
* @since 5.7.4
*/
synchronized public static void allowPatch() {
if (isAllowPatch) {
return;
}
final Field methodsField = ReflectUtil.getField(HttpURLConnection.class, "methods");
if (null == methodsField) {
throw new HttpException("None static field [methods] with Java version: [{}]", System.getProperty("java.version"));
}
// 去除final修饰
ReflectUtil.removeFinalModify(methodsField);
final String[] methods = {
"GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE", "PATCH"
};
ReflectUtil.setFieldValue(null, methodsField, methods);
// 检查注入是否成功
final Object staticFieldValue = ReflectUtil.getStaticFieldValue(methodsField);
if (!ArrayUtil.equals(methods, staticFieldValue)) {
throw new HttpException("Inject value to field [methods] failed!");
}
isAllowPatch = true;
}
}

View File

@ -0,0 +1,108 @@
package aiyh.utils.tool.cn.hutool.http;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
/**
* HTTPHttp
*
* @author Looly
*
*/
public class HttpInputStream extends InputStream {
/** 原始流 */
private InputStream in;
/**
*
*
* @param response
*/
public HttpInputStream(aiyh.utils.tool.cn.hutool.http.HttpResponse response) {
init(response);
}
@Override
public int read() throws IOException {
return this.in.read();
}
@SuppressWarnings("NullableProblems")
@Override
public int read(byte[] b, int off, int len) throws IOException {
return this.in.read(b, off, len);
}
@Override
public long skip(long n) throws IOException {
return this.in.skip(n);
}
@Override
public int available() throws IOException {
return this.in.available();
}
@Override
public void close() throws IOException {
this.in.close();
}
@Override
public synchronized void mark(int readlimit) {
this.in.mark(readlimit);
}
@Override
public synchronized void reset() throws IOException {
this.in.reset();
}
@Override
public boolean markSupported() {
return this.in.markSupported();
}
/**
*
*
* @param response
*/
private void init(HttpResponse response) {
try {
this.in = (response.status < HttpStatus.HTTP_BAD_REQUEST) ? response.httpConnection.getInputStream() : response.httpConnection.getErrorStream();
} catch (IOException e) {
if (false == (e instanceof FileNotFoundException)) {
throw new HttpException(e);
}
// 服务器无返回内容,忽略之
}
// 在一些情况下返回的流为null此时提供状态码说明
if (null == this.in) {
this.in = new ByteArrayInputStream(StrUtil.format("Error request, response status: {}", response.status).getBytes());
return;
}
if (response.isGzip() && false == (response.in instanceof GZIPInputStream)) {
// Accept-Encoding: gzip
try {
this.in = new GZIPInputStream(this.in);
} catch (IOException e) {
// 在类似于Head等方法中无body返回此时GZIPInputStream构造会出现错误在此忽略此错误读取普通数据
// ignore
}
} else if (response.isDeflate() && false == (this.in instanceof InflaterInputStream)) {
// Accept-Encoding: defalte
this.in = new InflaterInputStream(this.in, new Inflater(true));
}
}
}

View File

@ -0,0 +1,56 @@
package aiyh.utils.tool.cn.hutool.http;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* Http
*
* @param <T> HttpRequestHttpResponse
* @author looly
* @since 5.7.16
*/
@FunctionalInterface
public interface HttpInterceptor<T extends aiyh.utils.tool.cn.hutool.http.HttpBase<T>> {
/**
*
*
* @param httpObj
*/
void process(T httpObj);
/**
*
*
* @param <T> HttpRequestHttpResponse
* @author looly
* @since 5.7.16
*/
class Chain<T extends HttpBase<T>> implements aiyh.utils.tool.cn.hutool.core.lang.Chain<HttpInterceptor<T>, Chain<T>> {
private final List<HttpInterceptor<T>> interceptors = new LinkedList<>();
@Override
public Chain<T> addChain(HttpInterceptor<T> element) {
interceptors.add(element);
return this;
}
@Override
public Iterator<HttpInterceptor<T>> iterator() {
return interceptors.iterator();
}
/**
*
*
* @return this
* @since 5.8.0
*/
public Chain<T> clear() {
interceptors.clear();
return this;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,56 @@
package aiyh.utils.tool.cn.hutool.http;
import aiyh.utils.tool.cn.hutool.core.io.resource.Resource;
import aiyh.utils.tool.cn.hutool.core.lang.Assert;
import java.io.InputStream;
import java.io.Serializable;
import java.net.URL;
/**
* HTTPContent-Type
*
* @author looly
* @since 5.7.17
*/
public class HttpResource implements Resource, Serializable {
private static final long serialVersionUID = 1L;
private final Resource resource;
private final String contentType;
/**
*
*
* @param resource
* @param contentType Content-Type{@code null}
*/
public HttpResource(Resource resource, String contentType) {
this.resource = Assert.notNull(resource, "Resource must be not null !");
this.contentType = contentType;
}
@Override
public String getName() {
return resource.getName();
}
@Override
public URL getUrl() {
return resource.getUrl();
}
@Override
public InputStream getStream() {
return resource.getStream();
}
/**
* Content-Type
*
* @return Content-Type
*/
public String getContentType() {
return this.contentType;
}
}

View File

@ -0,0 +1,644 @@
package aiyh.utils.tool.cn.hutool.http;
import aiyh.utils.tool.cn.hutool.core.convert.Convert;
import aiyh.utils.tool.cn.hutool.core.io.FastByteArrayOutputStream;
import aiyh.utils.tool.cn.hutool.core.io.FileUtil;
import aiyh.utils.tool.cn.hutool.core.io.IORuntimeException;
import aiyh.utils.tool.cn.hutool.core.io.IoUtil;
import aiyh.utils.tool.cn.hutool.core.io.StreamProgress;
import aiyh.utils.tool.cn.hutool.core.lang.Assert;
import aiyh.utils.tool.cn.hutool.core.util.ObjUtil;
import aiyh.utils.tool.cn.hutool.core.util.ReUtil;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
import aiyh.utils.tool.cn.hutool.core.util.URLUtil;
import aiyh.utils.tool.cn.hutool.http.cookie.GlobalCookieManager;
import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.EOFException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpCookie;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map.Entry;
/**
* Http<br>
* 线
*
* @author Looly
*/
public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
/**
* Http
*/
protected HttpConfig config;
/**
*
*/
protected HttpConnection httpConnection;
/**
* Http
*/
protected InputStream in;
/**
* body
*/
private volatile boolean isAsync;
/**
*
*/
protected int status;
/**
* Http
*/
private final boolean ignoreBody;
/**
*
*/
private Charset charsetFromResponse;
/**
*
*
* @param httpConnection {@link HttpConnection}
* @param config Http
* @param charset
* @param isAsync
* @param isIgnoreBody
* @since 3.1.2
*/
protected HttpResponse(HttpConnection httpConnection, HttpConfig config, Charset charset, boolean isAsync, boolean isIgnoreBody) {
this.httpConnection = httpConnection;
this.config = config;
this.charset = charset;
this.isAsync = isAsync;
this.ignoreBody = isIgnoreBody;
initWithDisconnect();
}
/**
*
*
* @return
*/
public int getStatus() {
return this.status;
}
/**
* 200~299
*
* @return
* @since 4.1.9
*/
public boolean isOk() {
return this.status >= 200 && this.status < 300;
}
/**
* <br>
* Http{@link InputStream}<br>
* Httpbody
*
* @return this
*/
public HttpResponse sync() {
return this.isAsync ? forceSync() : this;
}
// ---------------------------------------------------------------- Http Response Header start
/**
*
*
* @return String
*/
public String contentEncoding() {
return header(aiyh.utils.tool.cn.hutool.http.Header.CONTENT_ENCODING);
}
/**
*
* <ul>
* <li>Transfer-Encoding: Chunked</li>
* <li>Content-Encoding: XXX</li>
* </ul>
* https://blog.csdn.net/jiang7701037/article/details/86304302
*
* @return -1
* @since 5.7.9
*/
public long contentLength() {
long contentLength = Convert.toLong(header(aiyh.utils.tool.cn.hutool.http.Header.CONTENT_LENGTH), -1L);
if (contentLength > 0 && (isChunked() || StrUtil.isNotBlank(contentEncoding()))) {
//按照HTTP协议规范在 Transfer-Encoding和Content-Encoding设置后 Content-Length 无效。
contentLength = -1;
}
return contentLength;
}
/**
* gzip
*
* @return gzip
*/
public boolean isGzip() {
final String contentEncoding = contentEncoding();
return "gzip".equalsIgnoreCase(contentEncoding);
}
/**
* zlib(Deflate)
*
* @return zlib(Deflate)
* @since 4.5.7
*/
public boolean isDeflate() {
final String contentEncoding = contentEncoding();
return "deflate".equalsIgnoreCase(contentEncoding);
}
/**
* Transfer-Encoding:Chunked
*
* @return Transfer-Encoding:Chunked
* @since 4.6.2
*/
public boolean isChunked() {
final String transferEncoding = header(aiyh.utils.tool.cn.hutool.http.Header.TRANSFER_ENCODING);
return "Chunked".equalsIgnoreCase(transferEncoding);
}
/**
* Cookie
*
* @return Cookie
* @since 3.1.1
*/
public String getCookieStr() {
return header(aiyh.utils.tool.cn.hutool.http.Header.SET_COOKIE);
}
/**
* Cookie
*
* @return Cookie
* @since 3.1.1
*/
public List<HttpCookie> getCookies() {
return GlobalCookieManager.getCookies(this.httpConnection);
}
/**
* Cookie
*
* @param name Cookie
* @return {@link HttpCookie}
* @since 4.1.4
*/
public HttpCookie getCookie(String name) {
List<HttpCookie> cookie = getCookies();
if (null != cookie) {
for (HttpCookie httpCookie : cookie) {
if (httpCookie.getName().equals(name)) {
return httpCookie;
}
}
}
return null;
}
/**
* Cookie
*
* @param name Cookie
* @return Cookie
* @since 4.1.4
*/
public String getCookieValue(String name) {
HttpCookie cookie = getCookie(name);
return (null == cookie) ? null : cookie.getValue();
}
// ---------------------------------------------------------------- Http Response Header end
// ---------------------------------------------------------------- Body start
/**
* <br>
* Http<br>
* {@link #sync()}<br>
*
*
* @return
*/
public InputStream bodyStream() {
if (isAsync) {
return this.in;
}
return new ByteArrayInputStream(this.bodyBytes);
}
/**
* <br>
*
*
* @return byte[]
*/
@Override
public byte[] bodyBytes() {
sync();
return this.bodyBytes;
}
/**
* <br>
* 使charset使UTF-8
*
* @param bodyBytes
* @return this
*/
public HttpResponse body(byte[] bodyBytes) {
sync();
if (null != bodyBytes) {
this.bodyBytes = bodyBytes;
}
return this;
}
/**
*
*
* @return String
* @throws HttpException IO
*/
public String body() throws HttpException {
return HttpUtil.getString(bodyBytes(), this.charset, null == this.charsetFromResponse);
}
/**
* {@link OutputStream}<br>
* Http<br>
* Http
*
* @param out
* @param isCloseOut
* @param streamProgress
* @return bytes
* @since 3.3.2
*/
public long writeBody(OutputStream out, boolean isCloseOut, StreamProgress streamProgress) {
Assert.notNull(out, "[out] must be not null!");
final long contentLength = contentLength();
try {
return copyBody(bodyStream(), out, contentLength, streamProgress, this.config.ignoreEOFError);
} finally {
IoUtil.close(this);
if (isCloseOut) {
IoUtil.close(out);
}
}
}
/**
* <br>
* Http<br>
* Http
*
* @param targetFileOrDir
* @param streamProgress
* @return bytes
* @since 3.3.2
*/
public long writeBody(File targetFileOrDir, StreamProgress streamProgress) {
Assert.notNull(targetFileOrDir, "[targetFileOrDir] must be not null!");
final File outFile = completeFileNameFromHeader(targetFileOrDir);
return writeBody(FileUtil.getOutputStream(outFile), true, streamProgress);
}
/**
* -<br>
* Http<br>
* Http<br>
* https://gitee.com/dromara/hutool/pulls/407<br>
*
*
* @param targetFileOrDir
* @param tempFileSuffix ".temp"
* @param streamProgress
* @return bytes
* @since 5.7.12
*/
public long writeBody(File targetFileOrDir, String tempFileSuffix, StreamProgress streamProgress) {
Assert.notNull(targetFileOrDir, "[targetFileOrDir] must be not null!");
File outFile = completeFileNameFromHeader(targetFileOrDir);
if (StrUtil.isBlank(tempFileSuffix)) {
tempFileSuffix = ".temp";
} else {
tempFileSuffix = StrUtil.addPrefixIfNot(tempFileSuffix, StrUtil.DOT);
}
// 目标文件真实名称
final String fileName = outFile.getName();
// 临时文件名称
final String tempFileName = fileName + tempFileSuffix;
// 临时文件
outFile = new File(outFile.getParentFile(), tempFileName);
long length;
try {
length = writeBody(outFile, streamProgress);
// 重命名下载好的临时文件
FileUtil.rename(outFile, fileName, true);
} catch (Throwable e) {
// 异常则删除临时文件
FileUtil.del(outFile);
throw new HttpException(e);
}
return length;
}
/**
* <br>
* Http<br>
* Http
*
* @param targetFileOrDir
* @param streamProgress
* @return
* @since 5.6.4
*/
public File writeBodyForFile(File targetFileOrDir, StreamProgress streamProgress) {
Assert.notNull(targetFileOrDir, "[targetFileOrDir] must be not null!");
final File outFile = completeFileNameFromHeader(targetFileOrDir);
writeBody(FileUtil.getOutputStream(outFile), true, streamProgress);
return outFile;
}
/**
* <br>
* Http<br>
* Http
*
* @param targetFileOrDir
* @return bytes
* @since 3.3.2
*/
public long writeBody(File targetFileOrDir) {
return writeBody(targetFileOrDir, null);
}
/**
* <br>
* Http<br>
* Http
*
* @param targetFileOrDir
* @return bytes
* @since 3.3.2
*/
public long writeBody(String targetFileOrDir) {
return writeBody(FileUtil.file(targetFileOrDir));
}
// ---------------------------------------------------------------- Body end
@Override
public void close() {
IoUtil.close(this.in);
this.in = null;
// 关闭连接
this.httpConnection.disconnectQuietly();
}
@Override
public String toString() {
StringBuilder sb = StrUtil.builder();
sb.append("Response Headers: ").append(StrUtil.CRLF);
for (Entry<String, List<String>> entry : this.headers.entrySet()) {
sb.append(" ").append(entry).append(StrUtil.CRLF);
}
sb.append("Response Body: ").append(StrUtil.CRLF);
sb.append(" ").append(this.body()).append(StrUtil.CRLF);
return sb.toString();
}
/**
*
*
* @param targetFileOrDir
* @return File
* @since 5.4.1
*/
public File completeFileNameFromHeader(File targetFileOrDir) {
if (false == targetFileOrDir.isDirectory()) {
// 非目录直接返回
return targetFileOrDir;
}
// 从头信息中获取文件名
String fileName = getFileNameFromDisposition(null);
if (StrUtil.isBlank(fileName)) {
final String path = httpConnection.getUrl().getPath();
// 从路径中获取文件名
fileName = StrUtil.subSuf(path, path.lastIndexOf('/') + 1);
if (StrUtil.isBlank(fileName)) {
// 编码后的路径做为文件名
fileName = URLUtil.encodeQuery(path, charset);
} else {
// issue#I4K0FS@Gitee
fileName = URLUtil.decode(fileName, charset);
}
}
return FileUtil.file(targetFileOrDir, fileName);
}
/**
* Content-Disposition
* @param paramName
*
* @return empty
*/
public String getFileNameFromDisposition(String paramName) {
paramName = ObjUtil.defaultIfNull(paramName, "filename");
String fileName = null;
final String disposition = header(Header.CONTENT_DISPOSITION);
if (StrUtil.isNotBlank(disposition)) {
fileName = ReUtil.get(paramName+"=\"(.*?)\"", disposition, 1);
if (StrUtil.isBlank(fileName)) {
fileName = StrUtil.subAfter(disposition, paramName + "=", true);
}
}
return fileName;
}
// ---------------------------------------------------------------- Private method start
/**
* Http<br>
*
*
* <pre>
* 1Http
* 2
* 3Http
* </pre>
*
* @return this
* @throws HttpException IO
*/
private HttpResponse initWithDisconnect() throws HttpException {
try {
init();
} catch (HttpException e) {
this.httpConnection.disconnectQuietly();
throw e;
}
return this;
}
/**
* Http<br>
*
*
* <pre>
* 1Http
* 2
* 3Http
* </pre>
*
* @return this
* @throws HttpException IO
*/
private HttpResponse init() throws HttpException {
// 获取响应状态码
try {
this.status = httpConnection.responseCode();
} catch (IOException e) {
if (false == (e instanceof FileNotFoundException)) {
throw new HttpException(e);
}
// 服务器无返回内容,忽略之
}
// 读取响应头信息
try {
this.headers = httpConnection.headers();
} catch (IllegalArgumentException e) {
// ignore
// StaticLog.warn(e, e.getMessage());
}
// 存储服务端设置的Cookie信息
GlobalCookieManager.store(httpConnection);
// 获取响应编码
final Charset charset = httpConnection.getCharset();
this.charsetFromResponse = charset;
if (null != charset) {
this.charset = charset;
}
// 获取响应内容流
this.in = new HttpInputStream(this);
// 同步情况下强制同步
return this.isAsync ? this : forceSync();
}
/**
* <br>
*
*
* <pre>
* 1body
* 2false
* 3Http
* 4
* </pre>
*
* @return this
*/
private HttpResponse forceSync() {
// 非同步状态转为同步状态
try {
this.readBody(this.in);
} catch (IORuntimeException e) {
//noinspection StatementWithEmptyBody
if (e.getCause() instanceof FileNotFoundException) {
// 服务器无返回内容,忽略之
} else {
throw new HttpException(e);
}
} finally {
if (this.isAsync) {
this.isAsync = false;
}
this.close();
}
return this;
}
/**
* EOFException
*
* @param in
* @throws IORuntimeException IO
*/
private void readBody(InputStream in) throws IORuntimeException {
if (ignoreBody) {
return;
}
final long contentLength = contentLength();
final FastByteArrayOutputStream out = new FastByteArrayOutputStream((int) contentLength);
copyBody(in, out, contentLength, null, this.config.ignoreEOFError);
this.bodyBytes = out.toByteArray();
}
/**
* {@link OutputStream}<br>
* Http<br>
* Http
*
* @param in
* @param out
* @param contentLength -1
* @param streamProgress
* @param isIgnoreEOFError EOF
* @return
*/
private static long copyBody(InputStream in, OutputStream out, long contentLength, StreamProgress streamProgress, boolean isIgnoreEOFError) {
if (null == out) {
throw new NullPointerException("[out] is null!");
}
long copyLength = -1;
try {
copyLength = IoUtil.copy(in, out, IoUtil.DEFAULT_BUFFER_SIZE, contentLength, streamProgress);
} catch (IORuntimeException e) {
//noinspection StatementWithEmptyBody
if (isIgnoreEOFError
&& (e.getCause() instanceof EOFException || StrUtil.containsIgnoreCase(e.getMessage(), "Premature EOF"))) {
// 忽略读取HTTP流中的EOF错误
} else {
throw e;
}
}
return copyLength;
}
// ---------------------------------------------------------------- Private method end
}

View File

@ -0,0 +1,222 @@
package aiyh.utils.tool.cn.hutool.http;
/**
* HTTP
*
* @author Looly
* @see java.net.HttpURLConnection
*
*/
public class HttpStatus {
/* 2XX: generally "OK" */
/**
* HTTP Status-Code 200: OK.
*/
public static final int HTTP_OK = 200;
/**
* HTTP Status-Code 201: Created.
*/
public static final int HTTP_CREATED = 201;
/**
* HTTP Status-Code 202: Accepted.
*/
public static final int HTTP_ACCEPTED = 202;
/**
* HTTP Status-Code 203: Non-Authoritative Information.
*/
public static final int HTTP_NOT_AUTHORITATIVE = 203;
/**
* HTTP Status-Code 204: No Content.
*/
public static final int HTTP_NO_CONTENT = 204;
/**
* HTTP Status-Code 205: Reset Content.
*/
public static final int HTTP_RESET = 205;
/**
* HTTP Status-Code 206: Partial Content.
*/
public static final int HTTP_PARTIAL = 206;
/* 3XX: relocation/redirect */
/**
* HTTP Status-Code 300: Multiple Choices.
*/
public static final int HTTP_MULT_CHOICE = 300;
/**
* HTTP Status-Code 301: Moved Permanently.
*/
public static final int HTTP_MOVED_PERM = 301;
/**
* HTTP Status-Code 302: Temporary Redirect.
*/
public static final int HTTP_MOVED_TEMP = 302;
/**
* HTTP Status-Code 303: See Other.
*/
public static final int HTTP_SEE_OTHER = 303;
/**
* HTTP Status-Code 304: Not Modified.
*/
public static final int HTTP_NOT_MODIFIED = 304;
/**
* HTTP Status-Code 305: Use Proxy.
*/
public static final int HTTP_USE_PROXY = 305;
/**
* HTTP 1.1 Status-Code 307: Temporary Redirect.<br>
* RFC-7231
*/
public static final int HTTP_TEMP_REDIRECT = 307;
/**
* HTTP 1.1 Status-Code 308: Permanent Redirect <br>
* RFC-7231
*/
public static final int HTTP_PERMANENT_REDIRECT = 308;
/* 4XX: client error */
/**
* HTTP Status-Code 400: Bad Request.
*/
public static final int HTTP_BAD_REQUEST = 400;
/**
* HTTP Status-Code 401: Unauthorized.
*/
public static final int HTTP_UNAUTHORIZED = 401;
/**
* HTTP Status-Code 402: Payment Required.
*/
public static final int HTTP_PAYMENT_REQUIRED = 402;
/**
* HTTP Status-Code 403: Forbidden.
*/
public static final int HTTP_FORBIDDEN = 403;
/**
* HTTP Status-Code 404: Not Found.
*/
public static final int HTTP_NOT_FOUND = 404;
/**
* HTTP Status-Code 405: Method Not Allowed.
*/
public static final int HTTP_BAD_METHOD = 405;
/**
* HTTP Status-Code 406: Not Acceptable.
*/
public static final int HTTP_NOT_ACCEPTABLE = 406;
/**
* HTTP Status-Code 407: Proxy Authentication Required.
*/
public static final int HTTP_PROXY_AUTH = 407;
/**
* HTTP Status-Code 408: Request Time-Out.
*/
public static final int HTTP_CLIENT_TIMEOUT = 408;
/**
* HTTP Status-Code 409: Conflict.
*/
public static final int HTTP_CONFLICT = 409;
/**
* HTTP Status-Code 410: Gone.
*/
public static final int HTTP_GONE = 410;
/**
* HTTP Status-Code 411: Length Required.
*/
public static final int HTTP_LENGTH_REQUIRED = 411;
/**
* HTTP Status-Code 412: Precondition Failed.
*/
public static final int HTTP_PRECON_FAILED = 412;
/**
* HTTP Status-Code 413: Request Entity Too Large.
*/
public static final int HTTP_ENTITY_TOO_LARGE = 413;
/**
* HTTP Status-Code 414: Request-URI Too Large.
*/
public static final int HTTP_REQ_TOO_LONG = 414;
/**
* HTTP Status-Code 415: Unsupported Media Type.
*/
public static final int HTTP_UNSUPPORTED_TYPE = 415;
/* 5XX: server error */
/**
* HTTP Status-Code 500: Internal Server Error.
*/
public static final int HTTP_INTERNAL_ERROR = 500;
/**
* HTTP Status-Code 501: Not Implemented.
*/
public static final int HTTP_NOT_IMPLEMENTED = 501;
/**
* HTTP Status-Code 502: Bad Gateway.
*/
public static final int HTTP_BAD_GATEWAY = 502;
/**
* HTTP Status-Code 503: Service Unavailable.
*/
public static final int HTTP_UNAVAILABLE = 503;
/**
* HTTP Status-Code 504: Gateway Timeout.
*/
public static final int HTTP_GATEWAY_TIMEOUT = 504;
/**
* HTTP Status-Code 505: HTTP Version Not Supported.
*/
public static final int HTTP_VERSION = 505;
/**
*
* @param responseCode
* @return
* @since 5.6.3
*/
public static boolean isRedirected(int responseCode){
return responseCode == HTTP_MOVED_PERM
|| responseCode == HTTP_MOVED_TEMP
|| responseCode == HTTP_SEE_OTHER
// issue#1504@Github307和308是RFC 7538中http 1.1定义的规范
|| responseCode == HTTP_TEMP_REDIRECT
|| responseCode == HTTP_PERMANENT_REDIRECT;
}
}

View File

@ -0,0 +1,893 @@
package aiyh.utils.tool.cn.hutool.http;
import aiyh.utils.tool.cn.hutool.core.codec.Base64;
import aiyh.utils.tool.cn.hutool.core.convert.Convert;
import aiyh.utils.tool.cn.hutool.core.io.FileUtil;
import aiyh.utils.tool.cn.hutool.core.io.IoUtil;
import aiyh.utils.tool.cn.hutool.core.io.StreamProgress;
import aiyh.utils.tool.cn.hutool.core.map.MapUtil;
import aiyh.utils.tool.cn.hutool.core.net.RFC3986;
import aiyh.utils.tool.cn.hutool.core.net.url.UrlQuery;
import aiyh.utils.tool.cn.hutool.core.text.StrBuilder;
import aiyh.utils.tool.cn.hutool.core.util.CharsetUtil;
import aiyh.utils.tool.cn.hutool.core.util.ObjectUtil;
import aiyh.utils.tool.cn.hutool.core.util.ReUtil;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
import aiyh.utils.tool.cn.hutool.core.util.URLUtil;
import aiyh.utils.tool.cn.hutool.http.cookie.GlobalCookieManager;
import aiyh.utils.tool.cn.hutool.http.server.SimpleServer;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.CookieManager;
import java.net.HttpURLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/**
* Http
*
* @author xiaoleilu
*/
public class HttpUtil {
/**
* Content-Type
*/
public static final Pattern CHARSET_PATTERN = Pattern.compile("charset\\s*=\\s*([a-z0-9-]*)", Pattern.CASE_INSENSITIVE);
/**
* meta
*/
public static final Pattern META_CHARSET_PATTERN = Pattern.compile("<meta[^>]*?charset\\s*=\\s*['\"]?([a-z0-9-]*)", Pattern.CASE_INSENSITIVE);
/**
* https
*
* @param url URL
* @return https
*/
public static boolean isHttps(String url) {
return StrUtil.startWithIgnoreCase(url, "https:");
}
/**
* http
*
* @param url URL
* @return http
* @since 5.3.8
*/
public static boolean isHttp(String url) {
return StrUtil.startWithIgnoreCase(url, "http:");
}
/**
* Http
*
* @param method {@link aiyh.utils.tool.cn.hutool.http.Method}
* @param url URL使HTTPHTTPS
* @return {@link aiyh.utils.tool.cn.hutool.http.HttpRequest}
* @since 3.0.9
*/
public static aiyh.utils.tool.cn.hutool.http.HttpRequest createRequest(Method method, String url) {
return aiyh.utils.tool.cn.hutool.http.HttpRequest.of(url).method(method);
}
/**
* Http GET
*
* @param url URL使HTTPHTTPS
* @return {@link aiyh.utils.tool.cn.hutool.http.HttpRequest}
* @since 3.2.0
*/
public static aiyh.utils.tool.cn.hutool.http.HttpRequest createGet(String url) {
return createGet(url, false);
}
/**
* Http GET
*
* @param url URL使HTTPHTTPS
* @param isFollowRedirects
* @return {@link aiyh.utils.tool.cn.hutool.http.HttpRequest}
* @since 5.6.4
*/
public static aiyh.utils.tool.cn.hutool.http.HttpRequest createGet(String url, boolean isFollowRedirects) {
return aiyh.utils.tool.cn.hutool.http.HttpRequest.get(url).setFollowRedirects(isFollowRedirects);
}
/**
* Http POST
*
* @param url URL使HTTPHTTPS
* @return {@link aiyh.utils.tool.cn.hutool.http.HttpRequest}
* @since 3.2.0
*/
public static aiyh.utils.tool.cn.hutool.http.HttpRequest createPost(String url) {
return aiyh.utils.tool.cn.hutool.http.HttpRequest.post(url);
}
/**
* get
*
* @param urlString
* @param customCharset 使
* @return "" null
*/
public static String get(String urlString, Charset customCharset) {
return aiyh.utils.tool.cn.hutool.http.HttpRequest.get(urlString).charset(customCharset).execute().body();
}
/**
* get
*
* @param urlString
* @return "" null
*/
public static String get(String urlString) {
return get(urlString, HttpGlobalConfig.getTimeout());
}
/**
* get
*
* @param urlString
* @param timeout -1
* @return "" null
* @since 3.2.0
*/
public static String get(String urlString, int timeout) {
return aiyh.utils.tool.cn.hutool.http.HttpRequest.get(urlString).timeout(timeout).execute().body();
}
/**
* get
*
* @param urlString
* @param paramMap post
* @return
*/
public static String get(String urlString, Map<String, Object> paramMap) {
return aiyh.utils.tool.cn.hutool.http.HttpRequest.get(urlString).form(paramMap).execute().body();
}
/**
* get
*
* @param urlString
* @param paramMap post
* @param timeout -1
* @return
* @since 3.3.0
*/
public static String get(String urlString, Map<String, Object> paramMap, int timeout) {
return aiyh.utils.tool.cn.hutool.http.HttpRequest.get(urlString).form(paramMap).timeout(timeout).execute().body();
}
/**
* post
*
* @param urlString
* @param paramMap post
* @return
*/
public static String post(String urlString, Map<String, Object> paramMap) {
return post(urlString, paramMap, HttpGlobalConfig.getTimeout());
}
/**
* post
*
* @param urlString
* @param paramMap post
* @param timeout -1
* @return
* @since 3.2.0
*/
public static String post(String urlString, Map<String, Object> paramMap, int timeout) {
return aiyh.utils.tool.cn.hutool.http.HttpRequest.post(urlString).form(paramMap).timeout(timeout).execute().body();
}
/**
* post<br>
* body
*
* <pre>
* 1. a=1&amp;b=2
* 2. RestbodyJSONXMLHutoolContent-Type
* </pre>
*
* @param urlString
* @param body post
* @return
*/
public static String post(String urlString, String body) {
return post(urlString, body, HttpGlobalConfig.getTimeout());
}
/**
* post<br>
* body
*
* <pre>
* 1. a=1&amp;b=2
* 2. RestbodyJSONXMLHutoolContent-Type
* </pre>
*
* @param urlString
* @param body post
* @param timeout -1
* @return
* @since 3.2.0
*/
public static String post(String urlString, String body, int timeout) {
return HttpRequest.post(urlString).timeout(timeout).body(body).execute().body();
}
// ---------------------------------------------------------------------------------------- download
/**
*
*
* @param url url
* @param customCharsetName
* @return
*/
public static String downloadString(String url, String customCharsetName) {
return downloadString(url, CharsetUtil.charset(customCharsetName), null);
}
/**
*
*
* @param url url
* @param customCharset 使{@link CharsetUtil#charset}
* @return
*/
public static String downloadString(String url, Charset customCharset) {
return downloadString(url, customCharset, null);
}
/**
*
*
* @param url url
* @param customCharset 使{@link CharsetUtil#charset}
* @param streamPress {@link StreamProgress}
* @return
*/
public static String downloadString(String url, Charset customCharset, StreamProgress streamPress) {
return HttpDownloader.downloadString(url, customCharset, streamPress);
}
/**
*
*
* @param url url
* @param dest URL使URL
* @return
*/
public static long downloadFile(String url, String dest) {
return downloadFile(url, FileUtil.file(dest));
}
/**
*
*
* @param url url
* @param destFile URL使URL
* @return
*/
public static long downloadFile(String url, File destFile) {
return downloadFile(url, destFile, null);
}
/**
*
*
* @param url url
* @param destFile URL使URL
* @param timeout -1
* @return
* @since 4.0.4
*/
public static long downloadFile(String url, File destFile, int timeout) {
return downloadFile(url, destFile, timeout, null);
}
/**
*
*
* @param url url
* @param destFile URL使URL
* @param streamProgress
* @return
*/
public static long downloadFile(String url, File destFile, StreamProgress streamProgress) {
return downloadFile(url, destFile, -1, streamProgress);
}
/**
*
*
* @param url url
* @param destFile URL使URL
* @param timeout -1
* @param streamProgress
* @return
* @since 4.0.4
*/
public static long downloadFile(String url, File destFile, int timeout, StreamProgress streamProgress) {
return HttpDownloader.downloadFile(url, destFile, timeout, streamProgress);
}
/**
*
*
* @param url url
* @param dest URL使URL
* @return
* @since 5.4.1
*/
public static File downloadFileFromUrl(String url, String dest) {
return downloadFileFromUrl(url, FileUtil.file(dest));
}
/**
*
*
* @param url url
* @param destFile URL使URL
* @return
* @since 5.4.1
*/
public static File downloadFileFromUrl(String url, File destFile) {
return downloadFileFromUrl(url, destFile, null);
}
/**
*
*
* @param url url
* @param destFile URL使URL
* @param timeout -1
* @return
* @since 5.4.1
*/
public static File downloadFileFromUrl(String url, File destFile, int timeout) {
return downloadFileFromUrl(url, destFile, timeout, null);
}
/**
*
*
* @param url url
* @param destFile URL使URL
* @param streamProgress
* @return
* @since 5.4.1
*/
public static File downloadFileFromUrl(String url, File destFile, StreamProgress streamProgress) {
return downloadFileFromUrl(url, destFile, -1, streamProgress);
}
/**
*
*
* @param url url
* @param destFile URL使URL
* @param timeout -1
* @param streamProgress
* @return
* @since 5.4.1
*/
public static File downloadFileFromUrl(String url, File destFile, int timeout, StreamProgress streamProgress) {
return HttpDownloader.downloadForFile(url, destFile, timeout, streamProgress);
}
/**
*
*
* @param url url
* @param out {@link OutputStream}
* @param isCloseOut
* @return
*/
public static long download(String url, OutputStream out, boolean isCloseOut) {
return download(url, out, isCloseOut, null);
}
/**
*
*
* @param url url
* @param out {@link OutputStream}
* @param isCloseOut
* @param streamProgress
* @return
*/
public static long download(String url, OutputStream out, boolean isCloseOut, StreamProgress streamProgress) {
return HttpDownloader.download(url, out, isCloseOut, streamProgress);
}
/**
* 30x
*
* @param url url
* @return
* @since 5.3.6
*/
public static byte[] downloadBytes(String url) {
return HttpDownloader.downloadBytes(url);
}
/**
* MapFormUrlurl
*
* @param paramMap
* @return url
*/
public static String toParams(Map<String, ?> paramMap) {
return toParams(paramMap, CharsetUtil.CHARSET_UTF_8);
}
/**
* MapFormUrl<br>
*
*
* @param paramMap
* @param charsetName
* @return url
* @deprecated 使 {@link #toParams(Map, Charset)}
*/
@Deprecated
public static String toParams(Map<String, Object> paramMap, String charsetName) {
return toParams(paramMap, CharsetUtil.charset(charsetName));
}
/**
* MapFormUrl<br>
* paramMapkeynull""valuenull""<br>
* url<br>
* URLQueryPOST
*
* <pre>
* key1=v1&amp;key2=&amp;key3=v3
* </pre>
*
* @param paramMap
* @param charset {@code null} encode
* @return url
* @see #toParams(Map, Charset, boolean)
*/
public static String toParams(Map<String, ?> paramMap, Charset charset) {
return toParams(paramMap, charset, false);
}
/**
* MapFormUrl<br>
* paramMapkeynull""valuenull""<br>
* url
*
* <pre>
* key1=v1&amp;key2=&amp;key3=v3
* </pre>
*
* @param paramMap
* @param charset nullencode
* @param isFormUrlEncoded x-www-form-urlencoded'+'
* @return url
* @since 5.7.16
*/
public static String toParams(Map<String, ?> paramMap, Charset charset, boolean isFormUrlEncoded) {
return UrlQuery.of(paramMap, isFormUrlEncoded).build(charset);
}
/**
* URL<br>
* urlurl
*
* <p>URL</p>
*
* @param urlWithParams urlurl
* @param charset
* @return url
* @since 4.0.1
*/
public static String encodeParams(String urlWithParams, Charset charset) {
if (StrUtil.isBlank(urlWithParams)) {
return StrUtil.EMPTY;
}
String urlPart = null; // url部分不包括问号
String paramPart; // 参数部分
final int pathEndPos = urlWithParams.indexOf('?');
if (pathEndPos > -1) {
// url + 参数
urlPart = StrUtil.subPre(urlWithParams, pathEndPos);
paramPart = StrUtil.subSuf(urlWithParams, pathEndPos + 1);
if (StrUtil.isBlank(paramPart)) {
// 无参数返回url
return urlPart;
}
} else if (false == StrUtil.contains(urlWithParams, '=')) {
// 无参数的URL
return urlWithParams;
} else {
// 无URL的参数
paramPart = urlWithParams;
}
paramPart = normalizeParams(paramPart, charset);
return StrUtil.isBlank(urlPart) ? paramPart : urlPart + "?" + paramPart;
}
/**
* URL
*
* <p>URL</p>
*
* @param paramPart
* @param charset
* @return
* @since 4.5.2
*/
public static String normalizeParams(String paramPart, Charset charset) {
if(StrUtil.isEmpty(paramPart)){
return paramPart;
}
final StrBuilder builder = StrBuilder.create(paramPart.length() + 16);
final int len = paramPart.length();
String name = null;
int pos = 0; // 未处理字符开始位置
char c; // 当前字符
int i; // 当前字符位置
for (i = 0; i < len; i++) {
c = paramPart.charAt(i);
if (c == '=') { // 键值对的分界点
if (null == name) {
// 只有=前未定义name时被当作键值分界符否则做为普通字符
name = (pos == i) ? StrUtil.EMPTY : paramPart.substring(pos, i);
pos = i + 1;
}
} else if (c == '&') { // 参数对的分界点
if (pos != i) {
if (null == name) {
// 对于像&a&这类无参数值的字符串我们将name为a的值设为""
name = paramPart.substring(pos, i);
builder.append(RFC3986.QUERY_PARAM_NAME.encode(name, charset)).append('=');
} else {
builder.append(RFC3986.QUERY_PARAM_NAME.encode(name, charset)).append('=')
.append(RFC3986.QUERY_PARAM_VALUE.encode(paramPart.substring(pos, i), charset)).append('&');
}
name = null;
}
pos = i + 1;
}
}
// 结尾处理
if (null != name) {
builder.append(URLUtil.encodeQuery(name, charset)).append('=');
}
if (pos != i) {
if (null == name && pos > 0) {
builder.append('=');
}
builder.append(URLUtil.encodeQuery(paramPart.substring(pos, i), charset));
}
// 以&结尾则去除之
int lastIndex = builder.length() - 1;
if ('&' == builder.charAt(lastIndex)) {
builder.delTo(lastIndex);
}
return builder.toString();
}
/**
* URLMapPost
*
* @param paramsStr Path
* @param charset
* @return Map
* @since 5.2.6
*/
public static Map<String, String> decodeParamMap(String paramsStr, Charset charset) {
final Map<CharSequence, CharSequence> queryMap = UrlQuery.of(paramsStr, charset).getQueryMap();
if (MapUtil.isEmpty(queryMap)) {
return MapUtil.empty();
}
return Convert.toMap(String.class, String.class, queryMap);
}
/**
* URLMapPost
*
* @param paramsStr Path
* @param charset
* @return Map
*/
public static Map<String, List<String>> decodeParams(String paramsStr, String charset) {
return decodeParams(paramsStr, charset, false);
}
/**
* URLMapPost
*
* @param paramsStr Path
* @param charset
* @param isFormUrlEncoded x-www-form-urlencoded'+'
* @return Map
* @since 5.8.12
*/
public static Map<String, List<String>> decodeParams(String paramsStr, String charset, boolean isFormUrlEncoded) {
return decodeParams(paramsStr, CharsetUtil.charset(charset), isFormUrlEncoded);
}
/**
* URL QueryStringMap
*
* @param paramsStr Path
* @param charset
* @return Map
* @since 5.2.6
*/
public static Map<String, List<String>> decodeParams(String paramsStr, Charset charset) {
return decodeParams(paramsStr, charset, false);
}
/**
* URLMapPost
*
* @param paramsStr Path
* @param charset
* @param isFormUrlEncoded x-www-form-urlencoded'+'
* @return Map
*/
public static Map<String, List<String>> decodeParams(String paramsStr, Charset charset, boolean isFormUrlEncoded) {
final Map<CharSequence, CharSequence> queryMap =
UrlQuery.of(paramsStr, charset, true, isFormUrlEncoded).getQueryMap();
if (MapUtil.isEmpty(queryMap)) {
return MapUtil.empty();
}
final Map<String, List<String>> params = new LinkedHashMap<>();
queryMap.forEach((key, value) -> {
final List<String> values = params.computeIfAbsent(StrUtil.str(key), k -> new ArrayList<>(1));
// 一般是一个参数
values.add(StrUtil.str(value));
});
return params;
}
/**
* URLGET<br>
* urlurl
*
* @param url URL
* @param form
* @param charset
* @param isEncodeParams
* @return URL
*/
public static String urlWithForm(String url, Map<String, Object> form, Charset charset, boolean isEncodeParams) {
if (isEncodeParams && StrUtil.contains(url, '?')) {
// 在需要编码的情况下如果url中已经有部分参数则编码之
url = encodeParams(url, charset);
}
// url和参数是分别编码的
return urlWithForm(url, toParams(form, charset), charset, false);
}
/**
* URLGET
*
* @param url URL
* @param queryString
* @param charset
* @param isEncode
* @return
*/
public static String urlWithForm(String url, String queryString, Charset charset, boolean isEncode) {
if (StrUtil.isBlank(queryString)) {
// 无额外参数
if (StrUtil.contains(url, '?')) {
// url中包含参数
return isEncode ? encodeParams(url, charset) : url;
}
return url;
}
// 始终有参数
final StrBuilder urlBuilder = StrBuilder.create(url.length() + queryString.length() + 16);
int qmIndex = url.indexOf('?');
if (qmIndex > 0) {
// 原URL带参数则对这部分参数单独编码如果选项为进行编码
urlBuilder.append(isEncode ? encodeParams(url, charset) : url);
if (false == StrUtil.endWith(url, '&')) {
// 已经带参数的情况下追加参数
urlBuilder.append('&');
}
} else {
// 原url无参数则不做编码
urlBuilder.append(url);
if (qmIndex < 0) {
// 无 '?' 追加之
urlBuilder.append('?');
}
}
urlBuilder.append(isEncode ? encodeParams(queryString, charset) : queryString);
return urlBuilder.toString();
}
/**
* Http<br>
* ContentType
*
* @param conn HTTP
* @return
*/
public static String getCharset(HttpURLConnection conn) {
if (conn == null) {
return null;
}
return getCharset(conn.getContentType());
}
/**
* Http<br>
* ContentType
*
* @param contentType Content-Type
* @return
* @since 5.2.6
*/
public static String getCharset(String contentType) {
if (StrUtil.isBlank(contentType)) {
return null;
}
return ReUtil.get(CHARSET_PATTERN, contentType, 1);
}
/**
* <br>
* 使charsetUTF-8isGetCharsetFromContenttrue
*
* @param in
* @param charset
* @param isGetCharsetFromContent
* @return
*/
public static String getString(InputStream in, Charset charset, boolean isGetCharsetFromContent) {
final byte[] contentBytes = IoUtil.readBytes(in);
return getString(contentBytes, charset, isGetCharsetFromContent);
}
/**
* <br>
* 使charsetUTF-8isGetCharsetFromContenttrue
*
* @param contentBytes byte
* @param charset
* @param isGetCharsetFromContent
* @return
*/
public static String getString(byte[] contentBytes, Charset charset, boolean isGetCharsetFromContent) {
if (null == contentBytes) {
return null;
}
if (null == charset) {
charset = CharsetUtil.CHARSET_UTF_8;
}
String content = new String(contentBytes, charset);
if (isGetCharsetFromContent) {
final String charsetInContentStr = ReUtil.get(META_CHARSET_PATTERN, content, 1);
if (StrUtil.isNotBlank(charsetInContentStr)) {
Charset charsetInContent = null;
try {
charsetInContent = Charset.forName(charsetInContentStr);
} catch (Exception e) {
if (StrUtil.containsIgnoreCase(charsetInContentStr, "utf-8") || StrUtil.containsIgnoreCase(charsetInContentStr, "utf8")) {
charsetInContent = CharsetUtil.CHARSET_UTF_8;
} else if (StrUtil.containsIgnoreCase(charsetInContentStr, "gbk")) {
charsetInContent = CharsetUtil.CHARSET_GBK;
}
// ignore
}
if (null != charsetInContent && false == charset.equals(charsetInContent)) {
content = new String(contentBytes, charsetInContent);
}
}
}
return content;
}
/**
* MimeType
*
* @param filePath
* @param defaultValue MimeTypenull
* @return MimeType
* @see FileUtil#getMimeType(String)
* @since 4.6.5
*/
public static String getMimeType(String filePath, String defaultValue) {
return ObjectUtil.defaultIfNull(getMimeType(filePath), defaultValue);
}
/**
* MimeType
*
* @param filePath
* @return MimeType
* @see FileUtil#getMimeType(String)
*/
public static String getMimeType(String filePath) {
return FileUtil.getMimeType(filePath);
}
/**
* bodyContent-Type
*
* <pre>
* 1. application/json
* 1. application/xml
* </pre>
*
* @param body
* @return Content-Typenull
* @see aiyh.utils.tool.cn.hutool.http.ContentType#get(String)
* @since 3.2.0
*/
public static String getContentTypeByRequestBody(String body) {
final aiyh.utils.tool.cn.hutool.http.ContentType contentType = ContentType.get(body);
return (null == contentType) ? null : contentType.toString();
}
/**
* Http
*
* @param port
* @return {@link SimpleServer}
* @since 5.2.6
*/
public static SimpleServer createServer(int port) {
return new SimpleServer(port);
}
/**
*
* <pre>
* Basic YWxhZGRpbjpvcGVuc2VzYW1l
* </pre>
*
* @param username
* @param password
* @param charset ASCII
* @return
* @since 5.4.6
*/
public static String buildBasicAuth(String username, String password, Charset charset) {
final String data = username.concat(":").concat(password);
return "Basic " + Base64.encode(data, charset);
}
/**
* Cookie
*
* @see GlobalCookieManager#setCookieManager(CookieManager)
* @since 5.6.5
*/
public static void closeCookie() {
GlobalCookieManager.setCookieManager(null);
}
}

View File

@ -0,0 +1,10 @@
package aiyh.utils.tool.cn.hutool.http;
/**
* Http
*
* @author Looly
*/
public enum Method {
GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE, CONNECT, PATCH
}

View File

@ -0,0 +1,185 @@
package aiyh.utils.tool.cn.hutool.http;
import aiyh.utils.tool.cn.hutool.core.convert.Convert;
import aiyh.utils.tool.cn.hutool.core.io.IORuntimeException;
import aiyh.utils.tool.cn.hutool.core.io.IoUtil;
import aiyh.utils.tool.cn.hutool.core.io.resource.MultiResource;
import aiyh.utils.tool.cn.hutool.core.io.resource.Resource;
import aiyh.utils.tool.cn.hutool.core.io.resource.StringResource;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
/**
* Multipart/form-data<br>
* RFC2388
*
* @author looly
* @since 5.7.17
*/
public class MultipartOutputStream extends OutputStream {
private static final String CONTENT_DISPOSITION_TEMPLATE = "Content-Disposition: form-data; name=\"{}\"\r\n";
private static final String CONTENT_DISPOSITION_FILE_TEMPLATE = "Content-Disposition: form-data; name=\"{}\"; filename=\"{}\"\r\n";
private static final String CONTENT_TYPE_FILE_TEMPLATE = "Content-Type: {}\r\n";
private final OutputStream out;
private final Charset charset;
private final String boundary;
private boolean isFinish;
/**
* 使
*
* @param out HTTP
* @param charset
*/
public MultipartOutputStream(OutputStream out, Charset charset) {
this(out, charset, HttpGlobalConfig.getBoundary());
}
/**
*
*
* @param out HTTP
* @param charset
* @param boundary
* @since 5.7.17
*/
public MultipartOutputStream(OutputStream out, Charset charset, String boundary) {
this.out = out;
this.charset = charset;
this.boundary = boundary;
}
/**
* Multipart<br>
* <pre>
* --(boundary)[]
* Content-Disposition: form-data; name="参数名"[]
* []
* []
* </pre>
* <p>
*
*
* <pre>
* --(boundary)[]
* Content-Disposition: form-data; name="表单名"; filename="文件名"[]
* Content-Type: MIME[]
* []
* []
* </pre>
*
* @param formFieldName
* @param value
* @return this
* @throws IORuntimeException IO
*/
public MultipartOutputStream write(String formFieldName, Object value) throws IORuntimeException {
// 多资源
if (value instanceof MultiResource) {
for (Resource subResource : (MultiResource) value) {
write(formFieldName, subResource);
}
return this;
}
// --分隔符(boundary)[换行]
beginPart();
if (value instanceof Resource) {
appendResource(formFieldName, (Resource) value);
} else {
appendResource(formFieldName,
new StringResource(Convert.toStr(value), null, this.charset));
}
write(StrUtil.CRLF);
return this;
}
@Override
public void write(int b) throws IOException {
this.out.write(b);
}
/**
*
*
* @throws IORuntimeException IO
*/
public void finish() throws IORuntimeException {
if (!isFinish) {
write(StrUtil.format("--{}--\r\n", boundary));
this.isFinish = true;
}
}
@Override
public void close() {
finish();
IoUtil.close(this.out);
}
/**
* MultipartResource{@link HttpResource}
*
* @param formFieldName
* @param resource
* @throws IORuntimeException IO
*/
private void appendResource(String formFieldName, Resource resource) throws IORuntimeException {
final String fileName = resource.getName();
// Content-Disposition
if (null == fileName) {
// Content-Disposition: form-data; name="参数名"[换行]
write(StrUtil.format(CONTENT_DISPOSITION_TEMPLATE, formFieldName));
} else {
// Content-Disposition: form-data; name="参数名"; filename="文件名"[换行]
write(StrUtil.format(CONTENT_DISPOSITION_FILE_TEMPLATE, formFieldName, fileName));
}
// Content-Type
if (resource instanceof HttpResource) {
final String contentType = ((HttpResource) resource).getContentType();
if (StrUtil.isNotBlank(contentType)) {
// Content-Type: 类型[换行]
write(StrUtil.format(CONTENT_TYPE_FILE_TEMPLATE, contentType));
}
} else if (StrUtil.isNotEmpty(fileName)) {
// 根据name的扩展名指定互联网媒体类型默认二进制流数据
write(StrUtil.format(CONTENT_TYPE_FILE_TEMPLATE,
HttpUtil.getMimeType(fileName, ContentType.OCTET_STREAM.getValue())));
}
// 内容
write("\r\n");
resource.writeTo(this);
}
/**
* part:<br>
* <pre>
* --(boundary)[]
* </pre>
*/
private void beginPart() {
// --分隔符(boundary)[换行]
write("--", boundary, StrUtil.CRLF);
}
/**
*
*
* @param objs
*/
private void write(Object... objs) {
IoUtil.write(this, this.charset, false, objs);
}
}

View File

@ -0,0 +1,189 @@
package aiyh.utils.tool.cn.hutool.http;
/**
*
*
* @author Looly
*/
interface Status {
/**
* HTTP Status-Code 200: OK.
*/
int HTTP_OK = 200;
/**
* HTTP Status-Code 201: Created.
*/
int HTTP_CREATED = 201;
/**
* HTTP Status-Code 202: Accepted.
*/
int HTTP_ACCEPTED = 202;
/**
* HTTP Status-Code 203: Non-Authoritative Information.
*/
int HTTP_NOT_AUTHORITATIVE = 203;
/**
* HTTP Status-Code 204: No Content.
*/
int HTTP_NO_CONTENT = 204;
/**
* HTTP Status-Code 205: Reset Content.
*/
int HTTP_RESET = 205;
/**
* HTTP Status-Code 206: Partial Content.
*/
int HTTP_PARTIAL = 206;
/* 3XX: relocation/redirect */
/**
* HTTP Status-Code 300: Multiple Choices.
*/
int HTTP_MULT_CHOICE = 300;
/**
* HTTP Status-Code 301: Moved Permanently.
*/
int HTTP_MOVED_PERM = 301;
/**
* HTTP Status-Code 302: Temporary Redirect.
*/
int HTTP_MOVED_TEMP = 302;
/**
* HTTP Status-Code 303: See Other.
*/
int HTTP_SEE_OTHER = 303;
/**
* HTTP Status-Code 304: Not Modified.
*/
int HTTP_NOT_MODIFIED = 304;
/**
* HTTP Status-Code 305: Use Proxy.
*/
int HTTP_USE_PROXY = 305;
/* 4XX: client error */
/**
* HTTP Status-Code 400: Bad Request.
*/
int HTTP_BAD_REQUEST = 400;
/**
* HTTP Status-Code 401: Unauthorized.
*/
int HTTP_UNAUTHORIZED = 401;
/**
* HTTP Status-Code 402: Payment Required.
*/
int HTTP_PAYMENT_REQUIRED = 402;
/**
* HTTP Status-Code 403: Forbidden.
*/
int HTTP_FORBIDDEN = 403;
/**
* HTTP Status-Code 404: Not Found.
*/
int HTTP_NOT_FOUND = 404;
/**
* HTTP Status-Code 405: Method Not Allowed.
*/
int HTTP_BAD_METHOD = 405;
/**
* HTTP Status-Code 406: Not Acceptable.
*/
int HTTP_NOT_ACCEPTABLE = 406;
/**
* HTTP Status-Code 407: Proxy Authentication Required.
*/
int HTTP_PROXY_AUTH = 407;
/**
* HTTP Status-Code 408: Request Time-Out.
*/
int HTTP_CLIENT_TIMEOUT = 408;
/**
* HTTP Status-Code 409: Conflict.
*/
int HTTP_CONFLICT = 409;
/**
* HTTP Status-Code 410: Gone.
*/
int HTTP_GONE = 410;
/**
* HTTP Status-Code 411: Length Required.
*/
int HTTP_LENGTH_REQUIRED = 411;
/**
* HTTP Status-Code 412: Precondition Failed.
*/
int HTTP_PRECON_FAILED = 412;
/**
* HTTP Status-Code 413: Request Entity Too Large.
*/
int HTTP_ENTITY_TOO_LARGE = 413;
/**
* HTTP Status-Code 414: Request-URI Too Large.
*/
int HTTP_REQ_TOO_LONG = 414;
/**
* HTTP Status-Code 415: Unsupported Media Type.
*/
int HTTP_UNSUPPORTED_TYPE = 415;
/* 5XX: server error */
/**
* HTTP Status-Code 500: Internal Server Error.
*/
int HTTP_INTERNAL_ERROR = 500;
/**
* HTTP Status-Code 501: Not Implemented.
*/
int HTTP_NOT_IMPLEMENTED = 501;
/**
* HTTP Status-Code 502: Bad Gateway.
*/
int HTTP_BAD_GATEWAY = 502;
/**
* HTTP Status-Code 503: Service Unavailable.
*/
int HTTP_UNAVAILABLE = 503;
/**
* HTTP Status-Code 504: Gateway Timeout.
*/
int HTTP_GATEWAY_TIMEOUT = 504;
/**
* HTTP Status-Code 505: HTTP Version Not Supported.
*/
int HTTP_VERSION = 505;
}

View File

@ -0,0 +1,40 @@
package aiyh.utils.tool.cn.hutool.http.body;
import aiyh.utils.tool.cn.hutool.core.io.IoUtil;
import java.io.OutputStream;
/**
* bytesHttp request bodyrest bodyJSONXML
*
* @author looly
* @since 5.7.17
*/
public class BytesBody implements RequestBody {
private final byte[] content;
/**
* Http request body
*
* @param content body
* @return BytesBody
*/
public static BytesBody create(byte[] content) {
return new BytesBody(content);
}
/**
*
*
* @param content Body
*/
public BytesBody(byte[] content) {
this.content = content;
}
@Override
public void write(OutputStream out) {
IoUtil.write(out, false, content);
}
}

View File

@ -0,0 +1,38 @@
package aiyh.utils.tool.cn.hutool.http.body;
import aiyh.utils.tool.cn.hutool.core.net.url.UrlQuery;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
import java.nio.charset.Charset;
import java.util.Map;
/**
* application/x-www-form-urlencoded body
*
* @author looly
* @since 5.7.17
*/
public class FormUrlEncodedBody extends BytesBody {
/**
* Http request body
*
* @param form
* @param charset
* @return FormUrlEncodedBody
*/
public static FormUrlEncodedBody create(Map<String, Object> form, Charset charset) {
return new FormUrlEncodedBody(form, charset);
}
/**
*
*
* @param form
* @param charset
*/
public FormUrlEncodedBody(Map<String, Object> form, Charset charset) {
super(StrUtil.bytes(UrlQuery.of(form, true).build(charset), charset));
}
}

View File

@ -0,0 +1,89 @@
package aiyh.utils.tool.cn.hutool.http.body;
import aiyh.utils.tool.cn.hutool.core.io.IoUtil;
import aiyh.utils.tool.cn.hutool.core.map.MapUtil;
import aiyh.utils.tool.cn.hutool.http.ContentType;
import aiyh.utils.tool.cn.hutool.http.HttpGlobalConfig;
import aiyh.utils.tool.cn.hutool.http.MultipartOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Map;
/**
* Multipart/form-data<br>
* RFC2388
*
* @author looly
* @since 5.3.5
*/
public class MultipartBody implements RequestBody {
private static final String CONTENT_TYPE_MULTIPART_PREFIX = ContentType.MULTIPART.getValue() + "; boundary=";
/**
*
*/
private final Map<String, Object> form;
/**
*
*/
private final Charset charset;
/**
*
*/
private final String boundary = HttpGlobalConfig.getBoundary();
/**
* MultipartBody
*
* @param form
* @param charset
* @return MultipartBody
*/
public static MultipartBody create(Map<String, Object> form, Charset charset) {
return new MultipartBody(form, charset);
}
/**
* MultipartContent-Type
*
* @return MultipartContent-Type
*/
public String getContentType() {
return CONTENT_TYPE_MULTIPART_PREFIX + boundary;
}
/**
*
*
* @param form
* @param charset
*/
public MultipartBody(Map<String, Object> form, Charset charset) {
this.form = form;
this.charset = charset;
}
/**
* Multiparty
*
* @param out out
*/
@Override
public void write(OutputStream out) {
final MultipartOutputStream stream = new MultipartOutputStream(out, this.charset, this.boundary);
if (MapUtil.isNotEmpty(this.form)) {
this.form.forEach(stream::write);
}
stream.finish();
}
@Override
public String toString() {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
write(out);
return IoUtil.toStr(out, this.charset);
}
}

View File

@ -0,0 +1,32 @@
package aiyh.utils.tool.cn.hutool.http.body;
import aiyh.utils.tool.cn.hutool.core.io.IoUtil;
import java.io.OutputStream;
/**
*
*/
public interface RequestBody {
/**
*
*
* @param out out
*/
void write(OutputStream out);
/**
* {@link OutputStream}
*
* @param out {@link OutputStream}
* @since 5.7.17
*/
default void writeClose(OutputStream out) {
try {
write(out);
} finally {
IoUtil.close(out);
}
}
}

View File

@ -0,0 +1,6 @@
/**
*
*
* @author looly
*/
package aiyh.utils.tool.cn.hutool.http.body;

View File

@ -0,0 +1,109 @@
package aiyh.utils.tool.cn.hutool.http.cookie;
import aiyh.utils.tool.cn.hutool.core.io.IORuntimeException;
import aiyh.utils.tool.cn.hutool.core.util.URLUtil;
import aiyh.utils.tool.cn.hutool.http.HttpConnection;
import java.io.IOException;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpCookie;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* CookieHutool
*
* @author Looly
* @since 4.5.15
*/
public class GlobalCookieManager {
/** Cookie管理 */
private static CookieManager cookieManager;
static {
cookieManager = new CookieManager(new ThreadLocalCookieStore(), CookiePolicy.ACCEPT_ALL);
}
/**
* {@link CookieManager}
*
* @param customCookieManager {@link CookieManager}
*/
public static void setCookieManager(CookieManager customCookieManager) {
cookieManager = customCookieManager;
}
/**
* {@link CookieManager}
*
* @return {@link CookieManager}
*/
public static CookieManager getCookieManager() {
return cookieManager;
}
/**
* Cookie
*
* @param conn HTTP
* @return Cookie
* @since 4.6.9
*/
public static List<HttpCookie> getCookies(aiyh.utils.tool.cn.hutool.http.HttpConnection conn) {
return cookieManager.getCookieStore().get(getURI(conn));
}
/**
* CookieHttpCookie
*
* @param conn {@link aiyh.utils.tool.cn.hutool.http.HttpConnection}
*/
public static void add(aiyh.utils.tool.cn.hutool.http.HttpConnection conn) {
if (null == cookieManager) {
// 全局Cookie管理器关闭
return;
}
Map<String, List<String>> cookieHeader;
try {
cookieHeader = cookieManager.get(getURI(conn), new HashMap<>(0));
} catch (IOException e) {
throw new IORuntimeException(e);
}
// 不覆盖模式回填Cookie头这样用户定义的Cookie将优先
conn.header(cookieHeader, false);
}
/**
* Cookie
*
* @param conn {@link aiyh.utils.tool.cn.hutool.http.HttpConnection}
*/
public static void store(aiyh.utils.tool.cn.hutool.http.HttpConnection conn) {
if (null == cookieManager) {
// 全局Cookie管理器关闭
return;
}
try {
cookieManager.put(getURI(conn), conn.headers());
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
/**
* URLURI
*
* @param conn HttpConnection
* @return URI
*/
private static URI getURI(HttpConnection conn) {
return URLUtil.toURI(conn.getUrl());
}
}

View File

@ -0,0 +1,75 @@
package aiyh.utils.tool.cn.hutool.http.cookie;
import java.net.CookieManager;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.URI;
import java.util.List;
/**
* 线Cookie线Cookie使Cookie<br>
* <p>
* https://stackoverflow.com/questions/16305486/cookiemanager-for-multiple-threads
*
* @author looly
* @since 4.1.18
*/
public class ThreadLocalCookieStore implements CookieStore {
private final static ThreadLocal<CookieStore> STORES = new ThreadLocal<CookieStore>() {
@Override
protected synchronized CookieStore initialValue() {
/* InMemoryCookieStore */
return (new CookieManager()).getCookieStore();
}
};
/**
* 线CookieStore
*
* @return CookieStore
*/
public CookieStore getCookieStore() {
return STORES.get();
}
/**
* 线Cookie
*
* @return this
*/
public ThreadLocalCookieStore removeCurrent() {
STORES.remove();
return this;
}
@Override
public void add(URI uri, HttpCookie cookie) {
getCookieStore().add(uri, cookie);
}
@Override
public List<HttpCookie> get(URI uri) {
return getCookieStore().get(uri);
}
@Override
public List<HttpCookie> getCookies() {
return getCookieStore().getCookies();
}
@Override
public List<URI> getURIs() {
return getCookieStore().getURIs();
}
@Override
public boolean remove(URI uri, HttpCookie cookie) {
return getCookieStore().remove(uri, cookie);
}
@Override
public boolean removeAll() {
return getCookieStore().removeAll();
}
}

View File

@ -0,0 +1,6 @@
/**
* Cookie
*
* @author looly
*/
package aiyh.utils.tool.cn.hutool.http.cookie;

View File

@ -0,0 +1,6 @@
/**
* Hutool-httpJDKHttpUrlConnectionHTTPSCookie使Http
*
* @author looly
*/
package aiyh.utils.tool.cn.hutool.http;

View File

@ -0,0 +1,57 @@
package aiyh.utils.tool.cn.hutool.http.server;
import aiyh.utils.tool.cn.hutool.core.util.CharsetUtil;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpExchange;
import java.io.Closeable;
import java.nio.charset.Charset;
/**
* HttpServerHttpExchange
*
* @author looly
* @since 5.2.6
*/
public class HttpServerBase implements Closeable {
final static Charset DEFAULT_CHARSET = CharsetUtil.CHARSET_UTF_8;
final HttpExchange httpExchange;
/**
*
*
* @param httpExchange {@link HttpExchange}
*/
public HttpServerBase(HttpExchange httpExchange) {
this.httpExchange = httpExchange;
}
/**
* {@link HttpExchange}
*
* @return {@link HttpExchange}
*/
public HttpExchange getHttpExchange() {
return this.httpExchange;
}
/**
* {@link HttpContext}
*
* @return {@link HttpContext}
* @since 5.5.7
*/
public HttpContext getHttpContext() {
return getHttpExchange().getHttpContext();
}
/**
* {@link HttpExchange#close()}
*/
@Override
public void close() {
this.httpExchange.close();
}
}

View File

@ -0,0 +1,442 @@
package aiyh.utils.tool.cn.hutool.http.server;
import aiyh.utils.tool.cn.hutool.core.collection.CollUtil;
import aiyh.utils.tool.cn.hutool.core.io.IORuntimeException;
import aiyh.utils.tool.cn.hutool.core.io.IoUtil;
import aiyh.utils.tool.cn.hutool.core.map.CaseInsensitiveMap;
import aiyh.utils.tool.cn.hutool.core.map.multi.ListValueMap;
import aiyh.utils.tool.cn.hutool.core.net.NetUtil;
import aiyh.utils.tool.cn.hutool.core.net.multipart.MultipartFormData;
import aiyh.utils.tool.cn.hutool.core.net.multipart.UploadSetting;
import aiyh.utils.tool.cn.hutool.core.util.ArrayUtil;
import aiyh.utils.tool.cn.hutool.core.util.CharsetUtil;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
import aiyh.utils.tool.cn.hutool.http.Header;
import aiyh.utils.tool.cn.hutool.http.HttpUtil;
import aiyh.utils.tool.cn.hutool.http.Method;
import aiyh.utils.tool.cn.hutool.http.useragent.UserAgent;
import aiyh.utils.tool.cn.hutool.http.useragent.UserAgentUtil;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpCookie;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* Http{@link HttpExchange}
*
* @author looly
* @since 5.2.6
*/
public class HttpServerRequest extends HttpServerBase {
private Map<String, HttpCookie> cookieCache;
private ListValueMap<String, String> paramsCache;
private MultipartFormData multipartFormDataCache;
private Charset charsetCache;
private byte[] bodyCache;
/**
*
*
* @param httpExchange {@link HttpExchange}
*/
public HttpServerRequest(HttpExchange httpExchange) {
super(httpExchange);
}
/**
* Http Method
*
* @return Http Method
*/
public String getMethod() {
return this.httpExchange.getRequestMethod();
}
/**
* GET
*
* @return GET
*/
public boolean isGetMethod() {
return aiyh.utils.tool.cn.hutool.http.Method.GET.name().equalsIgnoreCase(getMethod());
}
/**
* POST
*
* @return POST
*/
public boolean isPostMethod() {
return Method.POST.name().equalsIgnoreCase(getMethod());
}
/**
* URI
*
* @return URI
*/
public URI getURI() {
return this.httpExchange.getRequestURI();
}
/**
* Path
*
* @return
*/
public String getPath() {
return getURI().getPath();
}
/**
*
*
* @return
*/
public String getQuery() {
return getURI().getQuery();
}
/**
* header
*
* @return header
*/
public Headers getHeaders() {
return this.httpExchange.getRequestHeaders();
}
/**
* header
*
* @param headerKey KEY
* @return header
*/
public String getHeader(aiyh.utils.tool.cn.hutool.http.Header headerKey) {
return getHeader(headerKey.toString());
}
/**
* header
*
* @param headerKey KEY
* @return header
*/
public String getHeader(String headerKey) {
return getHeaders().getFirst(headerKey);
}
/**
* header
*
* @param headerKey KEY
* @param charset
* @return header
*/
public String getHeader(String headerKey, Charset charset) {
final String header = getHeader(headerKey);
if (null != header) {
return CharsetUtil.convert(header, CharsetUtil.CHARSET_ISO_8859_1, charset);
}
return null;
}
/**
* Content-Type
*
* @return Content-Type
*/
public String getContentType() {
return getHeader(aiyh.utils.tool.cn.hutool.http.Header.CONTENT_TYPE);
}
/**
* 使UTF-8
*
* <pre>
* 1Content-Typetext/html;charset=utf-8
* </pre>
*
* @return UTF-8
*/
public Charset getCharset() {
if (null == this.charsetCache) {
final String contentType = getContentType();
final String charsetStr = aiyh.utils.tool.cn.hutool.http.HttpUtil.getCharset(contentType);
this.charsetCache = CharsetUtil.parse(charsetStr, DEFAULT_CHARSET);
}
return this.charsetCache;
}
/**
* User-Agent
*
* @return User-Agent
*/
public String getUserAgentStr() {
return getHeader(aiyh.utils.tool.cn.hutool.http.Header.USER_AGENT);
}
/**
* User-Agentnull
*
* @return User-Agentnull
*/
public UserAgent getUserAgent() {
return UserAgentUtil.parse(getUserAgentStr());
}
/**
* Cookie
*
* @return cookie
*/
public String getCookiesStr() {
return getHeader(Header.COOKIE);
}
/**
* Cookie
*
* @return Cookie
*/
public Collection<HttpCookie> getCookies() {
return getCookieMap().values();
}
/**
* CookieMapCookieHttpCookie
*
* @return CookieMap
*/
public Map<String, HttpCookie> getCookieMap() {
if (null == this.cookieCache) {
cookieCache = Collections.unmodifiableMap(CollUtil.toMap(
NetUtil.parseCookies(getCookiesStr()),
new CaseInsensitiveMap<>(),
HttpCookie::getName));
}
return cookieCache;
}
/**
* CookieHttpCookie
*
* @param cookieName Cookie
* @return HttpCookie
*/
public HttpCookie getCookie(String cookieName) {
return getCookieMap().get(cookieName);
}
/**
* Multipart
*
* @return Multipart
*/
public boolean isMultipart() {
if (!isPostMethod()) {
return false;
}
final String contentType = getContentType();
if (StrUtil.isBlank(contentType)) {
return false;
}
return contentType.toLowerCase().startsWith("multipart/");
}
/**
* formjsonxml<br>
* 使{@link #getCharset()}使UTF-8
*
* @return
*/
public String getBody() {
return getBody(getCharset());
}
/**
* formjsonxml
*
* @param charset
* @return
*/
public String getBody(Charset charset) {
return StrUtil.str(getBodyBytes(), charset);
}
/**
* bodybytes
*
* @return bodybytes
*/
public byte[] getBodyBytes() {
if (null == this.bodyCache) {
this.bodyCache = IoUtil.readBytes(getBodyStream(), true);
}
return this.bodyCache;
}
/**
*
*
* @return
*/
public InputStream getBodyStream() {
return this.httpExchange.getRequestBody();
}
/**
*
*
* @param name
* @return
* @since 5.5.8
*/
public String getParam(String name) {
return getParams().get(name, 0);
}
/**
*
*
* @param name
* @return
* @since 5.5.8
*/
public List<String> getParams(String name) {
return getParams().get(name);
}
/**
* Map
*
* @return map
*/
public ListValueMap<String, String> getParams() {
if (null == this.paramsCache) {
this.paramsCache = new ListValueMap<>();
final Charset charset = getCharset();
// 解析URL中的参数
final String query = getQuery();
if (StrUtil.isNotBlank(query)) {
this.paramsCache.putAll(aiyh.utils.tool.cn.hutool.http.HttpUtil.decodeParams(query, charset, false));
}
// 解析multipart中的参数
if (isMultipart()) {
this.paramsCache.putAll(getMultipart().getParamListMap());
} else {
// 解析body中的参数
final String body = getBody();
if (StrUtil.isNotBlank(body)) {
this.paramsCache.putAll(HttpUtil.decodeParams(body, charset, true));
}
}
}
return this.paramsCache;
}
/**
* IP
*
* <p>
* Header:
*
* <pre>
* 1X-Forwarded-For
* 2X-Real-IP
* 3Proxy-Client-IP
* 4WL-Proxy-Client-IP
* </pre>
*
* <p>
* otherHeaderNamesHeader<br>
* 使IPHttpNginxIP
* </p>
*
* @param otherHeaderNames HttpNginx
* @return IP
*/
public String getClientIP(String... otherHeaderNames) {
String[] headers = {"X-Forwarded-For", "X-Real-IP", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR"};
if (ArrayUtil.isNotEmpty(otherHeaderNames)) {
headers = ArrayUtil.addAll(headers, otherHeaderNames);
}
return getClientIPByHeader(headers);
}
/**
* IP
*
* <p>
* headerNamesHeader<br>
* 使IPHttpNginxIP
* </p>
*
* @param headerNames HttpNginx
* @return IP
* @since 4.4.1
*/
public String getClientIPByHeader(String... headerNames) {
String ip;
for (String header : headerNames) {
ip = getHeader(header);
if (!NetUtil.isUnknown(ip)) {
return NetUtil.getMultistageReverseProxyIp(ip);
}
}
ip = this.httpExchange.getRemoteAddress().getHostName();
return NetUtil.getMultistageReverseProxyIp(ip);
}
/**
* MultiPart
*
* @return MultipartFormData
* @throws IORuntimeException IO
* @since 5.3.0
*/
public MultipartFormData getMultipart() throws IORuntimeException {
if (null == this.multipartFormDataCache) {
this.multipartFormDataCache = parseMultipart(new UploadSetting());
}
return this.multipartFormDataCache;
}
/**
* multipart/form-data <br>
* <br>
*
*
* @param uploadSetting
* @return MultiPart
* @throws IORuntimeException IO
* @since 5.3.0
*/
public MultipartFormData parseMultipart(UploadSetting uploadSetting) throws IORuntimeException {
final MultipartFormData formData = new MultipartFormData(uploadSetting);
try {
formData.parseRequestStream(getBodyStream(), getCharset());
} catch (IOException e) {
throw new IORuntimeException(e);
}
return formData;
}
}

View File

@ -0,0 +1,429 @@
package aiyh.utils.tool.cn.hutool.http.server;
import aiyh.utils.tool.cn.hutool.core.io.FileUtil;
import aiyh.utils.tool.cn.hutool.core.io.IORuntimeException;
import aiyh.utils.tool.cn.hutool.core.io.IoUtil;
import aiyh.utils.tool.cn.hutool.core.util.ObjectUtil;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
import aiyh.utils.tool.cn.hutool.core.util.URLUtil;
import aiyh.utils.tool.cn.hutool.http.ContentType;
import aiyh.utils.tool.cn.hutool.http.Header;
import aiyh.utils.tool.cn.hutool.http.HttpStatus;
import aiyh.utils.tool.cn.hutool.http.HttpUtil;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import java.io.*;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
/**
* Http
*/
public class HttpServerResponse extends HttpServerBase {
private Charset charset;
/**
* Http
*/
private boolean isSendCode;
/**
*
*
* @param httpExchange {@link HttpExchange}
*/
public HttpServerResponse(HttpExchange httpExchange) {
super(httpExchange);
}
/**
* HTTPContent-Length0Transfer-encoding: chunked
*
* @param httpStatusCode HTTPHttpStatus
* @return this
*/
public HttpServerResponse send(int httpStatusCode) {
return send(httpStatusCode, 0);
}
/**
*
*
* @return this
*/
public HttpServerResponse sendOk() {
return send(aiyh.utils.tool.cn.hutool.http.HttpStatus.HTTP_OK);
}
/**
*
*
* @param bodyLength 0Transfer-encoding: chunked
* @return this
* @since 5.5.7
*/
public HttpServerResponse sendOk(int bodyLength) {
return send(aiyh.utils.tool.cn.hutool.http.HttpStatus.HTTP_OK, bodyLength);
}
/**
* 404
*
* @param content text/html
* @return this
*/
public HttpServerResponse send404(String content) {
return sendError(HttpStatus.HTTP_NOT_FOUND, content);
}
/**
*
*
* @param errorCode HTTPHttpStatus
* @param content text/html
* @return this
*/
public HttpServerResponse sendError(int errorCode, String content) {
send(errorCode);
setContentType(aiyh.utils.tool.cn.hutool.http.ContentType.TEXT_HTML.toString());
return write(content);
}
/**
* HTTP
*
* @param httpStatusCode HTTPHttpStatus
* @param bodyLength 0Transfer-encoding: chunked
* @return this
*/
public HttpServerResponse send(int httpStatusCode, long bodyLength) {
if (this.isSendCode) {
throw new IORuntimeException("Http status code has been send!");
}
try {
this.httpExchange.sendResponseHeaders(httpStatusCode, bodyLength);
} catch (IOException e) {
throw new IORuntimeException(e);
}
this.isSendCode = true;
return this;
}
/**
*
*
* @return
*/
public Headers getHeaders() {
return this.httpExchange.getResponseHeaders();
}
/**
*
*
* @param header key
* @param value
* @return this
*/
public HttpServerResponse addHeader(String header, String value) {
getHeaders().add(header, value);
return this;
}
/**
*
*
* @param header key
* @param value
* @return this
*/
public HttpServerResponse setHeader(aiyh.utils.tool.cn.hutool.http.Header header, String value) {
return setHeader(header.getValue(), value);
}
/**
*
*
* @param header key
* @param value
* @return this
*/
public HttpServerResponse setHeader(String header, String value) {
getHeaders().set(header, value);
return this;
}
/**
*
*
* @param header key
* @param value
* @return this
*/
public HttpServerResponse setHeader(String header, List<String> value) {
getHeaders().put(header, value);
return this;
}
/**
*
*
* @param headers map
* @return this
*/
public HttpServerResponse setHeaders(Map<String, List<String>> headers) {
getHeaders().putAll(headers);
return this;
}
/**
* Content-Type:text/html;charset=utf-8<br>
* charsetcharsetcharset{@link #setCharset(Charset)}
*
* @param contentType Content-Type
* @return this
*/
public HttpServerResponse setContentType(String contentType) {
if (null != contentType && null != this.charset) {
if (!contentType.contains(";charset=")) {
contentType = ContentType.build(contentType, this.charset);
}
}
return setHeader(aiyh.utils.tool.cn.hutool.http.Header.CONTENT_TYPE, contentType);
}
/**
* Content-Length
*
* @param contentLength Content-Length
* @return this
*/
public HttpServerResponse setContentLength(long contentLength) {
return setHeader(aiyh.utils.tool.cn.hutool.http.Header.CONTENT_LENGTH, String.valueOf(contentLength));
}
/**
*
*
* @param charset
* @return this
*/
public HttpServerResponse setCharset(Charset charset) {
this.charset = charset;
return this;
}
/**
*
*
* @param name
* @param value
* @return this
*/
public HttpServerResponse setAttr(String name, Object value) {
this.httpExchange.setAttribute(name, value);
return this;
}
/**
*
*
* @return
*/
public OutputStream getOut() {
if (!this.isSendCode) {
sendOk();
}
return this.httpExchange.getResponseBody();
}
/**
*
*
* @return
*/
public PrintWriter getWriter() {
final Charset charset = ObjectUtil.defaultIfNull(this.charset, DEFAULT_CHARSET);
return new PrintWriter(new OutputStreamWriter(getOut(), charset));
}
/**
*
*
* @param data
* @param contentType Content-Type
* @return this
*/
public HttpServerResponse write(String data, String contentType) {
setContentType(contentType);
return write(data);
}
/**
*
*
* @param data
* @return this
*/
public HttpServerResponse write(String data) {
final Charset charset = ObjectUtil.defaultIfNull(this.charset, DEFAULT_CHARSET);
return write(StrUtil.bytes(data, charset));
}
/**
*
*
* @param data
* @param contentType
* @return this
*/
public HttpServerResponse write(byte[] data, String contentType) {
setContentType(contentType);
return write(data);
}
/**
*
*
* @param data
* @return this
*/
public HttpServerResponse write(byte[] data) {
final ByteArrayInputStream in = new ByteArrayInputStream(data);
return write(in, in.available());
}
/**
*
*
* @param in
* @param contentType
* @return this
* @since 5.2.6
*/
public HttpServerResponse write(InputStream in, String contentType) {
return write(in, 0, contentType);
}
/**
*
*
* @param in
* @param length 0Transfer-encoding: chunked
* @param contentType
* @return this
* @since 5.2.7
*/
public HttpServerResponse write(InputStream in, int length, String contentType) {
setContentType(contentType);
return write(in, length);
}
/**
*
*
* @param in
* @return this
*/
public HttpServerResponse write(InputStream in) {
return write(in, 0);
}
/**
*
*
* @param in
* @param length 0Transfer-encoding: chunked
* @return this
*/
public HttpServerResponse write(InputStream in, int length) {
if (!isSendCode) {
sendOk(Math.max(0, length));
}
OutputStream out = null;
try {
out = this.httpExchange.getResponseBody();
IoUtil.copy(in, out);
} finally {
IoUtil.close(out);
IoUtil.close(in);
}
return this;
}
/**
*
*
* @param file
* @return this
* @since 5.2.6
*/
public HttpServerResponse write(File file) {
return write(file, null);
}
/**
*
*
* @param file
* @param fileName
* @return this
* @since 5.5.8
*/
public HttpServerResponse write(File file, String fileName) {
final long fileSize = file.length();
if (fileSize > Integer.MAX_VALUE) {
throw new IllegalArgumentException("File size is too bigger than " + Integer.MAX_VALUE);
}
if (StrUtil.isBlank(fileName)) {
fileName = file.getName();
}
final String contentType = ObjectUtil.defaultIfNull(HttpUtil.getMimeType(fileName), "application/octet-stream");
BufferedInputStream in = null;
try {
in = FileUtil.getInputStream(file);
write(in, (int) fileSize, contentType, fileName);
} finally {
IoUtil.close(in);
}
return this;
}
/**
*
*
* @param in
* @param contentType
* @param fileName
* @since 5.2.6
*/
public void write(InputStream in, String contentType, String fileName) {
write(in, 0, contentType, fileName);
}
/**
*
*
* @param in
* @param length
* @param contentType
* @param fileName
* @return this
* @since 5.2.7
*/
public HttpServerResponse write(InputStream in, int length, String contentType, String fileName) {
final Charset charset = ObjectUtil.defaultIfNull(this.charset, DEFAULT_CHARSET);
if (!contentType.startsWith("text/")) {
// 非文本类型数据直接走下载
setHeader(Header.CONTENT_DISPOSITION, StrUtil.format("attachment;filename={}", URLUtil.encode(fileName, charset)));
}
return write(in, length, contentType);
}
}

View File

@ -0,0 +1,226 @@
package aiyh.utils.tool.cn.hutool.http.server;
import aiyh.utils.tool.cn.hutool.core.io.IORuntimeException;
import aiyh.utils.tool.cn.hutool.core.lang.Console;
import aiyh.utils.tool.cn.hutool.core.thread.GlobalThreadPool;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
import aiyh.utils.tool.cn.hutool.http.server.action.Action;
import aiyh.utils.tool.cn.hutool.http.server.action.RootAction;
import aiyh.utils.tool.cn.hutool.http.server.filter.HttpFilter;
import aiyh.utils.tool.cn.hutool.http.server.filter.SimpleFilter;
import aiyh.utils.tool.cn.hutool.http.server.handler.ActionHandler;
import com.sun.net.httpserver.*;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
/**
* Http{@link HttpServer}
*
* @author looly
* @since 5.2.5
*/
public class SimpleServer {
private final HttpServer server;
private final List<Filter> filters;
/**
*
*
* @param port
*/
public SimpleServer(int port) {
this(new InetSocketAddress(port));
}
/**
*
*
* @param hostname
* @param port
*/
public SimpleServer(String hostname, int port) {
this(new InetSocketAddress(hostname, port));
}
/**
*
*
* @param address
*/
public SimpleServer(InetSocketAddress address) {
this(address, null);
}
/**
*
*
* @param address
* @param configurator https使SSLTLS
*/
public SimpleServer(InetSocketAddress address, HttpsConfigurator configurator) {
try {
if (null != configurator) {
final HttpsServer server = HttpsServer.create(address, 0);
server.setHttpsConfigurator(configurator);
this.server = server;
} else {
this.server = HttpServer.create(address, 0);
}
} catch (IOException e) {
throw new IORuntimeException(e);
}
setExecutor(GlobalThreadPool.getExecutor());
filters = new ArrayList<>();
}
/**
* <br>
*
*
* <ul>
* <li>{@link #setRoot(File)} </li>
* <li>{@link #setRoot(String)} </li>
* <li>{@link #createContext(String, HttpHandler)} </li>
* <li>{@link #addHandler(String, HttpHandler)}</li>
* <li>{@link #addAction(String, Action)} </li>
* </ul>
*
* @param filter {@link Filter}
* @return this
* @since 5.5.7
*/
public SimpleServer addFilter(Filter filter) {
this.filters.add(filter);
return this;
}
/**
* <br>
*
*
* <ul>
* <li>{@link #setRoot(File)} </li>
* <li>{@link #setRoot(String)} </li>
* <li>{@link #createContext(String, HttpHandler)} </li>
* <li>{@link #addHandler(String, HttpHandler)}</li>
* <li>{@link #addAction(String, Action)} </li>
* </ul>
*
* @param filter {@link Filter}
* @return this
* @since 5.5.7
*/
public SimpleServer addFilter(HttpFilter filter) {
return addFilter(new SimpleFilter() {
@Override
public void doFilter(HttpExchange httpExchange, Chain chain) throws IOException {
filter.doFilter(new HttpServerRequest(httpExchange), new HttpServerResponse(httpExchange), chain);
}
});
}
/**
*
*
* @param path :/a/b a/b
* @param handler
* @return this
* @see #createContext(String, HttpHandler)
*/
public SimpleServer addHandler(String path, HttpHandler handler) {
createContext(path, handler);
return this;
}
/**
* 访使{@link HttpHandler}
*
* @param path :/a/b a/b
* @param handler
* @return {@link HttpContext}
* @since 5.5.7
*/
public HttpContext createContext(String path, HttpHandler handler) {
// 非/开头的路径会报错
path = StrUtil.addPrefixIfNot(path, StrUtil.SLASH);
final HttpContext context = this.server.createContext(path, handler);
// 增加整体过滤器
context.getFilters().addAll(this.filters);
return context;
}
/**
* root
*
* @param root
* @return this
*/
public SimpleServer setRoot(String root) {
return setRoot(new File(root));
}
/**
* root
*
* @param root
* @return this
*/
public SimpleServer setRoot(File root) {
return addAction("/", new RootAction(root));
}
/**
*
*
* @param path
* @param action
* @return this
*/
public SimpleServer addAction(String path, Action action) {
return addHandler(path, new ActionHandler(action));
}
/**
* 线
*
* @param executor {@link Executor}
* @return this
*/
public SimpleServer setExecutor(Executor executor) {
this.server.setExecutor(executor);
return this;
}
/**
* HttpServer
*
* @return {@link HttpServer}
*/
public HttpServer getRawServer() {
return this.server;
}
/**
*
*
* @return {@link InetSocketAddress}
*/
public InetSocketAddress getAddress() {
return this.server.getAddress();
}
/**
* Http线
*/
public void start() {
final InetSocketAddress address = getAddress();
Console.log("Hutool Simple Http Server listen on 【{}:{}】", address.getHostName(), address.getPort());
this.server.start();
}
}

View File

@ -0,0 +1,26 @@
package aiyh.utils.tool.cn.hutool.http.server.action;
import aiyh.utils.tool.cn.hutool.http.server.HttpServerRequest;
import aiyh.utils.tool.cn.hutool.http.server.HttpServerResponse;
import java.io.IOException;
/**
* <br>
* PathActiondoAction
*
* @author Looly
* @since 5.2.6
*/
@FunctionalInterface
public interface Action {
/**
*
*
* @param request
* @param response
* @throws IOException IO
*/
void doAction(HttpServerRequest request, HttpServerResponse response) throws IOException;
}

View File

@ -0,0 +1,86 @@
package aiyh.utils.tool.cn.hutool.http.server.action;
import aiyh.utils.tool.cn.hutool.core.collection.CollUtil;
import aiyh.utils.tool.cn.hutool.core.io.FileUtil;
import aiyh.utils.tool.cn.hutool.http.server.HttpServerRequest;
import aiyh.utils.tool.cn.hutool.http.server.HttpServerResponse;
import java.io.File;
import java.util.List;
/**
* path
*
* @author looly
* @since 5.2.6
*/
public class RootAction implements Action {
public static final String DEFAULT_INDEX_FILE_NAME = "index.html";
private final File rootDir;
private final List<String> indexFileNames;
/**
*
*
* @param rootDir
*/
public RootAction(String rootDir) {
this(new File(rootDir));
}
/**
*
*
* @param rootDir
*/
public RootAction(File rootDir) {
this(rootDir, DEFAULT_INDEX_FILE_NAME);
}
/**
*
*
* @param rootDir
* @param indexFileNames
*/
public RootAction(String rootDir, String... indexFileNames) {
this(new File(rootDir), indexFileNames);
}
/**
*
*
* @param rootDir
* @param indexFileNames
* @since 5.4.0
*/
public RootAction(File rootDir, String... indexFileNames) {
this.rootDir = rootDir;
this.indexFileNames = CollUtil.toList(indexFileNames);
}
@Override
public void doAction(HttpServerRequest request, HttpServerResponse response) {
final String path = request.getPath();
File file = FileUtil.file(rootDir, path);
if (file.exists()) {
if (file.isDirectory()) {
for (String indexFileName : indexFileNames) {
// 默认读取主页
file = FileUtil.file(file, indexFileName);
if (file.exists() && file.isFile()) {
response.write(file);
}
}
} else {
final String name = request.getParam("name");
response.write(file, name);
}
}
response.send404("404 Not Found !");
}
}

View File

@ -0,0 +1,6 @@
/**
* {@link com.sun.net.httpserver.HttpServer}
*
* @author looly
*/
package aiyh.utils.tool.cn.hutool.http.server.action;

View File

@ -0,0 +1,27 @@
package aiyh.utils.tool.cn.hutool.http.server.filter;
import aiyh.utils.tool.cn.hutool.http.server.HttpServerRequest;
import aiyh.utils.tool.cn.hutool.http.server.HttpServerResponse;
import com.sun.net.httpserver.Filter;
import java.io.IOException;
/**
* {@link Filter} 使
*
* @author looly
* @since 5.5.7
*/
@FunctionalInterface
public interface HttpFilter {
/**
*
*
* @param req {@link aiyh.utils.tool.cn.hutool.http.server.HttpServerRequest}
* @param res {@link aiyh.utils.tool.cn.hutool.http.server.HttpServerResponse}
* @param chain {@link Filter.Chain}
* @throws IOException IO
*/
void doFilter(HttpServerRequest req, HttpServerResponse res, Filter.Chain chain) throws IOException;
}

View File

@ -0,0 +1,17 @@
package aiyh.utils.tool.cn.hutool.http.server.filter;
import com.sun.net.httpserver.Filter;
/**
*
*
* @author looly
* @since 5.5.7
*/
public abstract class SimpleFilter extends Filter {
@Override
public String description() {
return "Anonymous Filter";
}
}

View File

@ -0,0 +1,4 @@
/**
* {@link com.sun.net.httpserver.Filter}
*/
package aiyh.utils.tool.cn.hutool.http.server.filter;

View File

@ -0,0 +1,38 @@
package aiyh.utils.tool.cn.hutool.http.server.handler;
import aiyh.utils.tool.cn.hutool.http.server.HttpServerRequest;
import aiyh.utils.tool.cn.hutool.http.server.HttpServerResponse;
import aiyh.utils.tool.cn.hutool.http.server.action.Action;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
/**
* ActionHttpHandlerAction
*
* @author looly
* @since 5.2.6
*/
public class ActionHandler implements HttpHandler {
private final Action action;
/**
*
*
* @param action Action
*/
public ActionHandler(Action action) {
this.action = action;
}
@Override
public void handle(HttpExchange httpExchange) throws IOException {
action.doAction(
new HttpServerRequest(httpExchange),
new HttpServerResponse(httpExchange)
);
httpExchange.close();
}
}

View File

@ -0,0 +1,4 @@
/**
* {@link com.sun.net.httpserver.HttpHandler}
*/
package aiyh.utils.tool.cn.hutool.http.server.handler;

View File

@ -0,0 +1,6 @@
/**
* Http
*
* @author looly
*/
package aiyh.utils.tool.cn.hutool.http.server;

View File

@ -0,0 +1,25 @@
package aiyh.utils.tool.cn.hutool.http.ssl;
import aiyh.utils.tool.cn.hutool.core.io.IORuntimeException;
import aiyh.utils.tool.cn.hutool.core.net.SSLProtocols;
/**
* androidSSL<br>
* HttpUrlConnection[GithubPage]
*
* <p>
* SSL
*
* @author MikaGuraNTK
*/
public class AndroidSupportSSLFactory extends CustomProtocolsSSLFactory {
// Android低版本不重置的话某些SSL访问就会失败
private static final String[] protocols = {
SSLProtocols.SSLv3, SSLProtocols.TLSv1, SSLProtocols.TLSv11, SSLProtocols.TLSv12};
public AndroidSupportSSLFactory() throws IORuntimeException {
super(protocols);
}
}

View File

@ -0,0 +1,97 @@
package aiyh.utils.tool.cn.hutool.http.ssl;
import aiyh.utils.tool.cn.hutool.core.io.IORuntimeException;
import aiyh.utils.tool.cn.hutool.core.net.SSLUtil;
import aiyh.utils.tool.cn.hutool.core.util.ArrayUtil;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
/**
* SSLSocketFactory
*
* @author looly
*/
public class CustomProtocolsSSLFactory extends SSLSocketFactory {
private final String[] protocols;
private final SSLSocketFactory base;
/**
*
*
* @param protocols
* @throws IORuntimeException IO
*/
public CustomProtocolsSSLFactory(String... protocols) throws IORuntimeException {
this.protocols = protocols;
this.base = SSLUtil.createSSLContext(null).getSocketFactory();
}
@Override
public String[] getDefaultCipherSuites() {
return base.getDefaultCipherSuites();
}
@Override
public String[] getSupportedCipherSuites() {
return base.getSupportedCipherSuites();
}
@Override
public Socket createSocket() throws IOException {
final SSLSocket sslSocket = (SSLSocket) base.createSocket();
resetProtocols(sslSocket);
return sslSocket;
}
@Override
public SSLSocket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
final SSLSocket socket = (SSLSocket) base.createSocket(s, host, port, autoClose);
resetProtocols(socket);
return socket;
}
@Override
public Socket createSocket(String host, int port) throws IOException {
final SSLSocket socket = (SSLSocket) base.createSocket(host, port);
resetProtocols(socket);
return socket;
}
@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
final SSLSocket socket = (SSLSocket) base.createSocket(host, port, localHost, localPort);
resetProtocols(socket);
return socket;
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
final SSLSocket socket = (SSLSocket) base.createSocket(host, port);
resetProtocols(socket);
return socket;
}
@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
final SSLSocket socket = (SSLSocket) base.createSocket(address, port, localAddress, localPort);
resetProtocols(socket);
return socket;
}
/**
*
*
* @param socket SSLSocket
*/
private void resetProtocols(SSLSocket socket) {
if (ArrayUtil.isNotEmpty(this.protocols)) {
socket.setEnabledProtocols(this.protocols);
}
}
}

View File

@ -0,0 +1,14 @@
package aiyh.utils.tool.cn.hutool.http.ssl;
/**
* SSLSocketFactory
*
* @author Looly
* @since 5.1.2
*/
public class DefaultSSLFactory extends CustomProtocolsSSLFactory {
public DefaultSSLFactory() {
}
}

View File

@ -0,0 +1,32 @@
package aiyh.utils.tool.cn.hutool.http.ssl;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
import javax.net.ssl.SSLSocketFactory;
/**
* SSL使
*
* @author looly
* @since 5.1.2
*/
public class DefaultSSLInfo {
/**
*
*/
public static final aiyh.utils.tool.cn.hutool.http.ssl.TrustAnyHostnameVerifier TRUST_ANY_HOSTNAME_VERIFIER;
/**
* SSLSocketFactory
*/
public static final SSLSocketFactory DEFAULT_SSF;
static {
TRUST_ANY_HOSTNAME_VERIFIER = new TrustAnyHostnameVerifier();
if (StrUtil.equalsIgnoreCase("dalvik", System.getProperty("java.vm.name"))) {
// 兼容android低版本SSL连接
DEFAULT_SSF = new AndroidSupportSSLFactory();
} else {
DEFAULT_SSF = new DefaultSSLFactory();
}
}
}

View File

@ -0,0 +1,95 @@
package aiyh.utils.tool.cn.hutool.http.ssl;
import aiyh.utils.tool.cn.hutool.core.net.SSLContextBuilder;
import aiyh.utils.tool.cn.hutool.core.net.SSLProtocols;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
/**
* SSLSocketFactory
*
* @author Looly
* @see SSLContextBuilder
* @deprecated 使 {@link SSLContextBuilder}
*/
@Deprecated
public class SSLSocketFactoryBuilder implements SSLProtocols {
SSLContextBuilder sslContextBuilder;
/**
*
*/
public SSLSocketFactoryBuilder() {
this.sslContextBuilder = SSLContextBuilder.create();
}
/**
* SSLSocketFactoryBuilder
*
* @return SSLSocketFactoryBuilder
*/
public static SSLSocketFactoryBuilder create() {
return new SSLSocketFactoryBuilder();
}
/**
*
*
* @param protocol
* @return
*/
public SSLSocketFactoryBuilder setProtocol(String protocol) {
this.sslContextBuilder.setProtocol(protocol);
return this;
}
/**
*
*
* @param trustManagers TrustManager
* @return
*/
public SSLSocketFactoryBuilder setTrustManagers(TrustManager... trustManagers) {
this.sslContextBuilder.setTrustManagers(trustManagers);
return this;
}
/**
* JSSE key managers
*
* @param keyManagers JSSE key managers
* @return
*/
public SSLSocketFactoryBuilder setKeyManagers(KeyManager... keyManagers) {
this.sslContextBuilder.setKeyManagers(keyManagers);
return this;
}
/**
* SecureRandom
*
* @param secureRandom SecureRandom
* @return
*/
public SSLSocketFactoryBuilder setSecureRandom(SecureRandom secureRandom) {
this.sslContextBuilder.setSecureRandom(secureRandom);
return this;
}
/**
* SSLSocketFactory
*
* @return SSLSocketFactory
* @throws NoSuchAlgorithmException
* @throws KeyManagementException Key
*/
public SSLSocketFactory build() throws NoSuchAlgorithmException, KeyManagementException {
return this.sslContextBuilder.buildChecked().getSocketFactory();
}
}

View File

@ -0,0 +1,17 @@
package aiyh.utils.tool.cn.hutool.http.ssl;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
/**
* https
*
* @author Looly
*/
public class TrustAnyHostnameVerifier implements HostnameVerifier {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;// 直接返回true
}
}

View File

@ -0,0 +1,6 @@
/**
* SSL
*
* @author looly
*/
package aiyh.utils.tool.cn.hutool.http.ssl;

View File

@ -0,0 +1,140 @@
package aiyh.utils.tool.cn.hutool.http.useragent;
import aiyh.utils.tool.cn.hutool.core.collection.CollUtil;
import aiyh.utils.tool.cn.hutool.core.util.ReUtil;
import java.util.List;
import java.util.regex.Pattern;
/**
*
*
* @author looly
* @since 4.2.1
*/
public class Browser extends UserAgentInfo {
private static final long serialVersionUID = 1L;
/**
*
*/
public static final Browser Unknown = new Browser(NameUnknown, null, null);
/**
*
*/
public static final String Other_Version = "[\\/ ]([\\d\\w\\.\\-]+)";
/**
*
*/
public static final List<Browser> browers = CollUtil.newArrayList(
// 部分特殊浏览器是基于安卓、Iphone等的需要优先判断
// 企业微信 企业微信使用微信浏览器内核,会包含 MicroMessenger 所以要放在前面
new Browser("wxwork", "wxwork", "wxwork\\/([\\d\\w\\.\\-]+)"),
// 微信
new Browser("MicroMessenger", "MicroMessenger", Other_Version),
// 微信小程序
new Browser("miniProgram", "miniProgram", Other_Version),
// QQ浏览器
new Browser("QQBrowser", "MQQBrowser", "MQQBrowser\\/([\\d\\w\\.\\-]+)"),
// 钉钉PC端浏览器
new Browser("DingTalk-win", "dingtalk-win", "DingTalk\\(([\\d\\w\\.\\-]+)\\)"),
// 钉钉内置浏览器
new Browser("DingTalk", "DingTalk", "AliApp\\(DingTalk\\/([\\d\\w\\.\\-]+)\\)"),
// 支付宝内置浏览器
new Browser("Alipay", "AlipayClient", "AliApp\\(AP\\/([\\d\\w\\.\\-]+)\\)"),
// 淘宝内置浏览器
new Browser("Taobao", "taobao", "AliApp\\(TB\\/([\\d\\w\\.\\-]+)\\)"),
// UC浏览器
new Browser("UCBrowser", "UC?Browser", "UC?Browser\\/([\\d\\w\\.\\-]+)"),
// XiaoMi 浏览器
new Browser("MiuiBrowser", "MiuiBrowser|mibrowser", "MiuiBrowser\\/([\\d\\w\\.\\-]+)"),
// 夸克浏览器
new Browser("Quark", "Quark", Other_Version),
// 联想浏览器
new Browser("Lenovo", "SLBrowser", "SLBrowser/([\\d\\w\\.\\-]+)"),
new Browser("MSEdge", "Edge|Edg", "(?:edge|Edg|EdgA)\\/([\\d\\w\\.\\-]+)"),
new Browser("Chrome", "chrome", Other_Version),
new Browser("Firefox", "firefox", Other_Version),
new Browser("IEMobile", "iemobile", Other_Version),
new Browser("Android Browser", "android", "version\\/([\\d\\w\\.\\-]+)"),
new Browser("Safari", "safari", "version\\/([\\d\\w\\.\\-]+)"),
new Browser("Opera", "opera", Other_Version),
new Browser("Konqueror", "konqueror", Other_Version),
new Browser("PS3", "playstation 3", "([\\d\\w\\.\\-]+)\\)\\s*$"),
new Browser("PSP", "playstation portable", "([\\d\\w\\.\\-]+)\\)?\\s*$"),
new Browser("Lotus", "lotus.notes", "Lotus-Notes\\/([\\w.]+)"),
new Browser("Thunderbird", "thunderbird", Other_Version),
new Browser("Netscape", "netscape", Other_Version),
new Browser("Seamonkey", "seamonkey", Other_Version),
new Browser("Outlook", "microsoft.outlook", Other_Version),
new Browser("Evolution", "evolution", Other_Version),
new Browser("MSIE", "msie", "msie ([\\d\\w\\.\\-]+)"),
new Browser("MSIE11", "rv:11", "rv:([\\d\\w\\.\\-]+)"),
new Browser("Gabble", "Gabble", Other_Version),
new Browser("Yammer Desktop", "AdobeAir", "([\\d\\w\\.\\-]+)\\/Yammer"),
new Browser("Yammer Mobile", "Yammer[\\s]+([\\d\\w\\.\\-]+)", "Yammer[\\s]+([\\d\\w\\.\\-]+)"),
new Browser("Apache HTTP Client", "Apache\\\\-HttpClient", "Apache\\-HttpClient\\/([\\d\\w\\.\\-]+)"),
new Browser("BlackBerry", "BlackBerry", "BlackBerry[\\d]+\\/([\\d\\w\\.\\-]+)")
);
/**
*
*
* @param name
* @param regex
* @param versionRegex
* @since 5.7.4
*/
synchronized public static void addCustomBrowser(String name, String regex, String versionRegex) {
browers.add(new Browser(name, regex, versionRegex));
}
private Pattern versionPattern;
/**
*
*
* @param name
* @param regex
* @param versionRegex
*/
public Browser(String name, String regex, String versionRegex) {
super(name, regex);
if (Other_Version.equals(versionRegex)) {
versionRegex = name + versionRegex;
}
if (null != versionRegex) {
this.versionPattern = Pattern.compile(versionRegex, Pattern.CASE_INSENSITIVE);
}
}
/**
*
*
* @param userAgentString User-Agent
* @return
*/
public String getVersion(String userAgentString) {
if (isUnknown()) {
return null;
}
return ReUtil.getGroup1(this.versionPattern, userAgentString);
}
/**
*
*
* @return
*/
public boolean isMobile() {
final String name = this.getName();
return "PSP".equals(name) ||
"Yammer Mobile".equals(name) ||
"Android Browser".equals(name) ||
"IEMobile".equals(name) ||
"MicroMessenger".equals(name) ||
"miniProgram".equals(name) ||
"DingTalk".equals(name);
}
}

View File

@ -0,0 +1,62 @@
package aiyh.utils.tool.cn.hutool.http.useragent;
import aiyh.utils.tool.cn.hutool.core.collection.CollUtil;
import aiyh.utils.tool.cn.hutool.core.util.ReUtil;
import java.util.List;
import java.util.regex.Pattern;
/**
*
*
* @author looly
* @since 4.2.1
*/
public class Engine extends UserAgentInfo {
private static final long serialVersionUID = 1L;
/** 未知 */
public static final Engine Unknown = new Engine(NameUnknown, null);
/**
*
*/
public static final List<Engine> engines = CollUtil.newArrayList(
new Engine("Trident", "trident"),
new Engine("Webkit", "webkit"),
new Engine("Chrome", "chrome"),
new Engine("Opera", "opera"),
new Engine("Presto", "presto"),
new Engine("Gecko", "gecko"),
new Engine("KHTML", "khtml"),
new Engine("Konqueror", "konqueror"),
new Engine("MIDP", "MIDP")
);
private final Pattern versionPattern;
/**
*
*
* @param name
* @param regex
*/
public Engine(String name, String regex) {
super(name, regex);
this.versionPattern = Pattern.compile(name + "[/\\- ]([\\d\\w.\\-]+)", Pattern.CASE_INSENSITIVE);
}
/**
*
*
* @param userAgentString User-Agent
* @return
* @since 5.7.4
*/
public String getVersion(String userAgentString) {
if (isUnknown()) {
return null;
}
return ReUtil.getGroup1(this.versionPattern, userAgentString);
}
}

View File

@ -0,0 +1,107 @@
package aiyh.utils.tool.cn.hutool.http.useragent;
import aiyh.utils.tool.cn.hutool.core.collection.CollUtil;
import aiyh.utils.tool.cn.hutool.core.util.ReUtil;
import java.util.List;
import java.util.regex.Pattern;
/**
*
*
* @author looly
* @since 4.2.1
*/
public class OS extends UserAgentInfo {
private static final long serialVersionUID = 1L;
/**
*
*/
public static final OS Unknown = new OS(NameUnknown, null);
/**
*
*/
public static final List<OS> oses = CollUtil.newArrayList(//
new OS("Windows 10 or Windows Server 2016", "windows nt 10\\.0", "windows nt (10\\.0)"),//
new OS("Windows 8.1 or Windows Server 2012R2", "windows nt 6\\.3", "windows nt (6\\.3)"),//
new OS("Windows 8 or Windows Server 2012", "windows nt 6\\.2", "windows nt (6\\.2)"),//
new OS("Windows Vista", "windows nt 6\\.0", "windows nt (6\\.0)"), //
new OS("Windows 7 or Windows Server 2008R2", "windows nt 6\\.1", "windows nt (6\\.1)"), //
new OS("Windows 2003", "windows nt 5\\.2", "windows nt (5\\.2)"), //
new OS("Windows XP", "windows nt 5\\.1", "windows nt (5\\.1)"), //
new OS("Windows 2000", "windows nt 5\\.0", "windows nt (5\\.0)"), //
new OS("Windows Phone", "windows (ce|phone|mobile)( os)?", "windows (?:ce|phone|mobile) (\\d+([._]\\d+)*)"), //
new OS("Windows", "windows"), //
new OS("OSX", "os x (\\d+)[._](\\d+)", "os x (\\d+([._]\\d+)*)"), //
new OS("Android", "Android", "Android (\\d+([._]\\d+)*)"),//
new OS("Android", "XiaoMi|MI\\s+", "\\(X(\\d+([._]\\d+)*)"),//
new OS("Linux", "linux"), //
new OS("Wii", "wii", "wii libnup/(\\d+([._]\\d+)*)"), //
new OS("PS3", "playstation 3", "playstation 3; (\\d+([._]\\d+)*)"), //
new OS("PSP", "playstation portable", "Portable\\); (\\d+([._]\\d+)*)"), //
new OS("iPad", "\\(iPad.*os (\\d+)[._](\\d+)", "\\(iPad.*os (\\d+([._]\\d+)*)"), //
new OS("iPhone", "\\(iPhone.*os (\\d+)[._](\\d+)", "\\(iPhone.*os (\\d+([._]\\d+)*)"), //
new OS("YPod", "iPod touch[\\s\\;]+iPhone.*os (\\d+)[._](\\d+)", "iPod touch[\\s\\;]+iPhone.*os (\\d+([._]\\d+)*)"), //
new OS("YPad", "iPad[\\s\\;]+iPhone.*os (\\d+)[._](\\d+)", "iPad[\\s\\;]+iPhone.*os (\\d+([._]\\d+)*)"), //
new OS("YPhone", "iPhone[\\s\\;]+iPhone.*os (\\d+)[._](\\d+)", "iPhone[\\s\\;]+iPhone.*os (\\d+([._]\\d+)*)"), //
new OS("Symbian", "symbian(os)?"), //
new OS("Darwin", "Darwin\\/([\\d\\w\\.\\-]+)", "Darwin\\/([\\d\\w\\.\\-]+)"), //
new OS("Adobe Air", "AdobeAir\\/([\\d\\w\\.\\-]+)", "AdobeAir\\/([\\d\\w\\.\\-]+)"), //
new OS("Java", "Java[\\s]+([\\d\\w\\.\\-]+)", "Java[\\s]+([\\d\\w\\.\\-]+)")//
);
/**
*
*
* @param name
* @param regex
* @param versionRegex
* @since 5.7.4
*/
synchronized public static void addCustomOs(String name, String regex, String versionRegex) {
oses.add(new OS(name, regex, versionRegex));
}
private Pattern versionPattern;
/**
*
*
* @param name
* @param regex
*/
public OS(String name, String regex) {
this(name, regex, null);
}
/**
*
*
* @param name
* @param regex
* @param versionRegex
* @since 5.7.4
*/
public OS(String name, String regex, String versionRegex) {
super(name, regex);
if (null != versionRegex) {
this.versionPattern = Pattern.compile(versionRegex, Pattern.CASE_INSENSITIVE);
}
}
/**
*
*
* @param userAgentString User-Agent
* @return
*/
public String getVersion(String userAgentString) {
if (isUnknown() || null == this.versionPattern) {
// 无版本信息
return null;
}
return ReUtil.getGroup1(this.versionPattern, userAgentString);
}
}

View File

@ -0,0 +1,147 @@
package aiyh.utils.tool.cn.hutool.http.useragent;
import aiyh.utils.tool.cn.hutool.core.collection.CollUtil;
import java.util.ArrayList;
import java.util.List;
/**
*
*
* @author looly
* @since 4.2.1
*/
public class Platform extends UserAgentInfo {
private static final long serialVersionUID = 1L;
/**
*
*/
public static final Platform Unknown = new Platform(NameUnknown, null);
/**
* Iphone
*/
public static final Platform IPHONE = new Platform("iPhone", "iphone");
/**
* ipod
*/
public static final Platform IPOD = new Platform("iPod", "ipod");
/**
* ipad
*/
public static final Platform IPAD = new Platform("iPad", "ipad");
/**
* android
*/
public static final Platform ANDROID = new Platform("Android", "android");
/**
* android
*/
public static final Platform GOOGLE_TV = new Platform("GoogleTV", "googletv");
/**
* Windows Phone
*/
public static final Platform WINDOWS_PHONE = new Platform("Windows Phone", "windows (ce|phone|mobile)( os)?");
/**
*
*/
public static final List<Platform> mobilePlatforms = CollUtil.newArrayList(//
WINDOWS_PHONE, //
IPAD, //
IPOD, //
IPHONE, //
new Platform("Android", "XiaoMi|MI\\s+"), //
ANDROID, //
GOOGLE_TV, //
new Platform("htcFlyer", "htc_flyer"), //
new Platform("Symbian", "symbian(os)?"), //
new Platform("Blackberry", "blackberry") //
);
/**
*
*/
public static final List<Platform> desktopPlatforms = CollUtil.newArrayList(//
new Platform("Windows", "windows"), //
new Platform("Mac", "(macintosh|darwin)"), //
new Platform("Linux", "linux"), //
new Platform("Wii", "wii"), //
new Platform("Playstation", "playstation"), //
new Platform("Java", "java") //
);
/**
*
*/
public static final List<Platform> platforms;
static {
platforms = new ArrayList<>(13);
platforms.addAll(mobilePlatforms);
platforms.addAll(desktopPlatforms);
}
/**
*
*
* @param name
* @param regex
*/
public Platform(String name, String regex) {
super(name, regex);
}
/**
*
*
* @return
*/
public boolean isMobile() {
return mobilePlatforms.contains(this);
}
/**
* IphoneiPod
*
* @return IphoneiPod
* @since 5.2.3
*/
public boolean isIPhoneOrIPod() {
return this.equals(IPHONE) || this.equals(IPOD);
}
/**
* IphoneiPod
*
* @return IphoneiPod
* @since 5.2.3
*/
public boolean isIPad() {
return this.equals(IPAD);
}
/**
* IOSIPhoneIPodIPad
*
* @return IOSIPhoneIPodIPad
* @since 5.2.3
*/
public boolean isIos() {
return isIPhoneOrIPod() || isIPad();
}
/**
* AndroidAndroidGoogle TV
*
* @return AndroidAndroidGoogle TV
* @since 5.2.3
*/
public boolean isAndroid() {
return this.equals(ANDROID) || this.equals(GOOGLE_TV);
}
}

View File

@ -0,0 +1,196 @@
package aiyh.utils.tool.cn.hutool.http.useragent;
import java.io.Serializable;
/**
* User-Agent
*
* @author looly
* @since 4.2.1
*/
public class UserAgent implements Serializable {
private static final long serialVersionUID = 1L;
/**
*
*/
private boolean mobile;
/**
*
*/
private aiyh.utils.tool.cn.hutool.http.useragent.Browser browser;
/**
*
*/
private String version;
/**
*
*/
private aiyh.utils.tool.cn.hutool.http.useragent.Platform platform;
/**
*
*/
private aiyh.utils.tool.cn.hutool.http.useragent.OS os;
/**
*
*/
private String osVersion;
/**
*
*/
private aiyh.utils.tool.cn.hutool.http.useragent.Engine engine;
/**
*
*/
private String engineVersion;
/**
*
*
* @return
*/
public boolean isMobile() {
return mobile;
}
/**
*
*
* @param mobile
*/
public void setMobile(boolean mobile) {
this.mobile = mobile;
}
/**
*
*
* @return
*/
public aiyh.utils.tool.cn.hutool.http.useragent.Browser getBrowser() {
return browser;
}
/**
*
*
* @param browser
*/
public void setBrowser(Browser browser) {
this.browser = browser;
}
/**
*
*
* @return
*/
public aiyh.utils.tool.cn.hutool.http.useragent.Platform getPlatform() {
return platform;
}
/**
*
*
* @param platform
*/
public void setPlatform(Platform platform) {
this.platform = platform;
}
/**
*
*
* @return
*/
public aiyh.utils.tool.cn.hutool.http.useragent.OS getOs() {
return os;
}
/**
*
*
* @param os
*/
public void setOs(OS os) {
this.os = os;
}
/**
*
*
* @return
* @since 5.7.4
*/
public String getOsVersion() {
return this.osVersion;
}
/**
*
*
* @param osVersion
* @since 5.7.4
*/
public void setOsVersion(String osVersion) {
this.osVersion = osVersion;
}
/**
*
*
* @return
*/
public aiyh.utils.tool.cn.hutool.http.useragent.Engine getEngine() {
return engine;
}
/**
*
*
* @param engine
*/
public void setEngine(Engine engine) {
this.engine = engine;
}
/**
*
*
* @return
*/
public String getVersion() {
return version;
}
/**
*
*
* @param version
*/
public void setVersion(String version) {
this.version = version;
}
/**
*
*
* @return
*/
public String getEngineVersion() {
return engineVersion;
}
/**
*
*
* @param engineVersion
*/
public void setEngineVersion(String engineVersion) {
this.engineVersion = engineVersion;
}
}

View File

@ -0,0 +1,114 @@
package aiyh.utils.tool.cn.hutool.http.useragent;
import aiyh.utils.tool.cn.hutool.core.util.ReUtil;
import java.io.Serializable;
import java.util.regex.Pattern;
/**
* User-agent
*
* @author looly
* @since 4.2.1
*/
public class UserAgentInfo implements Serializable {
private static final long serialVersionUID = 1L;
/**
*
*/
public static final String NameUnknown = "Unknown";
/** 信息名称 */
private final String name;
/** 信息匹配模式 */
private final Pattern pattern;
/**
*
*
* @param name
* @param regex
*/
public UserAgentInfo(String name, String regex) {
this(name, (null == regex) ? null : Pattern.compile(regex, Pattern.CASE_INSENSITIVE));
}
/**
*
*
* @param name
* @param pattern
*/
public UserAgentInfo(String name, Pattern pattern) {
this.name = name;
this.pattern = pattern;
}
/**
*
*
* @return
*/
public String getName() {
return name;
}
/**
*
*
* @return
*/
public Pattern getPattern() {
return pattern;
}
/**
*
*
* @param content User-Agent
* @return
*/
public boolean isMatch(String content) {
return ReUtil.contains(this.pattern, content);
}
/**
* Unknown
*
* @return Unknown
*/
public boolean isUnknown() {
return NameUnknown.equals(this.name);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final UserAgentInfo other = (UserAgentInfo) obj;
if (name == null) {
return other.name == null;
} else return name.equals(other.name);
}
@Override
public String toString() {
return this.name;
}
}

View File

@ -0,0 +1,108 @@
package aiyh.utils.tool.cn.hutool.http.useragent;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
/**
* User-Agent
*
* @author looly
* @since 4.2.1
*/
public class UserAgentParser {
/**
* User-Agent
*
* @param userAgentString User-Agent
* @return {@link UserAgent}
*/
public static UserAgent parse(String userAgentString) {
if (StrUtil.isBlank(userAgentString)) {
return null;
}
final UserAgent userAgent = new UserAgent();
// 浏览器
final aiyh.utils.tool.cn.hutool.http.useragent.Browser browser = parseBrowser(userAgentString);
userAgent.setBrowser(browser);
userAgent.setVersion(browser.getVersion(userAgentString));
// 浏览器引擎
final aiyh.utils.tool.cn.hutool.http.useragent.Engine engine = parseEngine(userAgentString);
userAgent.setEngine(engine);
userAgent.setEngineVersion(engine.getVersion(userAgentString));
// 操作系统
final aiyh.utils.tool.cn.hutool.http.useragent.OS os = parseOS(userAgentString);
userAgent.setOs(os);
userAgent.setOsVersion(os.getVersion(userAgentString));
// 平台
final aiyh.utils.tool.cn.hutool.http.useragent.Platform platform = parsePlatform(userAgentString);
userAgent.setPlatform(platform);
userAgent.setMobile(platform.isMobile() || browser.isMobile());
return userAgent;
}
/**
*
*
* @param userAgentString User-Agent
* @return
*/
private static aiyh.utils.tool.cn.hutool.http.useragent.Browser parseBrowser(String userAgentString) {
for (aiyh.utils.tool.cn.hutool.http.useragent.Browser browser : aiyh.utils.tool.cn.hutool.http.useragent.Browser.browers) {
if (browser.isMatch(userAgentString)) {
return browser;
}
}
return Browser.Unknown;
}
/**
*
*
* @param userAgentString User-Agent
* @return
*/
private static aiyh.utils.tool.cn.hutool.http.useragent.Engine parseEngine(String userAgentString) {
for (aiyh.utils.tool.cn.hutool.http.useragent.Engine engine : aiyh.utils.tool.cn.hutool.http.useragent.Engine.engines) {
if (engine.isMatch(userAgentString)) {
return engine;
}
}
return Engine.Unknown;
}
/**
*
*
* @param userAgentString User-Agent
* @return
*/
private static aiyh.utils.tool.cn.hutool.http.useragent.OS parseOS(String userAgentString) {
for (aiyh.utils.tool.cn.hutool.http.useragent.OS os : aiyh.utils.tool.cn.hutool.http.useragent.OS.oses) {
if (os.isMatch(userAgentString)) {
return os;
}
}
return OS.Unknown;
}
/**
*
*
* @param userAgentString User-Agent
* @return
*/
private static aiyh.utils.tool.cn.hutool.http.useragent.Platform parsePlatform(String userAgentString) {
for (aiyh.utils.tool.cn.hutool.http.useragent.Platform platform : aiyh.utils.tool.cn.hutool.http.useragent.Platform.platforms) {
if (platform.isMatch(userAgentString)) {
return platform;
}
}
return Platform.Unknown;
}
}

View File

@ -0,0 +1,20 @@
package aiyh.utils.tool.cn.hutool.http.useragent;
/**
* User-Agent
*
* @author looly
*/
public class UserAgentUtil {
/**
* User-Agent
*
* @param userAgentString User-Agent
* @return {@link UserAgent}
*/
public static UserAgent parse(String userAgentString) {
return UserAgentParser.parse(userAgentString);
}
}

View File

@ -0,0 +1,6 @@
/**
* User-Agent
*
* @author looly
*/
package aiyh.utils.tool.cn.hutool.http.useragent;

View File

@ -0,0 +1,654 @@
package aiyh.utils.tool.cn.hutool.http.webservice;
import aiyh.utils.tool.cn.hutool.core.collection.CollUtil;
import aiyh.utils.tool.cn.hutool.core.io.IoUtil;
import aiyh.utils.tool.cn.hutool.core.map.MapUtil;
import aiyh.utils.tool.cn.hutool.core.util.ObjectUtil;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
import aiyh.utils.tool.cn.hutool.core.util.XmlUtil;
import aiyh.utils.tool.cn.hutool.http.HttpBase;
import aiyh.utils.tool.cn.hutool.http.HttpGlobalConfig;
import aiyh.utils.tool.cn.hutool.http.HttpRequest;
import aiyh.utils.tool.cn.hutool.http.HttpResponse;
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import javax.xml.soap.*;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* SOAP
*
* <p>
* SOAPHTTP
* SOAPXML{@link #getMsgStr(boolean)}
* <p>
* 使
*
* <pre>
* SoapClient client = SoapClient.create(url)
* .setMethod(methodName, namespaceURI)
* .setCharset(CharsetUtil.CHARSET_GBK)
* .setParam(param1, "XXX");
*
* String response = client.send(true);
*
* </pre>
*
* @author looly
* @since 4.5.4
*/
public class SoapClient extends HttpBase<SoapClient> {
/**
* XMLContent-Type
* soap1.1 : text/xml
* soap1.2 : application/soap+xml
* soap1.1soap1.2: https://www.cnblogs.com/qlqwjy/p/7577147.html
*/
private static final String CONTENT_TYPE_SOAP11_TEXT_XML = "text/xml;charset=";
private static final String CONTENT_TYPE_SOAP12_SOAP_XML = "application/soap+xml;charset=";
/**
* URL
*/
private String url;
/**
*
*/
private int connectionTimeout = aiyh.utils.tool.cn.hutool.http.HttpGlobalConfig.getTimeout();
/**
*
*/
private int readTimeout = HttpGlobalConfig.getTimeout();
/**
*
*/
private MessageFactory factory;
/**
* SOAP
*/
private SOAPMessage message;
/**
*
*/
private SOAPBodyElement methodEle;
/**
* URI
*/
private final String namespaceURI;
/**
* Soap
* soap1.1 : text/xml
* soap1.2 : application/soap+xml
*/
private final SoapProtocol protocol;
/**
* SOAP使soap1.1
*
* @param url WSURL
* @return this
*/
public static SoapClient create(String url) {
return new SoapClient(url);
}
/**
* SOAP
*
* @param url WSURL
* @param protocol {@link SoapProtocol}
* @return this
*/
public static SoapClient create(String url, SoapProtocol protocol) {
return new SoapClient(url, protocol);
}
/**
* SOAP
*
* @param url WSURL
* @param protocol {@link SoapProtocol}
* @param namespaceURI URI
* @return this
* @since 4.5.6
*/
public static SoapClient create(String url, SoapProtocol protocol, String namespaceURI) {
return new SoapClient(url, protocol, namespaceURI);
}
/**
* 使soap1.1
*
* @param url WSURL
*/
public SoapClient(String url) {
this(url, SoapProtocol.SOAP_1_1);
}
/**
*
*
* @param url WSURL
* @param protocol {@link SoapProtocol}
*/
public SoapClient(String url, SoapProtocol protocol) {
this(url, protocol, null);
}
/**
*
*
* @param url WSURL
* @param protocol {@link SoapProtocol}
* @param namespaceURI URI
* @since 4.5.6
*/
public SoapClient(String url, SoapProtocol protocol, String namespaceURI) {
this.url = url;
this.namespaceURI = namespaceURI;
this.protocol = protocol;
init(protocol);
}
/**
*
*
* @param protocol {@link SoapProtocol}
* @return this
*/
public SoapClient init(SoapProtocol protocol) {
// 创建消息工厂
try {
this.factory = MessageFactory.newInstance(protocol.getValue());
// 根据消息工厂创建SoapMessage
this.message = factory.createMessage();
} catch (SOAPException e) {
throw new SoapRuntimeException(e);
}
return this;
}
/**
* SOAP
*
* <p>
* serMethodsetParam
*
* @return this
* @since 4.6.7
*/
public SoapClient reset() {
try {
this.message = factory.createMessage();
} catch (SOAPException e) {
throw new SoapRuntimeException(e);
}
this.methodEle = null;
return this;
}
/**
*
*
* @param charset
* @return this
* @see #charset(Charset)
*/
public SoapClient setCharset(Charset charset) {
return this.charset(charset);
}
@Override
public SoapClient charset(Charset charset) {
super.charset(charset);
try {
this.message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, this.charset());
this.message.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
} catch (SOAPException e) {
// ignore
}
return this;
}
/**
* Webservice
*
* @param url Webservice
* @return this
*/
public SoapClient setUrl(String url) {
this.url = url;
return this;
}
/**
* SOAP{@link SOAPHeaderElement}
*
* @param name
* @param actorURI
* @param roleUri RoleURI
* @param mustUnderstand
* @param relay relay
* @return {@link SOAPHeaderElement}
* @since 5.4.4
*/
public SOAPHeaderElement addSOAPHeader(QName name, String actorURI, String roleUri, Boolean mustUnderstand, Boolean relay) {
final SOAPHeaderElement ele = addSOAPHeader(name);
try {
if (StrUtil.isNotBlank(roleUri)) {
ele.setRole(roleUri);
}
if (null != relay) {
ele.setRelay(relay);
}
} catch (SOAPException e) {
throw new SoapRuntimeException(e);
}
if (StrUtil.isNotBlank(actorURI)) {
ele.setActor(actorURI);
}
if (null != mustUnderstand) {
ele.setMustUnderstand(mustUnderstand);
}
return ele;
}
/**
* SOAP{@link SOAPHeaderElement}
*
* @param localName
* @return {@link SOAPHeaderElement}
* @since 5.4.7
*/
public SOAPHeaderElement addSOAPHeader(String localName) {
return addSOAPHeader(new QName(localName));
}
/**
* SOAP{@link SOAPHeaderElement}
*
* @param localName
* @param value
* @return {@link SOAPHeaderElement}
* @since 5.4.7
*/
public SOAPHeaderElement addSOAPHeader(String localName, String value) {
final SOAPHeaderElement soapHeaderElement = addSOAPHeader(localName);
soapHeaderElement.setTextContent(value);
return soapHeaderElement;
}
/**
* SOAP{@link SOAPHeaderElement}
*
* @param name
* @return {@link SOAPHeaderElement}
* @since 5.4.4
*/
public SOAPHeaderElement addSOAPHeader(QName name) {
SOAPHeaderElement ele;
try {
ele = this.message.getSOAPHeader().addHeaderElement(name);
} catch (SOAPException e) {
throw new SoapRuntimeException(e);
}
return ele;
}
/**
*
*
* @param name
* @param params
* @param useMethodPrefix 使
* @return this
*/
public SoapClient setMethod(Name name, Map<String, Object> params, boolean useMethodPrefix) {
return setMethod(new QName(name.getURI(), name.getLocalName(), name.getPrefix()), params, useMethodPrefix);
}
/**
*
*
* @param name
* @param params
* @param useMethodPrefix 使
* @return this
*/
public SoapClient setMethod(QName name, Map<String, Object> params, boolean useMethodPrefix) {
setMethod(name);
final String prefix = useMethodPrefix ? name.getPrefix() : null;
final SOAPBodyElement methodEle = this.methodEle;
for (Entry<String, Object> entry : MapUtil.wrap(params)) {
setParam(methodEle, entry.getKey(), entry.getValue(), prefix);
}
return this;
}
/**
* <br>
* 使:<br>
* xmlnsnamespaceURI
*
* @param methodName
* @return this
*/
public SoapClient setMethod(String methodName) {
return setMethod(methodName, ObjectUtil.defaultIfNull(this.namespaceURI, XMLConstants.NULL_NS_URI));
}
/**
* <br>
* 使:<br>
* xmlnsnamespaceURI
*
* @param methodName
* @param namespaceURI URI
* @return this
*/
public SoapClient setMethod(String methodName, String namespaceURI) {
final List<String> methodNameList = StrUtil.split(methodName, ':');
final QName qName;
if (2 == methodNameList.size()) {
qName = new QName(namespaceURI, methodNameList.get(1), methodNameList.get(0));
} else {
qName = new QName(namespaceURI, methodName);
}
return setMethod(qName);
}
/**
*
*
* @param name
* @return this
*/
public SoapClient setMethod(QName name) {
try {
this.methodEle = this.message.getSOAPBody().addBodyElement(name);
} catch (SOAPException e) {
throw new SoapRuntimeException(e);
}
return this;
}
/**
* 使
*
* @param name
* @param value Map{@link SOAPElement}
* @return this
*/
public SoapClient setParam(String name, Object value) {
return setParam(name, value, true);
}
/**
*
*
* @param name
* @param value Map{@link SOAPElement}
* @param useMethodPrefix 使
* @return this
*/
public SoapClient setParam(String name, Object value, boolean useMethodPrefix) {
setParam(this.methodEle, name, value, useMethodPrefix ? this.methodEle.getPrefix() : null);
return this;
}
/**
* 使
*
* @param params
* @return this
* @since 4.5.6
*/
public SoapClient setParams(Map<String, Object> params) {
return setParams(params, true);
}
/**
*
*
* @param params
* @param useMethodPrefix 使
* @return this
* @since 4.5.6
*/
public SoapClient setParams(Map<String, Object> params, boolean useMethodPrefix) {
for (Entry<String, Object> entry : MapUtil.wrap(params)) {
setParam(entry.getKey(), entry.getValue(), useMethodPrefix);
}
return this;
}
/**
* <br>
*
*
* @return {@link SOAPBodyElement}
* @since 4.5.6
*/
public SOAPBodyElement getMethodEle() {
return this.methodEle;
}
/**
* SOAP {@link SOAPMessage}
*
* @return {@link SOAPMessage}
* @since 4.5.6
*/
public SOAPMessage getMessage() {
return this.message;
}
/**
* SOAP
*
* @param pretty
* @return
*/
public String getMsgStr(boolean pretty) {
return SoapUtil.toString(this.message, pretty, this.charset);
}
/**
* SOAPXML
*
* @param out
* @return this
* @since 4.5.6
*/
public SoapClient write(OutputStream out) {
try {
this.message.writeTo(out);
} catch (SOAPException | IOException e) {
throw new SoapRuntimeException(e);
}
return this;
}
/**
* <br>
*
*
* <pre>
* 1.
* 2.
* </pre>
*
* @param milliseconds
* @return this
* @see #setConnectionTimeout(int)
* @see #setReadTimeout(int)
*/
public SoapClient timeout(int milliseconds) {
setConnectionTimeout(milliseconds);
setReadTimeout(milliseconds);
return this;
}
/**
*
*
* @param milliseconds
* @return this
* @since 4.5.6
*/
public SoapClient setConnectionTimeout(int milliseconds) {
this.connectionTimeout = milliseconds;
return this;
}
/**
*
*
* @param milliseconds
* @return this
* @since 4.5.6
*/
public SoapClient setReadTimeout(int milliseconds) {
this.readTimeout = milliseconds;
return this;
}
/**
* WebserviceSOAP
*
* @return
*/
public SOAPMessage sendForMessage() {
final aiyh.utils.tool.cn.hutool.http.HttpResponse res = sendForResponse();
final MimeHeaders headers = new MimeHeaders();
for (Entry<String, List<String>> entry : res.headers().entrySet()) {
if (StrUtil.isNotEmpty(entry.getKey())) {
headers.setHeader(entry.getKey(), CollUtil.get(entry.getValue(), 0));
}
}
try {
return this.factory.createMessage(headers, res.bodyStream());
} catch (IOException | SOAPException e) {
throw new SoapRuntimeException(e);
} finally {
IoUtil.close(res);
}
}
/**
* WebserviceSOAP
*
* @return
*/
public String send() {
return send(false);
}
/**
* WebserviceSOAP
*
* @param pretty
* @return
*/
public String send(boolean pretty) {
final String body = sendForResponse().body();
return pretty ? XmlUtil.format(body) : body;
}
// -------------------------------------------------------------------------------------------------------- Private method start
/**
*
*
* @return
*/
public HttpResponse sendForResponse() {
return HttpRequest.post(this.url)//
.setFollowRedirects(true)//
.setConnectionTimeout(this.connectionTimeout)
.setReadTimeout(this.readTimeout)
.contentType(getXmlContentType())//
.header(this.headers())
.body(getMsgStr(false))//
.executeAsync();
}
/**
* Content-Type
*
* @return Content-Type
*/
private String getXmlContentType() {
switch (this.protocol) {
case SOAP_1_1:
return CONTENT_TYPE_SOAP11_TEXT_XML.concat(this.charset.toString());
case SOAP_1_2:
return CONTENT_TYPE_SOAP12_SOAP_XML.concat(this.charset.toString());
default:
throw new SoapRuntimeException("Unsupported protocol: {}", this.protocol);
}
}
/**
*
*
* @param ele
* @param name
* @param value
* @param prefix {@code null}使
* @return {@link SOAPElement}
*/
@SuppressWarnings("rawtypes")
private static SOAPElement setParam(SOAPElement ele, String name, Object value, String prefix) {
final SOAPElement childEle;
try {
if (StrUtil.isNotBlank(prefix)) {
childEle = ele.addChildElement(name, prefix);
} else {
childEle = ele.addChildElement(name);
}
} catch (SOAPException e) {
throw new SoapRuntimeException(e);
}
if (null != value) {
if (value instanceof SOAPElement) {
// 单个子节点
try {
ele.addChildElement((SOAPElement) value);
} catch (SOAPException e) {
throw new SoapRuntimeException(e);
}
} else if (value instanceof Map) {
// 多个字节点
Entry entry;
for (Object obj : ((Map) value).entrySet()) {
entry = (Entry) obj;
setParam(childEle, entry.getKey().toString(), entry.getValue(), prefix);
}
} else {
// 单个值
childEle.setValue(value.toString());
}
}
return childEle;
}
// -------------------------------------------------------------------------------------------------------- Private method end
}

View File

@ -0,0 +1,35 @@
package aiyh.utils.tool.cn.hutool.http.webservice;
import javax.xml.soap.SOAPConstants;
/**
* SOAP
*
* @author looly
*/
public enum SoapProtocol {
/** SOAP 1.1协议 */
SOAP_1_1(SOAPConstants.SOAP_1_1_PROTOCOL),
/** SOAP 1.2协议 */
SOAP_1_2(SOAPConstants.SOAP_1_2_PROTOCOL);
/**
*
*
* @param value {@link SOAPConstants}
*/
SoapProtocol(String value) {
this.value = value;
}
private final String value;
/**
*
*
* @return
*/
public String getValue() {
return this.value;
}
}

View File

@ -0,0 +1,32 @@
package aiyh.utils.tool.cn.hutool.http.webservice;
import aiyh.utils.tool.cn.hutool.core.util.StrUtil;
/**
* SOAP
*
* @author xiaoleilu
*/
public class SoapRuntimeException extends RuntimeException {
private static final long serialVersionUID = 8247610319171014183L;
public SoapRuntimeException(Throwable e) {
super(e.getMessage(), e);
}
public SoapRuntimeException(String message) {
super(message);
}
public SoapRuntimeException(String messageTemplate, Object... params) {
super(StrUtil.format(messageTemplate, params));
}
public SoapRuntimeException(String message, Throwable throwable) {
super(message, throwable);
}
public SoapRuntimeException(Throwable throwable, String messageTemplate, Object... params) {
super(StrUtil.format(messageTemplate, params), throwable);
}
}

View File

@ -0,0 +1,91 @@
package aiyh.utils.tool.cn.hutool.http.webservice;
import aiyh.utils.tool.cn.hutool.core.exceptions.UtilException;
import aiyh.utils.tool.cn.hutool.core.util.CharsetUtil;
import aiyh.utils.tool.cn.hutool.core.util.XmlUtil;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
/**
* SOAP
*
* @author looly
* @since 4.5.7
*/
public class SoapUtil {
/**
* SOAP使soap1.1
*
* @param url WSURL
* @return {@link aiyh.utils.tool.cn.hutool.http.webservice.SoapClient}
*/
public static aiyh.utils.tool.cn.hutool.http.webservice.SoapClient createClient(String url) {
return aiyh.utils.tool.cn.hutool.http.webservice.SoapClient.create(url);
}
/**
* SOAP
*
* @param url WSURL
* @param protocol {@link SoapProtocol}
* @return {@link aiyh.utils.tool.cn.hutool.http.webservice.SoapClient}
*/
public static aiyh.utils.tool.cn.hutool.http.webservice.SoapClient createClient(String url, SoapProtocol protocol) {
return aiyh.utils.tool.cn.hutool.http.webservice.SoapClient.create(url, protocol);
}
/**
* SOAP
*
* @param url WSURL
* @param protocol {@link SoapProtocol}
* @param namespaceURI URI
* @return {@link aiyh.utils.tool.cn.hutool.http.webservice.SoapClient}
* @since 4.5.6
*/
public static aiyh.utils.tool.cn.hutool.http.webservice.SoapClient createClient(String url, SoapProtocol protocol, String namespaceURI) {
return SoapClient.create(url, protocol, namespaceURI);
}
/**
* {@link SOAPMessage}
*
* @param message SOAP
* @param pretty
* @return SOAP XML
*/
public static String toString(SOAPMessage message, boolean pretty) {
return toString(message, pretty, CharsetUtil.CHARSET_UTF_8);
}
/**
* {@link SOAPMessage}
*
* @param message SOAP
* @param pretty
* @param charset
* @return SOAP XML
* @since 4.5.7
*/
public static String toString(SOAPMessage message, boolean pretty, Charset charset) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
message.writeTo(out);
} catch (SOAPException | IOException e) {
throw new SoapRuntimeException(e);
}
String messageToString;
try {
messageToString = out.toString(charset.toString());
} catch (UnsupportedEncodingException e) {
throw new UtilException(e);
}
return pretty ? XmlUtil.format(messageToString) : messageToString;
}
}

View File

@ -0,0 +1,6 @@
/**
* Webservice
*
* @author looly
*/
package aiyh.utils.tool.cn.hutool.http.webservice;

View File

@ -0,0 +1,206 @@
/*
*
* Copyright (c) 2001-2018 .
* ,.
*
*/
package com.api.chaoyang.he.browser.service.impl;
import com.api.browser.bean.ListHeadBean;
import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SplitTableBean;
import com.api.browser.bean.SplitTableColBean;
import com.api.browser.service.BrowserService;
import com.api.browser.util.*;
import com.cloudstore.dev.api.bean.SplitMobileDataBean;
import com.cloudstore.dev.api.bean.SplitMobileTemplateBean;
import com.cloudstore.dev.api.util.Util_MobileData;
import org.apache.commons.lang.StringEscapeUtils;
import weaver.conn.RecordSet;
import weaver.fna.e9.controller.base.FnaInvoiceLedgerController;
import weaver.fna.invoice.Constants;
import weaver.general.Util;
import weaver.systeminfo.SystemEnv;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author zhangwj
* @Description:
* @version 1.0
*/
public class MultiInvoiceBrowserService extends BrowserService {
//移动端返回数据格式的json配置
public static final String JSON_CONFIG="[" +
" {" +
" \"key\": \"col1\"," +
" \"configs\": [" +
" {" +
" \"key\": \"col1_row1\"," +
" \"configs\": [" +
" {" +
" \"key\": \"invoiceNumber\"" +
" }," +
" {" +
" \"key\": \"invoiceCode\"," +
" \"style\": {" +
" \"float\": \"right\"" +
" }" +
" }" +
" ]" +
" }," +
" {" +
" \"key\": \"col1_row2\"," +
" \"configs\": [" +
" {" +
" \"key\": \"taxIncludedPrice\"" +
" }," +
" {" +
" \"key\": \"invoiceTypeName\"," +
" \"style\": {" +
" \"float\": \"right\"" +
" }" +
" }" +
" ]" +
" }" +
" {" +
" \"key\": \"col1_row3\"," +
" \"configs\": [" +
" {" +
" \"key\": \"billingdate\"" +
" }," +
" ]" +
" }" +
" ]" +
" }" +
"]";
/**
*
* @param params
* @return
*/
@Override
public Map<String, Object> getBrowserData(Map<String, Object> params) throws Exception {
Map<String, Object> apidatas = new HashMap<String, Object>();
if(user == null){
apidatas.put(BrowserConstant.BROWSER_RESULT_DATA, null);
return apidatas;
}
String advQry_kprq1 = Util.null2String(params.get("createdatestart")).trim();
String advQry_kprq2 = Util.null2String(params.get("createdateend")).trim();
String invoiceNumber = Util.null2String(params.get("invoiceNumber"));
String seller = Util.null2String(params.get("seller"));
//发票代码,发票类型,开票日期,金额
FnaInvoiceLedgerController fnaInvoiceLedgerController = FnaInvoiceLedgerController.getInstance();
String sqlAppend = fnaInvoiceLedgerController.getCaseWhenSql4InvoiceTypeList("invoiceTypeName", "a.invoiceType", user.getLanguage())+" ";
//设置好搜索条件
String backFields =" a.*, "+
fnaInvoiceLedgerController.getCaseWhenSql4InvoiceTypeList("invoiceTypeName", "a.invoiceType", user.getLanguage())+" ";
String fromSql = " from FnaInvoiceLedger a ";
//验票为真的发票才能选择的到
StringBuffer sqlWhere = new StringBuffer(" where 1=1 and (checkStatus = 1 or checkStatus = 2) ");
if(!"".equals(advQry_kprq2)){
sqlWhere.append(" and a.billingDate <= '").append(StringEscapeUtils.escapeSql(advQry_kprq2)).append("' ");
}
if(!"".equals(advQry_kprq1)){
sqlWhere.append(" and a.billingDate >= '").append(StringEscapeUtils.escapeSql(advQry_kprq1)).append("' ");
}
if(!"".equals(invoiceNumber)){
sqlWhere.append(" and a.invoiceNumber like '%").append(StringEscapeUtils.escapeSql(invoiceNumber.trim())).append("%'");
}
if(!"".equals(seller)){
sqlWhere.append(" and a.seller like '%").append(StringEscapeUtils.escapeSql(seller.trim())).append("%'");
}
sqlWhere.append(" and (a.userid_new = ").append(user.getUID()).append(" or a.id in ( select invoiceId from fnaInvoiceSharer where sharer = ").append(user.getUID()).append(") ").append(") ");
sqlWhere.append(" and a.status = '0' ");
//AP航信过滤浏览按钮中的数据-hcy
// sqlWhere.append(" and hxjksflr is null ");
String orderBy=" a.id desc ";
//writeLog("select "+backFields+" "+fromSql+" "+sqlWhere);
List<SplitTableColBean> cols = new ArrayList<SplitTableColBean>();
cols.add(new SplitTableColBean("true","id"));
cols.add(new SplitTableColBean("30%",SystemEnv.getHtmlLabelName(900,user.getLanguage()),"invoiceNumber","invoiceNumber",1).setIsInputCol(BoolAttr.TRUE));
cols.add(new SplitTableColBean("30%",SystemEnv.getHtmlLabelName(17213,user.getLanguage()),"invoiceCode","invoiceCode"));
cols.add(new SplitTableColBean("30%",SystemEnv.getHtmlLabelName(17213,user.getLanguage()),"invoiceTypeName","invoiceTypeName"));
cols.add(new SplitTableColBean("30%",SystemEnv.getHtmlLabelName(17213,user.getLanguage()),"billingdate","billingdate"));
cols.add(new SplitTableColBean("30%",SystemEnv.getHtmlLabelName(17213,user.getLanguage()),"taxIncludedPrice","taxIncludedPrice"));
SplitTableBean tableBean = new SplitTableBean(backFields,fromSql,sqlWhere.toString(),orderBy,"a.id","DESC",cols);
//移动端返回数据
List<SplitMobileDataBean> mobileDataBeanList=Util_MobileData.createList(JSON_CONFIG);
SplitMobileTemplateBean bean=Util_MobileData.createJsonTemplateBean("theme_default", mobileDataBeanList);
tableBean.createMobileTemplate(bean);
apidatas.putAll(SplitTableUtil.makeListDataResult(tableBean));
return apidatas;
}
/**
*
* @param params
* @return
*/
@Override
public Map<String, Object> getBrowserConditionInfo(Map<String, Object> params) throws Exception {
Map<String, Object> apidatas = new HashMap<String, Object>();
List<SearchConditionItem> conditions = new ArrayList<SearchConditionItem>();
ConditionFactory conditionFactory = new ConditionFactory(user);
conditions.add(conditionFactory.createCondition(ConditionType.INPUT, 900, "invoiceNumber").setIsQuickSearch(true));
apidatas.put(BrowserConstant.BROWSER_RESULT_CONDITIONS, conditions);
return apidatas;
}
/**
*
* @param params
* @return
*/
@Override
public Map<String, Object> getMultBrowserDestData(Map<String, Object> params) throws Exception {
Map<String, Object> apidatas = new HashMap<String, Object>();
String selectids = Util.null2String(params.get(BrowserConstant.BROWSER_MULT_DEST_SELECTIDS));
List<Map<String,Object>> datas = new ArrayList<Map<String,Object>>();
StringBuffer buffer = new StringBuffer();
buffer.append(" select a.* from FnaInvoiceLedger a ");
buffer.append(" where a.id in (").append(selectids).append(")");
buffer.append(" order by a.id desc ");
RecordSet rs = new RecordSet();
rs.execute(buffer.toString());
while(rs.next()){
Map<String,Object> item = new HashMap<String,Object>();
item.put("id",Util.null2String(rs.getString("id")));
item.put("invoiceNumber",Util.null2String(rs.getString("invoiceNumber")));
item.put("invoiceCode",Util.null2String(rs.getString("invoiceCode")));
item.put("invoiceTypeName", Constants.INVOICETYPE.get(Util.null2String(rs.getString("invoicetype"))));
item.put("billingdate", Util.null2String(rs.getString("billingdate")));
item.put("taxIncludedPrice", Util.null2String(rs.getString("taxIncludedPrice")));
datas.add(item);
}
List<ListHeadBean> tableHeadColumns = new ArrayList<ListHeadBean>();
tableHeadColumns.add(new ListHeadBean("id",BoolAttr.TRUE).setIsPrimarykey(BoolAttr.TRUE));
tableHeadColumns.add(new ListHeadBean("invoiceNumber","",1,BoolAttr.TRUE));
tableHeadColumns.add(new ListHeadBean("invoiceCode",""));
tableHeadColumns.add(new ListHeadBean("invoiceTypeName",""));
tableHeadColumns.add(new ListHeadBean("billingdate",""));
tableHeadColumns.add(new ListHeadBean("taxIncludedPrice",""));
apidatas.put(BrowserConstant.BROWSER_RESULT_COLUMN, tableHeadColumns);
apidatas.put(BrowserConstant.BROWSER_RESULT_DATA, BrowserBaseUtil.sortDatas(datas,selectids,"id"));
apidatas.put(BrowserConstant.BROWSER_RESULT_TYPE, BrowserDataType.LIST_ALL_DATA.getTypeid());
return apidatas;
}
}

View File

@ -0,0 +1,45 @@
package com.api.chaoyang.he.hcy_aphangxin.controller;
import aiyh.utils.ApiResult;
import aiyh.utils.Util;
import com.api.chaoyang.he.hcy_aphangxin.service.ChangeFpytService;
import com.api.chaoyang.he.hcy_aphangxin.service.impl.ChangeFpytServiceImpl;
import org.apache.log4j.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
@Path("/hcy_aphangxin")
public class ChangeFpytController {
/**
*
*/
private ChangeFpytService changeFpytService = new ChangeFpytServiceImpl();
/**
*
*/
private Logger logger = Util.getLogger();
/**
* <h2>AP-</h2>
* @param request,response
* @return String
* @author hcy
* @Date 2023/3/13 14:28
*/
@POST
@Path("/change/fpyt")
@Produces(MediaType.APPLICATION_JSON)
public String checkRequestInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
String ids = request.getParameter("ids");
String[] split = ids.split(",");
String s = changeFpytService.changeFpytValue(split);
return ApiResult.success(s);
}
}

View File

@ -0,0 +1,73 @@
package com.api.chaoyang.he.hcy_aphangxin.deleteinvoicenotap.controller;
import aiyh.utils.ApiResult;
import aiyh.utils.Util;
import com.api.chaoyang.he.hcy_aphangxin.deleteinvoicenotap.service.DeleteInvoiceNotApService;
import com.api.chaoyang.he.hcy_aphangxin.deleteinvoicenotap.service.impl.DeleteInvoiceNotApServiceimpl;
import org.apache.log4j.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Path("/hcy_aphangxin/delete")
public class DeleteInvoiceNotApController {
/**
*
*/
private final Logger logger = Util.getLogger();
/**
* service
*/
private final DeleteInvoiceNotApService deleteInvoiceNotApService = new DeleteInvoiceNotApServiceimpl();
/**
* <h2>idAP</h2>
* @param request
* @param response
* @return String
* @author hcy
* @Date 2023/3/17 15:21
*/
@POST
@Path("/invoice/not/ap")
@Produces(MediaType.APPLICATION_JSON)
public String deleteInvoiceNotAp(@Context HttpServletRequest request, @Context HttpServletResponse response) {
//前端查询到的数据id
String ids = request.getParameter("ids");
String[] split = ids.split(",");
//备份数据之前首先查询要备份和要删除的数据的发票用途字段是否为非AP若为非AP字段则删除若不是非AP字段则不删除并返回前端提醒用户选择ap字段进行删除
List<String> fpytList = deleteInvoiceNotApService.selectFpyt(split);
List<String> fpytCollection = fpytList.stream().filter(items -> items.equals("1")).collect(Collectors.toList());
if (fpytList.size() != fpytCollection.size()){
logger.info("删除数据发票用途字段不为非AP,请重新选择");
return ApiResult.success(1,"删除数据发票用途字段不为非AP,请重新选择");
}
//删除非AP数据之前首先备份数据
boolean backupBoolean = deleteInvoiceNotApService.backUpAfterDelete(split);
if (backupBoolean==true){
logger.info("数据备份成功");
}else{
logger.info("数据备份失败");
}
//开始删除数据
boolean deleteBoolean = deleteInvoiceNotApService.deleteDateById(split);
if (deleteBoolean){
logger.info("数据删除成功");
return ApiResult.success(0,"数据删除成功!");
}else {
logger.info("数据删除失败");
return ApiResult.success(1,"数据删除失败!");
}
}
}

View File

@ -0,0 +1,61 @@
package com.api.chaoyang.he.hcy_aphangxin.deleteinvoicenotap.entity;
import lombok.Data;
@Data
public class BackupFnaDataEntity {
int requestId;
int formmodeid;
int modedatacreater;
int modedatacreatertype;
String modedatacreatedate;
String modedatacreatetime;
String MODEUUID;
String form_biz_id;
double taxRate;
String billingdate;
String invoicenumber;
String invoicecode;
String invoicetype;
double taxincludedprice;
double pricewithouttax;
double tax;
String purchaser;
String purchasertaxno;
String seller;
String salestaxno;
String purp;
String hxjksflr;
int checkStatus;
int status;
int authenticity;
int fpyt;
int check_status;
}

View File

@ -0,0 +1,39 @@
package com.api.chaoyang.he.hcy_aphangxin.deleteinvoicenotap.mapper;
import aiyh.utils.annotation.recordset.*;
import java.util.List;
import java.util.Map;
@SqlMapper
public interface DeleteInvoiceNotApMapper {
@Select("select * from fnainvoiceledger where id in ($t{split})")
List<Map<String, Object>> selectDate(@ParamMapper("split") String[] split);
@Insert("insert into uf_apfpscsj (billingDate,invoiceCode,invoiceNumber," +
"invoiceType,seller,purchaser,invoiceServiceYype,priceWithoutTax,taxRate," +
"tax,taxIncludedPrice,authenticity,reimbursementDate,reimbursePerson,requestId,userid_new," +
"invoiceSource_new,checkcode,status,card_id_new,encrypt_code_new,openid_new,wechatstatus,imageID,purchaserTaxNo," +
"salesTaxNo,entryTime,company_seal,form_type,form_name,kind,ciphertext,category,imageDocId,checkStatus," +
"updateOperate,cloudId,codeConfirm,numberConfirm,receiptor,reviewer,issuer,province,city,travel_tax,fylx," +
"purp,iele,orf,hxjksflr,fpyt) " +
"values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
boolean backupDate(@ParamMapper("backupDataLists") List<Object> backupDataLists);
@Delete("delete from fnainvoiceledger where id = #{id}")
boolean deleteDateById(@ParamMapper("id") String id);
/**
* <h2></h2>
* @param id id
* @return String
* @author hcy
* @Date 2023/3/20 13:56
*/
@Select("select fpyt from fnainvoiceledger where id = #{id}")
String selectFpyt(String id);
}

View File

@ -0,0 +1,11 @@
package com.api.chaoyang.he.hcy_aphangxin.deleteinvoicenotap.service;
import java.util.List;
public interface DeleteInvoiceNotApService {
boolean backUpAfterDelete(String[] split);
boolean deleteDateById(String[] split);
List<String> selectFpyt(String[] split);
}

View File

@ -0,0 +1,94 @@
package com.api.chaoyang.he.hcy_aphangxin.deleteinvoicenotap.service.impl;
import aiyh.utils.Util;
import com.api.chaoyang.he.hcy_aphangxin.deleteinvoicenotap.mapper.DeleteInvoiceNotApMapper;
import com.api.chaoyang.he.hcy_aphangxin.deleteinvoicenotap.service.DeleteInvoiceNotApService;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class DeleteInvoiceNotApServiceimpl implements DeleteInvoiceNotApService {
/**
* sql
*/
private final DeleteInvoiceNotApMapper deleteInvoiceNotApMapper = Util.getMapper(DeleteInvoiceNotApMapper.class);
/**
*
*/
private final Logger logger = Util.getLogger();
/**
* <h2></h2>
* @param split requestid
* @return boolean
* @author hcy
* @Date 2023/3/17 17:48
*/
public boolean backUpAfterDelete(String[] split) {
RecordSet recordSet = new RecordSet();
for (String id : split) {
String backupDataSql = "INSERT INTO fnabackup (billingdate,\t\n" +
"invoicenumber,\t\n" +
"invoicecode,\n" +
"invoicetype,\t\n" +
"taxincludedprice,pricewithouttax,\t\n" +
"tax,\n" +
"purchaser,purchasertaxno,\t\n" +
"seller,salestaxno,purp,hxjksflr,checkStatus,status,authenticity,fpyt,taxrate\n" +
") SELECT \n" +
"billingdate,\t\n" +
"invoicenumber,\t\n" +
"invoicecode,\n" +
"invoicetype,\t\n" +
"taxincludedprice,pricewithouttax,\t\n" +
"tax,\n" +
"purchaser,purchasertaxno,\t\n" +
"seller,salestaxno,purp,hxjksflr,checkStatus,status,authenticity,fpyt,taxrate\n" +
" FROM fnainvoiceledger WHERE id = ?";
boolean backupBool = recordSet.executeUpdate(backupDataSql, id);
if (!backupBool){
return false;
}
}
//开始备份数据
return true;
}
/**
* <h2>id</h2>
* @param split id
* @return boolean
* @author hcy
* @Date 2023/3/17 18:00
*/
public boolean deleteDateById(String[] split) {
List<Boolean> booleanList = new ArrayList<>();
for (String s : split) {
boolean deleteBoolean = deleteInvoiceNotApMapper.deleteDateById(s);
booleanList.add(deleteBoolean);
}
List<Boolean> collect = booleanList.stream().distinct().collect(Collectors.toList());
if (collect != null && collect.size()==1){
return true;
}
return false;
}
@Override
public List<String> selectFpyt(String[] split) {
List<String> fpytLists = new ArrayList<>();
for (String s : split) {
String fpyt = deleteInvoiceNotApMapper.selectFpyt(s);
fpytLists.add(fpyt);
}
return fpytLists;
}
}

View File

@ -0,0 +1,143 @@
package com.api.chaoyang.he.hcy_aphangxin.deleteinvoicenotap.service.impl;
import aiyh.utils.Util;
import com.api.chaoyang.he.hcy_aphangxin.deleteinvoicenotap.mapper.DeleteInvoiceNotApMapper;
import com.api.chaoyang.he.hcy_aphangxin.deleteinvoicenotap.service.DeleteInvoiceNotApService;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class DeleteInvoiceNotApServiceimpl_copy implements DeleteInvoiceNotApService {
/**
* sql
*/
private final DeleteInvoiceNotApMapper deleteInvoiceNotApMapper = Util.getMapper(DeleteInvoiceNotApMapper.class);
/**
*
*/
private final Logger logger = Util.getLogger();
/**
* <h2></h2>
* @param split requestid
* @return boolean
* @author hcy
* @Date 2023/3/17 17:48
*/
public boolean backUpAfterDelete(String[] split) {
//导入数据之前先查询数据
List<Map<String,Object>> backupDate = deleteInvoiceNotApMapper.selectDate(split);//拿到需要备份的数据
//开始备份数据
List<Boolean> bool = new ArrayList<>();//存放sql语句insert动作是否成功
for (Map<String, Object> map : backupDate) {
List<Object> backupDataLists = new ArrayList<>();
backupDataLists.add(map.get("billingDate"));
backupDataLists.add(map.get("invoiceCode"));
backupDataLists.add(map.get("invoiceNumber"));
backupDataLists.add(map.get("invoiceType"));
backupDataLists.add(map.get("seller"));
backupDataLists.add(map.get("purchaser"));
backupDataLists.add(map.get("invoiceServiceYype"));
backupDataLists.add(map.get("priceWithoutTax"));
backupDataLists.add(map.get("taxRate"));
backupDataLists.add(map.get("tax"));
backupDataLists.add(map.get("taxIncludedPrice"));
backupDataLists.add(map.get("authenticity"));
backupDataLists.add(map.get("reimbursementDate"));
backupDataLists.add(map.get("reimbursePerson"));
backupDataLists.add(map.get("requestId"));
backupDataLists.add(map.get("useridNew"));
backupDataLists.add(map.get("invoiceSourceNew"));
backupDataLists.add(map.get("checkcode"));
backupDataLists.add(map.get("status"));
backupDataLists.add(map.get("cardIdNew"));
backupDataLists.add(map.get("encryptCodeNew"));
backupDataLists.add(map.get("openidNew"));
backupDataLists.add(map.get("wechatstatus"));
backupDataLists.add(map.get("imageID"));
backupDataLists.add(map.get("purchaserTaxNo"));
backupDataLists.add(map.get("salesTaxNo"));
backupDataLists.add(map.get("entryTime"));
backupDataLists.add(map.get("companySeal"));
backupDataLists.add(map.get("formType"));
backupDataLists.add(map.get("formName"));
backupDataLists.add(map.get("kind"));
backupDataLists.add(map.get("ciphertext"));
backupDataLists.add(map.get("category"));
backupDataLists.add(map.get("imageDocId"));
backupDataLists.add(map.get("checkStatus"));
backupDataLists.add(map.get("updateOperate"));
backupDataLists.add(map.get("cloudId"));
backupDataLists.add(map.get("codeConfirm"));
backupDataLists.add(map.get("numberConfirm"));
backupDataLists.add(map.get("receiptor"));
backupDataLists.add(map.get("reviewer"));
backupDataLists.add(map.get("issuer"));
backupDataLists.add(map.get("province"));
backupDataLists.add(map.get("city"));
backupDataLists.add(map.get("travelTax"));
// backupDataLists.add(map.get("fylx"));
backupDataLists.add(map.get("purp"));
// backupDataLists.add(map.get("iele"));
backupDataLists.add(map.get("orf"));
backupDataLists.add(map.get("hxjksflr"));
backupDataLists.add(map.get("fpyt"));
RecordSet recordSet = new RecordSet();
String backupdate = "insert into fnabackup (billingDate,invoiceCode,invoiceNumber," +
"invoiceType,seller,purchaser,invoiceServiceYype,priceWithoutTax,taxRate," +
"tax,taxIncludedPrice,authenticity,reimbursementDate,reimbursePerson,requestId,userid_new," +
"invoiceSource_new,checkcode,status,card_id_new,encrypt_code_new,openid_new,wechatstatus,imageID,purchaserTaxNo," +
"salesTaxNo,entryTime,company_seal,form_type,form_name,kind,ciphertext,category,imageDocId,checkStatus," +
"updateOperate,cloudId,codeConfirm,numberConfirm,receiptor,reviewer,issuer,province,city,travel_tax," +
"purp,orf,hxjksflr,fpyt) " +
"values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
boolean insertIntoBoolean = recordSet.executeUpdate(backupdate, backupDataLists);
// boolean insertIntoBoolean = deleteInvoiceNotApMapper.backupDate(backupDataLists);
bool.add(insertIntoBoolean);
logger.info("插入语句是否成功==="+insertIntoBoolean);
}
List<Boolean> collect = bool.stream().distinct().collect(Collectors.toList());
if (collect!=null && collect.size()==1){
return true;
}
return false;
}
/**
* <h2>id</h2>
* @param split id
* @return boolean
* @author hcy
* @Date 2023/3/17 18:00
*/
public boolean deleteDateById(String[] split) {
List<Boolean> booleanList = new ArrayList<>();
for (String s : split) {
boolean deleteBoolean = deleteInvoiceNotApMapper.deleteDateById(s);
booleanList.add(deleteBoolean);
}
List<Boolean> collect = booleanList.stream().distinct().collect(Collectors.toList());
if (collect != null && collect.size()==1){
return true;
}
return false;
}
@Override
public List<String> selectFpyt(String[] split) {
List<String> fpytLists = new ArrayList<>();
for (String s : split) {
String fpyt = deleteInvoiceNotApMapper.selectFpyt(s);
fpytLists.add(fpyt);
}
return fpytLists;
}
}

View File

@ -0,0 +1,17 @@
package com.api.chaoyang.he.hcy_aphangxin.mapper;
import aiyh.utils.annotation.recordset.ParamMapper;
import aiyh.utils.annotation.recordset.SqlMapper;
import aiyh.utils.annotation.recordset.Update;
/**
* <h1>id,</h1>
* @Author hcy
* @Date 2023/3/13 14:41
*/
@SqlMapper
public interface ChangeFpytMapper {
@Update("update fnainvoiceledger set fpyt = 1 where id = #{id}")
boolean updateFpytValue(@ParamMapper("id") String id);
}

View File

@ -0,0 +1,12 @@
package com.api.chaoyang.he.hcy_aphangxin.service;
public interface ChangeFpytService {
/**
* <h2></h2>
* @param split
* @return String
* @author hcy
* @Date 2023/3/13 14:38
*/
public String changeFpytValue(String[] split);
}

View File

@ -0,0 +1,33 @@
package com.api.chaoyang.he.hcy_aphangxin.service.impl;
import aiyh.utils.Util;
import com.api.chaoyang.he.hcy_aphangxin.mapper.ChangeFpytMapper;
import com.api.chaoyang.he.hcy_aphangxin.service.ChangeFpytService;
public class ChangeFpytServiceImpl implements ChangeFpytService {
/**
* Mapper
*/
private final ChangeFpytMapper changeFpytMapper = Util.getMapper(ChangeFpytMapper.class);
@Override
public String changeFpytValue(String[] split) {
int num = 0;
for (String s : split) {
if (!s.equals("")){
boolean b = changeFpytMapper.updateFpytValue(s);
if (b){
num++;
}
}
}
if (num == split.length){
return "所有数据更新成功";
}else {
return "数据更新失败";
}
}
}

View File

@ -0,0 +1,92 @@
package com.api.chaoyang.he.hcy_dingxinjituan.exportpdf.controller;
import aiyh.utils.Util;
import com.api.chaoyang.he.hcy_dingxinjituan.exportpdf.service.ExportPDFService;
import com.api.chaoyang.he.hcy_dingxinjituan.exportpdf.service.impl.ExportPDFServiceImpl;
import org.apache.log4j.Logger;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
@Path("/hcy_dingxinjituan")
public class ExportPDFController {
private final ExportPDFService exportPDFService = new ExportPDFServiceImpl();
private final Logger logger = Util.getLogger();
@GET
@Path("/export/pdf")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response exportPDF(@Context HttpServletRequest request, @Context HttpServletResponse response) {
Response build = null;
Response.ResponseBuilder header = null;
StreamingOutput output = null;
String ids = request.getParameter("ids");
String[] split = ids.split(",");
//第二步更新导出次+1
exportPDFService.updateDccs(split);
//第一步查询数据封装到List<Map<String,Object>>类型的集合中
List<Map<String,Object>> dataList = exportPDFService.selectDataList(split);
User user = HrmUserVarify.getUser(request, response);
String exportUser = user.getLastname();
//第三步同步数据
List<Boolean> insertBooleanLists = exportPDFService.backupData(dataList,exportUser,this.getCurrentTime());
for (Boolean list : insertBooleanLists) {
if (list == false){
logger.info("备份数据失败");
}
}
//第四步根据第一步拿到的数据导出pdf
String exportTime = getCurrentDateTime();//导出时间
byte[] pdfBytes = exportPDFService.exportPDF(dataList,exportUser,exportTime);
output = outputStream -> {
outputStream.write(pdfBytes);
outputStream.close();
};
String fileName = "顶新集团-"+ getCurrentTime() + ".pdf";
try {
header = Response.ok(output,MediaType.APPLICATION_OCTET_STREAM)
.type("application/xlsx")
.header("Content-Disposition",
"attachment; filename=\""+ new String(fileName.getBytes("GBK"), StandardCharsets.ISO_8859_1)+"\"");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return header.build();
}
public static String getCurrentDateTime() {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return now.format(formatter);
}
public static String getCurrentTime() {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return now.format(formatter);
}
}

View File

@ -0,0 +1,32 @@
package com.api.chaoyang.he.hcy_dingxinjituan.exportpdf.mapper;
import aiyh.utils.annotation.recordset.ParamMapper;
import aiyh.utils.annotation.recordset.Select;
import aiyh.utils.annotation.recordset.SqlMapper;
import aiyh.utils.annotation.recordset.Update;
import org.directwebremoting.annotations.Param;
import java.util.List;
import java.util.Map;
@SqlMapper
public interface ExportPDFMapper {
@Select("select * from uf_sapywdj where id in ($t{split})")
List<Map<String, Object>> selectDataList(String[] split);
@Update("update uf_sapywdj set dccs = #{s} where id = #{id}")
boolean updateDccs(@ParamMapper("s") String s, @ParamMapper("id") String id);
@Select("select dccs from uf_sapywdj where id = #{id}")
String selectDccs(String id);
@Select("select requestname from workflow_requestbase where requestid = #{stringXglc}")
String selectRequestName(String stringXglc);
@Select("select gysmc from uf_sapgys where id= #{gfdw} ")
String setGfdw(String gfdw);
}

View File

@ -0,0 +1,14 @@
package com.api.chaoyang.he.hcy_dingxinjituan.exportpdf.service;
import java.util.List;
import java.util.Map;
public interface ExportPDFService {
List<Map<String, Object>> selectDataList(String[] split);
void updateDccs(String[] split);
byte[] exportPDF(List<Map<String, Object>> dataList, String exportUser, String exportTime);
List<Boolean> backupData(List<Map<String, Object>> dataList,String exportUser,String exportTime);
}

View File

@ -0,0 +1,275 @@
package com.api.chaoyang.he.hcy_dingxinjituan.exportpdf.service.impl;
import aiyh.utils.Util;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.api.chaoyang.he.hcy_dingxinjituan.exportpdf.mapper.ExportPDFMapper;
import com.api.chaoyang.he.hcy_dingxinjituan.exportpdf.service.ExportPDFService;
import com.itextpdf.text.Document;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import de.schlichtherle.io.File;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.general.GCONST;
public class ExportPDFServiceImpl implements ExportPDFService {
private final ExportPDFMapper exportPDFMapper = Util.getMapper(ExportPDFMapper.class);
private final Logger logger = Util.getLogger();
@Override
public List<Map<String, Object>> selectDataList(String[] split) {
return exportPDFMapper.selectDataList(split);
}
@Override
public void updateDccs(String[] split) {
for (String s : split) {
String dccs = Util.null2String(exportPDFMapper.selectDccs(s));
if (!"".equals(dccs) && Util.getIntValue(dccs)!=-1){
int dccsInt = Integer.parseInt(dccs);
dccsInt++;
dccs = String.valueOf(dccsInt);
boolean updateBool = exportPDFMapper.updateDccs(dccs,s);
}else if (!"".equals(dccs) && Util.getIntValue(dccs)==-1){
boolean updateBool = exportPDFMapper.updateDccs("1",s);
}
else{
boolean updateBool = exportPDFMapper.updateDccs("1",s);
}
}
}
@Override
public byte[] exportPDF(List<Map<String, Object>> dataList, String exportUser, String exportTime) {
// 创建PDF文档
Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
PdfWriter.getInstance(document, baos);
// 打开PDF文档
document.open();
// 创建页面并设置属性
// Rectangle pageSize = PageSize.A4;
// document.setPageSize(pageSize.rotate()); // 横向页面
// document.setMargins(36, 36, 36, 36); // 36pt边距
// 创建表格
PdfPTable table = new PdfPTable(10);
table.setWidthPercentage(100f);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.setHeaderRows(1);
//字体
String rootPath = GCONST.getRootPath();
// new File(GCONST.getRootPath()+"WEB-INF"+File.separatorChar+"meeting"+File.separatorChar);
// String fontPath = rootPath + "/WEB-INF/font/simfang.ttf";
String fontPath = "/app/ecology/WEB-INF/font/simfang.ttf";
logger.info("fontPath==="+fontPath);
// BaseFont bfChinese = BaseFont.createFont("D:\\WEAVER\\ecology\\WEB-INF\\font\\simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
BaseFont bfChinese = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// 添加标题
Font titleFont = new Font(bfChinese, 18, Font.BOLD);
Paragraph title = new Paragraph("SAP-业务单据数据", titleFont);
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
//// 添加导出人和导出日期信息
Paragraph info = new Paragraph("导出人:"+exportUser+" 导出日期:"+exportTime,
new Font(bfChinese, 10f, Font.NORMAL, BaseColor.BLACK));
info.setAlignment(Element.ALIGN_RIGHT);
document.add(info);
// 添加表头行
// 设置表头样式及内容
Font headerFont = new Font(bfChinese, 10, Font.BOLD, BaseColor.WHITE);
PdfPCell headerCell = new PdfPCell();
headerCell.setBackgroundColor(BaseColor.BLACK);
headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
headerCell.setPadding(5);
headerCell.setPhrase(new Phrase("申请编号", headerFont));
table.addCell(headerCell);
headerCell.setPhrase(new Phrase("相关流程", headerFont));
table.addCell(headerCell);
headerCell.setPhrase(new Phrase("法人公司编码", headerFont));
table.addCell(headerCell);
headerCell.setPhrase(new Phrase("地址号", headerFont));
table.addCell(headerCell);
headerCell.setPhrase(new Phrase("给付单位", headerFont));
table.addCell(headerCell);
headerCell.setPhrase(new Phrase("开户行", headerFont));
table.addCell(headerCell);
headerCell.setPhrase(new Phrase("银行账号", headerFont));
table.addCell(headerCell);
headerCell.setPhrase(new Phrase("金额", headerFont));
table.addCell(headerCell);
headerCell.setPhrase(new Phrase("SAP凭证号", headerFont));
table.addCell(headerCell);
headerCell.setPhrase(new Phrase("导出次数", headerFont));
table.addCell(headerCell);
// 添加数据行
Font dataFont = new Font(bfChinese, 9, Font.NORMAL, BaseColor.BLACK);
for (Map<String, Object> row : dataList) {
table.addCell(createCell(String.valueOf(row.get("sqbh")), dataFont, Element.ALIGN_CENTER));
String stringXglc = Util.null2String(row.get("xglc"));
String requstName = exportPDFMapper.selectRequestName(stringXglc);
table.addCell(createCell(requstName, dataFont, Element.ALIGN_CENTER));
table.addCell(createCell(String.valueOf(row.get("frgsbm")), dataFont, Element.ALIGN_CENTER));
table.addCell(createCell(String.valueOf(row.get("dzh")), dataFont, Element.ALIGN_CENTER));
String gfdw = Util.null2String(row.get("gfdw"));
String gfdwString = exportPDFMapper.setGfdw(gfdw);
table.addCell(createCell(gfdwString, dataFont, Element.ALIGN_CENTER));
table.addCell(createCell(String.valueOf(row.get("khh")), dataFont, Element.ALIGN_CENTER));
table.addCell(createCell(String.valueOf(row.get("yhzh")), dataFont, Element.ALIGN_CENTER));
table.addCell(createCell(String.valueOf(row.get("je")), dataFont, Element.ALIGN_CENTER));
table.addCell(createCell(String.valueOf(row.get("sappzh")), dataFont, Element.ALIGN_CENTER));
table.addCell(createCell(String.valueOf(row.get("dccs")), dataFont, Element.ALIGN_CENTER));
}
// 设置表格样式和行样式
table.setSpacingBefore(8f);
table.setSpacingAfter(8f);
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
table.getDefaultCell().setPadding(4f);
// 添加表格到页面
document.add(table);
// 添加导出人和导出日期信息
// 关闭PDF文档
document.close();
// 将PDF文档写入输出流
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
logger.info("导出PDF文档异常原因==="+e);
}
return new byte[0];
}
public List<Boolean> backupData(List<Map<String, Object>> dataList,String exportuser,String exporttime) {
RecordSet recordSet = new RecordSet();
List<Boolean> insertBooleanLists = new ArrayList();
try {
for (Map<String, Object> map : dataList) {
//toDo:
String insertSql = "insert into uf_sapywdj_backup (requestId,formmodeid,modedatacreater,modedatacreatertype," +
"modedatacreatedate,modedatacreatetime,MODEUUID,lclx,sfmdfy,qklx,fkfs,yfklx,dx,xglc,sqr,cgbs,fhxx,sappzh,sqbm," +
"khh,yhzh,ytsm,je,frgsbm,dpmc,dpbh,qwfkrq,dzh,szgs,sqbh,cwgzrq,ph,cwjb,paystatus,completetime,fygz,jybgse,jybgwsje,jybghsje," +
"jybgfhxx1,jybgfhxx2,jybgfhxx3,jybgfhzt1,jybgfhzt2,jybgfhzt3,jybgpzh1,jybgpzh2,jybgpzh3,dh,jybglshjl,gtddh,frgs,gfdw,cbzx,ywlx,mdcwjb," +
"mdcwkzg,cwkzg,zbchbzg,zbchbzg1,mdcwkzg1,dpmc1,zhcyr,modedatamodifier,modedatamodifydatetime,dccs,exportuser,exporttime) values (?,?,?,?,?,?,?,?,?,?," +
"?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
logger.info("sql==="+insertSql);
boolean insertBool = recordSet.executeUpdate(insertSql,
Util.null2String(map.get("requestId")),
Util.null2String(map.get("formmodeid")),
Util.null2String(map.get("formmodeid")),
Util.null2String(map.get("modedatacreatertype")),
Util.null2String(map.get("modedatacreatedate")),
Util.null2String(map.get("modedatacreatetime")),
Util.null2String(map.get("MODEUUID")),
Util.null2String(map.get("lclx")),
Util.null2String(map.get("sfmdfy")),
Util.null2String(map.get("qklx")),
Util.null2String(map.get("fkfs")),
Util.null2String(map.get("yfklx")),
Util.null2String(map.get("dx")),
Util.null2String(map.get("xglc")),
Util.null2String(map.get("sqr")),
Util.null2String(map.get("cgbs")),
Util.null2String(map.get("fhxx")),
Util.null2String(map.get("sappzh")),
Util.null2String(map.get("sqbm")),
Util.null2String(map.get("khh")),
Util.null2String(map.get("yhzh")),
Util.null2String(map.get("ytsm")),
Util.null2String(map.get("je")),
Util.null2String(map.get("frgsbm")),
Util.null2String(map.get("dpmc")),
Util.null2String(map.get("dpbh")),
Util.null2String(map.get("qwfkrq")),
Util.null2String(map.get("dzh")),
Util.null2String(map.get("szgs")),
Util.null2String(map.get("sqbh")),
Util.null2String(map.get("cwgzrq")),
Util.null2String(map.get("ph")),
Util.null2String(map.get("cwjb")),
Util.null2String(map.get("paystatus")),
Util.null2String(map.get("completetime")),
Util.null2String(map.get("fygz")),
Util.null2String(map.get("jybgse")),
Util.null2String(map.get("jybgwsje")),
Util.null2String(map.get("jybghsje")),
Util.null2String(map.get("jybgfhxx1")),
Util.null2String(map.get("jybgfhxx2")),
Util.null2String(map.get("jybgfhxx3")),
Util.null2String(map.get("jybgfhzt1")),
Util.null2String(map.get("jybgfhzt2")),
Util.null2String(map.get("jybgfhzt3")),
Util.null2String(map.get("jybgpzh1")),
Util.null2String(map.get("jybgpzh2")),
Util.null2String(map.get("jybgpzh3")),
Util.null2String(map.get("dh")),
Util.null2String(map.get("jybglshjl")),
Util.null2String(map.get("gtddh")),
Util.null2String(map.get("frgs")),
Util.null2String(map.get("gfdw")),
Util.null2String(map.get("cbzx")),
Util.null2String(map.get("ywlx")),
Util.null2String(map.get("mdcwjb")),
Util.null2String(map.get("mdcwkzg")),
Util.null2String(map.get("cwkzg")),
Util.null2String(map.get("zbchbzg")),
Util.null2String(map.get("zbchbzg1")),
Util.null2String(map.get("mdcwkzg1")),
Util.null2String(map.get("dpmc1")),
Util.null2String(map.get("zhcyr")),
Util.null2String(map.get("modedatamodifier")),
Util.null2String(map.get("modedatamodifydatetime")),
Util.null2String(map.get("dccs")),
exportuser,
exporttime
);
insertBooleanLists.add(insertBool);
}
} catch (Exception e) {
e.printStackTrace();
logger.error("错误原因==="+e.getMessage());
}
return insertBooleanLists;
}
private PdfPCell createCell(String text, Font font, int align) {
PdfPCell cell = new PdfPCell(new Phrase(text, font));
cell.setHorizontalAlignment(align);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(4f);
return cell;
}
}

View File

@ -0,0 +1,49 @@
package com.api.chaoyang.he.hcy_fengtianfangzhi.controller;
import aiyh.utils.Util;
import com.api.chaoyang.he.hcy_fengtianfangzhi.service.DetailDataExportExcelApi;
import com.api.chaoyang.he.hcy_fengtianfangzhi.service.impl.DetailDataExportExcelService;
import org.apache.log4j.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/hcy_fengtianfangzhi")
public class DetailDataExportExcelController {
private DetailDataExportExcelApi detailDataExportExcelService = new DetailDataExportExcelService();
private Logger logger = Util.getLogger();
/**
*AVPN Excel
* @param request
* @param response
* @return
*/
@GET
@Path("/detailDataExportExcel")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response checkRequestInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
String requestid = request.getParameter("requestid");
logger.info("requestid=="+requestid);
String lsxjd = request.getParameter("lsxjd");
logger.info("lsxjd=="+lsxjd);//lsxjd
Response response1 = detailDataExportExcelService.exportExcel(requestid,lsxjd);
return response1;
}
}

View File

@ -0,0 +1,280 @@
package com.api.chaoyang.he.hcy_fengtianfangzhi.controller;
import aiyh.utils.ApiResult;
import aiyh.utils.Util;
import com.engine.workflow.biz.requestForm.WfToDocBiz;
import com.engine.workflow.biz.requestForm.WfWaterMark4WfToDocBiz;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.file.FileUpload;
import weaver.file.ImageFileManager;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.WorkflowToDoc;
import weaver.system.SystemComInfo;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.UUID;
/**
* <h1></h1>
* @Author jiacheng.deng
* @Date 2023/1/18 14:50
*/
//"tb/djc/changeStatus/download/requestid=" + requestId + "&workflowid=" + workflowId + "&nodeid=" + nodeId
@Path("/tb")
public class TbSheetChangeDownloadStatusController {
//流程保存文档源码
private WorkflowToDoc workflowToDoc = new WorkflowToDoc();
//流程保存文档源码
private WfToDocBiz wfToDocBiz = new WfToDocBiz();
//数据库链接驱动
private RecordSet recordSet = new RecordSet();
//日志
private final Logger log = Util.getLogger();
//底部签字意见列表显示数量
private int pageSize =100;
//下载的输入流
private ImageFileManager imageFileManager = new ImageFileManager();
/**
* <h2></h2>
* @param request,response
* @return String
* @author jiacheng.deng
*/
@GET
@Path("/changeStatus/download")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response changeStatus(@Context HttpServletRequest request,
@Context HttpServletResponse response
) {
try {
User logInUser = HrmUserVarify.getUser(request, response);
String uid = String.valueOf(logInUser.getUID());
log.info("用户的id===>" + uid);
String findUserRole = "select * from HrmRoleMembers where resourceid = " + uid;
boolean b = recordSet.execute(findUserRole);
log.info("查询用户数据sql===> " + findUserRole + " ===>是否执行 " + b);
recordSet.next();
String roleid = recordSet.getString("roleid");
log.info("用户角色roleid===>" + roleid);
// log.info("requsetid===>" + requestid + "workflowid===>" + workflowid + "nodeid===>" + nodeid);
if ("1031".equals(roleid)) {
try {
// StreamingOutput downloadStream = download2changeStatus(uid,request,response,requestid,workflowid,nodeid);
// InputStream is = imageFileManager.getInputStreamById(this.getImageFileId(requestid));
String updateDownloadStatusSQl = "update formtable_main_368 set dczt = ?" ;
int flag = 1;
boolean b1 = recordSet.executeUpdate(updateDownloadStatusSQl, flag);
log.info("执行修改导出状态代码===>" + b1);
// return Response.ok(downloadStream, MediaType.APPLICATION_OCTET_STREAM).type("application/")
// .header("Content-Disposition", "attachment;filename=workflow.pdf").build();
} catch (Exception e) {
log.error("转换失败:" + e.toString());
return Response.ok(ApiResult.error("出现错误,错误原因: " + e), MediaType.APPLICATION_JSON).build();
}
} else {
try {
// StreamingOutput downloadStream = download2changeStatus(uid,request,response,requestid,workflowid,nodeid);
// return Response.ok(downloadStream, MediaType.APPLICATION_OCTET_STREAM).type("application/pdf")
// .header("Content-Disposition", "attachment;filename=contracts.zip").build();
} catch (Exception e) {
log.error("转换失败:" + e.toString());
return Response.ok(ApiResult.error("出现错误,错误原因: " + e), MediaType.APPLICATION_JSON).build();
}
}
}catch(Exception e){
}
return null;
}
/**
* <h2></h2>
* @param
* @return
* @author jiacheng.deng
*/
public StreamingOutput download2changeStatus(String uid,HttpServletRequest request,HttpServletResponse response,String requestid,String workflowid,String nodeid){
LinkedHashMap<String, String> fileids = new LinkedHashMap<String, String> ();
User user = new User(weaver.general.Util.getIntValue(uid));
String docFiles = getDocFiles(workflowid);
//是否签名
int keepSign = getKeepSign(workflowid);
String filename = UUID.randomUUID().toString();
String temppath = getFileSavePath();
//是否开启水印
boolean isOpenWaterMark = WfWaterMark4WfToDocBiz.isOpenWaterMark(weaver.general.Util.getIntValue(workflowid));
//在这里先获取到modeid 线程中获取时流程可能已经到了下个节点导致模板获取的不对
String modeid = wfToDocBiz.getModeid(Util.getIntValue(requestid), Util.getIntValue(workflowid), Util.getIntValue(nodeid));
WfToDocBiz wfToDocBiz = new WfToDocBiz(user,pageSize,keepSign,docFiles,modeid,isOpenWaterMark);
wfToDocBiz.generatepdfandhtml(requestid,filename,temppath,"1");
fileids.putAll(wfToDocBiz.getfileids("",filename,temppath));
StreamingOutput output = outputStream -> {
String docID;
if (recordSet.next()){
docID = wfToDocBiz.getWfDocPath(workflowid, requestid);
log.info("文档生成路径=="+docID);
ImageFileManager imageFileManager = new ImageFileManager();
int imageFileId = this.getImageFileId(requestid);
log.info("imageFileId ==" + imageFileId);
InputStream is = imageFileManager.getInputStreamById(this.getImageFileId(requestid));
String fileName = this.getImageFileName(requestid);
};
//关闭流
outputStream.flush();
outputStream.close();
};
return null;
}
private String getImageFileName(String requestid) {
return null;
}
/**
* <h2></h2>
* @param
* @return String
* @author jiacheng.deng
*/
public String getFileSavePath() {
SystemComInfo syscominfo = new SystemComInfo();
String createdir = FileUpload.getCreateDir(syscominfo.getFilesystem());
return createdir;
}
/**
* <h2>requestIddocimagefileimagefileId</h2>
* @param requestid
* @return int
* @author jiacheng.deng
*/
public int getImageFileId(String requestid){
RecordSet recordSet = new RecordSet();
String get_imagefileid_sql = "select tablename \n" +
"from workflow_bill \n" +
"where id = (select formid from workflow_base where id = \n" +
"(select workflowid from workflow_requestbase where requestid = ?)\n" +
")";
boolean b = recordSet.executeQuery(get_imagefileid_sql, requestid);
log.info("b是否执行"+b);
boolean next = recordSet.next();
String tablename = recordSet.getString("tablename");
String docid_key = this.getWfDocRelateFieldName(requestid);//每一流程表单中用来存放docid的字段名称
String get_docid_sql = "select "+docid_key+" from "+tablename+" where requestid = ?";
boolean b1 = recordSet.executeQuery(get_docid_sql, requestid);
log.info("b1是否执行=="+b1);
recordSet.next();
String docid = recordSet.getString(docid_key);
if (Util.null2String(docid).equals("")){
return -1;
}
String get_imagefileid = "select imagefileid from DocImageFile where docid = ? order by versionId";
boolean b2 = recordSet.executeQuery(get_imagefileid, docid);
log.info("b2是否执行=="+b2);
recordSet.next();
String imagefileid = recordSet.getString("imagefileid");
return Integer.parseInt(imagefileid);
}
/**
* requestId
* @param requestid
* @return
*/
public String getWfDocRelateFieldName(String requestid){
String get_wfdocrelatefilename_sql = "select fieldname from workflow_billfield \n" +
"where id =(select wfdocrelatefieldid from workflow_base \n" +
"where id = (select workflowid from workflow_requestbase where requestid = ?))";
RecordSet recordSet = new RecordSet();
boolean b = recordSet.executeQuery(get_wfdocrelatefilename_sql, requestid);
log.info("getWfDocRelateFieldName---b是否执行=="+b);
boolean next = recordSet.next();
String fieldname = recordSet.getString("fieldname");
return Util.null2String(fieldname);
}
/**
* <h2>docfiles 线/线(HTML)/线(PDF)</h2>
* @param workflowid
* @return String
* @author jiacheng.deng
*/
public String getDocFiles(String workflowid){
RecordSet rs = new RecordSet();
String docfiles = "";
String wfdocpath = "";
rs.executeQuery("select docfiles,wfdocpath from workflow_base where id = ?",workflowid);
if (rs.next()){
docfiles = weaver.general.Util.null2String(rs.getString("docfiles"));
wfdocpath = weaver.general.Util.null2String(rs.getString("wfdocpath"));
}
if ("".equals(docfiles)&&!"".equals(wfdocpath)){
docfiles="1";
rs.executeUpdate("update workflow_base set docfiles ='1' where id = ?",workflowid);
}
return docfiles;
}
/**
* <h2>keepsign </h2>
* @param workflowid
* @return int
* @author jiacheng.deng
*/
public int getKeepSign(String workflowid){
RecordSet rs = new RecordSet();
int keepsign = 0;
rs.executeQuery("select keepsign from workflow_base where id = ?",workflowid);
if (rs.next()){
keepsign = rs.getInt("keepsign");
}
return keepsign;
}
}

View File

@ -0,0 +1,22 @@
package com.api.chaoyang.he.hcy_fengtianfangzhi.mapper;
import aiyh.utils.annotation.recordset.Select;
import aiyh.utils.annotation.recordset.SqlMapper;
import java.util.List;
import java.util.Map;
@SqlMapper
public interface DetailDataExportExcelMapper{
@Select("select * from formtable_main_378 fm " +
"inner join formtable_main_378_dt1 fmdt " +
"on fm.id = fmdt.mainid where fm.requestid = #{requestid}")
List<Map<String, Object>> selectTotalData(String requestId);
// @Select("select * from formtable_main_35 fm " +
// "inner join formtable_main_35_dt1 fmdt " +
// "on fm.id = fmdt.mainid where fm.requestid = #{requestid}")
// List<Map<String, Object>> selectTotalData(String requestId);
}

View File

@ -0,0 +1,17 @@
package com.api.chaoyang.he.hcy_fengtianfangzhi.service;
import javax.ws.rs.core.Response;
/**
* excel
*/
public interface DetailDataExportExcelApi {
/**
* excel
*/
public Response exportExcel(String requestId,String lsxjd);
}

View File

@ -0,0 +1,403 @@
package com.api.chaoyang.he.hcy_fengtianfangzhi.service.impl;
import aiyh.utils.Util;
import com.api.chaoyang.he.hcy_fengtianfangzhi.mapper.DetailDataExportExcelMapper;
import com.api.chaoyang.he.hcy_fengtianfangzhi.service.DetailDataExportExcelApi;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import weaver.conn.RecordSet;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DetailDataExportExcelService implements DetailDataExportExcelApi {
/**
*
*/
public Logger logger = Util.getLogger();
/**
* sql
*/
private DetailDataExportExcelMapper detailDataExportExcelMapper = Util.getMapper(DetailDataExportExcelMapper.class);
/**
* excel
* @param requestId requestid
*/
public Response exportExcel(String requestId,String lsxjd) {
//创建一个工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
//创建一个工作表sheet
XSSFSheet sheet = workbook.createSheet();
//设置sheet表名称
workbook.setSheetName(0, "询价单明细表单");
//设置表单列宽
sheet.autoSizeColumn(1, true);
RecordSet recordSet = new RecordSet();//创建sql执行对象
List<Map<String,Object>> totalDataList = detailDataExportExcelMapper.selectTotalData(requestId);
for (Map<String, Object> map : totalDataList) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object v = entry.getValue();
if (key.equals("sqrxm")){//申请人姓名
String get_sqrxm_sql = "select lastname from hrmresource where id=?";
recordSet.executeQuery(get_sqrxm_sql,Util.null2String(v));
recordSet.next();
map.put("sqrxm",recordSet.getString("lastname"));
}
if (key.equals("gys")){//供应商
String get_gys_sql = "select gysqm from uf_gyszsjxx where id=?";
recordSet.executeQuery(get_gys_sql, Util.null2String(v));
recordSet.next();
map.put("gys",recordSet.getString("gysqm"));
}
if("xjr".equals(key)){//询价人
String get_sqrxm_sql = "select lastname from hrmresource where id=?";
recordSet.executeQuery(get_sqrxm_sql,Util.null2String(v));
recordSet.next();
map.put("xjr",recordSet.getString("lastname"));
}
if ("ylbm".equals(key) ||"sqrbm".equals(key) || "sqrfb".equals(key) || "xjbm".equals(key)){//依赖部门 //申请人部门 //申请人分部 //询价部门
String getYlbm = "select departmentname from HrmDepartment where id = ? ";
recordSet.executeQuery(getYlbm,Util.null2String(v));
recordSet.next();
map.put(key,recordSet.getString("departmentname"));
}
if ("lsxjd".equals(key)){//历史询价单
String getLsxjd = "select requestname from workflow_requestbase where requestid = ? ";
recordSet.executeQuery(getLsxjd,Util.null2String(v));
recordSet.next();
map.put(key,recordSet.getString("requestname"));
}
// if("sqrbm".equals(key)){//申请人部门
// String getYlbm = "select departmentname from HrmDepartment where id = ? ";
// recordSet.executeQuery(getYlbm,Util.null2String(v));
// recordSet.next();
// map.put(key,recordSet.getString("departmentname"));
// }
// if ("sqrfb".equals(key)){//申请人分部
// String getYlbm = "select departmentname from HrmDepartment where id = ? ";
// recordSet.executeQuery(getYlbm,Util.null2String(v));
// recordSet.next();
// map.put(key,recordSet.getString("departmentname"));
// }
// if("xjbm".equals(key)){//询价部门
// String getYlbm = "select departmentname from HrmDepartment where id = ? ";
// recordSet.executeQuery(getYlbm,Util.null2String(v));
// recordSet.next();
// map.put(key,recordSet.getString("departmentname"));
// }
}
}
String getMainTableValueSql = "select id from uf_xjdmxbdczdpzbd where wybs = 'ftfz_exportExcel' ";
boolean b = recordSet.executeQuery(getMainTableValueSql);
logger.info("getMainTableValueSql是否执行=="+b);
String mainId = "";
if (recordSet.next()){
mainId = recordSet.getString("id");
}
String getDetailTableValueSql = "select * from uf_xjdmxbdczdpzbd_dt1 where mainId = ?";
boolean b1 = recordSet.executeQuery(getDetailTableValueSql, mainId);
logger.info("getDetailTableValueSql是否执行==="+b1);
List<String> fieldNamesList = new ArrayList<>();//用于存放excel第一个行的明细表显示名
List<String> dBNamesList = new ArrayList<>();//用于存放明细表数据库名
while (recordSet.next()){
String mxzdxsm = recordSet.getString("mxzdxsm");//字段显示名
String mxzdsjkm = recordSet.getString("mxzdsjkm");//字段数据库名
fieldNamesList.add(mxzdxsm);
dBNamesList.add(mxzdsjkm);
}
logger.info("fieldNamesList=="+fieldNamesList);
XSSFRow row = sheet.createRow(0);
for (int colNum=0;colNum<fieldNamesList.size();colNum++){
XSSFCell cell = row.createCell(colNum);
cell.setCellStyle(setNolyWordStyle(workbook,true));
cell.setCellValue(fieldNamesList.get(colNum));
}
try {
for (int cowNum=1;cowNum<totalDataList.size()+1;cowNum++){
XSSFRow row1 = sheet.createRow(cowNum);
Map<String, Object> map1 = totalDataList.get(cowNum-1);
for (int colNum=0;colNum<dBNamesList.size();colNum++){
XSSFCell cell = row1.createCell(colNum);
String field = dBNamesList.get(colNum);
if (!"".equals(field)){
if (field.equals("pl")){//品类---下拉框 28-消耗品、29-工事、30-劳保用品、31-IT
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellValue(Util.null2String(map1.get(field)));
}else if (field.equals("sqsl")){//申请数量--浮点数
Object o = map1.get(field);
if (!String.valueOf(o).equals("")){
double value = Double.parseDouble(String.valueOf(o));
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(value);
}else {
cell.setCellValue("");
cell.setCellStyle(setNolyWordStyle(workbook));
}
}else if (field.equals("bz")){//币种--浏览按钮
Object o = map1.get(field);
if (!String.valueOf(o).equals("")){
if (String.valueOf(o).equals("1")){
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellValue("RMB");
}
}else {
cell.setCellValue("");
cell.setCellStyle(setNolyWordStyle(workbook));
}
}else if (field.equals("wsdj")){//未税单价--浮点数
Object o = map1.get(field);
if (!String.valueOf(o).equals("")){
double value = Double.parseDouble(String.valueOf(o));
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(value);
}else{
cell.setCellValue("");
cell.setCellStyle(setNolyWordStyle(workbook));
}
}else if (field.equals("wszj")){//未税总价--浮点数
Object o = map1.get(field);
if (!String.valueOf(o).equals("")){
double value = Double.parseDouble(String.valueOf(o));
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(value);
}else{
cell.setCellValue("");
cell.setCellStyle(setNolyWordStyle(workbook));
}
}else if (field.equals("sl")){//税率(%--整数
Object o = map1.get(field);
if (!String.valueOf(o).equals("")){
int value = Integer.parseInt(String.valueOf(o));
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(value);
}else {
cell.setCellValue("");
cell.setCellStyle(setNolyWordStyle(workbook));
}
}else if (field.equals("se")){//税额--浮点数
Object o = map1.get(field);
if (!String.valueOf(o).equals("")){
double value = Double.parseDouble(String.valueOf(o));
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(value);
}else{
cell.setCellValue("");
cell.setCellStyle(setNolyWordStyle(workbook));
}
}else if (field.equals("hsdj")){//含税单价--浮点数
Object o = map1.get(field);
if (!String.valueOf(o).equals("")){
double value = Double.parseDouble(String.valueOf(o));
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(value);
}else {
cell.setCellValue("");
cell.setCellStyle(setNolyWordStyle(workbook));
}
}else if (field.equals("hszj")){//含税总价--浮点数
Object o = map1.get(field);
if (!String.valueOf(o).equals("")){
double value = Double.parseDouble(String.valueOf(o));
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(value);
}else{
cell.setCellValue("");
cell.setCellStyle(setNolyWordStyle(workbook));
}
}else if (field.equals("ddhfqwnqt")){//调达回复期望纳期(天)--整数
Object o = map1.get(field);
if (!String.valueOf(o).equals("")){
int value = Integer.parseInt(String.valueOf(o));
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(value);
}else {
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellValue("");
}
}else if (field.equals("chzx")){//存货属性--下拉框32-临时品、33-定期品
if (map1.get(field).equals("0")){
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellValue("临时品");
}else if (map1.get(field).equals("1")){
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellValue("定期品");
}else {
cell.setCellValue("");
}
}else if (field.equals("chbm")){//存货编码--自定义单选
String get_chbm_sql = "select chbm from uf_chtz_tbtj where id=?";
RecordSet recordSet1 = new RecordSet();
boolean b4 = recordSet1.executeQuery(get_chbm_sql, map1.get(field));
logger.info("get_chbm_sql语句是否查询成功==="+b4);
if (recordSet1.next()){
String chbmValue = recordSet1.getString("chbm");
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellValue(chbmValue);
}
}else {
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellValue(String.valueOf(map1.get(field)));
}
}
}
}
} catch (NumberFormatException e) {
e.printStackTrace();
logger.info("NumberFormatException==="+e);
}
StreamingOutput output;
Response build;
Response.ResponseBuilder header = null;
try {
InputStream inputStream = workbookConvertorStream(workbook);
byte[] bytes = IOUtils.toByteArray(inputStream);
output = outputStream -> {
outputStream.write(bytes);
outputStream.close();
};
header = Response.ok(output,MediaType.APPLICATION_OCTET_STREAM)
.type("application/xlsx")
.header("Content-Disposition",
"attachment; filename=\""+ new String("询价单明细表单.xlsx".getBytes("GBK"), StandardCharsets.ISO_8859_1)+"\"");
} catch (Exception e) {
e.printStackTrace();
logger.error("导出excel错误" + Util.getErrString(e));
}
build = header.build();
logger.info("走到导出excel这一步");
return build;
}
/**
* ,
*
* @param workbook workbook
* @return CellStyle
*/
public static CellStyle setNolyWordStyle(Workbook workbook, Boolean setBold) {
// 设置字体
CellStyle cellStyle = workbook.createCellStyle();
Font font = workbook.createFont();
//颜色
font.setColor(Font.COLOR_NORMAL);
//设置字体大小
font.setFontHeightInPoints((short) 10);
//字体
font.setFontName("微软雅黑");
font.setBold(setBold);
cellStyle.setFont(font);
return cellStyle;
}
/**
*
*
* @param workbook worKbook
* @return CellStyle
*/
public static CellStyle setNolyWordStyle(Workbook workbook) {
// 设置字体
CellStyle cellStyle = workbook.createCellStyle();
Font font = workbook.createFont();
//颜色
font.setColor(Font.COLOR_NORMAL);
//设置字体大小
font.setFontHeightInPoints((short) 10);
//字体
font.setFontName("微软雅黑");
cellStyle.setFont(font);
return cellStyle;
}
/**
* SXSSFWorkbook InputStream
* @param workbook workbook
* @return inputStream
*/
public static InputStream workbookConvertorStream(Workbook workbook) {
InputStream in = null;
try{
//临时缓冲区
ByteArrayOutputStream out = new ByteArrayOutputStream();
//创建临时文件
workbook.write(out);
byte [] bookByteAry = out.toByteArray();
in = new ByteArrayInputStream(bookByteAry);
}
catch (Exception e){
e.printStackTrace();
}
return in;
}
}

View File

@ -0,0 +1,497 @@
package com.api.chaoyang.he.hcy_fengtianfangzhi.service.impl;
import aiyh.utils.Util;
import com.api.chaoyang.he.hcy_fengtianfangzhi.service.DetailDataExportExcelApi;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import weaver.conn.RecordSet;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DetailDataExportExcelService_copy implements DetailDataExportExcelApi {
public Logger logger = Util.getLogger();
/**
* excel
* @param requestId requestid
*/
public Response exportExcel(String requestId,String lsxjd) {
//创建一个工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
//创建一个工作表sheet
XSSFSheet sheet = workbook.createSheet();
//设置sheet表名称
workbook.setSheetName(0, "询价单明细表单");
//设置表单列宽
sheet.autoSizeColumn(1, true);
RecordSet recordSet = new RecordSet();//创建sql执行对象
String getMainIdSql = "select * from formtable_main_378 where requestId = ?";
// String getMainIdSql = "select * from formtable_main_35 where requestId = ?";
boolean b2 = recordSet.executeQuery(getMainIdSql, requestId);
logger.info("getMainIdSql语句是否执行==="+b2);
String formtable_main_35_id="";//询价单流程表单id,作为明细表的mianId使用
Map<String, Object> mainDataMap = new HashMap<>();//用于存放主表的数据
if (recordSet.next()){
formtable_main_35_id = recordSet.getString("id");
String sqdh = recordSet.getString("sqdh");//申请单号
mainDataMap.put("sqdh",sqdh);
String sqrxmID = recordSet.getString("sqrxm");//申请人姓名
String get_sqrxm_sql = "select lastname from hrmresource where id=?";
RecordSet recordSet1 = new RecordSet();
recordSet1.executeQuery(get_sqrxm_sql, sqrxmID);
if (recordSet1.next()){
String lastname = recordSet1.getString("lastname");
mainDataMap.put("sqrxm",lastname);
}
String sqrbm = recordSet.getString("sqrbm");//申请人部门
mainDataMap.put("sqrbm",sqrbm);
String sqrfb = recordSet.getString("sqrfb");//申请人分部
mainDataMap.put("sqrfb",sqrfb);
String sqrq = recordSet.getString("sqrq");//申请日期
mainDataMap.put("sqrq",sqrq);
// String lsxjd = recordSet.getString("lsxjd");//历史询价单
mainDataMap.put("lsxjd",lsxjd);
String xjbm = recordSet.getString("xjbm");//询价部门
mainDataMap.put("xjbm",xjbm);
String bz = recordSet.getString("bz");//备注
mainDataMap.put("bz",bz);
String bjfj = recordSet.getString("bjfj");//报价附件
mainDataMap.put("bjfj",bjfj);
String yafj = recordSet.getString("yafj");//议案附件
mainDataMap.put("yafj",yafj);
String sysfj = recordSet.getString("sysfj");//式样书附件
mainDataMap.put("sysfj",sysfj);
String hszje = recordSet.getString("hszje");//含税总金额
mainDataMap.put("hszje",hszje);
String wszje = recordSet.getString("wszje");//未税总金额
mainDataMap.put("wszje",wszje);
String fkblhj = recordSet.getString("fkblhj");//付款比例(合计)
mainDataMap.put("fkblhj",fkblhj);
String ylbmId = recordSet.getString("ylbm");//依赖部门
String getYlbm = "select departmentname from HrmDepartment where id = ? ";
recordSet1.executeQuery(getYlbm,ylbmId);
recordSet1.next();
String departmentname = recordSet1.getString("departmentname");
mainDataMap.put("ylbm",departmentname);
String xjrid = recordSet.getString("xjr");//询价人
recordSet1.executeQuery(get_sqrxm_sql,xjrid);
recordSet1.next();
String xjr = recordSet1.getString("lastname");
mainDataMap.put("xjr",xjr);
String gysId = recordSet.getString("gys");//供应商
String get_gys_sql = "select gysqm from uf_gyszsjxx where id=?";
recordSet1.executeQuery(get_gys_sql, gysId);
if (recordSet1.next()){
String gysqm = recordSet1.getString("gysqm");
mainDataMap.put("gys",gysqm);
}
}
logger.info("mainDataMap主表数据==="+mainDataMap);
String getDetailDataSql = "select * from formtable_main_378_dt1 where mainId = ?";
// String getDetailDataSql = "select * from formtable_main_35_dt1 where mainId = ?";
boolean b3 = recordSet.executeQuery(getDetailDataSql, formtable_main_35_id);
logger.info("getDetailDataSql语句是否执行==="+b3);
List<Map<String,Object>> listMap = new ArrayList<>();//用于存放明细表数据
while (recordSet.next()){
Map<String, Object> map = new HashMap<>();
String id = recordSet.getString("id");
map.put("id",id);
String mainId1 = recordSet.getString("mainId");
map.put("mainId",mainId1);
String pl = Util.null2String(recordSet.getString("pl"));
String plValue = "";
if (!"".equals(pl)){
//消耗品 设备工事 劳保用品 IT
switch(pl){
case "0":
plValue = "消耗品";
break;
case "1" :
plValue = "设备工事";
break;
case "2" :
plValue = "劳保用品";
break;
case "3":
break;
default :
plValue = "";
break;
}
}else{
map.put("pl", "");
}
map.put("pl", plValue);
String pm = recordSet.getString("pm");
map.put("pm",pm);
String pp = recordSet.getString("pp");
map.put("pp",pp);
String ggxh = recordSet.getString("ggxh");
map.put("ggxh",ggxh);
String jldw = recordSet.getString("jldw");
map.put("jldw",jldw);
String sqsl = recordSet.getString("sqsl");
map.put("sqsl",sqsl);
String bz = recordSet.getString("bz");
map.put("bz",bz);
String wsdj = recordSet.getString("wsdj");
map.put("wsdj",wsdj);
String wszj = recordSet.getString("wszj");
map.put("wszj",wszj);
String sl = recordSet.getString("sl");
map.put("sl",sl);
String se = recordSet.getString("se");
map.put("se",se);
String hsdj = recordSet.getString("hsdj");
map.put("hsdj",hsdj);
String hszj = recordSet.getString("hszj");
map.put("hszj",hszj);
String qwnq = recordSet.getString("qwnq");
map.put("qwnq",qwnq);
String ddhfqwnqt = recordSet.getString("ddhfqwnqt");
map.put("ddhfqwnqt",ddhfqwnqt);
String gys = recordSet.getString("gys");
map.put("gys",gys);
String cx = recordSet.getString("cx");
map.put("cx",cx);
String jd = recordSet.getString("jd");
map.put("jd",jd);
String bz1 = recordSet.getString("bz1");
map.put("bz1",bz1);
String fj = recordSet.getString("fj");
map.put("fj",fj);
String chzx = recordSet.getString("chzx");
map.put("chzx",chzx);
String chbm = recordSet.getString("chbm");
map.put("chbm",chbm);
listMap.add(map);
}
logger.info("listMap明细表数据==="+listMap);
String getMainTableValueSql = "select id from uf_xjdmxbdczdpzbd where wybs = 'ftfz_exportExcel' ";
boolean b = recordSet.executeQuery(getMainTableValueSql);
logger.info("getMainTableValueSql是否执行=="+b);
String mainId = "";
if (recordSet.next()){
mainId = recordSet.getString("id");
}
String getDetailTableValueSql = "select * from uf_xjdmxbdczdpzbd_dt1 where mainId = ?";
boolean b1 = recordSet.executeQuery(getDetailTableValueSql, mainId);
logger.info("getDetailTableValueSql是否执行==="+b1);
List<String> fieldNamesList = new ArrayList<>();//用于存放excel第一个行的明细表显示名
List<String> mainFieldNamesList = new ArrayList<>();//用于存放excel第一个行的主表显示名
List<String> detalsDBName = new ArrayList<>();//用于存放明细表数据库名
List<String> mainDBName = new ArrayList<>();//用于存放主表数据库字段名
while (recordSet.next()){
String sjly = recordSet.getString("sjly");//数据来源
String mxzdxsm = recordSet.getString("mxzdxsm");//字段显示名
// String mxzdlx = recordSet.getString("mxzdlx");//字段类型
String mxzdsjkm = recordSet.getString("mxzdsjkm");//字段数据库名
if (!"".equals(sjly)&&sjly.equals("0")){
mainFieldNamesList.add(mxzdxsm);
mainDBName.add(mxzdsjkm);
}else if (!"".equals(sjly)&&sjly.equals("1")){
fieldNamesList.add(mxzdxsm);
detalsDBName.add(mxzdsjkm);
}
}
logger.info("fieldNamesList=="+fieldNamesList);
logger.info("mainFieldNamesList=="+mainFieldNamesList);
logger.info("detalsDBName=="+detalsDBName);
logger.info("mainDBName=="+mainDBName);
XSSFRow row = sheet.createRow(0);
for (int colNum=0;colNum<mainFieldNamesList.size();colNum++){
XSSFCell cell = row.createCell(colNum);
cell.setCellStyle(setNolyWordStyle(workbook,true));
cell.setCellValue(mainFieldNamesList.get(colNum));
}
for (int colNum=mainFieldNamesList.size();colNum<fieldNamesList.size()+mainFieldNamesList.size();colNum++){
XSSFCell cell = row.createCell(colNum);
cell.setCellStyle(setNolyWordStyle(workbook,true));
cell.setCellValue(fieldNamesList.get(colNum-mainFieldNamesList.size()));
}
try {
for (int cowNum=1;cowNum<listMap.size()+1;cowNum++){
XSSFRow row1 = sheet.createRow(cowNum);
Map<String, Object> map1 = listMap.get(cowNum-1);
for (int colMainDataNum=0;colMainDataNum<mainDBName.size();colMainDataNum++){
XSSFCell cell = row1.createCell(colMainDataNum);
String value = mainDBName.get(colMainDataNum);
if (!"".equals(value)){
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellValue(String.valueOf(mainDataMap.get(value)));
}
}
for (int colNum=mainDBName.size();colNum<detalsDBName.size()+mainDBName.size();colNum++){
XSSFCell cell = row1.createCell(colNum);
String field = detalsDBName.get(colNum-mainDBName.size());
if (!"".equals(field)){
if (field.equals("pl")){//品类---下拉框 28-消耗品、29-工事、30-劳保用品、31-IT
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellValue(Util.null2String(map1.get(field)));
}else if (field.equals("sqsl")){//申请数量--浮点数
Object o = map1.get(field);
if (!String.valueOf(o).equals("")){
double value = Double.parseDouble(String.valueOf(o));
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(value);
}else {
cell.setCellValue("");
cell.setCellStyle(setNolyWordStyle(workbook));
}
}else if (field.equals("bz")){//币种--浏览按钮
Object o = map1.get(field);
if (!String.valueOf(o).equals("")){
if (String.valueOf(o).equals("1")){
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellValue("RMB");
}
}else {
cell.setCellValue("");
cell.setCellStyle(setNolyWordStyle(workbook));
}
}else if (field.equals("wsdj")){//未税单价--浮点数
Object o = map1.get(field);
if (!String.valueOf(o).equals("")){
double value = Double.parseDouble(String.valueOf(o));
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(value);
}else{
cell.setCellValue("");
cell.setCellStyle(setNolyWordStyle(workbook));
}
}else if (field.equals("wszj")){//未税总价--浮点数
Object o = map1.get(field);
if (!String.valueOf(o).equals("")){
double value = Double.parseDouble(String.valueOf(o));
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(value);
}else{
cell.setCellValue("");
cell.setCellStyle(setNolyWordStyle(workbook));
}
}else if (field.equals("sl")){//税率(%--整数
Object o = map1.get(field);
if (!String.valueOf(o).equals("")){
int value = Integer.parseInt(String.valueOf(o));
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(value);
}else {
cell.setCellValue("");
cell.setCellStyle(setNolyWordStyle(workbook));
}
}else if (field.equals("se")){//税额--浮点数
Object o = map1.get(field);
if (!String.valueOf(o).equals("")){
double value = Double.parseDouble(String.valueOf(o));
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(value);
}else{
cell.setCellValue("");
cell.setCellStyle(setNolyWordStyle(workbook));
}
}else if (field.equals("hsdj")){//含税单价--浮点数
Object o = map1.get(field);
if (!String.valueOf(o).equals("")){
double value = Double.parseDouble(String.valueOf(o));
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(value);
}else {
cell.setCellValue("");
cell.setCellStyle(setNolyWordStyle(workbook));
}
}else if (field.equals("hszj")){//含税总价--浮点数
Object o = map1.get(field);
if (!String.valueOf(o).equals("")){
double value = Double.parseDouble(String.valueOf(o));
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(value);
}else{
cell.setCellValue("");
cell.setCellStyle(setNolyWordStyle(workbook));
}
}else if (field.equals("ddhfqwnqt")){//调达回复期望纳期(天)--整数
Object o = map1.get(field);
if (!String.valueOf(o).equals("")){
int value = Integer.parseInt(String.valueOf(o));
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(value);
}else {
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellValue("");
}
}else if (field.equals("chzx")){//存货属性--下拉框32-临时品、33-定期品
if (map1.get(field).equals("0")){
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellValue("临时品");
}else if (map1.get(field).equals("1")){
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellValue("定期品");
}else {
cell.setCellValue("");
}
}else if (field.equals("chbm")){//存货编码--自定义单选
String get_chbm_sql = "select chbm from uf_chtz_tbtj where id=?";
RecordSet recordSet1 = new RecordSet();
boolean b4 = recordSet1.executeQuery(get_chbm_sql, map1.get(field));
logger.info("get_chbm_sql语句是否查询成功==="+b4);
if (recordSet1.next()){
String chbmValue = recordSet1.getString("chbm");
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellValue(chbmValue);
}
}else {
cell.setCellStyle(setNolyWordStyle(workbook));
cell.setCellValue(String.valueOf(map1.get(field)));
}
}
}
}
} catch (NumberFormatException e) {
e.printStackTrace();
logger.info("NumberFormatException==="+e);
}
StreamingOutput output;
Response build;
Response.ResponseBuilder header = null;
try {
InputStream inputStream = workbookConvertorStream(workbook);
byte[] bytes = IOUtils.toByteArray(inputStream);
output = outputStream -> {
outputStream.write(bytes);
outputStream.close();
};
header = Response.ok(output,MediaType.APPLICATION_OCTET_STREAM)
.type("application/xlsx")
.header("Content-Disposition",
"attachment; filename=\""+ new String("询价单明细表单.xlsx".getBytes("GBK"), StandardCharsets.ISO_8859_1)+"\"");
} catch (Exception e) {
e.printStackTrace();
logger.error("导出excel错误" + Util.getErrString(e));
}
build = header.build();
logger.info("走到导出excel这一步");
return build;
}
/**
* ,
*
* @param workbook workbook
* @return CellStyle
*/
public static CellStyle setNolyWordStyle(Workbook workbook, Boolean setBold) {
// 设置字体
CellStyle cellStyle = workbook.createCellStyle();
Font font = workbook.createFont();
//颜色
font.setColor(Font.COLOR_NORMAL);
//设置字体大小
font.setFontHeightInPoints((short) 10);
//字体
font.setFontName("微软雅黑");
font.setBold(setBold);
cellStyle.setFont(font);
return cellStyle;
}
/**
*
*
* @param workbook worKbook
* @return CellStyle
*/
public static CellStyle setNolyWordStyle(Workbook workbook) {
// 设置字体
CellStyle cellStyle = workbook.createCellStyle();
Font font = workbook.createFont();
//颜色
font.setColor(Font.COLOR_NORMAL);
//设置字体大小
font.setFontHeightInPoints((short) 10);
//字体
font.setFontName("微软雅黑");
cellStyle.setFont(font);
return cellStyle;
}
/**
* SXSSFWorkbook InputStream
* @param workbook workbook
* @return inputStream
*/
public static InputStream workbookConvertorStream(Workbook workbook) {
InputStream in = null;
try{
//临时缓冲区
ByteArrayOutputStream out = new ByteArrayOutputStream();
//创建临时文件
workbook.write(out);
byte [] bookByteAry = out.toByteArray();
in = new ByteArrayInputStream(bookByteAry);
}
catch (Exception e){
e.printStackTrace();
}
return in;
}
}

View File

@ -0,0 +1,257 @@
package com.api.chaoyang.he.hcy_fengtianfangzhi.wflistdonestore;
import aiyh.utils.ApiResult;
import aiyh.utils.Util;
import com.engine.workflow.biz.requestForm.WfToDocBiz;
import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import weaver.conn.RecordSet;
import weaver.file.ImageFileManager;
import weaver.hrm.HrmUserVarify;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Path("/hcy_fengtianfangzhi/wflistdonestore")
public class ExportPDF {
private final Logger logger = Util.getLogger();
/**
*
*/
private final WfToDocBiz wfToDocBiz = new WfToDocBiz();
/**
*pdf
* @param request
* @param response
* @return Response
*/
@GET
@Path("/ExportPDF")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response exportPDF(@Context HttpServletRequest request, @Context HttpServletResponse response) {
try {
String ids = request.getParameter("ids");
String[] split = ids.split(",");
String getWorkflowId_sql = "select workflowid from workflow_requestbase where requestid = ?";
RecordSet recordSet = new RecordSet();
int amountWorkflowToDoc = this.getAmountWorkflowToDoc(split);//用来统计流程转文档的流程的数量如果数量为0返回文件为空
StreamingOutput output = outputStream -> {
ZipOutputStream zipOut = new ZipOutputStream(outputStream);
for (String requestId : split) {
boolean b = recordSet.executeQuery(getWorkflowId_sql, requestId);
logger.info("b是否执行"+b);
String docID;
if (recordSet.next()){
String workflowid = recordSet.getString("workflowid");
docID = wfToDocBiz.getWfDocPath(workflowid, requestId);
logger.info("文档生成路径=="+docID);
int imageFileId = this.getImageFileId(requestId);
if (imageFileId==-1){
continue;
}
InputStream is = ImageFileManager.getInputStreamById(this.getImageFileId(requestId));
String fileName = this.getImageFileName(requestId);
if (Util.null2String(fileName).equals("")){
break;
}
this.addToZip(is,zipOut,fileName);//向zipout对象中插入每个pdf文件
}
}
//关闭流
zipOut.flush();
zipOut.close();
outputStream.flush();
outputStream.close();
};
if (amountWorkflowToDoc == 0){
return Response.ok(ApiResult.error("文件为空"),MediaType.TEXT_PLAIN).build();
}
String lastname = HrmUserVarify.getUser(request, response).getLastname();
String pdfName = lastname + "_" +getCurrentTime() + "_" + split.length;
return Response.ok(output, MediaType.APPLICATION_OCTET_STREAM).type("application/zip")
.header("Content-Disposition", "attachment;filename=\"" +
new String(pdfName.getBytes("GBK"), StandardCharsets.ISO_8859_1) + ".zip" + "\"").build();
} catch (Exception e) {
e.printStackTrace();
return Response.ok(ApiResult.error("出现错误,错误原因:"+e),MediaType.TEXT_PLAIN).build();
}
}
/**
* pdf
* @param requestId requestid
*/
public void updateValue(String requestId){
RecordSet recordSet = new RecordSet();
String get_imagefileid_sql = "select tablename \n" +
"from workflow_bill \n" +
"where id = (select formid from workflow_base where id = \n" +
"(select workflowid from workflow_requestbase where requestid = ?)\n" +
")";
boolean b = recordSet.executeQuery(get_imagefileid_sql, requestId);
logger.info("updateValue()----b是否执行"+b);
recordSet.next();
String tablename = recordSet.getString("tablename");
boolean b1 = recordSet.executeUpdate("update " + tablename + " set dczt = 0,dcsj = ? where requestid = ?",getCurrentTime(), requestId);
logger.info("updatevalue()----b1是否执行==="+b1);
}
/**
* 0
* pdf
* @param split requestid
* @return int
*/
public int getAmountWorkflowToDoc(String[] split){
int amount = 0;
String getWorkflowId_sql = "select workflowid from workflow_requestbase where requestid = ?";
RecordSet recordSet = new RecordSet();
for (String requestId : split) {
boolean b = recordSet.executeQuery(getWorkflowId_sql, requestId);
logger.info("getAmountWorkflowToDoc----b是否执行==="+b);
if (recordSet.next()){
int imageFileId = this.getImageFileId(requestId);
if (imageFileId==-1){
continue;
}else {
amount++;
//用来更新更新具体哪个流程表单的导出状态为已导出
this.updateValue(requestId);
}
}
}
return amount;
}
/**
* requestIddocimagefileimagefileId
* @param requestId requestid
* @return int
*/
public int getImageFileId(String requestId){
RecordSet recordSet = new RecordSet();
String get_imagefileid_sql = "select tablename \n" +
"from workflow_bill \n" +
"where id = (select formid from workflow_base where id = \n" +
"(select workflowid from workflow_requestbase where requestid = ?)\n" +
")";
boolean b = recordSet.executeQuery(get_imagefileid_sql, requestId);
logger.info("b是否执行"+b);
recordSet.next();
String tablename = recordSet.getString("tablename");
String docid_key = this.getWfDocRelateFieldName(requestId);//每一流程表单中用来存放docid的字段名称
String get_docid_sql = "select "+docid_key+" from "+tablename+" where requestid = ?";
boolean b1 = recordSet.executeQuery(get_docid_sql, requestId);
logger.info("b1是否执行=="+b1);
recordSet.next();
String docid = recordSet.getString(docid_key);
if (Util.null2String(docid).equals("")){
return -1;
}
String get_imagefileid = "select imagefileid from DocImageFile where docid = ? order by versionId";
boolean b2 = recordSet.executeQuery(get_imagefileid, docid);
logger.info("b2是否执行=="+b2);
recordSet.next();
String imagefileid = recordSet.getString("imagefileid");
return Integer.parseInt(imagefileid);
}
/**
* requestIddocimagefileimagefilename
* @param requestId requestid
* @return String imagefilename
*/
public String getImageFileName(String requestId){
RecordSet recordSet = new RecordSet();
String get_tablename_sql = "select tablename \n" +
"from workflow_bill \n" +
"where id = (select formid from workflow_base where id = \n" +
"(select workflowid from workflow_requestbase where requestid = ?)\n" +
")";
boolean b = recordSet.executeQuery(get_tablename_sql, requestId);
logger.info("b是否执行"+b);
recordSet.next();
String tablename = recordSet.getString("tablename");
String docid_key = this.getWfDocRelateFieldName(requestId);//每一流程表单中用来存放docid的字段名称
String get_docid_sql = "select "+docid_key+" from "+tablename+" where requestid = ?";
boolean b1 = recordSet.executeQuery(get_docid_sql, requestId);
logger.info("b1是否执行=="+b1);
recordSet.next();
String docid = recordSet.getString(docid_key);
String get_imagefilename = "select imagefilename from DocImageFile where docid = ? order by versionId";
boolean b2 = recordSet.executeQuery(get_imagefilename, docid);
logger.info("b2是否执行=="+b2);
recordSet.next();
return recordSet.getString("imagefilename");
}
/**
* requestId
* @param requestid requestid
* @return String filename
*/
public String getWfDocRelateFieldName(String requestid){
String get_wfdocrelatefilename_sql = "select fieldname from workflow_billfield \n" +
"where id =(select wfdocrelatefieldid from workflow_base \n" +
"where id = (select workflowid from workflow_requestbase where requestid = ?))";
RecordSet recordSet = new RecordSet();
boolean b = recordSet.executeQuery(get_wfdocrelatefilename_sql, requestid);
recordSet.next();
logger.info("getWfDocRelateFieldName---b是否执行=="+b);
String fieldname = recordSet.getString("fieldname");
return Util.null2String(fieldname);
}
/**
*
* @param is
* @param zipOut zipOUtPutStream
* @param fileName
*/
private void addToZip(InputStream is, ZipOutputStream zipOut, String fileName) {
try{
ZipEntry entry = new ZipEntry(fileName);
zipOut.putNextEntry(entry);
int len;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) > 0) {
zipOut.write(buffer, 0, len);
}
zipOut.closeEntry();
is.close();
}catch (Exception e){
e.printStackTrace();
}
}
/**
* <h2></h2>
* @return String
* @author hcy
* 2023/4/4 17:31
*/
public String getCurrentTime() {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return now.format(formatter);
}
}

View File

@ -0,0 +1,683 @@
package com.api.chaoyang.he.hcy_xintiantongxin;
import aiyh.utils.Util;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import weaver.conn.RecordSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
@Path("/hcy_xintiantongxin")
public class ExportExcelAVPNApi {
private Logger logger = Util.getLogger();
/**
*AVPN Excel
* @param request
* @param response
* @return
*/
@GET
@Path("/ExportExcelAVPNApi")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response checkRequestInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
Response build = null;
Response.ResponseBuilder header = null;
try {
//查询到 uf_avpn_export AVPN导出表中的一条数据的ids
String ids = request.getParameter("ids");
RecordSet rs = new RecordSet();
StreamingOutput output = null;
String sql_AVPN_db = "select * from uf_avpn_export where id = ?";
rs.executeQuery(sql_AVPN_db, ids);
//创建一个工作簿
Workbook workbook = new XSSFWorkbook();
String[] dbFiled = new String[10];
String mainId = "";
while (rs.next()) {
//查询到uf_avpn_export表的id作为查询明细表的mainid
mainId = rs.getString("id");
String pd_nameId = rs.getString("pd_name");//uf_pd_base_inf 得到的pd_name是字段的id
RecordSet recordSet = new RecordSet();
String getPd_nameByIdSql = "select pd_name from uf_pd_base_inf where id =?";
boolean b1 = recordSet.executeQuery(getPd_nameByIdSql, pd_nameId);
logger.info("查询pd_namesql语句是否成功==" + b1);
if (recordSet.next()) {
String pd_name = recordSet.getString("pd_name");//查询到pd_name的值
dbFiled[0] = pd_name;
}
String billing_model = rs.getString("billing_model");
if (billing_model.equals("0")){
billing_model = "Assignment Model Billing in China";
}
dbFiled[1] = billing_model;
String customer_name = rs.getString("customer_name");
dbFiled[2] = customer_name;
String ecrm_opportunity = rs.getString("ecrm_opportunity");
dbFiled[3] = ecrm_opportunity;
String igloo = rs.getString("igloo");
dbFiled[4] = igloo;
String terms = rs.getString("terms");
dbFiled[5] = terms;
String att_lead_pricer = rs.getString("att_lead_pricer");
dbFiled[6] = att_lead_pricer;
String requestjustificationoptional = rs.getString("requestjustificationoptional");
dbFiled[7] = requestjustificationoptional;
String specialrequesttosstoptional = rs.getString("specialrequesttosstoptional");
dbFiled[8] = specialrequesttosstoptional;
String sst_contact = rs.getString("sst_contact");
dbFiled[9] = sst_contact;
}
//表单字段
String[] tableFiled = new String[]{"Service Type", "Billing Model", "Customer Name", "ECRM Opportunity#", "Igloo #", "Contract Term (in months)", "AT&T Lead Pricer", "Request Justification (optional)", "Special Request to SST (Optional)", "SST Contact"};
//明细表字段
String[] mergerDetailTableFiled = new String[]{"Site ID", "Description", "Bandwidth", "OTC in RMB", "MRC in RMB"};
//创建一个工作表sheet
Sheet sheet = workbook.createSheet();
workbook.setSheetName(0, "AVPN表单");
sheet.autoSizeColumn(1, true);
//写入数据 //写入1-11行第一列 第二列数据
for (int rowNum = 1; rowNum < 11; rowNum++) {
Row row = sheet.createRow(rowNum);
Cell cell0 = row.createCell(0);
cell0.setCellStyle(this.setWordStyle(workbook, true));
cell0.setCellValue(tableFiled[rowNum - 1]);
Cell cell1 = row.createCell(1);
cell1.setCellStyle(setWordStyle(workbook, true));
cell1.setCellValue(dbFiled[rowNum - 1]);
}
Row row12 = sheet.createRow(12);//第13行
Cell cell12_11 = row12.createCell(11);
cell12_11.setCellStyle(setWordStyle(workbook, true));
cell12_11.setCellValue("Default Assignment Model-Billing in China in RMB");
Cell cell12_12 = row12.createCell(12);
cell12_12.setCellStyle(setNolyBorderStyle(workbook));
sheet.addMergedRegion(new CellRangeAddress(12, 12, 11, 12));
//设置14行到15行前5列内容
Row row13 = sheet.createRow(13);
Row row14 = sheet.createRow(14);
for (int colNum = 0; colNum < 5; colNum++) {
Cell cell = row13.createCell(colNum);
cell.setCellStyle(setWordStyle(workbook, true));
cell.setCellValue(mergerDetailTableFiled[colNum]);
Cell cell1 = row14.createCell(colNum);
cell1.setCellStyle(setWordStyle(workbook, true));
cell1.setCellValue(mergerDetailTableFiled[colNum]);
sheet.addMergedRegion(new CellRangeAddress(13, 14, colNum, colNum));
}
//设置14行第六列到第13列的内容
String[] mergerDetailTableFiled02 = new String[]{"Discount Requested (%)", "Discount SST approved (%)", "Net Price to AT&T Customer", "Royalty to SST without Tax(contra-revenue)"};
Cell cell13_5 = row13.createCell(5);
cell13_5.setCellStyle(setWordStyle(workbook, true));
cell13_5.setCellValue(mergerDetailTableFiled02[0]);
Cell cell113_6 = row13.createCell(6);
cell113_6.setCellStyle(setWordStyle(workbook, true));
cell113_6.setCellValue(mergerDetailTableFiled02[0]);
sheet.addMergedRegion(new CellRangeAddress(13, 13, 5, 6));
Cell cell13_7 = row13.createCell(7);
cell13_7.setCellStyle(setWordStyle(workbook, true));
cell13_7.setCellValue(mergerDetailTableFiled02[1]);
Cell cell13_8 = row13.createCell(8);
cell13_8.setCellStyle(setWordStyle(workbook, true));
cell13_8.setCellValue(mergerDetailTableFiled02[1]);
sheet.addMergedRegion(new CellRangeAddress(13, 13, 7, 8));
Cell cell13_9 = row13.createCell(9);
cell13_9.setCellStyle(setWordStyle(workbook, true));
cell13_9.setCellValue(mergerDetailTableFiled02[2]);
Cell cell13_10 = row13.createCell(10);
cell13_10.setCellStyle(setWordStyle(workbook, true));
cell13_10.setCellValue(mergerDetailTableFiled02[2]);
sheet.addMergedRegion(new CellRangeAddress(13, 13, 9, 10));
Cell cell13_11 = row13.createCell(11);
cell13_11.setCellStyle(setWordStyle(workbook, true));
cell13_11.setCellValue(mergerDetailTableFiled02[3]);
Cell cell13_12 = row13.createCell(12);
cell13_12.setCellStyle(setWordStyle(workbook, true));
cell13_12.setCellValue(mergerDetailTableFiled02[3]);
sheet.addMergedRegion(new CellRangeAddress(13, 13, 11, 12));
//设置15行第六列到第13列的内容
String[] mergerDetailTableFiled03 = new String[]{"OTC", "MRC", "OTC", "MRC", "OTC in RMB", "MRC in RMB", "OTC in RMB", "MRC in RMB"};
for (int colNum = 5; colNum < 13; colNum++) {
Cell cell = row14.createCell(colNum);
for (int num = 0; num < 8; num++) {
cell.setCellStyle(setWordStyle(workbook, true));
cell.setCellValue(mergerDetailTableFiled03[num]);
}
}
List<Map<String, String>> siteMap = new ArrayList<>();//用来存放site的类型和每个site的状态
String getSiteSql = "select site ,count(site) siteNum from uf_avpn_export_dt1 where mainId =? group by site ;";
RecordSet rs02 = new RecordSet();
boolean b = rs02.executeQuery(getSiteSql, mainId);
logger.info("查询site以及site的数量的sql语句===" + b);
while (rs02.next()) {
HashMap<String, String> map = new HashMap<>();
String site = rs02.getString("site");
String siteNum = rs02.getString("siteNum");
map.put(site, siteNum);
siteMap.add(map);//将site的类型和每个site的类型的个数放到siteList数组中用来循环遍历再放入单元格中
}
int firstCow = 15;//从第15行开始写入明细表固定死
int finalyCow = 0;
Double sum9 = 0D;
Double sum10 = 0D;
Double sum11 = 0D;
Double sum12 = 0D;
for (Map<String, String> map : siteMap) {
Set<String> siteKeySet = map.keySet();
List<List<Object>> detailDateList = new ArrayList();
int cowLength = 0;//每一个site类型的数据的长度
for (String s1 : siteKeySet) {
cowLength = Integer.parseInt(map.get(s1));//每一个site类型的数据的长度
String getDetailDataSql = "select * from uf_avpn_export_dt1 where mainId =? and site = ?";
rs02.executeQuery(getDetailDataSql, mainId, s1);
while (rs02.next()) {
String site = rs02.getString("site");//Site ID
String itemId = rs02.getString("Item");//Description
String getItemValueSql = "select item from uf_pd_item_inf where id =?";
RecordSet recordSet1 = new RecordSet();
boolean b2 = recordSet1.executeQuery(getItemValueSql, itemId);
logger.info("查询item值sql是否执行成功=" + b2);
String item = "";//从数据库中查询到item的值
if (recordSet1.next()) {
item = recordSet1.getString("Item");
}
String categoryId = rs02.getString("Category");//Bandwidth
String getCategoryByIdSql = "select category from uf_pd_category_inf where id = ?";
boolean b3 = recordSet1.executeQuery(getCategoryByIdSql, categoryId);
logger.info("查询categorySql是否执行成功==" + b3);
String category = "";
if (recordSet1.next()) {
category = recordSet1.getString("category");
}
String otc_price = rs02.getString("otc_price");//OTC in RMB
String mrc_price = rs02.getString("mrc_price");//MRC in RMB
String otcdiscountrequest = rs02.getString("otcdiscountrequest");//Discount Requested (%) OTC
String mrcdiscountrequest = rs02.getString("mrcdiscountrequest");//Discount Requested (%) MRC
String otcdiscountapproved = rs02.getString("otcdiscountapproved");//Discount SST approved (%) OTC
String mrcdiscountapproved = rs02.getString("mrcdiscountapproved");//Discount SST approved (%)MRC
String otc_tariff = rs02.getString("otc_tariff");//Net Price to AT&T Customer(OTC in RMB)
String mrc_tariff = rs02.getString("mrc_tariff");//Net Price to AT&T Customer(MRC in RMB)
String otc_scale = rs02.getString("otc_scale");//Royalty to SST without Tax (contra-revenue)(OTC in RMB)
String mrc_scale = rs02.getString("mrc_scale");//Royalty to SST without Tax (contra-revenue)(MRC in RMB)
ArrayList<Object> list = new ArrayList<>();
list.add(site);//list.add(site);
list.add(item);
list.add(category);
list.add(otc_price);
list.add(mrc_price);
list.add(otcdiscountrequest);
list.add(mrcdiscountrequest);
list.add(otcdiscountapproved);
list.add(mrcdiscountapproved);
list.add(otc_tariff);
list.add(mrc_tariff);
list.add(otc_scale);
list.add(mrc_scale);
detailDateList.add(list);
}
}
Double amount9 = 0D;//用来统计每一个site种类的 Grand-Total(RMB)
Double amount10 = 0D;
Double amount11 = 0D;
Double amount12 = 0D;
//得到list类型的数据、有得到每种site类型的数据长度
//开始向excel中导入明细表中的数据
int i = 0;//用它来增加detailDateList的下标
for (int cowNum = firstCow; cowNum < firstCow + cowLength; cowNum++) {
Row row = sheet.createRow(cowNum);
for (int colNum = 0; colNum < 13; colNum++) {
if (colNum == 9) {
String s = String.valueOf(detailDateList.get(i).get(colNum));
if (s!=""){
Double value = Double.valueOf(s);
amount9 = Double.valueOf(amount9) + value;
sum9 += value;
Cell cell = row.createCell(colNum);
cell.setCellType(CellType.NUMERIC);
cell.setCellStyle(setCellToNumber(workbook));
cell.setCellValue(value);
}else{
Cell cell = row.createCell(colNum);
cell.setCellStyle(setNolyBorderStyle(workbook));
}
} else if (colNum == 10) {
String s = String.valueOf(detailDateList.get(i).get(colNum));
if (s!=""){
Double value = Double.valueOf(s);
amount10 = amount10 + value;
sum10 += value;
Cell cell = row.createCell(colNum);
cell.setCellType(CellType.NUMERIC);
cell.setCellStyle(setCellToNumber(workbook));
cell.setCellValue(value);
}else {
Cell cell = row.createCell(colNum);
cell.setCellType(CellType.NUMERIC);
cell.setCellStyle(setCellToNumber(workbook));
}
} else if (colNum == 11) {
String s = String.valueOf(detailDateList.get(i).get(colNum));
if (s!=""){
Double value = Double.valueOf(s);
amount11 = amount11 + value;
sum11 += value;
Cell cell = row.createCell(colNum);
cell.setCellType(CellType.NUMERIC);
cell.setCellStyle(setCellToNumber(workbook));
cell.setCellValue(value);
}else {
Cell cell = row.createCell(colNum);
cell.setCellStyle(setNolyBorderStyle(workbook));
}
} else if (colNum == 12) {
String s = String.valueOf(detailDateList.get(i).get(colNum));
if (s!=""){
Double value = Double.valueOf(s);
amount12 = amount12 + value;
sum12 += value;
Cell cell = row.createCell(colNum);
cell.setCellType(CellType.NUMERIC);
cell.setCellStyle(setCellToNumber(workbook));
cell.setCellValue(value);
}else {
Cell cell = row.createCell(colNum);
cell.setCellStyle(setNolyBorderStyle(workbook));
}
}
if (colNum == 5 || colNum == 6 || colNum == 7 || colNum == 8) {
Cell cell = row.createCell(colNum);
String s = String.valueOf(detailDateList.get(i).get(colNum));
if (s!=""){
Double value = Double.valueOf(s);
cell.setCellStyle(setWordStyle(workbook));
cell.setCellValue( (int)(value*100) + "%");
}else {
cell.setCellStyle(setNolyBorderStyle(workbook));
}
}
else if (colNum == 0||colNum==1||colNum==2){
Cell cell = row.createCell(colNum);
String value = String.valueOf(detailDateList.get(i).get(colNum));
if (value!=""){
cell.setCellStyle(setWordStyle(workbook));
cell.setCellValue(value);
}else {
cell.setCellStyle(setNolyBorderStyle(workbook));
}
}else if (colNum == 3||colNum==4){
String s = String.valueOf(detailDateList.get(i).get(colNum));
if (s!=""){
Double value = Double.valueOf(s);
Cell cell = row.createCell(colNum);
cell.setCellType(CellType.NUMERIC);
cell.setCellStyle(setCellToNumber(workbook));
cell.setCellValue(value);
}else {
Cell cell = row.createCell(colNum);
cell.setCellStyle(setNolyBorderStyle(workbook));
}
}
}
i++;
}
//合并单元格
int lastCow = firstCow + cowLength;
if (cowLength > 1) {
sheet.addMergedRegion(new CellRangeAddress(firstCow, lastCow - 1, 0, 0));
}
//开始写入Sub-Total(RMB)
Row rowSubTotal = sheet.createRow(lastCow);
for (int colNum = 8; colNum < 13; colNum++) {
if (colNum == 8) {
Cell cellSubToTal = rowSubTotal.createCell(colNum);
cellSubToTal.setCellStyle(setNolyWordStyle(workbook, true));
cellSubToTal.setCellValue("Sub-Total(RMB)");
} else if (colNum == 9) {
Cell cellSubToTal = rowSubTotal.createCell(colNum);
cellSubToTal.setCellStyle(setNolyWordStyle(workbook, true));
cellSubToTal.setCellValue(amount9);
} else if (colNum == 10) {
Cell cellSubToTal = rowSubTotal.createCell(colNum);
cellSubToTal.setCellStyle(setNolyWordStyle(workbook, true));
cellSubToTal.setCellValue(amount10);
} else if (colNum == 11) {
Cell cellSubToTal = rowSubTotal.createCell(colNum);
cellSubToTal.setCellStyle(setNolyWordStyle(workbook, true));
cellSubToTal.setCellValue(amount11);
} else if (colNum == 12) {
Cell cellSubToTal = rowSubTotal.createCell(colNum);
cellSubToTal.setCellStyle(setNolyWordStyle(workbook, true));
cellSubToTal.setCellValue(amount12);
}
}
firstCow = firstCow + cowLength + 1;
finalyCow = firstCow;
}//处理插入数据以及合并单元格
//处理明细表最后一行的统计情况
Row row_Grand_Total_RMB = sheet.createRow(finalyCow);
for (int colNum = 8; colNum < 13; colNum++) {
if (colNum == 8) {
Cell cell = row_Grand_Total_RMB.createCell(colNum);
cell.setCellStyle(setNolyWordStyle(workbook,true));
cell.setCellValue("Grand-Total(RMB)");
} else if (colNum == 9) {
Cell cell = row_Grand_Total_RMB.createCell(colNum);
cell.setCellStyle(setNolyWordStyle(workbook,true));
cell.setCellValue(sum9);
} else if (colNum == 10) {
Cell cell = row_Grand_Total_RMB.createCell(colNum);
cell.setCellStyle(setNolyWordStyle(workbook,true));
cell.setCellValue(sum10);
} else if (colNum == 11) {
Cell cell = row_Grand_Total_RMB.createCell(colNum);
cell.setCellStyle(setNolyWordStyle(workbook,true));
cell.setCellValue(sum11);
} else if (colNum == 12) {
Cell cell = row_Grand_Total_RMB.createCell(colNum);
cell.setCellStyle(setNolyWordStyle(workbook,true));
cell.setCellValue(sum12);
}
}
//输入最下面的固定值
String[] lastStringLeft = new String[]{
"Notes:",
"1.This is NOT AT&T Pricing Approval. Forward this SST quote to your Lead ICB Pricer to issue the official Rate Letter",
"2.Access price must be quoted by SST, request SST as \"access provider\" in Igloo. Do NOT use quote from other access providers",
"3.Discount is only applicable to the demand set listed below, any changes in bandwidth/design/billing model, need to re-submit to SST and AT&T Pricer for approval", "" +
"4.Under in-country billing, AT&T must follow the discount% offered by SST. Do NOT deviate from the SST quoted discount.",
"5.SST can support repeatable discount for the same solution design(port/CDR/CPE) of same contract term which was approved already. Local Loop part are non-repeatable.",
"6.For ICB only - Royalty in columns L and M are contra-revenue,ICB to verify if BCRL captured these amount.",
"7.Save this SST approved quote to eCRM (without release) for record.",
"8.The quotation shall be exclusive of any applicable taxes and Customer shall pay all the taxes and charges relating to the payment if any. The VAT rate for CPE and related modules and license are 16%, the rest is 6%.",
"9.Thank you for considering SST, this engagement may be subject to a credit check, we appreciate your cooperation and look forward to doing business in the near future."
};
int lastLineTag = finalyCow + 2;//固定值Note所在的行
int k = 0;//用来增加lastStringLeft的下标
for (int rowNum = lastLineTag; rowNum < lastLineTag + 10; rowNum++) {
Row row = sheet.createRow(rowNum);
Cell cell = row.createCell(0);
cell.setCellStyle(setNolyWordStyle(workbook,true));
sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 0, 11));
cell.setCellStyle(setTotalStyle(workbook,"微软雅黑",true,false,HorizontalAlignment.LEFT, VerticalAlignment.CENTER,false));
cell.setCellValue(lastStringLeft[k]);
k++;
}
sheet.autoSizeColumn(0,true);
sheet.setColumnWidth(0,sheet.getColumnWidth(0)*5/10);
// 设置自动列宽
for (int num = 1; num < 13; num++) {
sheet.autoSizeColumn(num,true);
sheet.setColumnWidth(num, (sheet.getColumnWidth(num)));
}
InputStream inputStream = workbookConvertorStream(workbook);
byte[] bytes = IOUtils.toByteArray(inputStream);
output = outputStream -> {
outputStream.write(bytes);
outputStream.close();
};
header = Response.ok(output,MediaType.APPLICATION_OCTET_STREAM)
.type("application/xlsx")
.header("Content-Disposition",
"attachment; filename=\""+ new String("AVPN表单.xlsx".getBytes("GBK"), StandardCharsets.ISO_8859_1)+"\"");
} catch (Exception e) {
e.printStackTrace();
logger.error("导出excel错误" + Util.getErrString(e));
}
build = header.build();
return build;
}
/**
* <h2></h2>
* @param
* @return
* @author hcy
* @Date 2023/2/13 18:08
*/
public static CellStyle setTotalStyle(Workbook workbook,String type,boolean setBold,boolean setBorder,HorizontalAlignment horizontalDirection,VerticalAlignment verticalAlignmentDirection ,boolean setDataFormat){
//new一个cellStyle用于设置每一个单元格样式
CellStyle cellStyle = workbook.createCellStyle();
Font font = workbook.createFont();
//颜色
font.setColor(Font.COLOR_NORMAL);
//设置字体大小
font.setFontHeightInPoints((short) 10);
//字体
font.setFontName(type);
//设置字体是否加粗
font.setBold(setBold);
//将字体样式渲染cellStyle对象中
cellStyle.setFont(font);
if (setBorder == true){
//设置样式对象,这里仅设置了边框属性
cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
cellStyle.setBorderTop(BorderStyle.THIN);//上边框
cellStyle.setBorderRight(BorderStyle.THIN);//右边框
}
//水平居中
cellStyle.setAlignment(horizontalDirection);
//垂直居中
cellStyle.setVerticalAlignment(verticalAlignmentDirection);
//此处设置数据格式
if(setDataFormat == true){
DataFormat dataFormat = workbook.createDataFormat();
cellStyle.setDataFormat(dataFormat.getFormat("0.00_ "));
}
return cellStyle;
}
/**
* ,
*
* @param workbook
* @return
*/
public static CellStyle setWordStyle(Workbook workbook, Boolean setBold) {
// 设置字体
CellStyle cellStyle = workbook.createCellStyle();
//设置样式对象,这里仅设置了边框属性
cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
cellStyle.setBorderTop(BorderStyle.THIN);//上边框
cellStyle.setBorderRight(BorderStyle.THIN);//右边框
Font font = workbook.createFont();
//颜色
font.setColor(Font.COLOR_NORMAL);
//设置字体大小
font.setFontHeightInPoints((short) 10);
//字体
font.setFontName("微软雅黑");
font.setBold(setBold);
cellStyle.setFont(font);
return cellStyle;
}
/**
*
*
* @param workbook
* @return
*/
public static CellStyle setWordStyle(Workbook workbook) {
// 设置字体
CellStyle cellStyle = workbook.createCellStyle();
//设置样式对象,这里仅设置了边框属性
cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
cellStyle.setBorderTop(BorderStyle.THIN);//上边框
cellStyle.setBorderRight(BorderStyle.THIN);//右边框
Font font = workbook.createFont();
//颜色
font.setColor(Font.COLOR_NORMAL);
//设置字体大小
font.setFontHeightInPoints((short) 10);
//字体
font.setFontName("微软雅黑");
cellStyle.setFont(font);
return cellStyle;
}
/**
*
*
* @param workbook
* @return
*/
public static CellStyle setNolyBorderStyle(Workbook workbook) {
// 设置字体
CellStyle cellStyle = workbook.createCellStyle();
//设置样式对象,这里仅设置了边框属性
cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
cellStyle.setBorderTop(BorderStyle.THIN);//上边框
cellStyle.setBorderRight(BorderStyle.THIN);//右边框
return cellStyle;
}
/**
*
*
* @param workbook
* @return
*/
public static CellStyle setNolyWordStyle(Workbook workbook,Boolean setBold) {
// 设置字体
CellStyle cellStyle = workbook.createCellStyle();
Font font = workbook.createFont();
//颜色
font.setColor(Font.COLOR_NORMAL);
//设置字体大小
font.setFontHeightInPoints((short) 10);
//字体
font.setFontName("微软雅黑");
font.setBold(setBold);
cellStyle.setFont(font);
DataFormat dataFormat = workbook.createDataFormat();//此处设置数据格式
cellStyle.setDataFormat(dataFormat.getFormat("0.00_ "));
return cellStyle;
}
/**
* SXSSFWorkbook InputStream
* @param workbook
* @return
*/
public static InputStream workbookConvertorStream(Workbook workbook) {
InputStream in = null;
try{
//临时缓冲区
ByteArrayOutputStream out = new ByteArrayOutputStream();
//创建临时文件
workbook.write(out);
byte [] bookByteAry = out.toByteArray();
in = new ByteArrayInputStream(bookByteAry);
}
catch (Exception e){
e.printStackTrace();
}
return in;
}
/**
*
* @param workbook
* @return
*/
public static CellStyle setCellToNumber(Workbook workbook){
// 设置字体
CellStyle cellStyle = workbook.createCellStyle();
//设置样式对象,这里仅设置了边框属性
cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
cellStyle.setBorderTop(BorderStyle.THIN);//上边框
cellStyle.setBorderRight(BorderStyle.THIN);//右边框
Font font = workbook.createFont();
//颜色
font.setColor(Font.COLOR_NORMAL);
//设置字体大小
font.setFontHeightInPoints((short) 10);
//字体
font.setFontName("微软雅黑");
cellStyle.setFont(font);
DataFormat dataFormat = workbook.createDataFormat();//此处设置数据格式
cellStyle.setDataFormat(dataFormat.getFormat("0.00_ "));
return cellStyle;
}
}

View File

@ -0,0 +1,581 @@
package com.api.chaoyang.he.hcy_xintiantongxin;
import aiyh.utils.Util;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import weaver.conn.RecordSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
@Path("/hcy_xintiantongxin2")
public class ExportExcelL3NSApi {
private Logger logger = Util.getLogger();
/**
*
* @param request
* @param response
* @return
*/
@GET
@Path("/ExportExcelL3NSApi")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response checkRequestInfo(@Context HttpServletRequest request, @Context HttpServletResponse response){
Response build = null;
Response.ResponseBuilder header = null;
try {
//查询到 uf_L3NS_exportt AVPN导出表中的一条数据的ids
String ids = request.getParameter("ids");
RecordSet rs = new RecordSet();
StreamingOutput output = null;
String sql_L3NS_db = "select * from uf_L3NS_exportt where id = ?";
boolean b = rs.executeQuery(sql_L3NS_db, ids);
logger.info("是否查询uf_L3NS_exportt=="+b);
//创建一个工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
String[] dbFiled = new String[13];
String[] tableFiled = new String[]{"Service Type", "Billing Model", "Customer Name", "ROME Opportunity #", "ROME WR#", "Request Type", "Original SST Order NO", "Request Justification", "Special Request to SST", "Target Service Provision date","ATT Order number:","SST Pre-Sales Contact","Date of SST Approval"};
String mainId = "";
//得到主表数据用来写入excel中的固定值
while (rs.next()) {
//查询到uf_avpn_export表的id作为查询明细表的mainid
mainId = rs.getString("id");
String pd_nameId = rs.getString("pd_name");//uf_pd_base_inf 得到的pd_name是字段的id
RecordSet recordSet = new RecordSet();
String getPd_nameByIdSql = "select pd_name from uf_pd_base_inf where id =?";
boolean b1 = recordSet.executeQuery(getPd_nameByIdSql, pd_nameId);
logger.info("查询pd_namesql语句是否成功==" + b1);
if (recordSet.next()) {
String pd_name = recordSet.getString("pd_name");//查询到pd_name的值
dbFiled[0] = pd_name;
}
String billingmodel = rs.getString("billingmodel");//Billing Model===billingmodel
if (billingmodel.equals("0")){
billingmodel = "Custom Subcontract";
}else if (billingmodel == null){
billingmodel = "";
}
dbFiled[1] = billingmodel;
String customer_name = rs.getString("customer_name");//Customer Name===customer_name
dbFiled[2] = customer_name;
String rome_opportunity = rs.getString("rome_opportunity");//ROME Opportunity #====rome_opportunity
dbFiled[3] = rome_opportunity;
String rome_wr = rs.getString("rome_wr");//ROME WR#====rome_wr
dbFiled[4] = rome_wr;
String request_type = rs.getString("request_type");//Request Type====request_type
dbFiled[5] = request_type;
String original_sst_order_no = rs.getString("original_sst_order_no");//Original SST Order NO====original_sst_order_no
dbFiled[6] = original_sst_order_no;
String requestjustificationoptional = rs.getString("requestjustificationoptional");//Request Justification====requestjustificationoptional
dbFiled[7] = requestjustificationoptional;
String specialrequesttosstoptional = rs.getString("specialrequesttosstoptional");//Special Request to SST====specialrequesttosstoptional
dbFiled[8] = specialrequesttosstoptional;
String targetserviceprovisiondate = rs.getString("targetserviceprovisiondate");//Target Service Provision date====targetserviceprovisiondate
dbFiled[9] = targetserviceprovisiondate;
String attordernumber = rs.getString("attordernumber");//ATT Order number:====attordernumber
dbFiled[10] = attordernumber;
String sstpresalescontact = rs.getString("sstpresalescontact");//SST Pre-Sales Contact====sstpresalescontact
dbFiled[11] = sstpresalescontact;
String dateofsstapproval = rs.getString("dateofsstapproval");//Date of SST Approval====dateofsstapproval
dbFiled[12] = dateofsstapproval;
}
//开始写入固定值
Sheet sheet = workbook.createSheet();
workbook.setSheetName(0, "L3NS表单");
sheet.autoSizeColumn(1, true);
for (int cowNum=1;cowNum<14;cowNum++){
Row row = sheet.createRow(cowNum);
Cell cell0 = row.createCell(0);
cell0.setCellStyle(setWordStyle(workbook,true));
cell0.setCellValue(tableFiled[cowNum-1]);
Cell cell1 = row.createCell(1);
cell1.setCellStyle(setWordStyle(workbook,true));
cell1.setCellValue(dbFiled[cowNum-1]);
}
Row row14 = sheet.createRow(14);//写入15行数据
Cell cell14_0 = row14.createCell(0);
Cell cell114_1 = row14.createCell(1);
cell14_0.setCellStyle(setWordStyle(workbook,true));
cell14_0.setCellValue("This Quote is valid for 180 days from \"Date of SST approval\"");
cell114_1.setCellStyle(setWordStyle(workbook,true));
cell114_1.setCellValue("This Quote is valid for 180 days from \"Date of SST approval\"");
sheet.addMergedRegion(new CellRangeAddress(14, 14, 0, 1));
// 写入18行19行数据
// Site ID Description Bandwidth OTC in RMB MRC in RMB
String[] detailFileds = new String[]{"Site ID", "Description","Bandwidth", "OTC in RMB", "MRC in RMB"};
Row row17 = sheet.createRow(17);
Row row18 = sheet.createRow(18);
for (int colNum =0;colNum<5;colNum++){
Cell cell = row17.createCell(colNum);
cell.setCellStyle(setWordStyle(workbook));
cell.setCellStyle(setWordStyle(workbook,true));
cell.setCellValue(detailFileds[colNum]);
}
for (int colNum =0;colNum<5;colNum++){
Cell cell = row18.createCell(colNum);
cell.setCellStyle(setWordStyle(workbook));
cell.setCellStyle(setWordStyle(workbook,true));
cell.setCellValue(detailFileds[colNum]);
}
sheet.addMergedRegion(new CellRangeAddress(17, 18, 0, 0));
sheet.addMergedRegion(new CellRangeAddress(17, 18, 1, 1));
sheet.addMergedRegion(new CellRangeAddress(17, 18, 2, 2));
sheet.addMergedRegion(new CellRangeAddress(17, 18, 3, 3));
sheet.addMergedRegion(new CellRangeAddress(17, 18, 4, 4));
// Relief Requested (%) Relief SST approved (%) Tariff to SST (include VAT) 6-11列
// OTC MRC OTC MRC OTC in RMB MRC in RMB
String[] detailFileds01 = new String[]{"Relief Requested (%)","Relief Requested (%)","Relief SST approved (%)","Relief SST approved (%)","Tariff to SST (include VAT)","Tariff to SST (include VAT)",""};
String[] detailFileds02 = new String[]{"OTC","MRC","OTC","MRC","OTC in RMB","MRC in RMB"};
int i=0;
int j = 0;
for(int colNum=5;colNum<11;colNum++){
Cell cell17 = row17.createCell(colNum);
Cell cell18 = row18.createCell(colNum);
cell17.setCellValue(detailFileds01[i]);
cell17.setCellStyle(setWordStyle(workbook,true));
// cell17.setCellStyle(setCellStyleFrame(workbook));
cell18.setCellValue(detailFileds02[j]);
cell18.setCellStyle(setWordStyle(workbook,true));
// cell18.setCellStyle(setCellStyleFrame(workbook));
i++;
j++;
}
//合并18行19行
sheet.addMergedRegion(new CellRangeAddress(17, 17, 5, 6));
sheet.addMergedRegion(new CellRangeAddress(17, 17, 7, 8));
sheet.addMergedRegion(new CellRangeAddress(17, 17, 9, 10));
//开始写入20开始一直往下的动态表格中的内容
//
List<Map<String, String>> siteMap = new ArrayList<>();//用来存放site的类型和每个site的状态
String getSiteSql = "select site ,count(site) siteNum from uf_L3NS_exportt_dt1 where mainId =? group by site ;";
RecordSet rs02 = new RecordSet();
boolean b1 = rs02.executeQuery(getSiteSql, mainId);
logger.info("查询site以及site的数量的sql语句===" + b1);
while (rs02.next()) {
HashMap<String, String> map = new HashMap<>();
String site = rs02.getString("site");
String siteNum = rs02.getString("siteNum");
map.put(site, siteNum);
siteMap.add(map);//将site的类型和每个site的类型的个数放到siteList数组中用来循环遍历再放入单元格中
}
int firstCow = 19;//从第15行开始写入明细表固定死
int finalyCow = 0;
Double sum10 = 0D;//用来统计所有site种类的Grand-Total (RMB) #REF!
Double sum9 = 0D;
for (Map<String, String> map : siteMap) {
Set<String> siteKeySet = map.keySet();
List<List<Object>> detailDateList = new ArrayList();
int cowLength = 0;//每一个site类型的数据的长度
for (String s1 : siteKeySet) {
cowLength = Integer.parseInt(map.get(s1));//每一个site类型的数据的长度
String getDetailDataSql = "select * from uf_L3NS_exportt_dt1 where mainId =? and site = ?";
rs02.executeQuery(getDetailDataSql,mainId,s1);
while(rs02.next()){
//明细表数据
String site = rs02.getString("site");//Site ID
String itemId = rs02.getString("Item");//Description
String getItemValueSql = "select item from uf_pd_item_inf where id =?";//uf_pd_item_inf
RecordSet recordSet1 = new RecordSet();
boolean b2 = recordSet1.executeQuery(getItemValueSql, itemId);
logger.info("查询item值sql是否执行成功=" + b2);
String item = "";//从数据库中查询到item的值
if (recordSet1.next()) {
item = recordSet1.getString("Item");
}
String categoryId = rs02.getString("Category");//Bandwidth
String getCategoryByIdSql = "select category from uf_pd_category_inf where id = ?";//uf_pd_category_inf
boolean b3 = recordSet1.executeQuery(getCategoryByIdSql, categoryId);
logger.info("查询categorySql是否执行成功==" + b3);
String category = "";
if (recordSet1.next()) {
category = recordSet1.getString("category");
}
String otc_price = rs02.getString("otc_price");//OTC in RMB
String mrc_price = rs02.getString("mrc_price");//MRC in RMB
String otc_relief_requested = rs02.getString("otc_relief_requested");//Relief Requested (%)OTC
String mrc_relief_requested = rs02.getString("mrc_relief_requested");//Relief Requested (%)MRC
String otc_relief_approved = rs02.getString("otc_relief_approved");//Relief SST approved (%)OTC
String mrc_relief_approved = rs02.getString("mrc_relief_approved");//Relief SST approved (%)MRC
String otc_tariff = rs02.getString("otc_tariff");//Tariff to SST (include VAT)(OTC in RMB)
String mrc_tariff = rs02.getString("mrc_tariff");//Tariff to SST (include VAT)(MRC in RMB)
ArrayList<Object> list = new ArrayList<>();
list.add(site);
list.add(item);
list.add(category);
list.add(otc_price);
list.add(mrc_price);
list.add(otc_relief_requested);
list.add(mrc_relief_requested);
list.add(otc_relief_approved);
list.add(mrc_relief_approved);
list.add(otc_tariff);
list.add(mrc_tariff);
detailDateList.add(list);
}
}
Double amount10 = 0D;//用来统计每一个site种类的 Sub-Total
Double amount9 = 0D;
//得到list类型的数据、有得到每种site类型的数据长度
//开始向excel中导入明细表中的数据
int k = 0;//用它来增加detailDateList的下标
for (int cowNum = firstCow; cowNum < firstCow + cowLength; cowNum++) {
Row row = sheet.createRow(cowNum);
for (int colNum = 0; colNum < 11; colNum++) {
if (colNum == 10) {
String s = String.valueOf(detailDateList.get(k).get(colNum));
if (s != ""){
Double value = Double.valueOf(s);
amount10 = amount10 + value;
sum10 += value;
Cell cell = row.createCell(colNum);
cell.setCellStyle(setCellToNumber(workbook));
cell.setCellValue(value);
}else {
Cell cell = row.createCell(colNum);
cell.setCellStyle(setNolyBorderStyle(workbook));
}
}else if (colNum == 9){
String s = String.valueOf(detailDateList.get(k).get(colNum));
if (s != "") {
Double value = Double.valueOf(s);
amount9 = amount9 + value;
sum9 += value;
Cell cell = row.createCell(colNum);
cell.setCellStyle(setCellToNumber(workbook));
cell.setCellValue(value);
}else {
Cell cell = row.createCell(colNum);
cell.setCellStyle(setNolyBorderStyle(workbook));
}
}
if (colNum == 5 || colNum == 6 || colNum == 7 || colNum == 8) {
Cell cell = row.createCell(colNum);
String s = String.valueOf(detailDateList.get(k).get(colNum));
if (s!=""){
Double value = Double.valueOf(s);
cell.setCellStyle(setWordStyle(workbook));
cell.setCellValue((int)(value * 100) + "%");
}else {
cell.setCellStyle(setNolyBorderStyle(workbook));
}
} else if (colNum == 3 || colNum==4){
String s = String.valueOf(detailDateList.get(k).get(colNum));
if (s != ""){
Double value = Double.valueOf(s);
Cell cell = row.createCell(colNum);
cell.setCellStyle(setCellToNumber(workbook));
cell.setCellValue(value);
}else {
Cell cell = row.createCell(colNum);
cell.setCellStyle(setNolyBorderStyle(workbook));
}
}
else if (colNum == 0||colNum==1||colNum==2){
Cell cell = row.createCell(colNum);
String value = String.valueOf(detailDateList.get(k).get(colNum));
if (value!=""){
cell.setCellStyle(setWordStyle(workbook));
cell.setCellValue(value);
}else {
cell.setCellStyle(setNolyBorderStyle(workbook));
}
}
}
k++;
}
//合并单元格
int lastCow = firstCow + cowLength;
if (cowLength > 1) {
sheet.addMergedRegion(new CellRangeAddress(firstCow, lastCow - 1, 0, 0));
}
//开始写入Sub-Total(RMB)
Row rowSubTotal = sheet.createRow(lastCow);
Cell cellSubToTal10 = rowSubTotal.createCell(10);
cellSubToTal10.setCellStyle(setNolyWordStyle(workbook,true));
cellSubToTal10.setCellValue(amount10);
Cell cellSubToTal9 = rowSubTotal.createCell(9);
cellSubToTal9.setCellStyle(setNolyWordStyle(workbook,true));
cellSubToTal9.setCellValue(amount9);
Cell cellSubToTal8 = rowSubTotal.createCell(8);
cellSubToTal8.setCellStyle(setNolyWordStyle(workbook,true));
cellSubToTal8.setCellValue("Sub-Total");
firstCow = firstCow + cowLength + 1;
finalyCow = firstCow;
}
Row rowFinalyCow = sheet.createRow(finalyCow);
Cell cellFinalyCow_10 = rowFinalyCow.createCell(10);
cellFinalyCow_10.setCellStyle(setNolyWordStyle(workbook,true));
cellFinalyCow_10.setCellValue(sum10);
Cell cellFinalyCow_9 = rowFinalyCow.createCell(9);
cellFinalyCow_9.setCellStyle(setNolyWordStyle(workbook,true));
cellFinalyCow_9.setCellValue(sum9);
Cell cellFinalyCow_8 = rowFinalyCow.createCell(8);
cellFinalyCow_8.setCellStyle(setNolyWordStyle(workbook,true));
cellFinalyCow_8.setCellValue("Grand-Total (RMB)");
//输入最下面的固定值
String[] lastStringright = new String[]{
"Notes:",
"1.This form should be submitted by Lead ICB via a ROME WR to \"AP Pricing Team\". Do NOT send this direct to SST.",
"2.Fill in full Site Address or at least provide Name of City.",
"3.Tariff relief requested should be calculated by lead ICB. Do not fill in target AVPN discount as \"Relief Requested\".",
"4.VPN Tariff is port base pricing, no charges on COS or COS profile.",
"5.AT&T GAM will place order to SST on no longer than 12 months term for Custom Subcontract, regardless of the commitment term between end customer and AT&T.",
"6.China VAT is irrecoverable cost to AT&T under custom subcontracting billing. Ttariff and Access price listed below is VAT inclusive. ICB has to cater for this cost in deal financials.",
"7.Tariff Relief is only applicable to the demand set listed below, any changes in bandwidth/design/billing model, need to re-submit to SST for approval;",
};
int lastLineTag = finalyCow + 2;//固定值Note所在的行
int h=0;
for (int rowNum=lastLineTag;rowNum<lastLineTag+8;rowNum++){
Row row = sheet.createRow(rowNum);
Cell cell = row.createCell(0);
cell.setCellStyle(setNolyWordStyle(workbook,true));
sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 0, 11));
cell.setCellValue(lastStringright[h]);
h++;
}
sheet.autoSizeColumn(0,true);
sheet.setColumnWidth(0,sheet.getColumnWidth(0)*4/10);
// 设置自动列宽
for (int num = 1; num < 11; num++) {
sheet.autoSizeColumn(num,true);
sheet.setColumnWidth(num, (sheet.getColumnWidth(num)));
}
InputStream inputStream = workbookConvertorStream(workbook);
byte[] bytes = IOUtils.toByteArray(inputStream);
output = outputStream -> {
outputStream.write(bytes);
outputStream.close();
};
header = Response.ok(output,MediaType.APPLICATION_OCTET_STREAM)
.type("application/xlsx")
.header("Content-Disposition",
"attachment; filename=\""+ new String("L3NS表单.xlsx".getBytes("GBK"), StandardCharsets.ISO_8859_1)+"\"");
} catch (IOException e) {
logger.error("导出excel错误" + Util.getErrString(e));
e.printStackTrace();
}
build = header.build();
return build;
}
/**
* ,
*
* @param workbook
* @return
*/
public static CellStyle setWordStyle(Workbook workbook, Boolean setBold) {
// 设置字体
CellStyle cellStyle = workbook.createCellStyle();
//设置样式对象,这里仅设置了边框属性
cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
cellStyle.setBorderTop(BorderStyle.THIN);//上边框
cellStyle.setBorderRight(BorderStyle.THIN);//右边框
Font font = workbook.createFont();
//颜色
font.setColor(Font.COLOR_NORMAL);
//设置字体大小
font.setFontHeightInPoints((short) 10);
//字体
font.setFontName("微软雅黑");
font.setBold(setBold);
cellStyle.setFont(font);
return cellStyle;
}
/**
*
*
* @param workbook
* @return
*/
public static CellStyle setWordStyle(Workbook workbook) {
// 设置字体
CellStyle cellStyle = workbook.createCellStyle();
//设置样式对象,这里仅设置了边框属性
cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
cellStyle.setBorderTop(BorderStyle.THIN);//上边框
cellStyle.setBorderRight(BorderStyle.THIN);//右边框
Font font = workbook.createFont();
//颜色
font.setColor(Font.COLOR_NORMAL);
//设置字体大小
font.setFontHeightInPoints((short) 10);
//字体
font.setFontName("微软雅黑");
cellStyle.setFont(font);
return cellStyle;
}
/**
*
*
* @param workbook
* @return
*/
public static CellStyle setNolyWordStyle(Workbook workbook,Boolean setBold) {
// 设置字体
CellStyle cellStyle = workbook.createCellStyle();
Font font = workbook.createFont();
//颜色
font.setColor(Font.COLOR_NORMAL);
//设置字体大小
font.setFontHeightInPoints((short) 10);
//字体
font.setFontName("微软雅黑");
font.setBold(setBold);
cellStyle.setFont(font);
DataFormat dataFormat = workbook.createDataFormat();//此处设置数据格式
cellStyle.setDataFormat(dataFormat.getFormat("0.00_ "));
return cellStyle;
}
/**
*
*
* @param workbook
* @return
*/
public static CellStyle setNolyBorderStyle(Workbook workbook) {
// 设置字体
CellStyle cellStyle = workbook.createCellStyle();
//设置样式对象,这里仅设置了边框属性
cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
cellStyle.setBorderTop(BorderStyle.THIN);//上边框
cellStyle.setBorderRight(BorderStyle.THIN);//右边框
return cellStyle;
}
/**
* SXSSFWorkbook InputStream
* @param workbook
* @return
*/
public static InputStream workbookConvertorStream(Workbook workbook) {
InputStream in = null;
try{
//临时缓冲区
ByteArrayOutputStream out = new ByteArrayOutputStream();
//创建临时文件
workbook.write(out);
byte [] bookByteAry = out.toByteArray();
in = new ByteArrayInputStream(bookByteAry);
}
catch (Exception e){
e.printStackTrace();
}
return in;
}
/**
*
* @param workbook
* @return
*/
public static CellStyle setCellToNumber(Workbook workbook){
// 设置字体
CellStyle cellStyle = workbook.createCellStyle();
//设置样式对象,这里仅设置了边框属性
cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
cellStyle.setBorderTop(BorderStyle.THIN);//上边框
cellStyle.setBorderRight(BorderStyle.THIN);//右边框
Font font = workbook.createFont();
//颜色
font.setColor(Font.COLOR_NORMAL);
//设置字体大小
font.setFontHeightInPoints((short) 10);
//字体
font.setFontName("微软雅黑");
cellStyle.setFont(font);
DataFormat dataFormat = workbook.createDataFormat();//此处设置数据格式
cellStyle.setDataFormat(dataFormat.getFormat("0.00_ "));
return cellStyle;
}
}

Some files were not shown because too many files have changed in this diff Show More