Skip to content
This repository was archived by the owner on May 14, 2020. It is now read-only.

Commit 6f12c75

Browse files
committed
CQ-440 - basic functionality
1 parent 12fb5b8 commit 6f12c75

3 files changed

Lines changed: 88 additions & 19 deletions

File tree

api-sdk/pom.xml

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,24 @@
170170
<dependency>
171171
<groupId>org.apache.httpcomponents</groupId>
172172
<artifactId>httpclient</artifactId>
173-
<version>4.3.3</version>
173+
<version>4.5.5</version>
174+
<exclusions>
175+
<exclusion>
176+
<groupId>commons-logging</groupId>
177+
<artifactId>commons-logging</artifactId>
178+
</exclusion>
179+
</exclusions>
174180
</dependency>
175181
<dependency>
176182
<groupId>org.apache.httpcomponents</groupId>
177183
<artifactId>httpmime</artifactId>
178-
<version>4.3.3</version>
184+
<version>4.5.5</version>
185+
<exclusions>
186+
<exclusion>
187+
<groupId>org.apache.httpcomponents</groupId>
188+
<artifactId>httpclient</artifactId>
189+
</exclusion>
190+
</exclusions>
179191
</dependency>
180192
<dependency>
181193
<groupId>commons-io</groupId>
@@ -185,7 +197,7 @@
185197
<dependency>
186198
<groupId>commons-logging</groupId>
187199
<artifactId>commons-logging</artifactId>
188-
<version>1.1.1</version>
200+
<version>1.2</version>
189201
</dependency>
190202
<dependency>
191203
<groupId>com.google.code.gson</groupId>
@@ -195,7 +207,7 @@
195207
<dependency>
196208
<groupId>junit</groupId>
197209
<artifactId>junit</artifactId>
198-
<version>4.11</version>
210+
<version>4.12</version>
199211
<scope>test</scope>
200212
</dependency>
201213
<dependency>
@@ -209,21 +221,39 @@
209221
<artifactId>powermock-module-junit4</artifactId>
210222
<version>1.6.1</version>
211223
<scope>test</scope>
224+
<exclusions>
225+
<exclusion>
226+
<groupId>junit</groupId>
227+
<artifactId>junit</artifactId>
228+
</exclusion>
229+
</exclusions>
212230
</dependency>
213231
<dependency>
214232
<groupId>org.powermock</groupId>
215233
<artifactId>powermock-api-mockito</artifactId>
216234
<version>1.6.1</version>
217235
<scope>test</scope>
236+
<exclusions>
237+
<exclusion>
238+
<groupId>org.powermock</groupId>
239+
<artifactId>powermock-reflect</artifactId>
240+
</exclusion>
241+
<exclusion>
242+
<groupId>org.mockito</groupId>
243+
<artifactId>mockito-all</artifactId>
244+
</exclusion>
245+
<exclusion>
246+
<groupId>org.powermock</groupId>
247+
<artifactId>powermock-core</artifactId>
248+
</exclusion>
249+
</exclusions>
218250
</dependency>
219251
<dependency>
220252
<groupId>org.jmockit</groupId>
221253
<artifactId>jmockit</artifactId>
222254
<version>1.22</version>
223255
<scope>test</scope>
224256
</dependency>
225-
226-
227257
<dependency>
228258
<groupId>commons-collections</groupId>
229259
<artifactId>commons-collections</artifactId>

api-sdk/src/main/java/com/smartling/api/sdk/util/HttpProxyUtils.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.smartling.api.sdk.util;
1717

18+
import com.smartling.api.sdk.ProxyConfiguration;
19+
import com.smartling.api.sdk.exceptions.SmartlingApiException;
1820
import org.apache.commons.lang3.StringUtils;
1921
import org.apache.http.HttpHost;
2022
import org.apache.http.auth.AuthScope;
@@ -26,11 +28,12 @@
2628
import org.apache.http.impl.client.CloseableHttpClient;
2729
import org.apache.http.impl.client.HttpClientBuilder;
2830

29-
import com.smartling.api.sdk.ProxyConfiguration;
31+
import javax.net.ssl.SSLContext;
32+
import java.security.KeyManagementException;
33+
import java.security.NoSuchAlgorithmException;
3034

3135
class HttpProxyUtils
3236
{
33-
3437
HttpProxyUtils()
3538
{
3639

@@ -52,13 +55,33 @@ RequestConfig getProxyRequestConfig(HttpRequestBase httpRequest, final ProxyConf
5255
return null;
5356
}
5457

58+
private SSLContext getSSLConfig() throws SmartlingApiException {
59+
SSLContext sslContext = null;
60+
61+
try {
62+
sslContext = SSLContext.getInstance("TLSv1.2");
63+
initSSLContext(sslContext);
64+
} catch (NoSuchAlgorithmException e) {
65+
throw new SmartlingApiException("Error while creating HTTPClient, can not initialize TLS version", e);
66+
}
67+
68+
return sslContext;
69+
}
70+
71+
private void initSSLContext(SSLContext sslContext) throws SmartlingApiException {
72+
try {
73+
sslContext.init(null, null, null);
74+
} catch (KeyManagementException e) {
75+
throw new SmartlingApiException("Error while creating HTTPClient", e);
76+
}
77+
}
78+
5579
/**
5680
* Get an HttpClient given a proxy config if any
5781
* @param proxyConfiguration configuration of proxy to use
5882
* @return org.apache.http.impl.client.CloseableHttpClient
5983
*/
60-
CloseableHttpClient getHttpClient(final ProxyConfiguration proxyConfiguration)
61-
{
84+
CloseableHttpClient getHttpClient(final ProxyConfiguration proxyConfiguration) throws SmartlingApiException {
6285
HttpClientBuilder httpClientBuilder = getHttpClientBuilder();
6386

6487
if (proxyAuthenticationRequired(proxyConfiguration))
@@ -71,6 +94,8 @@ CloseableHttpClient getHttpClient(final ProxyConfiguration proxyConfiguration)
7194
httpClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
7295
}
7396

97+
httpClientBuilder.setSSLContext(getSSLConfig());
98+
7499
return httpClientBuilder.build();
75100
}
76101

api-sdk/src/test/java/com/smartling/api/sdk/util/HttpProxyUtilsTest.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.smartling.api.sdk.util;
1717

1818
import com.smartling.api.sdk.ProxyConfiguration;
19+
import com.smartling.api.sdk.exceptions.SmartlingApiException;
1920
import org.apache.http.auth.AuthScope;
2021
import org.apache.http.auth.Credentials;
2122
import org.apache.http.client.CredentialsProvider;
@@ -24,18 +25,25 @@
2425
import org.apache.http.impl.client.HttpClientBuilder;
2526
import org.junit.After;
2627
import org.junit.Before;
28+
import org.junit.Ignore;
2729
import org.junit.Test;
2830
import org.junit.runner.RunWith;
2931
import org.mockito.ArgumentCaptor;
3032
import org.powermock.api.mockito.PowerMockito;
3133
import org.powermock.core.classloader.annotations.PrepareForTest;
3234
import org.powermock.modules.junit4.PowerMockRunner;
35+
import sun.security.ssl.SSLContextImpl;
36+
37+
import javax.net.ssl.SSLContext;
38+
39+
import java.security.NoSuchAlgorithmException;
3340

3441
import static org.junit.Assert.assertEquals;
3542
import static org.junit.Assert.assertNotNull;
3643
import static org.junit.Assert.assertNull;
3744
import static org.mockito.Matchers.any;
3845
import static org.mockito.Mockito.RETURNS_MOCKS;
46+
import static org.mockito.Mockito.doNothing;
3947
import static org.mockito.Mockito.mock;
4048
import static org.mockito.Mockito.never;
4149
import static org.mockito.Mockito.spy;
@@ -44,7 +52,7 @@
4452
import static org.mockito.Mockito.when;
4553

4654
@RunWith(PowerMockRunner.class)
47-
@PrepareForTest(HttpClientBuilder.class)
55+
@PrepareForTest({HttpClientBuilder.class, SSLContext.class})
4856
public class HttpProxyUtilsTest
4957
{
5058
private static final String HOST = "HOST";
@@ -54,16 +62,24 @@ public class HttpProxyUtilsTest
5462
private HttpProxyUtils httpProxyUtils;
5563
private HttpRequestBase httpRequest;
5664
private HttpClientBuilder httpClientBuilder;
65+
private SSLContext context;
5766

5867
@Before
59-
public void setUp()
60-
{
68+
public void setUp() throws NoSuchAlgorithmException {
6169
httpProxyUtils = spy(new HttpProxyUtils());
6270
httpRequest = mock(HttpRequestBase.class);
71+
6372
httpClientBuilder = PowerMockito.mock(HttpClientBuilder.class);
6473
when(httpProxyUtils.getHttpClientBuilder()).thenReturn(httpClientBuilder);
6574

6675
PowerMockito.when(httpClientBuilder.build()).then(RETURNS_MOCKS);
76+
77+
// create the mock to return by getInstance()
78+
context = PowerMockito.mock(SSLContext.class);
79+
// mock the static method getInstance() to return above created mock context
80+
PowerMockito.mockStatic(SSLContext.class);
81+
82+
when(SSLContext.getInstance("TLSv1.2")).thenReturn(context);
6783
}
6884

6985
@After
@@ -95,27 +111,25 @@ public void testGetProxyRequestConfigWithProxyConfig()
95111
}
96112

97113
@Test
98-
public void testGetHttpClientNoProxy()
99-
{
114+
public void testGetHttpClientNoProxy() throws SmartlingApiException, NoSuchAlgorithmException {
100115
CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class);
101116
PowerMockito.when(httpClientBuilder.build()).thenReturn(closeableHttpClient);
102117
assertNotNull(httpProxyUtils.getHttpClient(null));
103118

104119
verify(httpClientBuilder).build();
105-
PowerMockito.verifyNoMoreInteractions(httpClientBuilder);
120+
verify(httpClientBuilder).setSSLContext(context);
106121
}
107122

108123
@Test
109-
public void testGetHttpClientNoProxyConfig()
110-
{
124+
public void testGetHttpClientNoProxyConfig() throws SmartlingApiException {
111125
CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class);
112126
PowerMockito.when(httpClientBuilder.build()).thenReturn(closeableHttpClient);
113127
ProxyConfiguration proxyConfiguration = new ProxyConfiguration();
114128

115129
assertNotNull(httpProxyUtils.getHttpClient(proxyConfiguration));
116130

117131
verify(httpClientBuilder).build();
118-
PowerMockito.verifyNoMoreInteractions(httpClientBuilder);
132+
verify(httpClientBuilder).setSSLContext(context);
119133
}
120134

121135
@Test

0 commit comments

Comments
 (0)