Authored by wangshusheng

Merge branch 'dev_广点通第二版加密体系'

@@ -7,8 +7,10 @@ import com.yoho.error.exception.ServiceException; @@ -7,8 +7,10 @@ import com.yoho.error.exception.ServiceException;
7 import com.yoho.service.model.union.request.*; 7 import com.yoho.service.model.union.request.*;
8 import com.yoho.service.model.union.response.*; 8 import com.yoho.service.model.union.response.*;
9 import com.yoho.unions.common.enums.ClientTypeEnum; 9 import com.yoho.unions.common.enums.ClientTypeEnum;
  10 +import com.yoho.unions.common.utils.HttpUtils;
10 import com.yoho.unions.interceptor.RemoteIPInterceptor; 11 import com.yoho.unions.interceptor.RemoteIPInterceptor;
11 import org.apache.commons.lang.StringUtils; 12 import org.apache.commons.lang.StringUtils;
  13 +import org.apache.commons.lang3.tuple.Pair;
12 14
13 import javax.servlet.http.HttpServletRequest; 15 import javax.servlet.http.HttpServletRequest;
14 import java.util.List; 16 import java.util.List;
@@ -49,6 +51,10 @@ public interface IUnionService { @@ -49,6 +51,10 @@ public interface IUnionService {
49 51
50 return clickUnionRequestBO; 52 return clickUnionRequestBO;
51 } 53 }
  54 +
  55 + default public Pair<Integer, String> visitCallbackUrl(String url) {
  56 + return HttpUtils.httpGet(url);
  57 + }
52 58
53 /** 59 /**
54 * 联盟点击 60 * 联盟点击
  1 +package com.yoho.unions.server.service.impl;
  2 +
  3 +import java.util.HashMap;
  4 +import java.util.Map;
  5 +
  6 +import javax.annotation.Resource;
  7 +import javax.servlet.http.HttpServletRequest;
  8 +
  9 +import org.apache.commons.lang3.StringUtils;
  10 +import org.apache.commons.lang3.tuple.Pair;
  11 +import org.slf4j.Logger;
  12 +import org.slf4j.LoggerFactory;
  13 +import org.springframework.stereotype.Service;
  14 +
  15 +import com.yoho.core.common.utils.MD5;
  16 +import com.yoho.service.model.union.request.ActivateUnionRequestBO;
  17 +import com.yoho.service.model.union.request.ClickUnionRequestBO;
  18 +import com.yoho.unions.common.enums.ClientTypeEnum;
  19 +import com.yoho.unions.common.utils.DateUtil;
  20 +import com.yoho.unions.common.utils.HttpUtils;
  21 +import com.yoho.unions.dal.IUnionConfigDAO;
  22 +import com.yoho.unions.dal.model.UnionConfig;
  23 +import com.yoho.unions.interceptor.RemoteIPInterceptor;
  24 +import com.yoho.unions.server.service.IUnionService;
  25 +
  26 +/**
  27 + * 广点通回调:新加密方法
  28 + *
  29 + * @author yoho
  30 + */
  31 +@Service("UnionServiceImpl_7")
  32 +public class GDT3ServiceImpl extends UnionServiceImpl implements IUnionService {
  33 +
  34 + static Logger log = LoggerFactory.getLogger(GDT3ServiceImpl.class);
  35 +
  36 + @Resource
  37 + IUnionConfigDAO unionConfigDAO;
  38 +
  39 + @Override
  40 + public String getCallbackUrl(ClickUnionRequestBO clickBO, ActivateUnionRequestBO activateUnionRequestBO) {
  41 +
  42 + // 直接调用原有的广点通获取URL的方法
  43 + ActivateUnionRequestBO requestBO = new ActivateUnionRequestBO();
  44 + requestBO.setAppid(clickBO.getAppid());
  45 + requestBO.setClient_type(clickBO.getClient_type());
  46 +
  47 + requestBO.setIdfa(clickBO.getIdfa());
  48 + requestBO.setImei(clickBO.getImei());
  49 + requestBO.setCommonUse(clickBO.getCommonUse());
  50 + // 广点通后台生成的点击id
  51 + requestBO.setUdid(clickBO.getClickId());
  52 +
  53 + String clientIp = RemoteIPInterceptor.getRemoteIP();
  54 + if (StringUtils.isNotEmpty(clientIp)) {
  55 + String[] ips = clientIp.split(",");
  56 + if (ips.length > 0) {
  57 + requestBO.setClientIp(ips[0]);
  58 + }
  59 + }
  60 + return processUrl(requestBO);
  61 + }
  62 +
  63 + @Override
  64 + public ClickUnionRequestBO clickHttpRequestTOBO(HttpServletRequest request,ClickUnionRequestBO bo){
  65 +
  66 + bo = new ClickUnionRequestBO();
  67 + String muid = request.getParameter("muid");
  68 + String app_type = request.getParameter("app_type");
  69 + bo.setClient_type(app_type);
  70 + if(app_type.equals(ClientTypeEnum.ANDROID.getName())){
  71 + bo.setImei(muid);
  72 + bo.setInterfaceType("addMonitor_android");
  73 + }else {
  74 + bo.setIdfa(muid);
  75 + bo.setInterfaceType("addMonitor_ios");
  76 + }
  77 + bo.setAppid(request.getParameter("appid"));
  78 + bo.setClickId(request.getParameter("click_id"));
  79 + bo.setUnion_type(request.getParameter("union_type"));
  80 + bo.setClickTime(Integer.valueOf(request.getParameter("click_time")));
  81 + bo.setTd(request.getParameter("td"));
  82 + //广告主在广点通的账户id
  83 + bo.setCommonUse(String.valueOf(request.getParameter("advertiser_id")));
  84 + return bo;
  85 + }
  86 +
  87 + @SuppressWarnings("unused")
  88 + public String processUrl(ActivateUnionRequestBO requestBO) {
  89 + log.info("enter GDTServiceImpl.processUrl param{} is ", requestBO);
  90 +
  91 + // 有货在广点通的账号id
  92 + String uid = getUid(requestBO, "3938107");
  93 + String clientType = getClientType(requestBO, ClientTypeEnum.IOS.getName());
  94 +// UnionConfig unionConfig = new UnionConfig();
  95 +// unionConfig.setEncryptKey("BAAAAAAAAAAAPBc7");
  96 +// unionConfig.setSignKey("d683620ba71ff13a");
  97 + UnionConfig unionConfig = unionConfigDAO.selectByUidAndClientType(Integer.valueOf(uid),clientType);
  98 + String encryptKey = getEncryptKey(unionConfig, null);
  99 + String signKey = getSignKey(unionConfig, null);
  100 + String url = getUrl(unionConfig, "https://t.gdt.qq.com/conv/app/");
  101 + String muid = getMuid(requestBO);
  102 + log.info("activateUnion muid is {} , clientType is {}, signKey is {}, url is {}", muid, clientType, signKey, url);
  103 +
  104 + int conv_time = DateUtil.getCurrentTimeSecond();
  105 + String client_ip = requestBO.getClientIp();
  106 + String clickId = requestBO.getUdid();
  107 + String appId = requestBO.getAppid();
  108 +
  109 + StringBuilder sixParamBuilder = new StringBuilder();
  110 + // app_type=ANDROID
  111 + sixParamBuilder.append("app_type=").append(clientTypeConver(clientType));
  112 + // &click_id=007210548a030059ccdfd1d4
  113 + sixParamBuilder.append("&click_id=").append(nullToEmpty(clickId));
  114 + // &client_ip=10.11.12.13
  115 + sixParamBuilder.append("&client_ip=").append(nullToEmpty(client_ip));
  116 + // &conv_time=1422263664
  117 + sixParamBuilder.append("&conv_time=").append(conv_time);
  118 + // &muid=0f074dc8e1f0547310e729032ac0730b
  119 + sixParamBuilder.append("&muid=").append(nullToEmpty(muid));
  120 + // &sign_key=08ebe39d34c421b8
  121 + sixParamBuilder.append("&sign_key=").append(signKey);
  122 +
  123 + log.info("sixParamBuilder is {} ", sixParamBuilder.toString());
  124 +
  125 + String encstr = MD5.md5(sixParamBuilder.toString());
  126 + String encver = "1.0";
  127 + String convType = "MOBILEAPP_ACTIVITE";
  128 +
  129 + // https://t.gdt.qq.com/conv/app/{appid}/conv
  130 + StringBuilder urlBuilder = new StringBuilder(url);
  131 + // + {appid}/conv
  132 + urlBuilder.append(appId).append("/conv");
  133 + log.info("urlBuilder is {} ", urlBuilder.toString());
  134 +
  135 + // post body
  136 + StringBuilder postBodyBuilder = new StringBuilder();
  137 + // click_id=
  138 + postBodyBuilder.append("click_id=").append(clickId);
  139 + // &appid=112233
  140 + postBodyBuilder.append("&appid=").append(appId);
  141 + // &muid=0f074dc8e1f0547310e729032ac0730b
  142 + postBodyBuilder.append("&muid=").append(muid);
  143 + // &conv_time=1422263664
  144 + postBodyBuilder.append("&conv_time=").append(conv_time);
  145 + // &client_ip=
  146 + postBodyBuilder.append("&client_ip=").append(client_ip);
  147 + // &encstr=8d3fb6477b01827dc2f635a5b45a7fdd
  148 + postBodyBuilder.append("&encstr=").append(encstr);
  149 + // &encver=1.0
  150 + postBodyBuilder.append("&encver=").append(encver);
  151 + // &advertiser_id=20345
  152 + postBodyBuilder.append("&advertiser_id=").append(uid);
  153 + // &app_type=IOS
  154 + postBodyBuilder.append("&app_type=").append(clientTypeConver(clientType));
  155 + // &conv_type=MOBILEAPP_ACTIVITE
  156 + postBodyBuilder.append("&conv_type=").append(convType);
  157 +
  158 + log.info("postBodyBuilder is {} ", postBodyBuilder.toString());
  159 + String finalUrl = urlBuilder.append("?").append(postBodyBuilder.toString()).toString();
  160 + log.info("finalUrl is {} ", finalUrl);
  161 + return finalUrl;
  162 + }
  163 +
  164 + public static void main(String[] args) {
  165 + // test code
  166 + ActivateUnionRequestBO bo = new ActivateUnionRequestBO();
  167 + bo.setClient_type("ios");
  168 + bo.setIdfa("29562890-1743-4005-BB7B-E942FD5E59F2");
  169 + bo.setAppid("490655927");
  170 + bo.setUdid("dxf7ewqkaaadlvncpnqa");
  171 + bo.setAppkey("yohobuy");
  172 + bo.setCommonUse("4376278");
  173 + GDT3ServiceImpl impl = new GDT3ServiceImpl();
  174 + String url = impl.processUrl(bo);
  175 + System.out.println(impl.visitCallbackUrl(url));
  176 + }
  177 +
  178 + @Override
  179 + public Pair<Integer, String> visitCallbackUrl(String url) {
  180 + log.info("visitCallbackUrl url is : {}", url);
  181 + int index = url.indexOf('?');
  182 + if (index < 0) {
  183 + return Pair.of(500, "url错误:没有问号和参数");
  184 + }
  185 + String param = url.substring(index + 1);
  186 + String[] paramPairs = param.split("&");
  187 + Map<String, Object> map = new HashMap<>(paramPairs.length);
  188 + for (String pair : paramPairs) {
  189 + String[] p = pair.split("=");
  190 + if (p.length == 2) {
  191 + map.put(p[0], p[1]);
  192 + }
  193 + }
  194 + try {
  195 + return HttpUtils.httpPost(url.substring(0, index), map);
  196 + } catch (Exception e) {
  197 + log.error("回调第三方错误,url为:" + url, e);
  198 + return Pair.of(500, "调用第三方回调url异常");
  199 + }
  200 + }
  201 +
  202 + private String getUid(ActivateUnionRequestBO requestBO, String defaultValue) {
  203 + if(StringUtils.isNotEmpty(requestBO.getCommonUse())){
  204 + return requestBO.getCommonUse();
  205 + } else {
  206 + return defaultValue;
  207 + }
  208 + }
  209 +
  210 + private String getClientType(ActivateUnionRequestBO requestBO, String defaultValue) {
  211 + if(StringUtils.isNotEmpty(requestBO.getClient_type())){
  212 + return requestBO.getClient_type().toLowerCase();
  213 + } else {
  214 + return defaultValue;
  215 + }
  216 + }
  217 +
  218 + private String getEncryptKey(UnionConfig unionConfig, String defaultValue) {
  219 + if(unionConfig!=null && StringUtils.isNotEmpty(unionConfig.getEncryptKey())){
  220 + return unionConfig.getEncryptKey();
  221 + } else {
  222 + return defaultValue;
  223 + }
  224 + }
  225 +
  226 + private String getSignKey(UnionConfig unionConfig, String defaultValue) {
  227 + if(unionConfig!=null && StringUtils.isNotEmpty(unionConfig.getSignKey())){
  228 + return unionConfig.getSignKey();
  229 + } else {
  230 + return defaultValue;
  231 + }
  232 + }
  233 +
  234 + private String getUrl(UnionConfig unionConfig, String defaultValue) {
  235 + if(unionConfig!=null && StringUtils.isNotEmpty(unionConfig.getUrl())){
  236 + return unionConfig.getUrl();
  237 + } else {
  238 + return defaultValue;
  239 + }
  240 + }
  241 +
  242 + private String getMuid(ActivateUnionRequestBO requestBO) {
  243 + String muid;
  244 + if (ClientTypeEnum.IOS.getName().equalsIgnoreCase(requestBO.getClient_type())) {
  245 + // IDFA 码(需转大写),进行 md5 以后得到的 32位全小写 md5 表现字符串。
  246 + muid = requestBO.getIdfa().toUpperCase();
  247 + } else {
  248 + // IMEI 号(需转小写),进行 md5 以后得到的 32位全小写 md5 表现字符串。
  249 + muid = requestBO.getImei().toLowerCase();
  250 + }
  251 + // 对设备id进行MD5加密,获取32位小写加密串
  252 + return MD5.md5(muid);
  253 + }
  254 +
  255 + private String clientTypeConver(String clientType) {
  256 + if (StringUtils.isEmpty(clientType)) {
  257 + return "";
  258 + }
  259 + if (ClientTypeEnum.ANDROID.getName().equalsIgnoreCase(clientType)) {
  260 + return clientType.toUpperCase();
  261 + } else {
  262 + return "IOS";
  263 + }
  264 + }
  265 +
  266 + private String nullToEmpty(String str) {
  267 + if (str == null) {
  268 + return "";
  269 + } else {
  270 + return str;
  271 + }
  272 + }
  273 +}
@@ -366,6 +366,18 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher @@ -366,6 +366,18 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
366 } 366 }
367 } 367 }
368 } 368 }
  369 +
  370 + if (StringUtils.isEmpty(value) && "100000000003503".equals(request.getChannelId())) {
  371 + ClickUnionRequestBO clickInfo = new ClickUnionRequestBO();
  372 + clickInfo.setClient_type(request.getClient_type());
  373 + clickInfo.setIdfa(request.getIdfa());
  374 + clickInfo.setImei(request.getImei());
  375 + clickInfo.setAppid(request.getAppid());
  376 + clickInfo.setCommonUse(request.getCommonUse());
  377 + clickInfo.setClickId(request.getUdid());
  378 + clickInfo.setUnion_type(request.getChannelId());
  379 + value = JSON.toJSONString(clickInfo);
  380 + }
369 381
370 if (StringUtils.isEmpty(value)) { 382 if (StringUtils.isEmpty(value)) {
371 if(StringUtils.isEmpty(request.getClientIp())){ 383 if(StringUtils.isEmpty(request.getClientIp())){
@@ -591,8 +603,9 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher @@ -591,8 +603,9 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
591 union_type = String.valueOf(unionTypeMatch.getUnionTypeMatch()); 603 union_type = String.valueOf(unionTypeMatch.getUnionTypeMatch());
592 } 604 }
593 String bean =buffer.append("UnionServiceImpl").append("_").append(union_type).toString(); 605 String bean =buffer.append("UnionServiceImpl").append("_").append(union_type).toString();
  606 + IUnionService uniteService = null;
594 if(SpringContextUtil.containsBean(bean)){ 607 if(SpringContextUtil.containsBean(bean)){
595 - IUnionService uniteService = SpringContextUtil.getBean(bean, IUnionService.class); 608 + uniteService = SpringContextUtil.getBean(bean, IUnionService.class);
596 url = uniteService.getCallbackUrl(click,request); 609 url = uniteService.getCallbackUrl(click,request);
597 }else{ 610 }else{
598 url = unionService.getCallbackUrl(click,request); 611 url = unionService.getCallbackUrl(click,request);
@@ -612,7 +625,12 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher @@ -612,7 +625,12 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
612 } 625 }
613 activeUnion.info("activateUnion in call url={}", url); 626 activeUnion.info("activateUnion in call url={}", url);
614 //改成httpclient方式调用 627 //改成httpclient方式调用
615 - Pair<Integer, String> pair = HttpUtils.httpGet(url); 628 + Pair<Integer, String> pair;
  629 + if (uniteService != null) {
  630 + pair = uniteService.visitCallbackUrl(url);
  631 + } else {
  632 + pair = visitCallbackUrl(url);
  633 + }
616 634
617 try{ 635 try{
618 UnionCallbackLogs unionCallbackLogs = new UnionCallbackLogs(); 636 UnionCallbackLogs unionCallbackLogs = new UnionCallbackLogs();