ClickUnionRest.java
14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
*
*/
package com.yoho.unions.server.restapi;
import java.io.IOException;
import java.util.Enumeration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yoho.unions.dal.IUnionTypeMatchDAO;
import com.yoho.unions.dal.model.UnionTypeMatch;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yoho.service.model.union.request.ClickUnionRequestBO;
import com.yoho.service.model.union.response.UnionResponse;
import com.yoho.unions.common.enums.ClientTypeEnum;
import com.yoho.unions.common.utils.SpringContextUtil;
import com.yoho.unions.interceptor.RemoteIPInterceptor;
import com.yoho.unions.server.service.IMobvistaService;
import com.yoho.unions.server.service.IUnionService;
/**
* 描述:统一的联盟入口
*
* @author ping.huang
* 2016年5月10日
*/
@Controller
@RequestMapping("/ClickUnionRest")
public class ClickUnionRest {
static Logger log = LoggerFactory.getLogger(ClickUnionRest.class);
static Logger clickUnion = LoggerFactory.getLogger("clickUnion");
@Resource(name="unionServiceImpl")
IUnionService unionService;
@Resource
IMobvistaService mobvistaService;
@Resource
IUnionTypeMatchDAO unionTypeMatchDAO;
/**
* 添加联盟
* @param vo
* @return
*/
@RequestMapping("/addUnion")
@ResponseBody
public UnionResponse addUnion(ClickUnionRequestBO request) {
if (StringUtils.isEmpty(request.getUnion_type())) {
return new UnionResponse(201, "union_type is empty");
}
log.info("addUnion with param is {}", request);
if ("iphone".equals(request.getClient_type())) {
request.setClient_type(ClientTypeEnum.IOS.getName());
}
String IP = RemoteIPInterceptor.getRemoteIP();
// String IP = "42.236.215.29, 123.151.42.48";
String clientIp = null;
if(StringUtils.isNotEmpty(IP)){
String[] IPS = IP.split(",");
clientIp = IPS[0];
}
request.setClientIp(clientIp);
//针对td做一下特殊处理,如果是0_0_ios_10.2 这种情况,则把0.0给截取掉
String td = request.getTd();
if (td.startsWith("0_0")){
String[] td1 = td.split("0_0_");
request.setTd(td1[1]);
}
clickUnion.info("addUnion with param is {}", request);
UnionResponse response = unionService.clickUnion(request);
log.info("addUnion with result is {}, and request is {}", response, request);
return response;
}
@RequestMapping("/addCommonUnion")
@ResponseBody
public UnionResponse addCommonUnion(HttpServletRequest httpServletRequest) {
log.info("addCommonUnion with param is {}", JSON.toJSONString(httpServletRequest.getParameterMap()));
clickUnion.info("addCommonUnion with param is {}", JSON.toJSONString(httpServletRequest.getParameterMap()));
String union_type = httpServletRequest.getParameter("union_type");
//根据union_type区分不同的厂商
StringBuffer stringBuffer = new StringBuffer();
String bean = stringBuffer.append("UnionServiceImpl").append("_").append(union_type).toString();
boolean containsBean = SpringContextUtil.containsBean(bean);
ClickUnionRequestBO request = null;
if(containsBean){
IUnionService service = SpringContextUtil.getBean(bean, IUnionService.class);
request = service.clickHttpRequestTOBO(httpServletRequest,request);
}else {
request = unionService.clickHttpRequestTOBO(httpServletRequest,request);
}
String IP = RemoteIPInterceptor.getRemoteIP();
String clientIp = null;
if(StringUtils.isNotEmpty(IP)){
String[] IPS = IP.split(",");
clientIp = IPS[0];
}
request.setClientIp(clientIp);
// IUnionService service = SpringContextUtil.getBean(bean, IUnionService.class);
clickUnion.info("addCommonUnion request is {}",request);
UnionResponse response = unionService.clickUnion(request);
log.info("addUnion with result is {}, and request is {}", response, request);
return response;
}
/**
* 对特殊的联盟进行特殊处理
* @param bo
* @param request
* @param response
*/
@RequestMapping("/addUnion4Special")
public void addUnion4Special(ClickUnionRequestBO bo, HttpServletRequest request, HttpServletResponse response) {
log.info("addUnion4Special with param is {}", bo);
try {
String clientIp = null;
String IP = RemoteIPInterceptor.getRemoteIP();
if(StringUtils.isNotEmpty(IP)){
String[] IPS = IP.split(",");
clientIp = IPS[0];
}
bo.setClientIp(clientIp);
String agent = request.getHeader("user-agent");
log.info("user-agent={}", agent);
if (StringUtils.isEmpty(agent)) {
log.warn("addUnion4Special user-agent is empty with bo={}", bo);
return;
}
if (agent.toLowerCase().indexOf("mac os x") < 0 && agent.toLowerCase().indexOf("iphone") < 0) {
log.warn("addUnion4Special is not ios with user-agent={}, bo={}", agent, bo);
return;
}
if (agent.indexOf(" like") < agent.indexOf(" OS ") + 4) {
log.warn("addUnion4Special agent is not correct with user-agent={}, bo={}", agent, bo);
return;
}
String version = agent.substring(agent.indexOf(" OS ") + 4, agent.indexOf(" like"));
log.info("addUnion4Special version={}", version);
bo.setTd("ios_" + version.replaceAll("_", "."));
bo.setClient_type("ios");
bo.setAppid("490655927");
log.info("addUnion4Special request={}", bo);
unionService.clickUnion(bo);
log.info("addUnion4Special with result is {}, and request is {}", response, bo);
clickUnion.info("addUnion4Special request is {}", bo);
response.setStatus(301);
response.sendRedirect("https://itunes.apple.com/cn/app/yoho!-you-huo/id490655927?mt=8");
} catch (IOException e) {
log.error("addUnion4Special error with request={}", bo, e);
}
}
public static void main(String[] args) {
String agent = "Mozilla/5.0 (iPhone; CPU iPhone OS like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile TADChid/0 AppVersion/4.5.0";
agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13B143 TADChid/0 AppVersion/4.8.5";
if (agent.toLowerCase().indexOf("mac os x") < 0 && agent.toLowerCase().indexOf("iphone") < 0) {
return;
}
if (agent.indexOf(" like") < agent.indexOf(" OS ") + 4) {
return;
}
String version = agent.substring(agent.indexOf(" OS ") + 4, agent.indexOf(" like"));
log.info("addUnion4Special version={}", version);
}
@RequestMapping("/addUnion4Jump")
public void addUnion4Stream(ClickUnionRequestBO bo, HttpServletRequest request, HttpServletResponse response) {
log.info("addUnion4Stream ClickUnionRequestBO{}", bo);
if (StringUtils.isEmpty(bo.getUnion_type())) {
return;
}
String clientIp = null;
String IP = RemoteIPInterceptor.getRemoteIP();
if(StringUtils.isNotEmpty(IP)){
String[] IPS = IP.split(",");
clientIp = IPS[0];
}
bo.setClientIp(clientIp);
bo.setClient_type("android");
bo.setAppid("com.yoho");
String agent = request.getHeader("user-agent");
// agent = "momochat/7.5.6 ios/664 (iphone 6s; ios 10.1.1; zh-hans_ae; iphone8,1; s1)";
agent = agent.toLowerCase();
log.info("addUnion4Stream user-agent={}", agent);
String url = "http://cdn.yoho.cn/app-downfiles/yohoBuy_YOHO_2953.apk";
try {
if (!StringUtils.isEmpty(agent)) {
if (agent.indexOf("mac os x") >= 0 || agent.indexOf("iphone") >= 0 ||agent.indexOf("ipad")>0) {
if(agent.contains("os")&&agent.contains("like")){
String version = agent.substring(agent.indexOf(" os ") + 4, agent.indexOf(" like"));
bo.setTd("ios_" + version.replaceAll("_", "."));
bo.setClient_type("ios");
bo.setAppid("490655927");
url = "https://itunes.apple.com/cn/app/yoho!-you-huo/id490655927?mt=8";
}
//youku;5.4;ios;10.2;iphone6,1 ---这种情况比较多
if(agent.contains("youku")){
int first = agent.indexOf("ios;")+4;
String version1 = agent.substring(first);
String version = version1.substring(version1.indexOf(" ")+1, version1.indexOf(";"));
bo.setTd("ios_" + version.replaceAll("_", "."));
bo.setClient_type("ios");
bo.setAppid("490655927");
url = "https://itunes.apple.com/cn/app/yoho!-you-huo/id490655927?mt=8";
}
//momochat/7.5.6 ios/664 (iphone 6; ios 10.2; zh_cn; iphone7,2; s1)
if(agent.contains("momochat")){
String version = "";
if(agent.contains("ios")){
int first = agent.indexOf(" ios ")+4;
String version1 = agent.substring(first);
version = version1.substring(version1.indexOf(" ")+1, version1.indexOf(";"));
bo.setTd("ios_" + version.replaceAll("_", "."));
bo.setClient_type("ios");
bo.setAppid("490655927");
url = "https://itunes.apple.com/cn/app/yoho!-you-huo/id490655927?mt=8";
} //momochat/7.5.6 ios/664 (iphone 6s; iphone os 9.2; zh_cn; iphone8,1; s1)
else if(agent.contains("os")){
int first = agent.indexOf(" os ")+3;
String version1 = agent.substring(first);
version = version1.substring(version1.indexOf(" ")+1, version1.indexOf(";"));
bo.setTd("ios_" + version.replaceAll("_", "."));
bo.setClient_type("ios");
bo.setAppid("490655927");
url = "https://itunes.apple.com/cn/app/yoho!-you-huo/id490655927?mt=8";
}
}
//ios/6.6 com.jugg.doctor ipad5,3/8.3/768x1024/2.0 /1---这个应该就是ios_6.6
if(agent.contains("ios/")){
String version = agent.substring(agent.indexOf("ios/")+4,agent.indexOf(" "));
bo.setTd("ios_" + version.replaceAll("_", "."));
bo.setClient_type("ios");
bo.setAppid("490655927");
url = "https://itunes.apple.com/cn/app/yoho!-you-huo/id490655927?mt=8";
}
if (agent.contains("ios")){
//user-agent=momochat/7.5.6 ios/664 (iphone 6; ios 10.2; zh_cn; iphone7,2; s1)
int first = agent.indexOf(" ios ")+4;
String version1 = agent.substring(first);
String version = version1.substring(version1.indexOf(" ")+1, version1.indexOf(";"));
bo.setTd("ios_" + version.replaceAll("_", "."));
bo.setClient_type("ios");
bo.setAppid("490655927");
url = "https://itunes.apple.com/cn/app/yoho!-you-huo/id490655927?mt=8";
}
} else {
int index = agent.lastIndexOf("android");
if (index > 0) {
String version = agent.substring(index + 8, index + 11);
bo.setTd("android_" + version.replaceAll("_", ""));
bo.setClient_type("android");
bo.setAppid("com.yoho");
}
}
}
} catch (Exception e) {
log.error("addUnion4Stream error with request={}", bo, e);
}
try {
StringBuffer stringBuffer = new StringBuffer();
String bean = stringBuffer.append("UnionServiceImpl").append("_").append(bo.getUnion_type()).toString();
boolean containsBean = SpringContextUtil.containsBean(bean);
if (containsBean) {
IUnionService service = SpringContextUtil.getBean(bean, IUnionService.class);
bo = service.clickHttpRequestTOBO(request, bo);
}
unionService.clickUnion(bo);
clickUnion.info("addUnion4Jump request is {}",bo);
log.info("addUnion4Stream with request is {}", bo);
response.setStatus(200);
response.sendRedirect(url);
} catch (Exception e) {
log.error("addUnion4Stream error with request={}", bo, e);
}
}
@RequestMapping("/addMonitor")
@ResponseBody
public JSON addMonitor(ClickUnionRequestBO bo, HttpServletRequest request, HttpServletResponse response) {
if (StringUtils.isEmpty(bo.getUnion_type())) {
return new JSONObject();
}
log.info("addMonitor with param is {}", bo);
try {
Enumeration<String> e = request.getParameterNames();
while (e.hasMoreElements()) {
String key = e.nextElement();
log.info("addMonitor with key={}, value={}", key, request.getParameter(key));
}
String IP = RemoteIPInterceptor.getRemoteIP();
String clientIp = null;
if(StringUtils.isNotEmpty(IP)){
String[] IPS = IP.split(",");
clientIp = IPS[0];
}
bo.setClientIp(clientIp);
String agent = request.getHeader("user-agent");
log.info("addMonitor user-agent={}", agent);
try{
if (!StringUtils.isEmpty(agent)) {
if (agent.toLowerCase().indexOf("mac os x") >= 0 || agent.toLowerCase().indexOf("iphone") >= 0) {
String version = agent.substring(agent.indexOf(" OS ") + 4, agent.indexOf(" like"));
bo.setTd("ios_" + version.replaceAll("_", "."));
bo.setClient_type("ios");
bo.setAppid("490655927");
}else {
int index = agent.indexOf("android");
if (index > 0) {
String version = agent.substring(index + 8, index + 11);
bo.setTd("android_"+version.replaceAll("_",""));
bo.setClient_type("android");
bo.setAppid("com.yoho");
}
}
}
}catch (Exception e1){
log.error("addMonitor error with request={}",e1.getMessage());
}
log.info("addMonitor request={}", bo);
//根据union_type区分不同的厂商
String union_type = request.getParameter("union_type");
//先去union_type_match表里面查看,是否有对应关系
UnionTypeMatch unionTypeMatch = unionTypeMatchDAO.selectByPrimaryKey(Long.valueOf(union_type));
if(unionTypeMatch!=null && StringUtils.isNotEmpty(String.valueOf(unionTypeMatch.getUnionTypeMatch()))){
union_type = String.valueOf(unionTypeMatch.getUnionTypeMatch());
}
StringBuffer stringBuffer = new StringBuffer();
String bean = stringBuffer.append("UnionServiceImpl").append("_").append(union_type).toString();
boolean containsBean = SpringContextUtil.containsBean(bean);
if(containsBean){
IUnionService service = SpringContextUtil.getBean(bean, IUnionService.class);
bo = service.clickHttpRequestTOBO(request,bo);
}
unionService.clickUnion(bo);
clickUnion.info("addMonitor with param is {}", bo);
log.info("addMonitor with result is {}, and request is {}", response, bo);
response.setStatus(200);
JSONObject j = new JSONObject();
j.put("ret", "0");
return j;
} catch (Exception e) {
log.error("addMonitor error with request={}", bo, e);
}
return new JSONObject();
}
}