修复SDKPOS异常

1.x
tanghc 6 years ago
parent a778ffb497
commit 8fb4a66109
  1. 2
      sop-sdk/sdk-java/pom.xml
  2. 18
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenClient.java
  3. 35
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenHttp.java
  4. 2
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java
  5. 10
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/common/OpenConfig.java
  6. 34
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/BaseRequest.java
  7. 5
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/CommonRequest.java
  8. 2
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/response/CommonResponse.java
  9. 2
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/response/GetStoryResponse.java
  10. 4
      sop-sdk/sdk-java/src/test/java/com/gitee/sop/sdk/SdkTest.java

@ -17,7 +17,7 @@
<dependency> <dependency>
<groupId>com.squareup.okhttp3</groupId> <groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId> <artifactId>okhttp</artifactId>
<version>3.10.0</version> <version>3.14.0</version>
</dependency> </dependency>
<!-- json处理 --> <!-- json处理 -->

@ -27,27 +27,25 @@ public class OpenClient {
private static final char DOT = '.'; private static final char DOT = '.';
private static final char UNDERLINE = '_'; private static final char UNDERLINE = '_';
public static final String GATEWAY_CODE_NAME = "code";
public static final String GATEWAY_MSG_NAME = "msg";
public static final String DATA_SUFFIX = "_response"; public static final String DATA_SUFFIX = "_response";
private String url; private String url;
private String appKey; private String appId;
private String privateKey; private String privateKey;
private OpenConfig openConfig; private OpenConfig openConfig;
private OpenRequest openRequest; private OpenRequest openRequest;
public OpenClient(String url, String appKey, String privateKey) { public OpenClient(String url, String appId, String privateKey) {
this(url, appKey, privateKey, DEFAULT_CONFIG); this(url, appId, privateKey, DEFAULT_CONFIG);
} }
public OpenClient(String url, String appKey, String privateKey, OpenConfig openConfig) { public OpenClient(String url, String appId, String privateKey, OpenConfig openConfig) {
if (openConfig == null) { if (openConfig == null) {
throw new IllegalArgumentException("openConfig不能为null"); throw new IllegalArgumentException("openConfig不能为null");
} }
this.url = url; this.url = url;
this.appKey = appKey; this.appId = appId;
this.privateKey = privateKey; this.privateKey = privateKey;
this.openConfig = openConfig; this.openConfig = openConfig;
@ -74,13 +72,13 @@ public class OpenClient {
* @return 返回Response * @return 返回Response
*/ */
public <T extends BaseResponse> T execute(BaseRequest<T> request, String accessToken) { public <T extends BaseResponse> T execute(BaseRequest<T> request, String accessToken) {
RequestForm requestForm = request.createRequestForm(); RequestForm requestForm = request.createRequestForm(this.openConfig);
// 表单数据 // 表单数据
Map<String, String> form = requestForm.getForm(); Map<String, String> form = requestForm.getForm();
if (accessToken != null) { if (accessToken != null) {
form.put(this.openConfig.getAccessTokenName(), accessToken); form.put(this.openConfig.getAccessTokenName(), accessToken);
} }
form.put(this.openConfig.getAppKeyName(), this.appKey); form.put(this.openConfig.getAppKeyName(), this.appId);
String content = SopSignature.getSignContent(form); String content = SopSignature.getSignContent(form);
String sign = null; String sign = null;
@ -106,7 +104,7 @@ public class OpenClient {
} }
protected String doExecute(String url, RequestForm requestForm, Map<String, String> header) { protected String doExecute(String url, RequestForm requestForm, Map<String, String> header) {
return openRequest.request(this.url, requestForm, header); return openRequest.request(url, requestForm, header);
} }
protected <T extends BaseResponse> T parseResponse(String resp, BaseRequest<T> request) { protected <T extends BaseResponse> T parseResponse(String resp, BaseRequest<T> request) {

@ -4,6 +4,7 @@ import com.gitee.sop.sdk.common.OpenConfig;
import com.gitee.sop.sdk.common.UploadFile; import com.gitee.sop.sdk.common.UploadFile;
import okhttp3.Cookie; import okhttp3.Cookie;
import okhttp3.CookieJar; import okhttp3.CookieJar;
import okhttp3.FormBody;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.MediaType; import okhttp3.MediaType;
import okhttp3.MultipartBody; import okhttp3.MultipartBody;
@ -13,6 +14,7 @@ import okhttp3.RequestBody;
import okhttp3.Response; import okhttp3.Response;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -38,6 +40,7 @@ public class OpenHttp {
httpClient = new OkHttpClient.Builder() httpClient = new OkHttpClient.Builder()
.connectTimeout(openConfig.getConnectTimeoutSeconds(), TimeUnit.SECONDS) // 设置链接超时时间,默认10秒 .connectTimeout(openConfig.getConnectTimeoutSeconds(), TimeUnit.SECONDS) // 设置链接超时时间,默认10秒
.readTimeout(openConfig.getReadTimeoutSeconds(), TimeUnit.SECONDS) .readTimeout(openConfig.getReadTimeoutSeconds(), TimeUnit.SECONDS)
.writeTimeout(openConfig.getWriteTimeoutSeconds(), TimeUnit.SECONDS)
.cookieJar(new CookieJar() { .cookieJar(new CookieJar() {
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) { public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
cookieStore.put(httpUrl.host(), list); cookieStore.put(httpUrl.host(), list);
@ -69,23 +72,35 @@ public class OpenHttp {
} }
/** /**
* 提交json字符串到请求体 * 提交表单
* *
* @param url * @param url
* @param json * @param form
* @param header header内容 * @param header header内容
* @return * @return
* @throws IOException * @throws IOException
*/ */
public String postJsonBody(String url, String json, Map<String, String> header) throws IOException { public String postFormBody(String url, Map<String, String> form, Map<String, String> header) throws IOException {
RequestBody body = RequestBody.create(JSON, json); FormBody.Builder paramBuilder = new FormBody.Builder(StandardCharsets.UTF_8);
Request.Builder builder = new Request.Builder().url(url).post(body); for (Map.Entry<String, String> entry : form.entrySet()) {
paramBuilder.add(entry.getKey(), entry.getValue());
}
FormBody formBody = paramBuilder.build();
Request.Builder requestBuilder = new Request.Builder()
.url(url)
.post(formBody);
// 添加header // 添加header
addHeader(builder, header); addHeader(requestBuilder, header);
Request request = builder.build(); Request request = requestBuilder.build();
Response response = httpClient.newCall(request).execute(); Response response = httpClient
.newCall(request)
.execute();
try {
return response.body().string(); return response.body().string();
} finally {
response.close();
}
} }
/** /**
@ -125,7 +140,11 @@ public class OpenHttp {
Request request = builder.build(); Request request = builder.build();
Response response = httpClient.newCall(request).execute(); Response response = httpClient.newCall(request).execute();
try {
return response.body().string(); return response.body().string();
} finally {
response.close();
}
} }
private void addHeader(Request.Builder builder, Map<String, String> header) { private void addHeader(Request.Builder builder, Map<String, String> header) {

@ -36,7 +36,7 @@ public class OpenRequest {
if (files != null && files.size() > 0) { if (files != null && files.size() > 0) {
return openHttp.postFile(url, form, header, files); return openHttp.postFile(url, form, header, files);
} else { } else {
return openHttp.postJsonBody(url, JsonUtil.toJSONString(form), header); return openHttp.postFormBody(url, form, header);
} }
} catch (IOException e) { } catch (IOException e) {
return this.causeException(e); return this.causeException(e);

@ -12,9 +12,11 @@ public class OpenConfig {
/** 默认版本号 */ /** 默认版本号 */
private String defaultVersion = SdkConfig.DEFAULT_VERSION; private String defaultVersion = SdkConfig.DEFAULT_VERSION;
/** 接口属性名 */ /** 接口属性名 */
private String apiName = "method"; private String methodName = "method";
/** 版本号名称 */ /** 版本号名称 */
private String versionName = "version"; private String versionName = "version";
/** 编码名称 */
private String charsetName = "charset";
/** appKey名称 */ /** appKey名称 */
private String appKeyName = "app_id"; private String appKeyName = "app_id";
/** data名称 */ /** data名称 */
@ -25,9 +27,11 @@ public class OpenConfig {
private String timestampPattern = "yyyy-MM-dd HH:mm:ss"; private String timestampPattern = "yyyy-MM-dd HH:mm:ss";
/** 签名串名称 */ /** 签名串名称 */
private String signName = "sign"; private String signName = "sign";
/** 签名类型名称 */
private String signTypeName = "sign_type";
/** 格式化名称 */ /** 格式化名称 */
private String formatName = "format"; private String formatName = "format";
/** 格式类型 */ /** 格式类型名称 */
private String formatType = "json"; private String formatType = "json";
/** accessToken名称 */ /** accessToken名称 */
private String accessTokenName = "app_auth_token"; private String accessTokenName = "app_auth_token";
@ -39,4 +43,6 @@ public class OpenConfig {
private int connectTimeoutSeconds = 10; private int connectTimeoutSeconds = 10;
/** http读取超时时间 */ /** http读取超时时间 */
private int readTimeoutSeconds = 10; private int readTimeoutSeconds = 10;
/** http写超时时间 */
private int writeTimeoutSeconds = 10;
} }

@ -1,13 +1,12 @@
package com.gitee.sop.sdk.request; package com.gitee.sop.sdk.request;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.gitee.sop.sdk.common.OpenConfig;
import com.gitee.sop.sdk.common.RequestForm; import com.gitee.sop.sdk.common.RequestForm;
import com.gitee.sop.sdk.common.SdkConfig; import com.gitee.sop.sdk.common.SdkConfig;
import com.gitee.sop.sdk.common.UploadFile; import com.gitee.sop.sdk.common.UploadFile;
import com.gitee.sop.sdk.response.BaseResponse; import com.gitee.sop.sdk.response.BaseResponse;
import com.gitee.sop.sdk.util.ClassUtil; import com.gitee.sop.sdk.util.ClassUtil;
import lombok.Getter;
import lombok.Setter;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
@ -55,9 +54,16 @@ public abstract class BaseRequest<T extends BaseResponse> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public BaseRequest() { public BaseRequest() {
this.method = this.method(); this.setMethodVersion(this.method(), this.version());
this.version = this.version(); }
protected BaseRequest(String method, String version) {
this.setMethodVersion(method, version);
}
private void setMethodVersion(String method, String version) {
this.method = method;
this.version = version == null ? SdkConfig.DEFAULT_VERSION : version;
this.responseClass = (Class<T>) ClassUtil.getSuperClassGenricType(this.getClass(), 0); this.responseClass = (Class<T>) ClassUtil.getSuperClassGenricType(this.getClass(), 0);
} }
@ -65,20 +71,20 @@ public abstract class BaseRequest<T extends BaseResponse> {
return SdkConfig.DEFAULT_VERSION; return SdkConfig.DEFAULT_VERSION;
} }
public RequestForm createRequestForm() { public RequestForm createRequestForm(OpenConfig openConfig) {
// 公共请求参数 // 公共请求参数
Map<String, String> params = new HashMap<String, String>(); Map<String, String> params = new HashMap<String, String>();
params.put("method", this.method); params.put(openConfig.getMethodName(), this.method);
params.put("format", this.format); params.put(openConfig.getFormatName(), this.format);
params.put("charset", this.charset); params.put(openConfig.getCharsetName(), this.charset);
params.put("sign_type", this.signType); params.put(openConfig.getSignTypeName(), this.signType);
params.put("timestamp", this.timestamp); params.put(openConfig.getTimestampName(), this.timestamp);
params.put("version", this.version); params.put(openConfig.getVersionName(), this.version);
// 业务参数 // 业务参数
String biz_content = buildBizContent(); String biz_content = buildBizContent();
params.put("biz_content", biz_content); params.put(openConfig.getDataName(), biz_content);
RequestForm requestForm = new RequestForm(params); RequestForm requestForm = new RequestForm(params);
requestForm.setFiles(this.files); requestForm.setFiles(this.files);
@ -97,10 +103,6 @@ public abstract class BaseRequest<T extends BaseResponse> {
return method; return method;
} }
protected void setMethod(String method) {
this.method = method;
}
public void setVersion(String version) { public void setVersion(String version) {
this.version = version; this.version = version;
} }

@ -8,12 +8,11 @@ import com.gitee.sop.sdk.response.CommonResponse;
public class CommonRequest extends BaseRequest<CommonResponse> { public class CommonRequest extends BaseRequest<CommonResponse> {
public CommonRequest(String method) { public CommonRequest(String method) {
this.setMethod(method); super(method, null);
} }
public CommonRequest(String method, String version) { public CommonRequest(String method, String version) {
this.setMethod(method); super(method, version);
this.setVersion(version);
} }
@Override @Override

@ -1,7 +1,5 @@
package com.gitee.sop.sdk.response; package com.gitee.sop.sdk.response;
import java.util.Map;
/** /**
* @author tanghc * @author tanghc
*/ */

@ -8,5 +8,5 @@ import java.util.Date;
public class GetStoryResponse extends BaseResponse { public class GetStoryResponse extends BaseResponse {
private Long id; private Long id;
private String name; private String name;
private Date gmtCreate; private Date gmt_create;
} }

@ -41,7 +41,7 @@ public class SdkTest extends TestCase {
// 返回结果 // 返回结果
System.out.println(response); System.out.println(response);
} else { } else {
System.out.println(response); System.out.println("错误,subCode:" + response.getSubCode() + ", subMsg:" + response.getSubMsg());
} }
} }
@ -65,7 +65,7 @@ public class SdkTest extends TestCase {
JSONObject jsonObject = JSON.parseObject(body); JSONObject jsonObject = JSON.parseObject(body);
System.out.println(jsonObject); System.out.println(jsonObject);
} else { } else {
System.out.println(response); System.out.println("错误,subCode:" + response.getSubCode() + ", subMsg:" + response.getSubMsg());
} }
} }

Loading…
Cancel
Save