Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public DefaultDDALDataSource(String url) {
* jdbc:ddal:thin:
*
*/
public DefaultDDALDataSource(String url, String username, String password) {
public DefaultDDALDataSource(String url, String user, String password) {
if (url != null) {
url = url.trim();
}
Expand All @@ -82,10 +82,9 @@ public DefaultDDALDataSource(String url, String username, String password) {
context = new ClassPathXmlApplicationContext(url2);
} else if (url2.startsWith("file:")) {
context = new FileSystemXmlApplicationContext(url2);
} else if (url2.startsWith("//")) {
url2 = "http:" + url2;
} else if (url2.startsWith("http:") || url2.startsWith("https:")) {
Map<String, String> param = new LinkedHashMap<>();
param.put("username", username);
param.put("user", user);
param.put("password", password);
String content = HttpUtils.sendPost(url2, param);
Resource resource = new ByteArrayResource(content.getBytes());
Expand Down
3 changes: 1 addition & 2 deletions ddal-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

<properties>
<maven.test.skip>true</maven.test.skip>
<ddal.version>1.0.0-SNAPSHOT</ddal.version>
<spring.version>4.2.6.RELEASE</spring.version>
<jdk.version>1.7</jdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -98,7 +97,7 @@
<dependency>
<groupId>org.hellojavaer.ddal</groupId>
<artifactId>ddal-bom</artifactId>
<version>${ddal.version}</version>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

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

/**
* param: client_id=1&access_token=12
* return: begin_value=1&end_value=2
* param: clientId= &accessToken= &schemaName= &tableName= &step=
* return: beginValue= &endValue= &errorCode= &errorMessage=
*/
@Override
public SequenceRange get(String schemaName, String tableName, int step) throws Exception {
Map<String, String> param = new HashMap<>();
param.put("client_id", clientId);
param.put("access_token", accessToken);
param.put("schema_name", schemaName);
param.put("table_name", tableName);
param.put("clientId", clientId);
param.put("accessToken", accessToken);
param.put("schemaName", schemaName);
param.put("tableName", tableName);
param.put("step", String.valueOf(step));
String result = HttpUtils.sendPost(accessUrl, param);
String[] kvs = result.split("&");
Long beginValue = null;
Long endValue = null;
boolean hasErrorCode = false;
for (String item : kvs) {
String[] kv = item.split("=");
if ("error_code".equals(kv[0])) {
hasErrorCode = true;
break;
} else if ("begin_value".equals(kv[0])) {
beginValue = Long.valueOf(kv[1].trim());
} else if ("end_value".equals(kv[0])) {
endValue = Long.valueOf(kv[1].trim());
}
}
if (hasErrorCode) {
authorize();
result = HttpUtils.sendPost(accessUrl, param);
kvs = result.split("&");
for (String item : kvs) {
String[] kv = item.split("=");
if ("error_code".equals(kv[0])) {
Map<String, String> resultMap = parseHttpKvString(result);
if (resultMap.isEmpty()) {
return null;
} else {
if (resultMap.get("errorCode") != null) {
authorize();
result = HttpUtils.sendPost(accessUrl, param);
resultMap = parseHttpKvString(result);
if (resultMap.get("errorCode") != null) {
throw new GetSequenceFailedException("clientId:" + clientId
+ " access data failed, return message is:" + result);
} else if ("begin_value".equals(kv[0])) {
beginValue = Long.valueOf(kv[1].trim());
} else if ("end_value".equals(kv[0])) {
endValue = Long.valueOf(kv[1].trim());
}
}
SequenceRange sequenceRange = new SequenceRange();
sequenceRange.setBeginValue(parseLong(resultMap.get("beginValue")));
sequenceRange.setEndValue(parseLong(resultMap.get("endValue")));
return sequenceRange;
}
SequenceRange sequenceRange = new SequenceRange();
sequenceRange.setBeginValue(beginValue);
sequenceRange.setEndValue(endValue);
return sequenceRange;
}

/**
* param: client_id=&client_token=
* return: access_url=http&access_token=1&error_code=1
* param: clientId= &clientToken=
* return: accessUrl= &accessToken= &errorCode= &errorMessage=
*/
private void authorize() {
Map<String, String> param = new HashMap<>();
param.put("client_id", clientId);
param.put("authorize_token", authorizeToken);
param.put("clientId", clientId);
param.put("authorizeToken", authorizeToken);
String result = HttpUtils.sendPost(authorizeUrl, param);
String[] kvs = result.split("&");
for (String item : kvs) {
String[] kv = item.split("=");
if ("error_code".equals(kv[0]) && kv.length >= 2 && kv[1] != null && kv[1].trim().length() > 0) {
Map<String, String> resultMap = parseHttpKvString(result);
if (resultMap.isEmpty()) {
throw new GetSequenceFailedException("clientId:" + clientId + " authorize failed, return message is empty");
} else {
if (resultMap.get("errorCode") != null) {
throw new GetSequenceFailedException("clientId:" + clientId + " authorize failed, return message is: "
+ result);
} else if ("access_url".equals(kv[0])) {
this.accessUrl = kv[1];
} else if ("access_token".equals(kv[0])) {
this.accessToken = kv[1];
}
}
if (accessUrl != null) {
String accessUrl = resultMap.get("accessUrl");
String accessToken = resultMap.get("accessToken");
if (accessUrl == null || accessToken == null) {
throw new GetSequenceFailedException(
"clientId:"
+ clientId
+ " authorize failed, accessUrl or accessToken is null, detail message is "
+ result);
}
try {
accessUrl = URLDecoder.decode(accessUrl.trim(), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
throw new GetSequenceFailedException(e);
}
this.accessUrl = accessUrl;
this.accessToken = accessToken;
}
}

private Map<String, String> parseHttpKvString(String string) {
if (string == null) {
return Collections.emptyMap();
}
string = string.trim();
if (string.length() == 0) {
return Collections.emptyMap();
}
Map<String, String> map = new HashMap<>();
String[] kvs = string.split("&");
for (String kvString : kvs) {
String[] kv = kvString.split("=");
if (kv.length == 2) {
String val = kv[1];
val = val.trim();
if (val.length() == 0) {
val = null;
}
map.put(kv[0], val);
} else if (kv.length == 1) {
map.put(kv[0], null);
} else {
throw new RuntimeException("Illegaled http kv string:" + kvString);
}
}
if (accessToken != null) {
accessToken = accessToken.trim();
return map;
}

private Long parseLong(String str) {
if (str == null) {
return null;
}
if (accessUrl == null || accessUrl.length() == 0 || accessToken == null || accessToken.length() == 0) {
throw new GetSequenceFailedException("clientId:" + clientId + " authorize failed, return message is: "
+ result);
str = str.trim();
if (str.length() == 0) {
return null;
}
return Long.parseLong(str);
}

static class HttpUtils {
Expand Down