|
| 1 | +package com.jtool.http; |
| 2 | + |
| 3 | +import com.jtool.http.exception.RequestBeanErrorException; |
| 4 | +import org.apache.commons.beanutils.BeanUtils; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | + |
| 8 | +import java.io.UnsupportedEncodingException; |
| 9 | +import java.lang.reflect.InvocationTargetException; |
| 10 | +import java.net.URLEncoder; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +public class CommonCommand { |
| 16 | + |
| 17 | + private static Logger log = LoggerFactory.getLogger(CommonCommand.class); |
| 18 | + |
| 19 | + public static String readRequest(String host, String uri, Map<String, String> map) { |
| 20 | + |
| 21 | + if(log.isDebugEnabled()) { |
| 22 | + log.debug("curl '" + host + uri + "?" + join(getParamStrings(map)) + "'"); |
| 23 | + } |
| 24 | + |
| 25 | + String content; |
| 26 | + |
| 27 | + int i = 0; |
| 28 | + |
| 29 | + do { |
| 30 | + content = WebGet.sent(host + uri, map); |
| 31 | + i++; |
| 32 | + } while ((content == null || content.equals("")) && i < 5); |
| 33 | + |
| 34 | + log.debug("response: " + content); |
| 35 | + |
| 36 | + return content; |
| 37 | + } |
| 38 | + |
| 39 | + public static String writeRequest(String host, String uri, Map<String, String> map) { |
| 40 | + |
| 41 | + if(log.isDebugEnabled()) { |
| 42 | + log.debug("curl -X POST -d \"" + join(getParamStrings(map)) + "\" " + host + uri); |
| 43 | + } |
| 44 | + |
| 45 | + String content = WebPost.sent(host + uri, map); |
| 46 | + |
| 47 | + log.debug("response: " + content); |
| 48 | + |
| 49 | + return content; |
| 50 | + } |
| 51 | + |
| 52 | + public static String readRequest(String host, String uri, Object param) { |
| 53 | + return readRequest(host, uri, convertBeanToRequestMap(param)); |
| 54 | + } |
| 55 | + |
| 56 | + public static String writeRequest(String host, String uri, Object param) { |
| 57 | + return writeRequest(host, uri, convertBeanToRequestMap(param)); |
| 58 | + } |
| 59 | + |
| 60 | + private static List<String> getParamStrings(Map<String, ?> paramsMap) { |
| 61 | + List<String> paramsList = new ArrayList<>(); |
| 62 | + for(String key : paramsMap.keySet()){ |
| 63 | + try { |
| 64 | + if(paramsMap.get(key) != null) { |
| 65 | + paramsList.add(URLEncoder.encode(key, "UTF-8") + "=" + URLEncoder.encode(paramsMap.get(key).toString(), "UTF-8")); |
| 66 | + } |
| 67 | + } catch (UnsupportedEncodingException e) { |
| 68 | + e.printStackTrace(); |
| 69 | + } |
| 70 | + } |
| 71 | + return paramsList; |
| 72 | + } |
| 73 | + |
| 74 | + private static Map<String, String> convertBeanToRequestMap(Object bean) { |
| 75 | + Map<String, String> params; |
| 76 | + try { |
| 77 | + params = BeanUtils.describe(bean); |
| 78 | + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { |
| 79 | + throw new RequestBeanErrorException(e); |
| 80 | + } |
| 81 | + params.remove("class"); |
| 82 | + return params; |
| 83 | + } |
| 84 | + |
| 85 | + private static String join(final List<String> params){ |
| 86 | + StringBuilder sb = new StringBuilder(); |
| 87 | + for(int i = 0; i < params.size(); i++){ |
| 88 | + if(i != 0) { |
| 89 | + sb.append("&"); |
| 90 | + } |
| 91 | + sb.append(params.get(i)); |
| 92 | + } |
| 93 | + return sb.toString(); |
| 94 | + } |
| 95 | +} |
0 commit comments