GatewayController.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package com.yihu.hos.controllers;
  2. import com.yihu.hos.core.constants.ExceptionConstant;
  3. import com.yihu.hos.core.exception.ESBException;
  4. import com.yihu.hos.models.GatewayRequsetResult;
  5. import com.yihu.hos.models.GatewayResponseResult;
  6. import com.yihu.hos.services.GatewayService;
  7. import net.sf.json.JSONObject;
  8. import org.apache.commons.beanutils.BeanUtils;
  9. import org.springframework.stereotype.Controller;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.bind.annotation.ResponseBody;
  13. import javax.annotation.Resource;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import java.io.Writer;
  17. @Controller
  18. @RequestMapping("/esb")
  19. public class GatewayController {
  20. @Resource(name = GatewayService.BEAN_ID)
  21. private GatewayService gatewayService;
  22. @ResponseBody
  23. @RequestMapping(value = "/gateway", method = RequestMethod.POST)
  24. public void transfer(HttpServletRequest request, HttpServletResponse response) throws Exception {
  25. request.setCharacterEncoding("UTF-8");
  26. response.setContentType("application/json;charset=UTF-8");
  27. Writer writer = response.getWriter();
  28. response.setCharacterEncoding("UTF-8");
  29. String returnString;
  30. String resultData;
  31. GatewayRequsetResult gatewayRequsetResult = new GatewayRequsetResult();
  32. try {
  33. BeanUtils.populate(gatewayRequsetResult, request.getParameterMap());
  34. //---end
  35. gatewayService.paramsIsNotNull(gatewayRequsetResult.getApi());
  36. resultData = gatewayService.getResultData(gatewayRequsetResult);
  37. returnString = resultData;
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. if (e instanceof ESBException) {
  41. ESBException esbException = (ESBException) e;
  42. returnString = JSONObject.fromObject(GatewayResponseResult.getError(esbException.getExceptionCode(), esbException.getExceptionMessage())).toString();
  43. } else {
  44. returnString = JSONObject.fromObject(GatewayResponseResult.getError(ExceptionConstant.EHREXCEPTION_SYSTEMEXCEPTION, ExceptionConstant.EHREXCEPTION_SYSTEMEXCEPTION_MESSAGE)).toString();
  45. }
  46. }
  47. writer.write(returnString);
  48. writer.flush();
  49. writer.close();
  50. }
  51. }