ClickUnionRest.java 14.5 KB
/**
 * 
 */
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();
	}

}