Skip to content

Commit 62b05ac

Browse files
committed
Merge branch 'master' into 1.0.0.M6
2 parents 9e9d2e7 + 55e12ad commit 62b05ac

File tree

2 files changed

+85
-62
lines changed

2 files changed

+85
-62
lines changed

ddal-datasource/src/main/java/org/hellojavaer/ddal/datasource/DefaultDDALDataSource.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public DefaultDDALDataSource(String url) {
6464
* jdbc:ddal:thin:
6565
*
6666
*/
67-
public DefaultDDALDataSource(String url, String username, String password) {
67+
public DefaultDDALDataSource(String url, String user, String password) {
6868
if (url != null) {
6969
url = url.trim();
7070
}
@@ -82,10 +82,9 @@ public DefaultDDALDataSource(String url, String username, String password) {
8282
context = new ClassPathXmlApplicationContext(url2);
8383
} else if (url2.startsWith("file:")) {
8484
context = new FileSystemXmlApplicationContext(url2);
85-
} else if (url2.startsWith("//")) {
86-
url2 = "http:" + url2;
85+
} else if (url2.startsWith("http:") || url2.startsWith("https:")) {
8786
Map<String, String> param = new LinkedHashMap<>();
88-
param.put("username", username);
87+
param.put("user", user);
8988
param.put("password", password);
9089
String content = HttpUtils.sendPost(url2, param);
9190
Resource resource = new ByteArrayResource(content.getBytes());
@@ -233,7 +232,8 @@ public static String sendPost(String url, Map<String, String> params) {
233232
}
234233
int responseCode = con.getResponseCode();
235234
if (responseCode != 200) {
236-
throw new IllegalStateException("http code " + responseCode + "\n" + response.toString());
235+
throw new IllegalStateException("the requested url: " + url + " returned error code: "
236+
+ responseCode + "\n" + response.toString());
237237
} else {
238238
return response.toString();
239239
}

ddal-sequence/src/main/java/org/hellojavaer/ddal/sequence/HttpSequenceRangeGetter.java

Lines changed: 80 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.net.URL;
2323
import java.net.URLDecoder;
2424
import java.net.URLEncoder;
25+
import java.util.Collections;
2526
import java.util.HashMap;
2627
import java.util.Map;
2728

@@ -45,90 +46,111 @@ public HttpSequenceRangeGetter(String authorizeUrl, String clientId, String auth
4546
}
4647

4748
/**
48-
* param: client_id=1&access_token=12
49-
* return: begin_value=1&end_value=2
49+
* param: clientId= &accessToken= &schemaName= &tableName= &step=
50+
* return: beginValue= &endValue= &errorCode= &errorMessage=
5051
*/
5152
@Override
5253
public SequenceRange get(String schemaName, String tableName, int step) throws Exception {
5354
Map<String, String> param = new HashMap<>();
54-
param.put("client_id", clientId);
55-
param.put("access_token", accessToken);
56-
param.put("schema_name", schemaName);
57-
param.put("table_name", tableName);
55+
param.put("clientId", clientId);
56+
param.put("accessToken", accessToken);
57+
param.put("schemaName", schemaName);
58+
param.put("tableName", tableName);
5859
param.put("step", String.valueOf(step));
5960
String result = HttpUtils.sendPost(accessUrl, param);
60-
String[] kvs = result.split("&");
61-
Long beginValue = null;
62-
Long endValue = null;
63-
boolean hasErrorCode = false;
64-
for (String item : kvs) {
65-
String[] kv = item.split("=");
66-
if ("error_code".equals(kv[0])) {
67-
hasErrorCode = true;
68-
break;
69-
} else if ("begin_value".equals(kv[0])) {
70-
beginValue = Long.valueOf(kv[1].trim());
71-
} else if ("end_value".equals(kv[0])) {
72-
endValue = Long.valueOf(kv[1].trim());
73-
}
74-
}
75-
if (hasErrorCode) {
76-
authorize();
77-
result = HttpUtils.sendPost(accessUrl, param);
78-
kvs = result.split("&");
79-
for (String item : kvs) {
80-
String[] kv = item.split("=");
81-
if ("error_code".equals(kv[0])) {
61+
Map<String, String> resultMap = parseHttpKvString(result);
62+
if (resultMap.isEmpty()) {
63+
return null;
64+
} else {
65+
if (resultMap.get("errorCode") != null) {
66+
authorize();
67+
result = HttpUtils.sendPost(accessUrl, param);
68+
resultMap = parseHttpKvString(result);
69+
if (resultMap.get("errorCode") != null) {
8270
throw new GetSequenceFailedException("clientId:" + clientId
8371
+ " access data failed, return message is:" + result);
84-
} else if ("begin_value".equals(kv[0])) {
85-
beginValue = Long.valueOf(kv[1].trim());
86-
} else if ("end_value".equals(kv[0])) {
87-
endValue = Long.valueOf(kv[1].trim());
8872
}
8973
}
74+
SequenceRange sequenceRange = new SequenceRange();
75+
sequenceRange.setBeginValue(parseLong(resultMap.get("beginValue")));
76+
sequenceRange.setEndValue(parseLong(resultMap.get("endValue")));
77+
return sequenceRange;
9078
}
91-
SequenceRange sequenceRange = new SequenceRange();
92-
sequenceRange.setBeginValue(beginValue);
93-
sequenceRange.setEndValue(endValue);
94-
return sequenceRange;
9579
}
9680

9781
/**
98-
* param: client_id=&client_token=
99-
* return: access_url=http&access_token=1&error_code=1
82+
* param: clientId= &clientToken=
83+
* return: accessUrl= &accessToken= &errorCode= &errorMessage=
10084
*/
10185
private void authorize() {
10286
Map<String, String> param = new HashMap<>();
103-
param.put("client_id", clientId);
104-
param.put("authorize_token", authorizeToken);
87+
param.put("clientId", clientId);
88+
param.put("authorizeToken", authorizeToken);
10589
String result = HttpUtils.sendPost(authorizeUrl, param);
106-
String[] kvs = result.split("&");
107-
for (String item : kvs) {
108-
String[] kv = item.split("=");
109-
if ("error_code".equals(kv[0]) && kv.length >= 2 && kv[1] != null && kv[1].trim().length() > 0) {
90+
Map<String, String> resultMap = parseHttpKvString(result);
91+
if (resultMap.isEmpty()) {
92+
throw new GetSequenceFailedException("clientId:" + clientId + " authorize failed, return message is empty");
93+
} else {
94+
if (resultMap.get("errorCode") != null) {
11095
throw new GetSequenceFailedException("clientId:" + clientId + " authorize failed, return message is: "
11196
+ result);
112-
} else if ("access_url".equals(kv[0])) {
113-
this.accessUrl = kv[1];
114-
} else if ("access_token".equals(kv[0])) {
115-
this.accessToken = kv[1];
11697
}
117-
}
118-
if (accessUrl != null) {
98+
String accessUrl = resultMap.get("accessUrl");
99+
String accessToken = resultMap.get("accessToken");
100+
if (accessUrl == null || accessToken == null) {
101+
throw new GetSequenceFailedException(
102+
"clientId:"
103+
+ clientId
104+
+ " authorize failed, accessUrl or accessToken is null, detail message is "
105+
+ result);
106+
}
119107
try {
120108
accessUrl = URLDecoder.decode(accessUrl.trim(), "UTF-8");
121109
} catch (UnsupportedEncodingException e) {
122-
throw new RuntimeException(e);
110+
throw new GetSequenceFailedException(e);
111+
}
112+
this.accessUrl = accessUrl;
113+
this.accessToken = accessToken;
114+
}
115+
}
116+
117+
private Map<String, String> parseHttpKvString(String string) {
118+
if (string == null) {
119+
return Collections.emptyMap();
120+
}
121+
string = string.trim();
122+
if (string.length() == 0) {
123+
return Collections.emptyMap();
124+
}
125+
Map<String, String> map = new HashMap<>();
126+
String[] kvs = string.split("&");
127+
for (String kvString : kvs) {
128+
String[] kv = kvString.split("=");
129+
if (kv.length == 2) {
130+
String val = kv[1];
131+
val = val.trim();
132+
if (val.length() == 0) {
133+
val = null;
134+
}
135+
map.put(kv[0], val);
136+
} else if (kv.length == 1) {
137+
map.put(kv[0], null);
138+
} else {
139+
throw new RuntimeException("Illegaled http kv string:" + kvString);
123140
}
124141
}
125-
if (accessToken != null) {
126-
accessToken = accessToken.trim();
142+
return map;
143+
}
144+
145+
private Long parseLong(String str) {
146+
if (str == null) {
147+
return null;
127148
}
128-
if (accessUrl == null || accessUrl.length() == 0 || accessToken == null || accessToken.length() == 0) {
129-
throw new GetSequenceFailedException("clientId:" + clientId + " authorize failed, return message is: "
130-
+ result);
149+
str = str.trim();
150+
if (str.length() == 0) {
151+
return null;
131152
}
153+
return Long.parseLong(str);
132154
}
133155

134156
static class HttpUtils {
@@ -180,7 +202,8 @@ public static String sendPost(String url, Map<String, String> params) {
180202
}
181203
int responseCode = con.getResponseCode();
182204
if (responseCode != 200) {
183-
throw new IllegalStateException("http code " + responseCode + "\n" + response.toString());
205+
throw new IllegalStateException("the requested url: " + url + " returned error code: "
206+
+ responseCode + "\n" + response.toString());
184207
} else {
185208
return response.toString();
186209
}

0 commit comments

Comments
 (0)