diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ServiceParamValidator.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ServiceParamValidator.java index 80a3bd65..31ca519f 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ServiceParamValidator.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ServiceParamValidator.java @@ -22,11 +22,10 @@ public class ServiceParamValidator implements ParamValidator { public static final String COMMA = ","; private static Object[] EMPTY_OBJ_ARRAY = {}; - private static ValidatorFactory factory; private static javax.validation.Validator validator; static { - factory = Validation.buildDefaultValidatorFactory(); + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); validator = factory.getValidator(); } diff --git a/sop-test/src/main/java/com/gitee/sop/test/Client.java b/sop-test/src/main/java/com/gitee/sop/test/Client.java new file mode 100644 index 00000000..023d68ae --- /dev/null +++ b/sop-test/src/main/java/com/gitee/sop/test/Client.java @@ -0,0 +1,175 @@ +package com.gitee.sop.test; + +import com.alibaba.fastjson.JSON; +import com.gitee.sop.test.alipay.AlipayApiException; +import com.gitee.sop.test.alipay.AlipaySignature; +import lombok.Data; + +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * 简易客户端 + * @author tanghc + */ +@Data +public class Client { + private static HttpTool httpTool = new HttpTool(); + + private String url; + private String appId; + private String privateKey; + + private Callback callback; + + public Client(String url, String appId, String privateKey) { + this.url = url; + this.appId = appId; + this.privateKey = privateKey; + } + + public Client(String url, String appId, String privateKey, Callback callback) { + this.url = url; + this.appId = appId; + this.privateKey = privateKey; + this.callback = callback; + } + + public String execute(RequestBuilder requestBuilder) { + RequestInfo requestInfo = requestBuilder.build(appId, privateKey); + HttpTool.HTTPMethod httpMethod = requestInfo.getHttpMethod(); + boolean postJson = requestInfo.isPostJson(); + Map form = requestInfo.getForm(); + Map header = requestInfo.getHeader(); + String requestUrl = requestInfo.getUrl() != null ? requestInfo.getUrl() : url; + String responseData = null; + try { + // 发送请求 + if (httpMethod == HttpTool.HTTPMethod.POST && postJson) { + responseData = httpTool.requestJson(requestUrl, JSON.toJSONString(form), header); + } else { + responseData = httpTool.request(requestUrl, form, header, httpMethod); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + if (callback != null) { + callback.callback(requestInfo, responseData); + } + return responseData; + } + + public interface Callback { + void callback(RequestInfo requestInfo, String responseData); + } + + public static class RequestBuilder { + private static final String DEFAULT_VERSION = "1.0"; + + private String url; + private String method; + private String version = DEFAULT_VERSION; + private Map bizContent; + private HttpTool.HTTPMethod httpMethod; + private Map header; + private boolean ignoreSign; + private boolean postJson; + + public RequestBuilder url(String url) { + this.url = url; + return this; + } + + public RequestBuilder method(String method) { + this.method = method; + return this; + } + + public RequestBuilder version(String version) { + this.version = version; + return this; + } + + public RequestBuilder bizContent(Map bizContent) { + this.bizContent = bizContent; + return this; + } + + public RequestBuilder httpMethod(HttpTool.HTTPMethod httpMethod) { + this.httpMethod = httpMethod; + return this; + } + + public RequestBuilder header(Map header) { + this.header = header; + return this; + } + + public RequestBuilder ignoreSign(boolean ignoreSign) { + this.ignoreSign = ignoreSign; + return this; + } + + public RequestBuilder postJson(boolean postJson) { + this.postJson = postJson; + return this; + } + + + public RequestInfo build(String appId, String privateKey) { + // 公共请求参数 + Map params = new HashMap(); + params.put("app_id", appId); + if (method != null) { + params.put("method", method); + } + if (version != null) { + params.put("version", version); + } + params.put("format", "json"); + params.put("charset", "utf-8"); + params.put("sign_type", "RSA2"); + params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); + + // 业务参数 + params.put("biz_content", JSON.toJSONString(bizContent == null ? Collections.emptyMap() : bizContent)); + + if (!ignoreSign) { + String content = AlipaySignature.getSignContent(params); + String sign = null; + try { + sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8"); + } catch (AlipayApiException e) { + throw new RuntimeException(e); + } + params.put("sign", sign); + } + + RequestInfo requestInfo = new RequestInfo(); + requestInfo.setUrl(url); + requestInfo.setMethod(method); + requestInfo.setVersion(version); + requestInfo.setForm(params); + requestInfo.setHeader(header); + requestInfo.setPostJson(postJson); + requestInfo.setHttpMethod(httpMethod); + return requestInfo; + } + } + + @Data + public static class RequestInfo { + private String url; + private String method; + private String version; + private boolean postJson; + private Map form; + private Map header; + private HttpTool.HTTPMethod httpMethod; + } + +} diff --git a/sop-test/src/main/java/com/gitee/sop/test/HttpTool.java b/sop-test/src/main/java/com/gitee/sop/test/HttpTool.java index 68e6031d..39025dc0 100644 --- a/sop-test/src/main/java/com/gitee/sop/test/HttpTool.java +++ b/sop-test/src/main/java/com/gitee/sop/test/HttpTool.java @@ -1,6 +1,5 @@ package com.gitee.sop.test; -import com.alibaba.fastjson.JSON; import lombok.Data; import lombok.Getter; import lombok.Setter; @@ -112,9 +111,6 @@ public class HttpTool { * @throws IOException */ public String request(String url, Map form, Map header, HTTPMethod method) throws IOException { - if (method == HTTPMethod.JSON) { - return requestJson(url, JSON.toJSONString(form), header); - } Request.Builder requestBuilder = buildRequestBuilder(url, form, method); // 添加header addHeader(requestBuilder, header); @@ -264,8 +260,7 @@ public class HttpTool { POST, PUT, HEAD, - DELETE, - JSON; + DELETE; private HTTPMethod() { } diff --git a/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java b/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java index f2bc1546..3eb66c28 100644 --- a/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java @@ -2,16 +2,9 @@ package com.gitee.sop.test; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import com.gitee.sop.test.alipay.AlipayApiException; -import com.gitee.sop.test.alipay.AlipaySignature; import org.junit.Assert; -import java.io.IOException; -import java.text.SimpleDateFormat; -import java.util.Collections; -import java.util.Date; import java.util.HashMap; -import java.util.Map; import java.util.Optional; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; @@ -23,120 +16,130 @@ import java.util.concurrent.atomic.AtomicInteger; */ public class AllInOneTest extends TestBase { - static String url = "http://localhost:8081/api"; - static String appId = "2019032617262200001"; - static String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf/K8wX5T4EB1b8x8QJ7Ua4GcIUqeUxGHdQpzNbJdaQvoi06lgccmL+PHzminkFYON7alj1CjDN833j7QMHdPtS9l7B67fOU/p2LAAkPMtoVBfxQt9aFj7B8rEhGCz02iJIBAgMBAAECggEARqOuIpY0v6WtJBfmR3lGIOOokLrhfJrGTLF8CiZMQha+SRJ7/wOLPlsH9SbjPlopyViTXCuYwbzn2tdABigkBHYXxpDV6CJZjzmRZ+FY3S/0POlTFElGojYUJ3CooWiVfyUMhdg5vSuOq0oCny53woFrf32zPHYGiKdvU5Djku1onbDU0Lw8w+5tguuEZ76kZ/lUcccGy5978FFmYpzY/65RHCpvLiLqYyWTtaNT1aQ/9pw4jX9HO9NfdJ9gYFK8r/2f36ZE4hxluAfeOXQfRC/WhPmiw/ReUhxPznG/WgKaa/OaRtAx3inbQ+JuCND7uuKeRe4osP2jLPHPP6AUwQKBgQDUNu3BkLoKaimjGOjCTAwtp71g1oo+k5/uEInAo7lyEwpV0EuUMwLA/HCqUgR4K9pyYV+Oyb8d6f0+Hz0BMD92I2pqlXrD7xV2WzDvyXM3s63NvorRooKcyfd9i6ccMjAyTR2qfLkxv0hlbBbsPHz4BbU63xhTJp3Ghi0/ey/1HQKBgQC2VsgqC6ykfSidZUNLmQZe3J0p/Qf9VLkfrQ+xaHapOs6AzDU2H2osuysqXTLJHsGfrwVaTs00ER2z8ljTJPBUtNtOLrwNRlvgdnzyVAKHfOgDBGwJgiwpeE9voB1oAV/mXqSaUWNnuwlOIhvQEBwekqNyWvhLqC7nCAIhj3yvNQKBgQCqYbeec56LAhWP903Zwcj9VvG7sESqXUhIkUqoOkuIBTWFFIm54QLTA1tJxDQGb98heoCIWf5x/A3xNI98RsqNBX5JON6qNWjb7/dobitti3t99v/ptDp9u8JTMC7penoryLKK0Ty3bkan95Kn9SC42YxaSghzqkt+uvfVQgiNGQKBgGxU6P2aDAt6VNwWosHSe+d2WWXt8IZBhO9d6dn0f7ORvcjmCqNKTNGgrkewMZEuVcliueJquR47IROdY8qmwqcBAN7Vg2K7r7CPlTKAWTRYMJxCT1Hi5gwJb+CZF3+IeYqsJk2NF2s0w5WJTE70k1BSvQsfIzAIDz2yE1oPHvwVAoGAA6e+xQkVH4fMEph55RJIZ5goI4Y76BSvt2N5OKZKd4HtaV+eIhM3SDsVYRLIm9ZquJHMiZQGyUGnsvrKL6AAVNK7eQZCRDk9KQz+0GKOGqku0nOZjUbAu6A2/vtXAaAuFSFx1rUQVVjFulLexkXR3KcztL1Qu2k5pB6Si0K/uwQ="; + String url = "http://localhost:8081/api"; + String appId = "2019032617262200001"; + String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf/K8wX5T4EB1b8x8QJ7Ua4GcIUqeUxGHdQpzNbJdaQvoi06lgccmL+PHzminkFYON7alj1CjDN833j7QMHd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1BSvQsfIzAIDz2yE1oPHvwVAoGAA6e+xQkVH4fMEph55RJIZ5goI4Y76BSvt2N5OKZKd4HtaV+eIhM3SDsVYRLIm9ZquJHMiZQGyUGnsvrKL6AAVNK7eQZCRDk9KQz+0GKOGqku0nOZjUbAu6A2/vtXAaAuFSFx1rUQVVjFulLexkXR3KcztL1Qu2k5pB6Si0K/uwQ="; - Client client = new Client() - .url(url) - .appId(appId) - .privateKey(privateKey) - .callback(AllInOneTest::assertResult); + private Client client = new Client(url, appId, privateKey, AllInOneTest::assertResult); /** - * 校验基本功能 - * - * @throws AlipayApiException + * 以get方式提交 */ - public void testBase() { - client.request( - "alipay.story.get" - , "1.0" - , new BizContent().add("id", "1").add("name", "葫芦娃") - , HttpTool.HTTPMethod.GET - ); + public void testGet() { + Client.RequestBuilder requestBuilder = new Client.RequestBuilder() + .method("alipay.story.get") + .version("1.0") + .bizContent(new BizContent().add("id", "1").add("name", "葫芦娃")) + .httpMethod(HttpTool.HTTPMethod.GET); + + client.execute(requestBuilder); + } - client.request( - "alipay.story.get" - , "1.0" - , new BizContent().add("id", "1").add("name", "葫芦娃") - , HttpTool.HTTPMethod.POST - ); + /** + * 以表单方式提交(application/x-www-form-urlencoded) + */ + public void testPostForm() { + Client.RequestBuilder requestBuilder = new Client.RequestBuilder() + .method("alipay.story.get") + .version("1.0") + .bizContent(new BizContent().add("id", "1").add("name", "葫芦娃")) + .httpMethod(HttpTool.HTTPMethod.POST); + + client.execute(requestBuilder); + } - client.request( - "alipay.story.get" - , "1.0" - , new BizContent().add("id", "1").add("name", "葫芦娃") - , HttpTool.HTTPMethod.JSON - ); + /** + * 以json方式提交(application/json) + */ + public void testPostJSON() { + Client.RequestBuilder requestBuilder = new Client.RequestBuilder() + .method("alipay.story.get") + .version("1.0") + // 以json方式提交 + .postJson(true) + .bizContent(new BizContent().add("id", "1").add("name", "葫芦娃")) + .httpMethod(HttpTool.HTTPMethod.POST); + + client.execute(requestBuilder); } /** * 测试feign。gateway -> book-service(consumer) -> story-service(provider) */ public void testFeign() { - client.request( - "alipay.book.story.get" - , "1.0" - , new BizContent() - , HttpTool.HTTPMethod.GET - ); + Client.RequestBuilder requestBuilder = new Client.RequestBuilder() + .method("alipay.book.story.get") + .version("1.0") + .bizContent(new BizContent()) + .httpMethod(HttpTool.HTTPMethod.GET); + + client.execute(requestBuilder); } /** * 测试dubbo服务,book会调用story提供的服务。参见:DemoConsumerController.java */ public void testDubbo() { - client.request( - "dubbo.story.get" - , "1.0" - , new BizContent().add("id", "222") - , HttpTool.HTTPMethod.GET - ); + Client.RequestBuilder requestBuilder = new Client.RequestBuilder() + .method("dubbo.story.get") + .version("1.0") + .bizContent(new BizContent().add("id", "222")) + .httpMethod(HttpTool.HTTPMethod.GET); + + client.execute(requestBuilder); + } /** * 忽略验证,不校验签名,只需传接口名、版本号、业务参数 */ public void testIgnoreSign() { - new Client() - .url(url) - .appId(appId) - .privateKey(privateKey) + Client.RequestBuilder requestBuilder = new Client.RequestBuilder() + .method("story.get") + .version("2.1") .ignoreSign(true) - .request( - "story.get" - , "2.1" - , new BizContent().add("id", "222").add("name", "忽略222") - , HttpTool.HTTPMethod.GET); + .bizContent(new BizContent().add("id", "222").add("name", "忽略222")) + .httpMethod(HttpTool.HTTPMethod.GET); + + client.execute(requestBuilder); } /** * JSR-303参数校验 */ public void testJSR303() { - client.request( - "goods.add" - , "1.0" - , new BizContent().add("goods_name", "iphone6").add("goods_remark", "iphone6").add("goods_comment", "1") - , HttpTool.HTTPMethod.POST - ); + Client.RequestBuilder requestBuilder = new Client.RequestBuilder() + .method("goods.add") + .version("1.0") + .bizContent(new BizContent().add("goods_name", "iphone6").add("goods_remark", "iphone6").add("goods_comment", "1")) + .httpMethod(HttpTool.HTTPMethod.POST); + client.execute(requestBuilder); } /** * 测试是否有权限访问,可在sop-admin中设置权限 */ public void testPermission() { - client.request("permission.story.get" - , "1.0" - , new BizContent() - , HttpTool.HTTPMethod.GET - ); + Client.RequestBuilder requestBuilder = new Client.RequestBuilder() + .method("permission.story.get") + .version("1.0") + .bizContent(new BizContent()) + .httpMethod(HttpTool.HTTPMethod.GET); + + client.execute(requestBuilder); } /** * 演示将接口名版本号跟在url后面,规则:http://host:port/{method}/{version}/ */ public void testRestful() { - new Client() + Client.RequestBuilder requestBuilder = new Client.RequestBuilder() .url("http://localhost:8081/alipay.story.get/1.0/") - .appId(appId) - .privateKey(privateKey) - .callback(((method, responseData) -> { - assertResult("alipay.story.get", responseData); - })) - .request(new BizContent().add("name", "name111"), HttpTool.HTTPMethod.GET); + .bizContent(new BizContent().add("name", "name111")) + .httpMethod(HttpTool.HTTPMethod.GET); + + client.execute(requestBuilder); } /** @@ -156,12 +159,13 @@ public class AllInOneTest extends TestBase { try { countDownLatch.await(); // 等在这里,执行countDownLatch.countDown();集体触发 // 业务方法 - client.request( - "alipay.story.get" - , "1.0" - , new BizContent().add("id", "1").add("name", "葫芦娃") - , HttpTool.HTTPMethod.GET - ); + Client.RequestBuilder requestBuilder = new Client.RequestBuilder() + .method("alipay.story.get") + .version("1.0") + .bizContent(new BizContent().add("id", "1").add("name", "葫芦娃")) + .httpMethod(HttpTool.HTTPMethod.GET); + + client.execute(requestBuilder); success.incrementAndGet(); } catch (Exception e) { } finally { @@ -175,88 +179,6 @@ public class AllInOneTest extends TestBase { System.out.println("成功次数:" + success); } - static class Client { - private String url; - private String appId; - private String privateKey; - private Callback callback; - private boolean ignoreSign; - - public Client url(String url) { - this.url = url; - return this; - } - - public Client appId(String appId) { - this.appId = appId; - return this; - } - - public Client privateKey(String privateKey) { - this.privateKey = privateKey; - return this; - } - - public Client callback(Callback callback) { - this.callback = callback; - return this; - } - - public Client ignoreSign(boolean ignoreSign) { - this.ignoreSign = ignoreSign; - return this; - } - - public String request(Map bizContent, HttpTool.HTTPMethod httpMethod) { - return request(null, null, bizContent, httpMethod); - } - - public String request(String method, String version, Map bizContent, HttpTool.HTTPMethod httpMethod) { - // 公共请求参数 - Map params = new HashMap(); - params.put("app_id", appId); - if (method != null) { - params.put("method", method); - } - if (version != null) { - params.put("version", version); - } - params.put("format", "json"); - params.put("charset", "utf-8"); - params.put("sign_type", "RSA2"); - params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); - - // 业务参数 - params.put("biz_content", JSON.toJSONString(bizContent == null ? Collections.emptyMap() : bizContent)); - - if (!ignoreSign) { - String content = AlipaySignature.getSignContent(params); - String sign = null; - try { - sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8"); - } catch (AlipayApiException e) { - throw new RuntimeException(e); - } - params.put("sign", sign); - } - String responseData = null; - try { - // 发送请求 - responseData = httpTool.request(url, params, null, httpMethod); - } catch (IOException e) { - throw new RuntimeException(e); - } - if (callback != null) { - callback.callback(method, responseData); - } - return responseData; - } - - interface Callback { - void callback(String method, String responseData); - } - } - class BizContent extends HashMap { public BizContent add(String key, String value) { this.put(key, value); @@ -264,11 +186,12 @@ public class AllInOneTest extends TestBase { } } - public static void assertResult(String method, String responseData) { + public static void assertResult(Client.RequestInfo requestInfo, String responseData) { + System.out.println(responseData); + String method = requestInfo.getMethod(); if (method == null) { return; } - System.out.println(responseData); String node = method.replace('.', '_') + "_response"; JSONObject jsonObject = JSON.parseObject(responseData).getJSONObject(node); String code = Optional.ofNullable(jsonObject).map(json -> json.getString("code")).orElse("20000");