Skip to content

Commit 0273b9f

Browse files
committed
add happydns
1 parent ec30922 commit 0273b9f

File tree

9 files changed

+75
-69
lines changed

9 files changed

+75
-69
lines changed

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ repositories {
1212
}
1313

1414
dependencies {
15-
// String version = System.getProperty("java.version")
16-
// println("JDK: " + version)
15+
// String ver = System.getProperty("java.version")
16+
// println("JDK: " + ver)
1717
// int c = version.compareTo("1.7")
1818
// if (c < 0) {
1919
// compile fileTree(dir: 'libs', include: '*.jar')
2020
// } else {
2121
compile group:'com.squareup.okhttp3', name:'okhttp', version:'3.3.1'
2222
// }
2323
compile group:'com.google.code.gson', name:'gson', version:'2.6.2'
24+
compile group:'com.qiniu', name:'happy-dns-java', version:'0.1.3'
2425
testCompile group: 'junit', name: 'junit', version: '4.12'
25-
println("JDK : " + version)
2626
}
2727

2828

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Wed Jan 07 13:33:33 CST 2015
1+
#Thu Jul 14 15:30:24 CST 2016
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14-bin.zip

src/main/java/com/qiniu/common/Config.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.qiniu.common;
22

3+
import qiniu.happydns.DnsClient;
4+
35
import java.nio.charset.Charset;
46

57
// CHECKSTYLE:OFF
@@ -54,6 +56,11 @@ public final class Config {
5456
*/
5557
public static int RETRY_MAX = 5;
5658

59+
/**
60+
* 外部dns
61+
*/
62+
public static DnsClient dns = null;
63+
5764
private Config() {
5865
}
5966
}

src/main/java/com/qiniu/http/Client.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@
66
import com.qiniu.util.StringUtils;
77
import okhttp3.*;
88
import okio.BufferedSink;
9+
import qiniu.happydns.DnsClient;
10+
import qiniu.happydns.Domain;
911

1012
import java.io.File;
1113
import java.io.IOException;
14+
import java.net.InetAddress;
15+
import java.net.UnknownHostException;
16+
import java.util.ArrayList;
17+
import java.util.Collections;
18+
import java.util.List;
1219
import java.util.concurrent.TimeUnit;
1320

1421
/**
@@ -46,10 +53,32 @@ public okhttp3.Response intercept(Chain chain) throws IOException {
4653
return response;
4754
}
4855
});
56+
if (Config.dns != null) {
57+
final DnsClient d = Config.dns;
58+
builder.dns(new Dns() {
59+
@Override
60+
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
61+
InetAddress[] ips;
62+
try {
63+
ips = d.queryInetAddress(new Domain(hostname));
64+
} catch (IOException e) {
65+
e.printStackTrace();
66+
throw new UnknownHostException(e.getMessage());
67+
}
68+
if (ips == null) {
69+
throw new UnknownHostException(hostname + " resolve failed");
70+
}
71+
List<InetAddress> l = new ArrayList<>();
72+
Collections.addAll(l, ips);
73+
return l;
74+
}
75+
});
76+
}
4977
builder.connectTimeout(Config.CONNECT_TIMEOUT, TimeUnit.SECONDS);
5078
builder.readTimeout(Config.RESPONSE_TIMEOUT, TimeUnit.SECONDS);
5179
builder.writeTimeout(Config.WRITE_TIMEOUT, TimeUnit.SECONDS);
5280
httpClient = builder.build();
81+
5382
}
5483

5584
private static String userAgent() {

src/main/java/com/qiniu/util/Dns.java

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/test/java/com/qiniu/HttpTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
package com.qiniu;
22

3+
import com.qiniu.common.Config;
34
import com.qiniu.common.QiniuException;
45
import com.qiniu.http.Client;
56
import com.qiniu.http.Response;
67
import org.junit.Assert;
78
import org.junit.Test;
9+
import qiniu.happydns.DnsClient;
10+
import qiniu.happydns.IResolver;
11+
import qiniu.happydns.local.Hosts;
12+
import qiniu.happydns.local.Resolver;
13+
import qiniu.happydns.local.SystemDnsServer;
14+
15+
import java.io.IOException;
16+
import java.net.InetAddress;
817

918

1019
public class HttpTest {
@@ -32,6 +41,27 @@ public void testPost2() {
3241
}
3342
}
3443

44+
@Test
45+
public void testDns() {
46+
IResolver r1 = SystemDnsServer.defaultResolver();
47+
IResolver r2 = null;
48+
try {
49+
r2 = new Resolver(InetAddress.getByName("119.29.29.29"));
50+
} catch (IOException ex) {
51+
ex.printStackTrace();
52+
}
53+
Hosts h = new Hosts();
54+
h.put("upnonodns.qiniu.com", "115.231.183.168");
55+
Config.dns = new DnsClient(new IResolver[]{r1, r2}, h);
56+
Response r = null;
57+
try {
58+
r = new Client().post("http://upnonodns.qiniu.com", "hello", null);
59+
Assert.fail();
60+
} catch (QiniuException e) {
61+
Assert.assertNotNull(e.response.reqId);
62+
}
63+
}
64+
3565
@Test
3666
public void testPost3() {
3767
Response r = null;

src/test/java/com/qiniu/TempFile.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
* Created by bailong on 14/10/11.
1010
*/
1111
public final class TempFile {
12+
static final Random r = new Random();
13+
1214
private TempFile() {
1315
}
1416

15-
static final Random r = new Random();
16-
1717
public static void remove(File f) {
1818
f.delete();
1919
}

src/test/java/com/qiniu/storage/RecordUploadTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
*/
2525
public class RecordUploadTest {
2626
final Random r = new Random();
27-
private Response response = null;
2827
final RecordKeyGenerator keyGen = new RecordKeyGenerator() {
2928
@Override
3029
public String gen(String key, File file) {
3130
return key + "_._" + file.getAbsolutePath();
3231
}
3332
};
34-
FileRecorder recorder = null;
3533
final Client client = new Client();
34+
FileRecorder recorder = null;
35+
private Response response = null;
3636

3737
private void template(final int size) throws IOException {
3838
response = null;

src/test/java/com/qiniu/util/DnsTest.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)