作者 朱兆平

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

  1 +package com.sy.feign;
  2 +
  3 +import feign.RequestInterceptor;
  4 +import feign.RequestTemplate;
  5 +import org.springframework.context.annotation.Bean;
  6 +import org.springframework.context.annotation.Configuration;
  7 +import org.springframework.web.context.request.RequestContextHolder;
  8 +import org.springframework.web.context.request.ServletRequestAttributes;
  9 +
  10 +import javax.servlet.http.HttpServletRequest;
  11 +
  12 +@Configuration
  13 +public class NmmsInterceptor {
  14 +
  15 + @Bean
  16 + public RequestInterceptor requestInterceptor() {
  17 + return new RequestInterceptor() {
  18 + @Override
  19 + public void apply(RequestTemplate template) {
  20 + //给新请求同步老请求的cookie
  21 + template.header("Cookie", "username=admin;userid=1903e605-a2e1-404d-a1a6-a749752fb030;");
  22 + System.out.println("我进入了远程feign");
  23 + }
  24 +
  25 + };
  26 + }
  27 +
  28 +
  29 +
  30 + public void apply(RequestTemplate requestTemplate) {
  31 +
  32 + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();//拿到当前的所有请求属性
  33 + HttpServletRequest request = attributes.getRequest();//获取到当前请求
  34 + if (request != null) {
  35 + //同步请求头数据,cookie
  36 + String cookie = request.getHeader("Cookie");
  37 + //给新请求同步老请求的cookie
  38 + requestTemplate.header("Cookie", "username=admin;userid=1903e605-a2e1-404d-a1a6-a749752fb030;");
  39 + System.out.println("我进入了远程feign");
  40 + return;
  41 + }
  42 +
  43 + requestTemplate.header("Cookie",
  44 + "username=admin;userid=1903e605-a2e1-404d-a1a6-a749752fb030;");
  45 +
  46 + requestTemplate.query("Cookie",
  47 + "username=admin; Path=/;",
  48 + "userid=1903e605-a2e1-404d-a1a6-a749752fb030; Path=/;");
  49 +// requestTemplate.query("cookie",
  50 +// "username=admin; Path=/;",
  51 +// "userid=1903e605-a2e1-404d-a1a6-a749752fb030; Path=/;");
  52 + }
  53 +}
@@ -98,8 +98,6 @@ class BillAllocateCheck extends Script implements ChannelCheckScript { @@ -98,8 +98,6 @@ class BillAllocateCheck extends Script implements ChannelCheckScript {
98 waybill = waybill.replace(",", ","); 98 waybill = waybill.replace(",", ",");
99 String[] maifest = waybill.split(","); 99 String[] maifest = waybill.split(",");
100 logger.info("运单列表:" + waybill); 100 logger.info("运单列表:" + waybill);
101 - boolean flag = false;  
102 -  
103 for (String awb : maifest) { 101 for (String awb : maifest) {
104 Map map= nmmsAllocate(awb); 102 Map map= nmmsAllocate(awb);
105 if (map.containsKey("reslut")){ 103 if (map.containsKey("reslut")){
@@ -110,9 +108,9 @@ class BillAllocateCheck extends Script implements ChannelCheckScript { @@ -110,9 +108,9 @@ class BillAllocateCheck extends Script implements ChannelCheckScript {
110 JSONObject awbinfo = ds.getJSONObject(0); 108 JSONObject awbinfo = ds.getJSONObject(0);
111 if (awbinfo.containsKey("RECEIPTINFORMATION")){ 109 if (awbinfo.containsKey("RECEIPTINFORMATION")){
112 if (awbinfo.getString("RECEIPTINFORMATION").contains("39301") || awbinfo.getString("RECEIPTINFORMATION").contains("39103")){ 110 if (awbinfo.getString("RECEIPTINFORMATION").contains("39301") || awbinfo.getString("RECEIPTINFORMATION").contains("39103")){
113 - log.info("运单:{},分拨回执查询结果:{}",awb,awbinfo.getString("RECEIPTINFORMATION")); 111 + logger.info("运单:{},分拨回执查询结果:{}",awb,awbinfo.getString("RECEIPTINFORMATION"));
114 }else { 112 }else {
115 - log.info("运单{}分拨申请回执不正常:{}",awb,awbinfo.getString("RECEIPTINFORMATION")); 113 + logger.info("运单{}分拨申请回执不正常:{}",awb,awbinfo.getString("RECEIPTINFORMATION"));
116 return false; 114 return false;
117 } 115 }
118 } 116 }
@@ -117,6 +117,7 @@ class LockNoticeCheck extends Script implements ChannelCheckScript { @@ -117,6 +117,7 @@ class LockNoticeCheck extends Script implements ChannelCheckScript {
117 }else { 117 }else {
118 CommandClient.Client(info,"流转业务-未申请关锁号"); 118 CommandClient.Client(info,"流转业务-未申请关锁号");
119 record(info,false,"业务异常:流转业务-未申请关锁号",null); 119 record(info,false,"业务异常:流转业务-未申请关锁号",null);
  120 + log.error("[LockNoticeCheck-ERR]:流转业务-未申请关锁号");
120 //这里需要返回true,是关锁业务,但是中断,不抬杆不放行,给予关锁业务异常通知 121 //这里需要返回true,是关锁业务,但是中断,不抬杆不放行,给予关锁业务异常通知
121 return false; 122 return false;
122 } 123 }
1 package com.sy.service; 1 package com.sy.service;
2 2
3 3
  4 +import com.sy.feign.NmmsInterceptor;
4 import com.sy.model.Feign_Allocate_Search; 5 import com.sy.model.Feign_Allocate_Search;
5 import com.sy.model.NmmsPrepaidMasterSearch; 6 import com.sy.model.NmmsPrepaidMasterSearch;
6 import com.sy.service.impl.NmmsServiceHystrix; 7 import com.sy.service.impl.NmmsServiceHystrix;
@@ -15,7 +16,7 @@ import java.util.Map; @@ -15,7 +16,7 @@ import java.util.Map;
15 * todo:生产要把这里改成内网舱单地址,已更新为在配置文件改 16 * todo:生产要把这里改成内网舱单地址,已更新为在配置文件改
16 */ 17 */
17 @Component 18 @Component
18 -@FeignClient(name = "nmmsFeign",url = "${nmms.site.host}",fallback = NmmsServiceHystrix.class) 19 +@FeignClient(name = "nmmsFeign",url = "${nmms.site.host}",fallback = NmmsServiceHystrix.class,configuration = NmmsInterceptor.class)
19 public interface NmmsService { 20 public interface NmmsService {
20 21
21 @ResponseBody 22 @ResponseBody
@@ -67,6 +67,7 @@ public class EnginCheckServiceImpl implements EnginCheckService { @@ -67,6 +67,7 @@ public class EnginCheckServiceImpl implements EnginCheckService {
67 @Override 67 @Override
68 public Boolean enginCheckByGatherInfo(GatherInfo gatherInfo,ExecuteParams executeParams) { 68 public Boolean enginCheckByGatherInfo(GatherInfo gatherInfo,ExecuteParams executeParams) {
69 69
  70 + try {
70 //获取申请信息 71 //获取申请信息
71 LandBusinessTypeList chanelFormInfo = (LandBusinessTypeList) executeParams.get("ChanelFormInfo"); 72 LandBusinessTypeList chanelFormInfo = (LandBusinessTypeList) executeParams.get("ChanelFormInfo");
72 if (chanelFormInfo!=null){ 73 if (chanelFormInfo!=null){
@@ -88,35 +89,39 @@ public class EnginCheckServiceImpl implements EnginCheckService { @@ -88,35 +89,39 @@ public class EnginCheckServiceImpl implements EnginCheckService {
88 // 执行脚本中指定的方法 changeProduct 89 // 执行脚本中指定的方法 changeProduct
89 EngineExecutorResult executorResult = engineExecutor.execute( 90 EngineExecutorResult executorResult = engineExecutor.execute(
90 ruleChannelConfigs.get(i).ruleConfig.getScriptMethodName(), new ScriptQuery(ruleChannelConfigs.get(i).ruleConfig.getScriptKey()), executeParams); 91 ruleChannelConfigs.get(i).ruleConfig.getScriptMethodName(), new ScriptQuery(ruleChannelConfigs.get(i).ruleConfig.getScriptKey()), executeParams);
91 - log.info("验证脚本名称:{},验证KEY-NAME:{}", ruleChannelConfigs.get(i).ruleConfig.getRuleName(),ruleChannelConfigs.get(i).ruleConfig.getScriptKey());  
92 - log.info("使用groovy脚本来验证过卡判定放行结果=========>>>>>>>>>>>执行结果:{}", executorResult); 92 + log.info("验证脚本名称:{},验证KEY-NAME:{},执行结果:{}", ruleChannelConfigs.get(i).ruleConfig.getRuleName(),ruleChannelConfigs.get(i).ruleConfig.getScriptKey(),executorResult);
93 if (executorResult.getExecutionStatus().equals(ExecutionStatus.SUCCESS)){ 93 if (executorResult.getExecutionStatus().equals(ExecutionStatus.SUCCESS)){
94 if (executorResult.getContext() instanceof Boolean){ 94 if (executorResult.getContext() instanceof Boolean){
95 Boolean execResult = (Boolean) executorResult.getContext(); 95 Boolean execResult = (Boolean) executorResult.getContext();
96 if (execResult) { 96 if (execResult) {
97 - log.info("验证脚本名称:{},验证KEY-NAME:{}-[验放成功]",ruleChannelConfigs.get(i).ruleConfig.getRuleName(),ruleChannelConfigs.get(i).ruleConfig.getScriptKey()); 97 + log.info("[ENGIN-CHECK-SUCCESS] - 验证脚本名称:{},验证KEY-NAME:{}-[验放成功]",ruleChannelConfigs.get(i).ruleConfig.getRuleName(),ruleChannelConfigs.get(i).ruleConfig.getScriptKey());
98 }else { 98 }else {
99 - log.info("验放失败"); 99 + log.info("[ENGIN-CHECK-FAILD]验放失败");
100 return false; 100 return false;
101 } 101 }
102 } 102 }
103 }else { 103 }else {
104 - log.error("验证脚本名称:{},验证KEY-NAME:{}-[验放失败]",ruleChannelConfigs.get(i).ruleConfig.getRuleName(),ruleChannelConfigs.get(i).ruleConfig.getScriptKey()); 104 + log.error("[ENGIN-CHECK-FAILD]-验证脚本名称:{},验证KEY-NAME:{}-[验放失败]",ruleChannelConfigs.get(i).ruleConfig.getRuleName(),ruleChannelConfigs.get(i).ruleConfig.getScriptKey());
105 return false; 105 return false;
106 } 106 }
107 } 107 }
108 return true; 108 return true;
109 }else { 109 }else {
110 - log.error("[{}]-通道未配置规则,不支持此业务",gatherInfo.getChnlno()); 110 + log.error("[ENGIN-CHECK-FAILD]-[{}]-通道未配置规则,不支持此业务",gatherInfo.getChnlno());
111 sendBw(gatherInfo,false,"通道未配置规则,不支持此业务",executeParams); 111 sendBw(gatherInfo,false,"通道未配置规则,不支持此业务",executeParams);
112 return false; 112 return false;
113 } 113 }
114 } 114 }
115 sendBw(gatherInfo,false,"未找到流转申请信息",executeParams); 115 sendBw(gatherInfo,false,"未找到流转申请信息",executeParams);
116 - log.error("未找到流转申请信息"); 116 + log.error("[ENGIN-CHECK-FAILD]-未找到流转申请信息");
  117 + return false;
  118 + }catch (Exception e){
  119 + log.error("[ENGIN-CHECK-ERR]",e);
117 return false; 120 return false;
118 } 121 }
119 122
  123 + }
  124 +
120 @Override 125 @Override
121 public ExecuteParams makeParaByGagherInfo(GatherInfo gatherInfo){ 126 public ExecuteParams makeParaByGagherInfo(GatherInfo gatherInfo){
122 ExecuteParams executeParams = new ExecuteParams(); 127 ExecuteParams executeParams = new ExecuteParams();
@@ -80,7 +80,7 @@ public class CommandClient { @@ -80,7 +80,7 @@ public class CommandClient {
80 OutputStream op = null; 80 OutputStream op = null;
81 try { 81 try {
82 //ip+端口 82 //ip+端口
83 - socket = new Socket("10.50.7.10", 9003); 83 + socket = new Socket("10.50.7.10", 9998);
84 log.info("socket通讯创建连接成功"); 84 log.info("socket通讯创建连接成功");
85 op = socket.getOutputStream(); 85 op = socket.getOutputStream();
86 //xml字节流 86 //xml字节流
@@ -91,7 +91,7 @@ public class CommandClient { @@ -91,7 +91,7 @@ public class CommandClient {
91 head[1]=(byte)0x5C; 91 head[1]=(byte)0x5C;
92 head[2]=(byte)0x4B; 92 head[2]=(byte)0x4B;
93 head[3]=(byte)0x89; 93 head[3]=(byte)0x89;
94 - //消息类型 94 + //消息类型,0x21为gatherInfo 0x22为commandInfo
95 byte[] mType = new byte[1]; 95 byte[] mType = new byte[1];
96 mType[0] = (byte)0x22; 96 mType[0] = (byte)0x22;
97 //场站号 97 //场站号
@@ -130,6 +130,63 @@ public class CommandClient { @@ -130,6 +130,63 @@ public class CommandClient {
130 log.info("文件发送失败"+e.getMessage()); 130 log.info("文件发送失败"+e.getMessage());
131 } 131 }
132 } 132 }
  133 +
  134 + private static void sendWithSocket_x21(GatherInfo info,String xmlBody){
  135 + Socket socket =null;
  136 + OutputStream op = null;
  137 + try {
  138 + //ip+端口
  139 + socket = new Socket("10.50.7.10", 9998);
  140 + log.info("socket通讯创建连接成功");
  141 + op = socket.getOutputStream();
  142 + //xml字节流
  143 + byte[]xBody =xmlBody.getBytes("GB2312");
  144 + //包头
  145 + byte[] head = new byte[4];
  146 + head[0]=(byte)0xE2;
  147 + head[1]=(byte)0x5C;
  148 + head[2]=(byte)0x4B;
  149 + head[3]=(byte)0x89;
  150 + //消息类型,0x21为gatherInfo 0x22为commandInfo
  151 + byte[] mType = new byte[1];
  152 + mType[0] = (byte)0x21;
  153 + //场站号
  154 + byte[]station =info.getAreaid().getBytes("ASCII");
  155 + //通道号
  156 + byte[]aisle =info.getChnlno().getBytes("ASCII");
  157 + //进出标识
  158 + byte[]eType =info.getIetype().getBytes("ASCII");
  159 + //标识符
  160 + byte[] bwFlag = new byte[4];
  161 + bwFlag[0]=(byte)0x00;
  162 + bwFlag[1]=(byte)0x00;
  163 + bwFlag[2]=(byte)0x00;
  164 + bwFlag[3]=(byte)0x00;
  165 + //xml字节流长度
  166 + byte[]xmlLength = intToByte4(xBody.length);
  167 + //包尾
  168 + byte[]end = new byte[2];
  169 + end[0]=(byte)0xFF;
  170 + end[1]=(byte)0xFF;
  171 + System.out.println();
  172 + //总长 4为总长的length
  173 + byte [] packge = intToByte4((head.length+xBody.length+mType.length+station.length+aisle.length+eType
  174 + .length+bwFlag.length+xmlLength.length+end.length+4));
  175 + byte[]allByte = byteMergerAll(head,packge,mType,station,aisle,eType,bwFlag,xmlLength,xBody,end);
  176 + op.write(allByte);
  177 + op.flush();
  178 + op.close();
  179 + log.info("发送完毕");
  180 + socket.close();
  181 + } catch (UnknownHostException e) {
  182 + e.printStackTrace();
  183 + log.info("创建连接失败"+e.getMessage());
  184 + } catch (IOException e) {
  185 + e.printStackTrace();
  186 + log.info("文件发送失败"+e.getMessage());
  187 + }
  188 + }
  189 +
133 /** 190 /**
134 * @Param info 卡口采集信息 191 * @Param info 卡口采集信息
135 * @Param flag 放行标识 192 * @Param flag 放行标识
@@ -185,7 +242,7 @@ public class CommandClient { @@ -185,7 +242,7 @@ public class CommandClient {
185 //给三宝的去掉BARCODE,todo:不去掉做个测试 242 //给三宝的去掉BARCODE,todo:不去掉做个测试
186 .replace("${BAR_CODE}",info.getBarcode()); 243 .replace("${BAR_CODE}",info.getBarcode());
187 244
188 - sendWithSocket(info,gatherXML); 245 + sendWithSocket_x21(info,gatherXML);
189 } 246 }
190 247
191 248
@@ -28,6 +28,26 @@ class WeightCheckHandleServiceImplTest { @@ -28,6 +28,26 @@ class WeightCheckHandleServiceImplTest {
28 28
29 @Test 29 @Test
30 void checkAllocateOrDispatch(){ 30 void checkAllocateOrDispatch(){
31 - weightCheckHandleService.checkAllocateOrDispatch(12740,10500,2200+4630,14960); 31 + weightCheckHandleService.checkAllocateOrDispatch(10560,10500,175.6,10310);
  32 + }
  33 +
  34 + @Test
  35 + void checkResult() {
  36 + }
  37 +
  38 + @Test
  39 + void checkImportAtCheckIN() {
  40 + }
  41 +
  42 + @Test
  43 + void checkEmpty() {
  44 + }
  45 +
  46 + @Test
  47 + void checkFlag() {
  48 + }
  49 +
  50 + @Test
  51 + void valueDob() {
32 } 52 }
33 } 53 }