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

Commit f35758f

Browse files
committed
SLDSS-6468: Improve proxy using in Java SDK
1 parent 538b10b commit f35758f

25 files changed

Lines changed: 317 additions & 104 deletions
1.91 MB
Binary file not shown.

api-sdk/pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
44

55
<groupId>smartling</groupId>
66
<artifactId>smartling-api-sdk</artifactId>
7-
<version>1.2.0</version>
7+
<version>1.3.0</version>
88
<packaging>jar</packaging>
99

1010
<build>
@@ -123,7 +123,7 @@
123123

124124
<name>smartling-api-sdk</name>
125125
<url>http://maven.apache.org</url>
126-
126+
127127
<properties>
128128
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
129129
<spring.version>3.0.5.RELEASE</spring.version>
@@ -178,4 +178,4 @@
178178
<version>2.1</version>
179179
</dependency>
180180
</dependencies>
181-
</project>
181+
</project>

api-sdk/src/main/java/com/smartling/api/sdk/file/DateFormatter.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,22 @@
2020
/**
2121
* Utility class for handling dates with the Smartling Translation API.
2222
*/
23-
public class DateFormatter
23+
public abstract class DateFormatter
2424
{
2525
/** Date format used by the Smartling Translation API */
2626
private static final String API_DATE_FORMAT = "YYYY-MM-DDThh:mm:ss";
2727

28+
private DateFormatter()
29+
{
30+
}
31+
2832
/**
2933
* Simple method to format a date into the string format used by the Smartling Translation API.
3034
*
3135
* @param date the date to format
3236
* @return formatted string version of the date or null if the date is null.
3337
*/
34-
public static final String formatDate(Date date)
38+
public static String formatDate(Date date)
3539
{
3640
if (null == date)
3741
return null;

api-sdk/src/main/java/com/smartling/api/sdk/file/FileApiClientAdapter.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Copyright 2012 Smartling, Inc.
1+
/*
2+
* Copyright 2012 Smartling, Inc.
23
*
34
* Licensed under the Apache License, Version 2.0 (the "License");
45
* you may not use this work except in compliance with the License.
@@ -10,7 +11,8 @@
1011
* distributed under the License is distributed on an "AS IS" BASIS,
1112
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1213
* See the License for the specific language governing permissions and
13-
* limitations under the License. */
14+
* limitations under the License.
15+
*/
1416
package com.smartling.api.sdk.file;
1517

1618
import com.smartling.api.sdk.file.response.ApiResponse;
@@ -88,4 +90,4 @@ public interface FileApiClientAdapter
8890
* @throws FileApiException if a non success is returned from the service.
8991
*/
9092
ApiResponse<EmptyResponse> renameFile(String fileUri, String newFileUri) throws FileApiException;
91-
}
93+
}

api-sdk/src/main/java/com/smartling/api/sdk/file/FileApiClientAdapterImpl.java

Lines changed: 101 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Copyright 2012 Smartling, Inc.
1+
/*
2+
* Copyright 2012 Smartling, Inc.
23
*
34
* Licensed under the Apache License, Version 2.0 (the "License");
45
* you may not use this work except in compliance with the License.
@@ -10,7 +11,8 @@
1011
* distributed under the License is distributed on an "AS IS" BASIS,
1112
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1213
* See the License for the specific language governing permissions and
13-
* limitations under the License. */
14+
* limitations under the License.
15+
*/
1416
package com.smartling.api.sdk.file;
1517

1618
import static com.smartling.api.sdk.file.FileApiParams.API_KEY;
@@ -35,6 +37,7 @@
3537
import com.google.gson.reflect.TypeToken;
3638
import com.smartling.api.sdk.file.response.ApiResponse;
3739
import com.smartling.api.sdk.file.response.ApiResponseWrapper;
40+
import com.smartling.api.sdk.file.response.Data;
3841
import com.smartling.api.sdk.file.response.EmptyResponse;
3942
import com.smartling.api.sdk.file.response.FileList;
4043
import com.smartling.api.sdk.file.response.FileStatus;
@@ -44,7 +47,6 @@
4447
import java.io.IOException;
4548
import java.io.InputStream;
4649
import java.io.StringWriter;
47-
import java.lang.reflect.Type;
4850
import java.util.ArrayList;
4951
import java.util.Collections;
5052
import java.util.List;
@@ -101,6 +103,8 @@ public class FileApiClientAdapterImpl implements FileApiClientAdapter
101103
private String apiKey;
102104
private String projectId;
103105

106+
private ProxyConfiguration proxyConfiguration;
107+
104108
/**
105109
* Instantiate a {@link FileApiClientAdapterImpl} using the production mode setting (non sandbox).
106110
*
@@ -112,6 +116,18 @@ public FileApiClientAdapterImpl(String apiKey, String projectId)
112116
this(SMARTLING_API_URL, apiKey, projectId);
113117
}
114118

119+
/**
120+
* Instantiate a {@link FileApiClientAdapterImpl} using the production mode setting (non sandbox).
121+
*
122+
* @param apiKey your apiKey. Can be found at https://dashboard.smartling.com/settings/api
123+
* @param projectId your projectId. Can be found at https://dashboard.smartling.com/settings/api
124+
* @param proxyConfiguration proxy configuration, pass {@code NULL} to never use proxy
125+
*/
126+
public FileApiClientAdapterImpl(String apiKey, String projectId, ProxyConfiguration proxyConfiguration)
127+
{
128+
this(SMARTLING_API_URL, apiKey, projectId, proxyConfiguration);
129+
}
130+
115131
/**
116132
* Instantiate a {@link FileApiClientAdapterImpl}.
117133
*
@@ -127,6 +143,22 @@ public FileApiClientAdapterImpl(boolean productionMode, String apiKey, String pr
127143
this(productionMode ? SMARTLING_API_URL : SMARTLING_SANDBOX_API_URL, apiKey, projectId);
128144
}
129145

146+
/**
147+
* Instantiate a {@link FileApiClientAdapterImpl}.
148+
*
149+
* @param productionMode True if the production version of the api should be used, false if the Sandbox should be used.
150+
* It is recommended when first integrating your application with the API, that you use the Sandbox and not the production version.
151+
* For more information on the Sandbox, please see https://docs.smartling.com.
152+
*
153+
* @param apiKey your apiKey. Can be found at https://dashboard.smartling.com/settings/api
154+
* @param projectId your projectId. Can be found at https://dashboard.smartling.com/settings/api
155+
* @param proxyConfiguration proxy configuration, pass {@code NULL} to never use proxy
156+
*/
157+
public FileApiClientAdapterImpl(boolean productionMode, String apiKey, String projectId, ProxyConfiguration proxyConfiguration)
158+
{
159+
this(productionMode ? SMARTLING_API_URL : SMARTLING_SANDBOX_API_URL, apiKey, projectId, proxyConfiguration);
160+
}
161+
130162
/**
131163
* Instantiate a {@link FileApiClientAdapterImpl}.
132164
*
@@ -135,6 +167,19 @@ public FileApiClientAdapterImpl(boolean productionMode, String apiKey, String pr
135167
* @param projectId your projectId. Can be found at https://dashboard.smartling.com/settings/api
136168
*/
137169
public FileApiClientAdapterImpl(String baseApiUrl, String apiKey, String projectId)
170+
{
171+
this(baseApiUrl, apiKey, projectId, null);
172+
}
173+
174+
/**
175+
* Instantiate a {@link FileApiClientAdapterImpl}.
176+
*
177+
* @param baseApiUrl the apiUrl to use for interacting with the Smartling Translation API.
178+
* @param apiKey your apiKey. Can be found at https://dashboard.smartling.com/settings/api
179+
* @param projectId your projectId. Can be found at https://dashboard.smartling.com/settings/api
180+
* @param proxyConfiguration proxy configuration, pass {@code NULL} to never use proxy
181+
*/
182+
public FileApiClientAdapterImpl(String baseApiUrl, String apiKey, String projectId, ProxyConfiguration proxyConfiguration)
138183
{
139184
Assert.notNull(baseApiUrl, "Api url is required");
140185
Assert.notNull(apiKey, "apiKey is required");
@@ -143,64 +188,69 @@ public FileApiClientAdapterImpl(String baseApiUrl, String apiKey, String project
143188
this.baseApiUrl = baseApiUrl;
144189
this.apiKey = apiKey;
145190
this.projectId = projectId;
191+
this.proxyConfiguration = proxyConfiguration;
146192
}
147193

194+
@Override
148195
public StringResponse getFile(String fileUri, String locale, RetrievalType retrievalType) throws FileApiException
149196
{
150197
String params = buildParamsQuery(new BasicNameValuePair(FILE_URI, fileUri), new BasicNameValuePair(LOCALE, locale),
151198
new BasicNameValuePair(RETRIEVAL_TYPE, null == retrievalType ? null : retrievalType.name()));
152199
HttpGet getRequest = new HttpGet(buildUrl(GET_FILE_API_URL, params));
153-
StringResponse response = executeHttpcall(getRequest);
154-
155-
return response;
200+
return executeHttpCall(getRequest);
156201
}
157202

203+
@Override
158204
public ApiResponse<FileList> getFilesList(FileListSearchParams fileListSearchParams) throws FileApiException
159205
{
160206
String params = buildFileListParams(fileListSearchParams);
161207
HttpGet getRequest = new HttpGet(buildUrl(GET_FILE_LIST_API_URL, params));
162-
StringResponse response = executeHttpcall(getRequest);
208+
StringResponse response = executeHttpCall(getRequest);
163209

164-
return getApiResponse(response.getContents(), new TypeToken<ApiResponseWrapper<FileList>>() {}.getType());
210+
return getApiResponse(response.getContents(), new TypeToken<ApiResponseWrapper<FileList>>(){});
165211
}
166212

213+
@Override
167214
public ApiResponse<FileStatus> getFileStatus(String fileUri, String locale) throws FileApiException
168215
{
169216
String params = buildParamsQuery(new BasicNameValuePair(FILE_URI, fileUri), new BasicNameValuePair(LOCALE, locale));
170217
HttpGet getRequest = new HttpGet(buildUrl(GET_FILE_STATUS_API_URL, params));
171-
StringResponse response = executeHttpcall(getRequest);
218+
StringResponse response = executeHttpCall(getRequest);
172219

173-
return getApiResponse(response.getContents(), new TypeToken<ApiResponseWrapper<FileStatus>>() {}.getType());
220+
return getApiResponse(response.getContents(), new TypeToken<ApiResponseWrapper<FileStatus>>(){});
174221
}
175222

223+
@Override
176224
public ApiResponse<UploadData> uploadFile(FileType fileType, String fileUri, File fileToUpload, Boolean approveContent, String fileEncoding, String callbackUrl)
177225
throws FileApiException
178226
{
179227
String params = buildParamsQuery(new BasicNameValuePair(FILE_URI, fileUri), new BasicNameValuePair(FILE_TYPE, fileType.getIdentifier()),
180228
new BasicNameValuePair(APPROVED, null == approveContent ? null : Boolean.toString(approveContent)),
181229
new BasicNameValuePair(CALLBACK_URL, callbackUrl));
182230
HttpPost httpPostFile = createFileUploadHttpPostRequest(params, fileToUpload, fileEncoding);
183-
StringResponse response = executeHttpcall(httpPostFile);
231+
StringResponse response = executeHttpCall(httpPostFile);
184232

185-
return getApiResponse(response.getContents(), new TypeToken<ApiResponseWrapper<UploadData>>() {}.getType());
233+
return getApiResponse(response.getContents(), new TypeToken<ApiResponseWrapper<UploadData>>(){});
186234
}
187235

236+
@Override
188237
public ApiResponse<EmptyResponse> deleteFile(String fileUri) throws FileApiException
189238
{
190239
String params = buildParamsQuery(new BasicNameValuePair(FILE_URI, fileUri));
191240
HttpDelete httpDeleteFileRequest = new HttpDelete(buildUrl(DELETE_FILE_URL, params));
192-
StringResponse response = executeHttpcall(httpDeleteFileRequest);
241+
StringResponse response = executeHttpCall(httpDeleteFileRequest);
193242

194-
return getApiResponse(response.getContents(), new TypeToken<ApiResponseWrapper<EmptyResponse>>() {}.getType());
243+
return getApiResponse(response.getContents(), new TypeToken<ApiResponseWrapper<EmptyResponse>>(){});
195244
}
196245

246+
@Override
197247
public ApiResponse<EmptyResponse> renameFile(String fileUri, String newFileUri) throws FileApiException
198248
{
199249
String params = buildParamsQuery(new BasicNameValuePair(FILE_URI, fileUri), new BasicNameValuePair(NEW_FILE_URI, newFileUri));
200250
HttpPost httpPostRequest = new HttpPost(buildUrl(RENAME_FILE_URL, params));
201-
StringResponse response = executeHttpcall(httpPostRequest);
251+
StringResponse response = executeHttpCall(httpPostRequest);
202252

203-
return getApiResponse(response.getContents(), new TypeToken<ApiResponseWrapper<EmptyResponse>>() {}.getType());
253+
return getApiResponse(response.getContents(), new TypeToken<ApiResponseWrapper<EmptyResponse>>(){});
204254
}
205255

206256
private HttpPost createFileUploadHttpPostRequest(String apiParameters, File fileToUpload, String fileEncoding)
@@ -217,12 +267,12 @@ private HttpPost createFileUploadHttpPostRequest(String apiParameters, File file
217267

218268
private String buildUrl(String apiServerUrl, String apiParameters)
219269
{
220-
StringBuffer urlWithParameters = new StringBuffer(String.format(apiServerUrl, baseApiUrl));
270+
StringBuilder urlWithParameters = new StringBuilder(String.format(apiServerUrl, baseApiUrl));
221271
urlWithParameters.append(apiParameters);
222272
return urlWithParameters.toString();
223273
}
224274

225-
private StringResponse executeHttpcall(HttpRequestBase httpRequest) throws FileApiException
275+
private StringResponse executeHttpCall(HttpRequestBase httpRequest) throws FileApiException
226276
{
227277
HttpClient httpClient = null;
228278
try
@@ -249,30 +299,46 @@ private StringResponse executeHttpcall(HttpRequestBase httpRequest) throws FileA
249299

250300
private void setupProxy(HttpClient httpClient)
251301
{
252-
HttpHost proxy;
253-
String protocol = null;
254-
255-
if(StringUtils.isNotBlank(System.getProperty(SCHEME_HTTPS + PROPERTY_SUFFIX_PROXY_HOST)) && StringUtils.isNotBlank(System.getProperty(SCHEME_HTTPS + PROPERTY_SUFFIX_PROXY_PORT)))
256-
protocol = SCHEME_HTTPS;
257-
else if(StringUtils.isNotBlank(System.getProperty(SCHEME_HTTP + PROPERTY_SUFFIX_PROXY_HOST)) && StringUtils.isNotBlank(System.getProperty(SCHEME_HTTP + PROPERTY_SUFFIX_PROXY_PORT)))
258-
protocol = SCHEME_HTTP;
302+
String proxyHost = null;
303+
Integer proxyPort = null;
304+
String proxyUsername = null;
305+
String proxyPassword = null;
259306

260-
if(null != protocol)
307+
if (proxyConfiguration != null)
308+
{
309+
proxyHost = proxyConfiguration.getHost();
310+
proxyPort = proxyConfiguration.getPort();
311+
proxyUsername = proxyConfiguration.getUsername();
312+
proxyPassword = proxyConfiguration.getPassword();
313+
}
314+
else
261315
{
262-
String proxyHost = System.getProperty(protocol + PROPERTY_SUFFIX_PROXY_HOST);
263-
Integer proxyPort = Integer.valueOf(System.getProperty(protocol + PROPERTY_SUFFIX_PROXY_PORT));
264-
String proxyUsername = System.getProperty(protocol + PROPERTY_SUFFIX_PROXY_USERNAME);
265-
String proxyPassword = System.getProperty(protocol + PROPERTY_SUFFIX_PROXY_PASSWORD);
316+
String protocol = null;
317+
if (StringUtils.isNotBlank(System.getProperty(SCHEME_HTTPS + PROPERTY_SUFFIX_PROXY_HOST)) && StringUtils.isNotBlank(System.getProperty(SCHEME_HTTPS + PROPERTY_SUFFIX_PROXY_PORT)))
318+
protocol = SCHEME_HTTPS;
319+
else if (StringUtils.isNotBlank(System.getProperty(SCHEME_HTTP + PROPERTY_SUFFIX_PROXY_HOST)) && StringUtils.isNotBlank(System.getProperty(SCHEME_HTTP + PROPERTY_SUFFIX_PROXY_PORT)))
320+
protocol = SCHEME_HTTP;
266321

267-
if(StringUtils.isNotBlank(proxyUsername) && StringUtils.isNotBlank(proxyPassword) && httpClient instanceof DefaultHttpClient)
322+
if (protocol != null)
323+
{
324+
proxyHost = System.getProperty(protocol + PROPERTY_SUFFIX_PROXY_HOST);
325+
proxyPort = Integer.valueOf(System.getProperty(protocol + PROPERTY_SUFFIX_PROXY_PORT));
326+
proxyUsername = System.getProperty(protocol + PROPERTY_SUFFIX_PROXY_USERNAME);
327+
proxyPassword = System.getProperty(protocol + PROPERTY_SUFFIX_PROXY_PASSWORD);
328+
}
329+
}
330+
331+
if (proxyHost != null && proxyPort != null)
332+
{
333+
if (StringUtils.isNotBlank(proxyUsername) && StringUtils.isNotBlank(proxyPassword) && httpClient instanceof DefaultHttpClient)
268334
{
269335
((DefaultHttpClient)httpClient).getCredentialsProvider().setCredentials(
270-
new AuthScope(proxyHost, proxyPort, protocol),
336+
new AuthScope(proxyHost, proxyPort),
271337
new UsernamePasswordCredentials(proxyUsername, proxyPassword)
272338
);
273339
}
274340

275-
proxy = new HttpHost(proxyHost, proxyPort, protocol);
341+
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
276342
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
277343
}
278344
}
@@ -321,10 +387,9 @@ private List<BasicNameValuePair> getNameValuePairs(String name, List<String> val
321387
return nameValuePairs;
322388
}
323389

324-
@SuppressWarnings("rawtypes")
325-
private ApiResponse getApiResponse(String response, Type responseType)
390+
private <T extends Data> ApiResponse<T> getApiResponse(String response, TypeToken<ApiResponseWrapper<T>> responseType)
326391
{
327-
ApiResponseWrapper responseWrapper = new Gson().fromJson(response, responseType);
392+
ApiResponseWrapper<T> responseWrapper = new Gson().fromJson(response, responseType.getType());
328393
return responseWrapper.getResponse();
329394
}
330395

api-sdk/src/main/java/com/smartling/api/sdk/file/FileApiParams.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Copyright 2012 Smartling, Inc.
1+
/*
2+
* Copyright 2012 Smartling, Inc.
23
*
34
* Licensed under the Apache License, Version 2.0 (the "License");
45
* you may not use this work except in compliance with the License.
@@ -10,7 +11,8 @@
1011
* distributed under the License is distributed on an "AS IS" BASIS,
1112
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1213
* See the License for the specific language governing permissions and
13-
* limitations under the License. */
14+
* limitations under the License.
15+
*/
1416
package com.smartling.api.sdk.file;
1517

1618
/**

api-sdk/src/main/java/com/smartling/api/sdk/file/FileListSearchParams.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Copyright 2012 Smartling, Inc.
1+
/*
2+
* Copyright 2012 Smartling, Inc.
23
*
34
* Licensed under the Apache License, Version 2.0 (the "License");
45
* you may not use this work except in compliance with the License.

0 commit comments

Comments
 (0)