123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package com.sec.xinhua.gateway.handler;
- import com.sec.xinhua.gateway.beans.ApiParamType;
- import com.sec.xinhua.gateway.utils.ApiHandlerException;
- import org.apache.commons.lang3.StringUtils;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- public class ApiParamValueConvertor {
- private static final Log logger = LogFactory.getLog("RiskGateway");
- public static Object convert(Object httpValue, String name, int type) throws ApiHandlerException {
- Object invokeValue = null;
- try {
- //String
- if (type == ApiParamType.STRING.getCode()) {
- invokeValue = httpValue;
- }
- //说明:使用blank判断,避免客户端或者测试用例使用空字符串
- //int
- else if (type == ApiParamType.P_INT.getCode()) {
- invokeValue = Integer.parseInt(httpValue.toString());
- } else if (type == ApiParamType.W_INT.getCode()) {
- if (httpValue == null || StringUtils.isBlank(httpValue.toString())) {
- invokeValue = null;
- } else {
- invokeValue = Integer.parseInt(httpValue.toString());
- }
- }
- //double
- else if (type == ApiParamType.P_DOUBLE.getCode()) {
- invokeValue = Double.parseDouble(httpValue.toString());
- } else if (type == ApiParamType.W_DOUBLE.getCode()) {
- if (httpValue == null || StringUtils.isBlank(httpValue.toString())) {
- invokeValue = null;
- } else {
- invokeValue = Double.parseDouble(httpValue.toString());
- }
- }
- //byte
- else if (type == ApiParamType.P_BYTE.getCode()) {
- invokeValue = Byte.parseByte(httpValue.toString());
- } else if (type == ApiParamType.W_BYTE.getCode()) {
- if (httpValue == null || StringUtils.isBlank(httpValue.toString())) {
- invokeValue = null;
- } else {
- invokeValue = Byte.parseByte(httpValue.toString());
- }
- }
- //float
- else if (type == ApiParamType.P_FLOAT.getCode()) {
- invokeValue = Float.parseFloat(httpValue.toString());
- } else if (type == ApiParamType.W_FLOAT.getCode()) {
- if (httpValue == null || StringUtils.isBlank(httpValue.toString())) {
- invokeValue = null;
- } else {
- invokeValue = Float.parseFloat(httpValue.toString());
- }
- }
- //long
- else if (type == ApiParamType.P_LONG.getCode()) {
- invokeValue = Long.parseLong(httpValue.toString());
- } else if (type == ApiParamType.W_LONG.getCode()) {
- if (httpValue == null || StringUtils.isBlank(httpValue.toString())) {
- invokeValue = null;
- } else {
- invokeValue = Long.parseLong(httpValue.toString());
- }
- }
- //short
- else if (type == ApiParamType.P_SHORT.getCode()) {
- invokeValue = Short.parseShort(httpValue.toString());
- } else if (type == ApiParamType.W_SHORT.getCode()) {
- if (httpValue == null || StringUtils.isBlank(httpValue.toString())) {
- invokeValue = null;
- } else {
- invokeValue = Short.parseShort(httpValue.toString());
- }
- }
- //boolean
- else if (type == ApiParamType.P_BOOLEAN.getCode()) {
- String boolStr = httpValue.toString().trim();
- //修复boolean parse异常
- ApiParamType apt = ApiParamType.getByCode(type);
- if (StringUtils.isBlank(boolStr)) {
- throw new ApiHandlerException("Parse parameter error;type:"
- + (apt == null ? type : apt.getClassName()) + ",name:" + name
- + ",value:" + httpValue);
- } else {
- if (boolStr.equalsIgnoreCase("true")) {
- invokeValue = true;
- } else if (boolStr.equalsIgnoreCase("false")) {
- invokeValue = false;
- } else {
- throw new ApiHandlerException("value is not type:'" + apt.getClassName() + ", param name:"
- + name + ",param value:" + boolStr);
- }
- }
- } else if (type == ApiParamType.W_BOOLEAN.getCode()) {
- ApiParamType apt = ApiParamType.getByCode(type);
- if (httpValue == null) {
- invokeValue = null;
- } else {
- String boolStr = httpValue.toString();
- //修正
- if (StringUtils.isBlank(httpValue.toString())) {
- invokeValue = null;
- } else {
- if (boolStr.equalsIgnoreCase("true")) {
- invokeValue = Boolean.TRUE;
- } else if (boolStr.equalsIgnoreCase("false")) {
- invokeValue = Boolean.FALSE;
- } else {
- throw new ApiHandlerException("value is not type:'" + apt.getClassName() + ", param name:"
- + name + ",param value:" + boolStr);
- }
- }
- }
- } else {
- logger.error("Unsuported parametertype:'" + type + ", param name:" + name);
- throw new ApiHandlerException("Unsuported parametertype:'" + type + ", param name:" + name);
- }
- } catch (Exception e) {
- if (e instanceof ApiHandlerException) {
- throw (ApiHandlerException) e;
- }
- ApiParamType apt = ApiParamType.getByCode(type);
- throw new ApiHandlerException("Parse parameter error;type:" + (apt == null ? type : apt.getClassName())
- + ",name:" + name + ",value:" + httpValue, e);
- }
- return invokeValue;
- }
- }
|