ClickUnionRest.java 9.73 KB
/**
 * 
 */
package com.yoho.unions.server.restapi;

import java.io.IOException;
import java.util.Enumeration;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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;

	/**
	 * 添加联盟
	 * @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);
		clickUnion.info("addUnion with param is {}", request);
		if ("iphone".equals(request.getClient_type())) {
			request.setClient_type(ClientTypeEnum.IOS.getName());
		}
		request.setClientIp(RemoteIPInterceptor.getRemoteIP());
		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);
		}

		request.setClientIp(RemoteIPInterceptor.getRemoteIP());
//		IUnionService service = SpringContextUtil.getBean(bean, IUnionService.class);
		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);
		clickUnion.info("addUnion4Special with param is {}", bo);
		try {
			bo.setClientIp(RemoteIPInterceptor.getRemoteIP());
			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);
			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) {
		if (StringUtils.isEmpty(bo.getUnion_type())) {
			return;
		}
		bo.setClientIp(RemoteIPInterceptor.getRemoteIP());
		bo.setClient_type("android");
		bo.setAppid("com.yoho");
		String agent = request.getHeader("user-agent");
		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) {
					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";
				} 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 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);
			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);
		clickUnion.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));
			}

			bo.setClientIp(RemoteIPInterceptor.getRemoteIP());
			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");
			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);
			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();
	}

}