ApiParamValueConvertor.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package com.sec.xinhua.gateway.handler;
  2. import com.sec.xinhua.gateway.beans.ApiParamType;
  3. import com.sec.xinhua.gateway.utils.ApiHandlerException;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.apache.commons.logging.Log;
  6. import org.apache.commons.logging.LogFactory;
  7. public class ApiParamValueConvertor {
  8. private static final Log logger = LogFactory.getLog("RiskGateway");
  9. public static Object convert(Object httpValue, String name, int type) throws ApiHandlerException {
  10. Object invokeValue = null;
  11. try {
  12. //String
  13. if (type == ApiParamType.STRING.getCode()) {
  14. invokeValue = httpValue;
  15. }
  16. //说明:使用blank判断,避免客户端或者测试用例使用空字符串
  17. //int
  18. else if (type == ApiParamType.P_INT.getCode()) {
  19. invokeValue = Integer.parseInt(httpValue.toString());
  20. } else if (type == ApiParamType.W_INT.getCode()) {
  21. if (httpValue == null || StringUtils.isBlank(httpValue.toString())) {
  22. invokeValue = null;
  23. } else {
  24. invokeValue = Integer.parseInt(httpValue.toString());
  25. }
  26. }
  27. //double
  28. else if (type == ApiParamType.P_DOUBLE.getCode()) {
  29. invokeValue = Double.parseDouble(httpValue.toString());
  30. } else if (type == ApiParamType.W_DOUBLE.getCode()) {
  31. if (httpValue == null || StringUtils.isBlank(httpValue.toString())) {
  32. invokeValue = null;
  33. } else {
  34. invokeValue = Double.parseDouble(httpValue.toString());
  35. }
  36. }
  37. //byte
  38. else if (type == ApiParamType.P_BYTE.getCode()) {
  39. invokeValue = Byte.parseByte(httpValue.toString());
  40. } else if (type == ApiParamType.W_BYTE.getCode()) {
  41. if (httpValue == null || StringUtils.isBlank(httpValue.toString())) {
  42. invokeValue = null;
  43. } else {
  44. invokeValue = Byte.parseByte(httpValue.toString());
  45. }
  46. }
  47. //float
  48. else if (type == ApiParamType.P_FLOAT.getCode()) {
  49. invokeValue = Float.parseFloat(httpValue.toString());
  50. } else if (type == ApiParamType.W_FLOAT.getCode()) {
  51. if (httpValue == null || StringUtils.isBlank(httpValue.toString())) {
  52. invokeValue = null;
  53. } else {
  54. invokeValue = Float.parseFloat(httpValue.toString());
  55. }
  56. }
  57. //long
  58. else if (type == ApiParamType.P_LONG.getCode()) {
  59. invokeValue = Long.parseLong(httpValue.toString());
  60. } else if (type == ApiParamType.W_LONG.getCode()) {
  61. if (httpValue == null || StringUtils.isBlank(httpValue.toString())) {
  62. invokeValue = null;
  63. } else {
  64. invokeValue = Long.parseLong(httpValue.toString());
  65. }
  66. }
  67. //short
  68. else if (type == ApiParamType.P_SHORT.getCode()) {
  69. invokeValue = Short.parseShort(httpValue.toString());
  70. } else if (type == ApiParamType.W_SHORT.getCode()) {
  71. if (httpValue == null || StringUtils.isBlank(httpValue.toString())) {
  72. invokeValue = null;
  73. } else {
  74. invokeValue = Short.parseShort(httpValue.toString());
  75. }
  76. }
  77. //boolean
  78. else if (type == ApiParamType.P_BOOLEAN.getCode()) {
  79. String boolStr = httpValue.toString().trim();
  80. //修复boolean parse异常
  81. ApiParamType apt = ApiParamType.getByCode(type);
  82. if (StringUtils.isBlank(boolStr)) {
  83. throw new ApiHandlerException("Parse parameter error;type:"
  84. + (apt == null ? type : apt.getClassName()) + ",name:" + name
  85. + ",value:" + httpValue);
  86. } else {
  87. if (boolStr.equalsIgnoreCase("true")) {
  88. invokeValue = true;
  89. } else if (boolStr.equalsIgnoreCase("false")) {
  90. invokeValue = false;
  91. } else {
  92. throw new ApiHandlerException("value is not type:'" + apt.getClassName() + ", param name:"
  93. + name + ",param value:" + boolStr);
  94. }
  95. }
  96. } else if (type == ApiParamType.W_BOOLEAN.getCode()) {
  97. ApiParamType apt = ApiParamType.getByCode(type);
  98. if (httpValue == null) {
  99. invokeValue = null;
  100. } else {
  101. String boolStr = httpValue.toString();
  102. //修正
  103. if (StringUtils.isBlank(httpValue.toString())) {
  104. invokeValue = null;
  105. } else {
  106. if (boolStr.equalsIgnoreCase("true")) {
  107. invokeValue = Boolean.TRUE;
  108. } else if (boolStr.equalsIgnoreCase("false")) {
  109. invokeValue = Boolean.FALSE;
  110. } else {
  111. throw new ApiHandlerException("value is not type:'" + apt.getClassName() + ", param name:"
  112. + name + ",param value:" + boolStr);
  113. }
  114. }
  115. }
  116. } else {
  117. logger.error("Unsuported parametertype:'" + type + ", param name:" + name);
  118. throw new ApiHandlerException("Unsuported parametertype:'" + type + ", param name:" + name);
  119. }
  120. } catch (Exception e) {
  121. if (e instanceof ApiHandlerException) {
  122. throw (ApiHandlerException) e;
  123. }
  124. ApiParamType apt = ApiParamType.getByCode(type);
  125. throw new ApiHandlerException("Parse parameter error;type:" + (apt == null ? type : apt.getClassName())
  126. + ",name:" + name + ",value:" + httpValue, e);
  127. }
  128. return invokeValue;
  129. }
  130. }