EntranceController.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.persagy.web.controller.entrance;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.InputStream;
  4. import java.net.URLDecoder;
  5. import java.util.Map;
  6. import javax.annotation.Resource;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import org.apache.log4j.Logger;
  10. import org.springframework.context.ApplicationContext;
  11. import org.springframework.context.annotation.Scope;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.web.bind.annotation.PathVariable;
  14. import org.springframework.web.bind.annotation.RequestBody;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RequestParam;
  17. import org.springframework.web.bind.annotation.ResponseBody;
  18. import com.persagy.constant.WeatherConstant;
  19. import com.persagy.core.component.JsonObjectMapper;
  20. import com.persagy.core.constant.EMSConstant.Result;
  21. import com.persagy.core.dto.interpreter.InterfaceResult;
  22. import com.persagy.core.service.BusinessService;
  23. import com.persagy.core.utils.CommonUtils;
  24. import com.persagy.weather.constant.Const;
  25. import com.persagy.weather.constant.ConstBusinessStatic;
  26. import com.persagy.weather.thread.RequestIpStatisticThread;
  27. @Scope("prototype")
  28. @Controller("entranceController")
  29. @RequestMapping("/entrance")
  30. public class EntranceController {
  31. @Resource(name = "objectMapper")
  32. protected JsonObjectMapper objectMapper;
  33. private static final Logger log = Logger.getLogger(EntranceController.class);
  34. public static final String HANDLE_TYPE = "handleType";
  35. public static final String JSON_STRING = "jsonString";
  36. @Resource
  37. protected ApplicationContext context;
  38. @RequestMapping(value = "/unifier/{" + HANDLE_TYPE + "}")
  39. @ResponseBody
  40. public InterfaceResult unifier(@PathVariable(HANDLE_TYPE) String handleType,
  41. @RequestParam(value = JSON_STRING) String jsonString, HttpServletRequest request) throws Exception {
  42. String ip = getIpAddr(request);
  43. ConstBusinessStatic.add(ip, handleType);
  44. RequestIpStatisticThread.addupdateIpApiEndDateMap(ip, handleType);
  45. Long start = System.currentTimeMillis();
  46. BusinessService businessService = (BusinessService) context.getBean(handleType);
  47. InterfaceResult interfaceResult = CommonUtils.createInterfaceResult();
  48. try {
  49. jsonString = URLDecoder.decode(jsonString, "UTF-8");
  50. businessService.handle(jsonString, interfaceResult.getContent());
  51. interfaceResult.setResult(Result.SUCCESS);
  52. } catch (Exception e) {
  53. interfaceResult.setResult(Result.FAILURE);
  54. interfaceResult.setReason(e.getMessage());
  55. interfaceResult.getContent().clear();
  56. log.error("Request Error! requesterIp="+ip+" API=" + handleType + ", Param=" + jsonString + ", Msg=" + e.getMessage(), e);
  57. }
  58. long end = System.currentTimeMillis();
  59. long timeInterval = (end - start.longValue()) / 1000L;
  60. if (timeInterval > 3L) {
  61. log.warn("TimeCost Warning! requesterIp=" +ip+ timeInterval + "s, API=" + handleType + ", Param=" + jsonString);
  62. }
  63. return interfaceResult;
  64. }
  65. @RequestMapping(value = "/unifierJson/{" + HANDLE_TYPE + "}")
  66. @ResponseBody
  67. public InterfaceResult unifierJson(@PathVariable(HANDLE_TYPE) String handleType,
  68. @RequestBody Map<String, Object> param, HttpServletRequest request) throws Exception {
  69. String ip = getIpAddr(request);
  70. ConstBusinessStatic.add(ip, handleType);
  71. RequestIpStatisticThread.addupdateIpApiEndDateMap(ip, handleType);
  72. Long start = System.currentTimeMillis();
  73. BusinessService businessService = (BusinessService) context.getBean(handleType);
  74. InterfaceResult interfaceResult = CommonUtils.createInterfaceResult();
  75. String jsonString = objectMapper.writeValueAsString(param);
  76. try {
  77. jsonString = URLDecoder.decode(jsonString, "UTF-8");
  78. businessService.handle(jsonString, interfaceResult.getContent());
  79. interfaceResult.setResult(Result.SUCCESS);
  80. } catch (Exception e) {
  81. interfaceResult.setResult(Result.FAILURE);
  82. interfaceResult.setReason(e.getMessage());
  83. interfaceResult.getContent().clear();
  84. log.error("Request Error! requesterIp="+ip+" API=" + handleType + ", Param=" + jsonString + ", Msg=" + e.getMessage(), e);
  85. }
  86. long end = System.currentTimeMillis();
  87. long timeInterval = (end - start.longValue()) / 1000L;
  88. if (timeInterval > 3L) {
  89. log.warn("TimeCost Warning! requesterIp=" +ip+ timeInterval + "s, API=" + handleType + ", Param=" + jsonString);
  90. }
  91. return interfaceResult;
  92. }
  93. @RequestMapping(value = "/download/icon/{param}")
  94. public void iconDownload(@PathVariable("param") String param, HttpServletRequest request, HttpServletResponse response) throws Exception {
  95. String ip = getIpAddr(request);
  96. ConstBusinessStatic.add(ip, "download-icon");
  97. RequestIpStatisticThread.addupdateIpApiEndDateMap(ip, "download-icon");
  98. String weatherCode = "", theme = "", size = "";
  99. if(param != null) {
  100. if(param.contains("-")) {
  101. String[] paramArray = param.split("-");
  102. weatherCode = paramArray[0];
  103. if(paramArray.length>1) {
  104. theme = paramArray[1];
  105. }
  106. if(paramArray.length>2) {
  107. size = paramArray[2];
  108. }
  109. }else {
  110. weatherCode = param;
  111. }
  112. }
  113. if(WeatherConstant.getStandCodeDesc().keySet().contains(weatherCode)) {
  114. if(null == theme || !Const.iconTheme.contains(theme)) {
  115. theme = Const.iconTheme.get(0);
  116. }
  117. size = size == null ? "" : size;
  118. if(!Const.iconSize.keySet().contains(size)) {
  119. size = Const.iconSize.lastEntry().getValue();
  120. }else {
  121. size = Const.iconSize.get(size);
  122. }
  123. String fileName = weatherCode + "@" + size + "x.png";
  124. String filePath = "/static/icon/" + theme + "/" + fileName;
  125. InputStream in = this.getClass().getResourceAsStream(filePath);
  126. byte[] body = null;
  127. ByteArrayOutputStream output = new ByteArrayOutputStream();
  128. if(null != in) {
  129. byte[] buffer = new byte[4096];
  130. int n = 0;
  131. while (-1 != (n = in.read(buffer))) {
  132. output.write(buffer, 0, n);
  133. }
  134. body = output.toByteArray();
  135. in.close();
  136. output.close();
  137. }
  138. response.setContentType("image/png");
  139. response.getOutputStream().write(body);
  140. // 设置浏览器访问下载
  141. // response.addHeader("Content-Disposition", "attachment;filename="+fileName);
  142. }
  143. }
  144. private String getIpAddr(HttpServletRequest request) {
  145. String ip="";
  146. if(request!=null){
  147. ip = request.getHeader("x-forwarded-for");
  148. if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  149. ip = request.getHeader("Proxy-Client-IP");
  150. }
  151. if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  152. ip = request.getHeader("WL-Proxy-Client-IP");
  153. }
  154. if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  155. ip = request.getRemoteAddr();
  156. }
  157. }
  158. return ip;
  159. }
  160. }