package com.yihu.hos.controllers; import com.yihu.hos.core.constants.ExceptionConstant; import com.yihu.hos.core.exception.ESBException; import com.yihu.hos.models.GatewayRequsetResult; import com.yihu.hos.models.GatewayResponseResult; import com.yihu.hos.services.GatewayService; import net.sf.json.JSONObject; import org.apache.commons.beanutils.BeanUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.Writer; @Controller @RequestMapping("/esb") public class GatewayController { @Resource(name = GatewayService.BEAN_ID) private GatewayService gatewayService; @ResponseBody @RequestMapping(value = "/gateway", method = RequestMethod.POST) public void transfer(HttpServletRequest request, HttpServletResponse response) throws Exception { request.setCharacterEncoding("UTF-8"); response.setContentType("application/json;charset=UTF-8"); Writer writer = response.getWriter(); response.setCharacterEncoding("UTF-8"); String returnString; String resultData; GatewayRequsetResult gatewayRequsetResult = new GatewayRequsetResult(); try { BeanUtils.populate(gatewayRequsetResult, request.getParameterMap()); //---end gatewayService.paramsIsNotNull(gatewayRequsetResult.getApi()); resultData = gatewayService.getResultData(gatewayRequsetResult); returnString = resultData; } catch (Exception e) { e.printStackTrace(); if (e instanceof ESBException) { ESBException esbException = (ESBException) e; returnString = JSONObject.fromObject(GatewayResponseResult.getError(esbException.getExceptionCode(), esbException.getExceptionMessage())).toString(); } else { returnString = JSONObject.fromObject(GatewayResponseResult.getError(ExceptionConstant.EHREXCEPTION_SYSTEMEXCEPTION, ExceptionConstant.EHREXCEPTION_SYSTEMEXCEPTION_MESSAGE)).toString(); } } writer.write(returnString); writer.flush(); writer.close(); } }