作者 朱兆平

FEIGN 携带cookie 调用新舱单接口查询调拨数据

package com.sy.feign;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
@Configuration
public class NmmsInterceptor {
@Bean
public RequestInterceptor requestInterceptor() {
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate template) {
//给新请求同步老请求的cookie
template.header("Cookie", "username=admin;userid=1903e605-a2e1-404d-a1a6-a749752fb030;");
System.out.println("我进入了远程feign");
}
};
}
public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();//拿到当前的所有请求属性
HttpServletRequest request = attributes.getRequest();//获取到当前请求
if (request != null) {
//同步请求头数据,cookie
String cookie = request.getHeader("Cookie");
//给新请求同步老请求的cookie
requestTemplate.header("Cookie", "username=admin;userid=1903e605-a2e1-404d-a1a6-a749752fb030;");
System.out.println("我进入了远程feign");
return;
}
requestTemplate.header("Cookie",
"username=admin;userid=1903e605-a2e1-404d-a1a6-a749752fb030;");
requestTemplate.query("Cookie",
"username=admin; Path=/;",
"userid=1903e605-a2e1-404d-a1a6-a749752fb030; Path=/;");
// requestTemplate.query("cookie",
// "username=admin; Path=/;",
// "userid=1903e605-a2e1-404d-a1a6-a749752fb030; Path=/;");
}
}
... ...
... ... @@ -98,8 +98,6 @@ class BillAllocateCheck extends Script implements ChannelCheckScript {
waybill = waybill.replace(",", ",");
String[] maifest = waybill.split(",");
logger.info("运单列表:" + waybill);
boolean flag = false;
for (String awb : maifest) {
Map map= nmmsAllocate(awb);
if (map.containsKey("reslut")){
... ... @@ -110,9 +108,9 @@ class BillAllocateCheck extends Script implements ChannelCheckScript {
JSONObject awbinfo = ds.getJSONObject(0);
if (awbinfo.containsKey("RECEIPTINFORMATION")){
if (awbinfo.getString("RECEIPTINFORMATION").contains("39301") || awbinfo.getString("RECEIPTINFORMATION").contains("39103")){
log.info("运单:{},分拨回执查询结果:{}",awb,awbinfo.getString("RECEIPTINFORMATION"));
logger.info("运单:{},分拨回执查询结果:{}",awb,awbinfo.getString("RECEIPTINFORMATION"));
}else {
log.info("运单{}分拨申请回执不正常:{}",awb,awbinfo.getString("RECEIPTINFORMATION"));
logger.info("运单{}分拨申请回执不正常:{}",awb,awbinfo.getString("RECEIPTINFORMATION"));
return false;
}
}
... ...
... ... @@ -117,6 +117,7 @@ class LockNoticeCheck extends Script implements ChannelCheckScript {
}else {
CommandClient.Client(info,"流转业务-未申请关锁号");
record(info,false,"业务异常:流转业务-未申请关锁号",null);
log.error("[LockNoticeCheck-ERR]:流转业务-未申请关锁号");
//这里需要返回true,是关锁业务,但是中断,不抬杆不放行,给予关锁业务异常通知
return false;
}
... ...
package com.sy.service;
import com.sy.feign.NmmsInterceptor;
import com.sy.model.Feign_Allocate_Search;
import com.sy.model.NmmsPrepaidMasterSearch;
import com.sy.service.impl.NmmsServiceHystrix;
... ... @@ -15,7 +16,7 @@ import java.util.Map;
* todo:生产要把这里改成内网舱单地址,已更新为在配置文件改
*/
@Component
@FeignClient(name = "nmmsFeign",url = "${nmms.site.host}",fallback = NmmsServiceHystrix.class)
@FeignClient(name = "nmmsFeign",url = "${nmms.site.host}",fallback = NmmsServiceHystrix.class,configuration = NmmsInterceptor.class)
public interface NmmsService {
@ResponseBody
... ...
... ... @@ -67,54 +67,59 @@ public class EnginCheckServiceImpl implements EnginCheckService {
@Override
public Boolean enginCheckByGatherInfo(GatherInfo gatherInfo,ExecuteParams executeParams) {
//获取申请信息
LandBusinessTypeList chanelFormInfo = (LandBusinessTypeList) executeParams.get("ChanelFormInfo");
if (chanelFormInfo!=null){
//查询验放配置参数生成
RuleChannelConfig ruleChannelConfig = new RuleChannelConfig();
ruleChannelConfig.setBusinessType(chanelFormInfo.getBusinesstype());
ruleChannelConfig.setGoodsType(chanelFormInfo.getCocode());
ruleChannelConfig.setChannelNum(gatherInfo.getChnlno());
List<RuleChannelConfig> ruleChannelConfigs = ruleChannelConfigDao.selectByChannelAndBusiTypeAndGoodsType(ruleChannelConfig);
if (ruleChannelConfigs!=null && !ruleChannelConfigs.isEmpty()){
for (int i = 0; i < ruleChannelConfigs.size(); i++) {
/**
* 核销判定需要另外执行.这里需要跳过.
*/
if (ruleChannelConfigs.get(i).ruleConfig != null && "核销判定".equals(ruleChannelConfigs.get(i).ruleConfig.getRuleType())){
return true;
}
// 执行脚本中指定的方法 changeProduct
EngineExecutorResult executorResult = engineExecutor.execute(
ruleChannelConfigs.get(i).ruleConfig.getScriptMethodName(), new ScriptQuery(ruleChannelConfigs.get(i).ruleConfig.getScriptKey()), executeParams);
log.info("验证脚本名称:{},验证KEY-NAME:{}", ruleChannelConfigs.get(i).ruleConfig.getRuleName(),ruleChannelConfigs.get(i).ruleConfig.getScriptKey());
log.info("使用groovy脚本来验证过卡判定放行结果=========>>>>>>>>>>>执行结果:{}", executorResult);
if (executorResult.getExecutionStatus().equals(ExecutionStatus.SUCCESS)){
if (executorResult.getContext() instanceof Boolean){
Boolean execResult = (Boolean) executorResult.getContext();
if (execResult) {
log.info("验证脚本名称:{},验证KEY-NAME:{}-[验放成功]",ruleChannelConfigs.get(i).ruleConfig.getRuleName(),ruleChannelConfigs.get(i).ruleConfig.getScriptKey());
}else {
log.info("验放失败");
return false;
try {
//获取申请信息
LandBusinessTypeList chanelFormInfo = (LandBusinessTypeList) executeParams.get("ChanelFormInfo");
if (chanelFormInfo!=null){
//查询验放配置参数生成
RuleChannelConfig ruleChannelConfig = new RuleChannelConfig();
ruleChannelConfig.setBusinessType(chanelFormInfo.getBusinesstype());
ruleChannelConfig.setGoodsType(chanelFormInfo.getCocode());
ruleChannelConfig.setChannelNum(gatherInfo.getChnlno());
List<RuleChannelConfig> ruleChannelConfigs = ruleChannelConfigDao.selectByChannelAndBusiTypeAndGoodsType(ruleChannelConfig);
if (ruleChannelConfigs!=null && !ruleChannelConfigs.isEmpty()){
for (int i = 0; i < ruleChannelConfigs.size(); i++) {
/**
* 核销判定需要另外执行.这里需要跳过.
*/
if (ruleChannelConfigs.get(i).ruleConfig != null && "核销判定".equals(ruleChannelConfigs.get(i).ruleConfig.getRuleType())){
return true;
}
// 执行脚本中指定的方法 changeProduct
EngineExecutorResult executorResult = engineExecutor.execute(
ruleChannelConfigs.get(i).ruleConfig.getScriptMethodName(), new ScriptQuery(ruleChannelConfigs.get(i).ruleConfig.getScriptKey()), executeParams);
log.info("验证脚本名称:{},验证KEY-NAME:{},执行结果:{}", ruleChannelConfigs.get(i).ruleConfig.getRuleName(),ruleChannelConfigs.get(i).ruleConfig.getScriptKey(),executorResult);
if (executorResult.getExecutionStatus().equals(ExecutionStatus.SUCCESS)){
if (executorResult.getContext() instanceof Boolean){
Boolean execResult = (Boolean) executorResult.getContext();
if (execResult) {
log.info("[ENGIN-CHECK-SUCCESS] - 验证脚本名称:{},验证KEY-NAME:{}-[验放成功]",ruleChannelConfigs.get(i).ruleConfig.getRuleName(),ruleChannelConfigs.get(i).ruleConfig.getScriptKey());
}else {
log.info("[ENGIN-CHECK-FAILD]验放失败");
return false;
}
}
}else {
log.error("[ENGIN-CHECK-FAILD]-验证脚本名称:{},验证KEY-NAME:{}-[验放失败]",ruleChannelConfigs.get(i).ruleConfig.getRuleName(),ruleChannelConfigs.get(i).ruleConfig.getScriptKey());
return false;
}
}else {
log.error("验证脚本名称:{},验证KEY-NAME:{}-[验放失败]",ruleChannelConfigs.get(i).ruleConfig.getRuleName(),ruleChannelConfigs.get(i).ruleConfig.getScriptKey());
return false;
}
return true;
}else {
log.error("[ENGIN-CHECK-FAILD]-[{}]-通道未配置规则,不支持此业务",gatherInfo.getChnlno());
sendBw(gatherInfo,false,"通道未配置规则,不支持此业务",executeParams);
return false;
}
return true;
}else {
log.error("[{}]-通道未配置规则,不支持此业务",gatherInfo.getChnlno());
sendBw(gatherInfo,false,"通道未配置规则,不支持此业务",executeParams);
return false;
}
sendBw(gatherInfo,false,"未找到流转申请信息",executeParams);
log.error("[ENGIN-CHECK-FAILD]-未找到流转申请信息");
return false;
}catch (Exception e){
log.error("[ENGIN-CHECK-ERR]",e);
return false;
}
sendBw(gatherInfo,false,"未找到流转申请信息",executeParams);
log.error("未找到流转申请信息");
return false;
}
@Override
... ...
... ... @@ -80,7 +80,7 @@ public class CommandClient {
OutputStream op = null;
try {
//ip+端口
socket = new Socket("10.50.7.10", 9003);
socket = new Socket("10.50.7.10", 9998);
log.info("socket通讯创建连接成功");
op = socket.getOutputStream();
//xml字节流
... ... @@ -91,7 +91,7 @@ public class CommandClient {
head[1]=(byte)0x5C;
head[2]=(byte)0x4B;
head[3]=(byte)0x89;
//消息类型
//消息类型,0x21为gatherInfo 0x22为commandInfo
byte[] mType = new byte[1];
mType[0] = (byte)0x22;
//场站号
... ... @@ -130,6 +130,63 @@ public class CommandClient {
log.info("文件发送失败"+e.getMessage());
}
}
private static void sendWithSocket_x21(GatherInfo info,String xmlBody){
Socket socket =null;
OutputStream op = null;
try {
//ip+端口
socket = new Socket("10.50.7.10", 9998);
log.info("socket通讯创建连接成功");
op = socket.getOutputStream();
//xml字节流
byte[]xBody =xmlBody.getBytes("GB2312");
//包头
byte[] head = new byte[4];
head[0]=(byte)0xE2;
head[1]=(byte)0x5C;
head[2]=(byte)0x4B;
head[3]=(byte)0x89;
//消息类型,0x21为gatherInfo 0x22为commandInfo
byte[] mType = new byte[1];
mType[0] = (byte)0x21;
//场站号
byte[]station =info.getAreaid().getBytes("ASCII");
//通道号
byte[]aisle =info.getChnlno().getBytes("ASCII");
//进出标识
byte[]eType =info.getIetype().getBytes("ASCII");
//标识符
byte[] bwFlag = new byte[4];
bwFlag[0]=(byte)0x00;
bwFlag[1]=(byte)0x00;
bwFlag[2]=(byte)0x00;
bwFlag[3]=(byte)0x00;
//xml字节流长度
byte[]xmlLength = intToByte4(xBody.length);
//包尾
byte[]end = new byte[2];
end[0]=(byte)0xFF;
end[1]=(byte)0xFF;
System.out.println();
//总长 4为总长的length
byte [] packge = intToByte4((head.length+xBody.length+mType.length+station.length+aisle.length+eType
.length+bwFlag.length+xmlLength.length+end.length+4));
byte[]allByte = byteMergerAll(head,packge,mType,station,aisle,eType,bwFlag,xmlLength,xBody,end);
op.write(allByte);
op.flush();
op.close();
log.info("发送完毕");
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
log.info("创建连接失败"+e.getMessage());
} catch (IOException e) {
e.printStackTrace();
log.info("文件发送失败"+e.getMessage());
}
}
/**
* @Param info 卡口采集信息
* @Param flag 放行标识
... ... @@ -185,7 +242,7 @@ public class CommandClient {
//给三宝的去掉BARCODE,todo:不去掉做个测试
.replace("${BAR_CODE}",info.getBarcode());
sendWithSocket(info,gatherXML);
sendWithSocket_x21(info,gatherXML);
}
... ...
... ... @@ -28,6 +28,26 @@ class WeightCheckHandleServiceImplTest {
@Test
void checkAllocateOrDispatch(){
weightCheckHandleService.checkAllocateOrDispatch(12740,10500,2200+4630,14960);
weightCheckHandleService.checkAllocateOrDispatch(10560,10500,175.6,10310);
}
@Test
void checkResult() {
}
@Test
void checkImportAtCheckIN() {
}
@Test
void checkEmpty() {
}
@Test
void checkFlag() {
}
@Test
void valueDob() {
}
}
... ...