Skip to content

Commit 9f6bedc

Browse files
committed
CORE SDK Auto Released By yixiong.jxy,Version:4.0.8
发布日志: 1, add proxy for urlconnection
1 parent 37c27ff commit 9f6bedc

4 files changed

Lines changed: 48 additions & 19 deletions

File tree

aliyun-java-sdk-core/ChangeLog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2018-08-17 Version: 4.0.8
2+
1, add proxy for urlconnection
3+
14
2018-08-10 Version: 4.0.8
25
1, fix ecsRamRole
36

aliyun-java-sdk-core/pom.xml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,17 @@
128128
</execution>
129129
</executions>
130130
</plugin>
131-
<!--<plugin>-->
132-
<!--<groupId>org.sonatype.plugins</groupId>-->
133-
<!--<artifactId>nexus-staging-maven-plugin</artifactId>-->
134-
<!--<version>1.6.3</version>-->
135-
<!--<extensions>true</extensions>-->
136-
<!--<configuration>-->
137-
<!--<serverId>sonatype-nexus-staging</serverId>-->
138-
<!--<nexusUrl>https://oss.sonatype.org/</nexusUrl>-->
139-
<!--<autoReleaseAfterClose>true</autoReleaseAfterClose>-->
140-
<!--</configuration>-->
141-
<!--</plugin>-->
131+
<plugin>
132+
<groupId>org.sonatype.plugins</groupId>
133+
<artifactId>nexus-staging-maven-plugin</artifactId>
134+
<version>1.6.3</version>
135+
<extensions>true</extensions>
136+
<configuration>
137+
<serverId>sonatype-nexus-staging</serverId>
138+
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
139+
<autoReleaseAfterClose>true</autoReleaseAfterClose>
140+
</configuration>
141+
</plugin>
142142
</plugins>
143143
</build>
144144
</project>

aliyun-java-sdk-core/src/main/java/com/aliyuncs/DefaultAcsClient.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,8 @@ private <T extends AcsResponse> HttpResponse doAction(AcsRequest<T> request,
281281
HttpResponse response;
282282
response = this.httpClient.syncInvoke(httpRequest);
283283

284-
if (500 <= response.getStatus() || response.getHttpContent() == null) {
285-
if (shouldRetry) {
284+
if ((500 <= response.getStatus() || response.getHttpContent() == null) && shouldRetry) {
286285
continue;
287-
} else {
288-
throw new ClientException("SDK.UnknownError", response.getHttpContentString());
289-
}
290286
}
291287
return response;
292288
} catch (SocketTimeoutException exp) {

aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/clients/CompatibleUrlConnClient.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
import java.io.IOException;
55
import java.io.InputStream;
66
import java.io.OutputStream;
7+
import java.io.UnsupportedEncodingException;
78
import java.net.HttpURLConnection;
9+
import java.net.InetSocketAddress;
10+
import java.net.MalformedURLException;
11+
import java.net.Proxy;
12+
import java.net.SocketAddress;
813
import java.net.URL;
914
import java.security.KeyManagementException;
1015
import java.security.NoSuchAlgorithmException;
@@ -22,6 +27,7 @@
2227
import javax.net.ssl.SSLSocketFactory;
2328
import javax.net.ssl.TrustManager;
2429
import javax.net.ssl.X509TrustManager;
30+
import javax.xml.bind.DatatypeConverter;
2531

2632
import com.aliyuncs.exceptions.ClientException;
2733
import com.aliyuncs.http.CallBack;
@@ -94,7 +100,6 @@ public static HttpResponse compatibleGetResponse(HttpRequest request) throws IOE
94100
}
95101

96102
private HttpURLConnection buildHttpConnection(HttpRequest request) throws IOException {
97-
Map<String, String> mappedHeaders = request.getHeaders();
98103
String strUrl = request.getUrl();
99104

100105
if (null == strUrl) {
@@ -115,14 +120,16 @@ private HttpURLConnection buildHttpConnection(HttpRequest request) throws IOExce
115120
HttpURLConnection httpConn = null;
116121
if (url.getProtocol().equalsIgnoreCase("https")) {
117122
if (sslSocketFactory != null) {
118-
HttpsURLConnection httpsConn = (HttpsURLConnection)url.openConnection();
123+
Proxy proxy = getProxy("HTTPS_PROXY", request);
124+
HttpsURLConnection httpsConn = (HttpsURLConnection)url.openConnection(proxy);
119125
httpsConn.setSSLSocketFactory(sslSocketFactory);
120126
httpConn = httpsConn;
121127
}
122128
}
123129

124130
if (httpConn == null) {
125-
httpConn = (HttpURLConnection)url.openConnection();
131+
Proxy proxy = getProxy("HTTP_PROXY", request);
132+
httpConn = (HttpURLConnection)url.openConnection(proxy);
126133
}
127134

128135
httpConn.setRequestMethod(request.getMethod().toString());
@@ -138,6 +145,7 @@ private HttpURLConnection buildHttpConnection(HttpRequest request) throws IOExce
138145
httpConn.setReadTimeout(request.getReadTimeout());
139146
}
140147

148+
Map<String, String> mappedHeaders = request.getHeaders();
141149
httpConn.setRequestProperty(ACCEPT_ENCODING, "identity");
142150
for (Entry<String, String> entry : mappedHeaders.entrySet()) {
143151
httpConn.setRequestProperty(entry.getKey(), entry.getValue());
@@ -222,6 +230,28 @@ public void close() throws IOException {
222230

223231
}
224232

233+
private Proxy getProxy(String env, HttpRequest request) throws MalformedURLException, UnsupportedEncodingException {
234+
Proxy proxy = Proxy.NO_PROXY;
235+
String httpProxy = System.getenv(env);
236+
if (httpProxy != null) {
237+
URL proxyUrl = new URL(httpProxy);
238+
String userInfo = proxyUrl.getUserInfo();
239+
if (userInfo != null) {
240+
byte[] bytes = userInfo.getBytes("UTF-8");
241+
String auth = DatatypeConverter.printBase64Binary(bytes);
242+
request.putHeaderParameter("Proxy-Authorization", "Basic " + auth);
243+
}
244+
String hostname = proxyUrl.getHost();
245+
int port = proxyUrl.getPort();
246+
if (port == -1) {
247+
port = proxyUrl.getDefaultPort();
248+
}
249+
SocketAddress addr = new InetSocketAddress(hostname, port);
250+
proxy = new Proxy(Proxy.Type.HTTP, addr);
251+
}
252+
return proxy;
253+
}
254+
225255
public static final class HttpsCertIgnoreHelper implements X509TrustManager, HostnameVerifier {
226256

227257
private static HostnameVerifier defaultVerifier;

0 commit comments

Comments
 (0)