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.
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+ */
1416package com .smartling .api .sdk .file ;
1517
1618import static com .smartling .api .sdk .file .FileApiParams .API_KEY ;
3537import com .google .gson .reflect .TypeToken ;
3638import com .smartling .api .sdk .file .response .ApiResponse ;
3739import com .smartling .api .sdk .file .response .ApiResponseWrapper ;
40+ import com .smartling .api .sdk .file .response .Data ;
3841import com .smartling .api .sdk .file .response .EmptyResponse ;
3942import com .smartling .api .sdk .file .response .FileList ;
4043import com .smartling .api .sdk .file .response .FileStatus ;
4447import java .io .IOException ;
4548import java .io .InputStream ;
4649import java .io .StringWriter ;
47- import java .lang .reflect .Type ;
4850import java .util .ArrayList ;
4951import java .util .Collections ;
5052import 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
0 commit comments