Skip to content

Commit ab170fb

Browse files
author
mike.wq
committed
Merge branch 'feature/feature/proxy'
2 parents cbdddfc + 0dd15eb commit ab170fb

File tree

22 files changed

+124
-45
lines changed

22 files changed

+124
-45
lines changed

app-stream-api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>open-app-stream-client</artifactId>
77
<groupId>com.dingtalk.open</groupId>
8-
<version>1.2.0</version>
8+
<version>1.2.1</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111
<modelVersion>4.0.0</modelVersion>

app-stream-api/src/main/java/com/dingtalk/open/app/api/OpenDingTalkStreamClient.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.dingtalk.open.app.stream.network.core.NetWorkService;
1515
import com.dingtalk.open.app.stream.network.core.Subscription;
1616

17+
import java.net.Proxy;
1718
import java.util.Collections;
1819
import java.util.Set;
1920
import java.util.concurrent.ExecutorService;
@@ -33,23 +34,26 @@ class OpenDingTalkStreamClient implements OpenDingTalkClient {
3334
private OpenApiClient openApiClient;
3435
private Set<Subscription> subscriptions;
3536
private final AtomicReference<Status> status;
37+
private final Proxy proxy;
3638

37-
public OpenDingTalkStreamClient(DingTalkCredential credential, CommandDispatcher dispatcher, ExecutorService executor, ClientOption option, Set<Subscription> subscriptions) {
39+
public OpenDingTalkStreamClient(DingTalkCredential credential, CommandDispatcher dispatcher, ExecutorService executor, ClientOption option, Set<Subscription> subscriptions,
40+
Proxy proxy) {
3841
this.credential = credential;
3942
this.dispatcher = dispatcher;
4043
this.executor = executor;
4144
this.option = option;
4245
this.subscriptions = Collections.unmodifiableSet(subscriptions);
4346
this.status = new AtomicReference<>(Status.INIT);
47+
this.proxy = proxy;
4448
}
4549

4650
@Override
4751
public synchronized void start() throws OpenDingTalkAppException {
4852
if (status.get() == Status.INIT) {
4953
this.openApiClient = OpenApiClientBuilder.create().setHost(option.getOpenApiHost()).setTimeout(option.getConnectionTTL()).build();
50-
final EndPointConnectionFactory factory = () -> openConnection(this.credential, subscriptions);
54+
final EndPointConnectionFactory factory = () -> openConnection(this.credential, subscriptions, proxy);
5155
ClientConnectionListener listener = new AppServiceListener(dispatcher, executor);
52-
this.netWorkService = new NetWorkService(factory, listener, option.getMaxConnectionCount(), option.getConnectionTTL(), option.getConnectTimeout(),option.getKeepAliveOption().getKeepAliveIdleMill());
56+
this.netWorkService = new NetWorkService(factory, listener, option.getMaxConnectionCount(), option.getConnectionTTL(), option.getConnectTimeout(), option.getKeepAliveOption().getKeepAliveIdleMill());
5357
this.netWorkService.start();
5458
this.status.set(Status.ACTIVE);
5559
} else if (status.get() == Status.INACTIVE) {
@@ -70,15 +74,15 @@ public synchronized void stop() throws Exception {
7074
}
7175
}
7276

73-
private EndPointConnection openConnection(DingTalkCredential credential, Set<Subscription> subscriptions) throws Exception {
77+
private EndPointConnection openConnection(DingTalkCredential credential, Set<Subscription> subscriptions, Proxy proxy) throws Exception {
7478
OpenConnectionRequest request = new OpenConnectionRequest();
7579
request.setClientId(credential.getClientId());
7680
request.setClientSecret(credential.getClientSecret());
7781
request.setUa(UserAgent.getUserAgent().getUa());
7882
request.setSubscriptions(subscriptions);
7983
request.setLocalIp(IpUtils.getLocalIP());
8084
OpenConnectionResponse response = openApiClient.openConnection(request);
81-
return new EndPointConnection(credential.getClientId(), response.getEndpoint(), response.getTicket());
85+
return new EndPointConnection(credential.getClientId(), response.getEndpoint(), response.getTicket(), proxy);
8286
}
8387

8488

app-stream-api/src/main/java/com/dingtalk/open/app/api/OpenDingTalkStreamClientBuilder.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package com.dingtalk.open.app.api;
22

3-
import com.dingtalk.open.app.api.command.CommandDispatcher;
4-
import com.dingtalk.open.app.api.util.ThreadUtil;
53
import com.dingtalk.open.app.api.callback.CallbackCommandExecutor;
64
import com.dingtalk.open.app.api.callback.OpenDingTalkCallbackListener;
5+
import com.dingtalk.open.app.api.command.CommandDispatcher;
76
import com.dingtalk.open.app.api.protocol.CommandExecutor;
87
import com.dingtalk.open.app.api.protocol.EventCommandExecutor;
98
import com.dingtalk.open.app.api.security.DingTalkCredential;
9+
import com.dingtalk.open.app.api.util.ThreadUtil;
10+
import com.dingtalk.open.app.stream.network.api.NetProxy;
1011
import com.dingtalk.open.app.stream.network.core.Subscription;
1112
import com.dingtalk.open.app.stream.protocol.CommandType;
1213

13-
import java.util.*;
14+
import java.net.InetSocketAddress;
15+
import java.net.Proxy;
16+
import java.util.HashMap;
17+
import java.util.HashSet;
18+
import java.util.Map;
19+
import java.util.Set;
1420
import java.util.concurrent.ExecutorService;
1521

1622
/**
@@ -27,6 +33,8 @@ public class OpenDingTalkStreamClientBuilder {
2733
private int connectionTimeToLive = 6 * 60 * 60 * 1000;
2834
private long connectTimeout = 3 * 1000L;
2935

36+
private Proxy proxy;
37+
3038
private KeepAliveOption keepAliveOption = new KeepAliveOption();
3139

3240
private String openApiHost = "https://api.dingtalk.com";
@@ -99,6 +107,17 @@ public OpenDingTalkStreamClientBuilder preEnv() {
99107
return this.openApiHost("https://pre-api.dingtalk.com");
100108
}
101109

110+
/**
111+
* 设置代理方式
112+
*
113+
* @param netProxy
114+
* @return
115+
*/
116+
public OpenDingTalkStreamClientBuilder proxy(NetProxy netProxy) {
117+
this.proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(netProxy.getIp(), netProxy.getPort()));
118+
return this;
119+
}
120+
102121
public OpenDingTalkClient build() {
103122
ClientOption option = new ClientOption();
104123
option.setConnectTimeout(connectTimeout);
@@ -107,7 +126,7 @@ public OpenDingTalkClient build() {
107126
option.setOpenApiHost(openApiHost);
108127
option.setKeepAliveOption(keepAliveOption);
109128
ExecutorService executor = ThreadUtil.newFixedExecutor(consumeThreads, "DingTalk-Consumer");
110-
return new OpenDingTalkStreamClient(credential, new CommandDispatcher(commands), executor, option, subscriptions);
129+
return new OpenDingTalkStreamClient(credential, new CommandDispatcher(commands), executor, option, subscriptions, proxy);
111130
}
112131

113132
private void subscribe(CommandType type, String topic) {

app-stream-api/src/main/java/com/dingtalk/open/app/api/open/HttpOpenApiClient.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.dingtalk.open.app.api.util.IoUtils;
88

99
import java.net.HttpURLConnection;
10+
import java.net.Proxy;
1011
import java.net.URL;
1112

1213
/**
@@ -17,16 +18,26 @@ class HttpOpenApiClient implements OpenApiClient {
1718

1819
private final String host;
1920

21+
private final Proxy proxy;
22+
2023
private final int timeout;
2124

22-
public HttpOpenApiClient(String host, int timeout) {
25+
public HttpOpenApiClient(String host, int timeout, Proxy proxy) {
2326
this.host = host;
2427
this.timeout = timeout;
28+
this.proxy = proxy;
2529
}
2630

2731
@Override
2832
public OpenConnectionResponse openConnection(OpenConnectionRequest request) throws Exception {
29-
final HttpURLConnection connection = (HttpURLConnection) new URL(host + "/v1.0/gateway/connections/open").openConnection();
33+
URL url = new URL(host + "/v1.0/gateway/connections/open");
34+
35+
HttpURLConnection connection;
36+
if (proxy != null) {
37+
connection = (HttpURLConnection) url.openConnection(proxy);
38+
} else {
39+
connection = (HttpURLConnection) url.openConnection();
40+
}
3041
connection.setRequestMethod(HttpConstants.METHOD_POST);
3142
connection.setReadTimeout(this.timeout);
3243
connection.setConnectTimeout(this.timeout);

app-stream-api/src/main/java/com/dingtalk/open/app/api/open/OpenApiClientBuilder.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.dingtalk.open.app.api.open;
22

3+
import java.net.Proxy;
4+
35
/**
46
* @author feiyin
57
* @date 2023/3/1
@@ -16,6 +18,8 @@ public static OpenApiClientBuilder create() {
1618

1719
public String host;
1820

21+
private Proxy proxy;
22+
1923
private int timeout = 3000;
2024

2125
public OpenApiClientBuilder setHost(String host) {
@@ -28,7 +32,12 @@ public OpenApiClientBuilder setTimeout(int timeout) {
2832
return this;
2933
}
3034

35+
public OpenApiClientBuilder setProxy(Proxy proxy) {
36+
this.proxy = proxy;
37+
return this;
38+
}
39+
3140
public OpenApiClient build() {
32-
return new HttpOpenApiClient(host, timeout);
41+
return new HttpOpenApiClient(host, timeout, proxy);
3342
}
3443
}

app-stream-api/src/main/java/com/dingtalk/open/app/api/protocol/AppServiceListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void receive(Context context) {
2929
try {
3030
commandDispatcher.execute(context);
3131
} catch (Exception e) {
32-
LOGGER.error("[DingTalk] dispatch command failed", e);
32+
LOGGER.error("[DingTalk] dispatch command failed, {}", e);
3333
}
3434
});
3535
}

app-stream-client/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
<parent>
55
<groupId>com.dingtalk.open</groupId>
66
<artifactId>open-app-stream-client</artifactId>
7-
<version>1.2.0</version>
7+
<version>1.2.1</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010

1111
<artifactId>app-stream-client</artifactId>
1212
<packaging>jar</packaging>
13-
<version>1.2.0</version>
13+
<version>1.2.1</version>
1414
<name>app-stream-client</name>
1515

1616
<dependencies>

app-stream-network/app-stream-network-api/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
<parent>
55
<groupId>com.dingtalk.open</groupId>
66
<artifactId>app-stream-network</artifactId>
7-
<version>1.2.0</version>
7+
<version>1.2.1</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010

1111
<artifactId>app-stream-network-api</artifactId>
12-
<version>1.2.0</version>
12+
<version>1.2.1</version>
1313
<packaging>jar</packaging>
1414

1515
<name>app-stream-network-api</name>

app-stream-network/app-stream-network-api/src/main/java/com/dingtalk/open/app/stream/network/api/EndPointConnection.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.dingtalk.open.app.stream.network.api;
22

3+
import java.net.Proxy;
34
import java.net.URI;
45
import java.net.URISyntaxException;
56

@@ -12,14 +13,17 @@ public class EndPointConnection {
1213
private final URI endPoint;
1314
private final String connectionId;
1415

15-
public EndPointConnection(String clientId, String endPoint, String connectionId) {
16+
private final Proxy proxy;
17+
18+
public EndPointConnection(String clientId, String endPoint, String connectionId, Proxy proxy) {
1619
this.clientId = clientId;
1720
try {
1821
this.endPoint = new URI(endPoint);
1922
} catch (URISyntaxException e) {
2023
throw new RuntimeException(e);
2124
}
2225
this.connectionId = connectionId;
26+
this.proxy = proxy;
2327
}
2428

2529
public URI getEndPoint() {
@@ -38,4 +42,7 @@ public TransportProtocol getProtocol() {
3842
return TransportProtocol.parseScheme(endPoint.getScheme());
3943
}
4044

45+
public Proxy getProxy() {
46+
return proxy;
47+
}
4148
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.dingtalk.open.app.stream.network.api;
2+
3+
/**
4+
* @author feiyin
5+
* @date 2023/11/21
6+
*/
7+
public class NetProxy {
8+
private String host;
9+
private Integer port;
10+
11+
public NetProxy(String host, Integer port) {
12+
this.host = host;
13+
this.port = port;
14+
}
15+
16+
public String getIp() {
17+
return host;
18+
}
19+
20+
public Integer getPort() {
21+
return port;
22+
}
23+
}

0 commit comments

Comments
 (0)