import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public abstract class AbstractServicePlugin implements IServicePlugin { private static final Logger LOG = LoggerFactory.getLogger(AbstractServicePlugin.class); /** * get value from map * @param map * @param key * @param defaultValue * @return * @author www */ public static <T> T v(Map<String, ?> map,String key,T defaultValue){ Object obj = map.get(key); 0 return obj == null ? defaultValue : (T)obj; } /** * judge if a value is null or empty * @param map * @param key * @return */ public static boolean isNotEmpty(Map<String, ?> map,String key){ return map.containsKey(key) && map.get(key) != null && String.valueOf(map.get(key)).length() > 0; } /** * make sif request * @param serverName serverName * @param methodName methodName * @param params * @return * @throws RosException */ public static Map<String, Object> sifRequest(String serverName,String methodName,Map<String, Object> params) throws RosException{ Map<String, Object> result = SifRequester.sendMessage(serverName, methodName, params); if(!"0000".equals(result.get("sif_response_Code"))){ LOG.error(String.format("sif调用异常,serverName=%s,methodName=%s,params=%s,sif_response_Msg=%s", serverName,methodName,params,result.get("sif_response_Msg"))); throw new RosException(ErrorCode.ERROR_SIF); }else{//返回干净的结果,去掉sif_response_Code与sif_response_Msg result.remove("sif_response_Msg"); result.remove("sif_response_Code"); return result; } } /** * download pics * @param params * @param key * @return * @throws RosException */ public static byte[] downloadPicsIfNeeded(Map<String,Object> params,String key) throws IOException{ if(params.get(key) instanceof byte[]) return (byte[])params.get(key); String token = v(params, key, "default_token"); return PicsDownloader.downloadPics(token); } /** * encode url string * @param str * @return */ public static String encode(Object str){ try { return str == null ? null : URLEncoder.encode(String.valueOf(str),DEFAULT_CHARSET); } catch (UnsupportedEncodingException e) { return String.valueOf(str); } } /** * decode url string * @param str * @return */ public static String decode(String str){ try { return URLDecoder.decode(str, DEFAULT_CHARSET); } catch (UnsupportedEncodingException e) { return str; } } @Override public boolean match(String filter) { return identity() == filter || identity().equals(filter); } }
111
最新推荐文章于 2023-08-04 07:30:00 发布