Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,11 @@ public void onCreate() {
new String[]{"en", "el", "de", "fr", "ar", "sl", "es_ES", "es_MX"},
null);

TxNative.init(
// application context
getApplicationContext(),
// a LocaleState instance
localeState,
// token
token,
// cdsHost URL
null,
// a TxCache implementation
null,
// a MissingPolicy implementation
null);
TxNative.initializer(base, localeState, token)
.withCdsHost(null) // cdsHost URL
.withCache(null) // a TxCache implementation
.withMissingPolicy(null) // a MissingPolicy implementation
.init();

// Fetch all translations from CDS
TxNative.fetchTranslations(null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ protected void attachBaseContext(Context base) {
new String[]{"en", "el", "de", "fr", "ar", "sl"},
null);

TxNative.init(
base, // application context
localeState, // a LocaleState instance
token, // token
null, // cdsHost URL
null, // a TxCache implementation
null); // a MissingPolicy implementation
TxNative.initializer(base, localeState, token)
.withCdsHost(null) // cdsHost URL
.withCache(null) // a TxCache implementation
.withMissingPolicy(null) // a MissingPolicy implementation
.init();

// OPTIONAL:
// Wrap the application's base context to allow TxNative to intercept all string resource
Expand All @@ -64,7 +62,8 @@ protected void attachBaseContext(Context base) {
// strings translated.
super.attachBaseContext(TxNative.wrap(base));

// SAFER: Do not wrap the application's base context.
// SAFER: Do not wrap the application's base context. Just pass the base context as you
// would normally do.
// super.attachBaseContext(base);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public Integer call() throws Exception {

// Push to CDS
CDSHandler cdsHandler = new CDSHandler(null, token, secret,
mainClass.hostURL);
mainClass.hostURL, null);
LocaleData.TxJobStatus jobStatus = null;
try {
jobStatus = cdsHandler.pushSourceStrings(postData);
Expand Down Expand Up @@ -265,7 +265,7 @@ public Integer call() throws Exception {

// Push to CDS
CDSHandler cdsHandler = new CDSHandler(null, token, secret,
mainClass.hostURL);
mainClass.hostURL, null);
LocaleData.TxJobStatus jobStatus = null;
try {
jobStatus = cdsHandler.pushSourceStrings(postData);
Expand Down Expand Up @@ -358,7 +358,7 @@ public Integer call() throws Exception {

// Pull from CDS
CDSHandler cdsHandler = new CDSHandler(translatedLocales, token, null,
mainClass.hostURL);
mainClass.hostURL, null);
TranslationsDownloader downloader = new TranslationsDownloader(cdsHandler);
HashMap<String, File> downloadedFiles = downloader.downloadTranslations(null, tags, outDir, OUT_FILE_NAME);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class CDSHandler {
// The host of the Content Delivery Service
private final String mCdsHost;

// A custom authorization header key
private final String mCustomAuthorizationHeaderKey;

private final Gson mGson;

/**
Expand Down Expand Up @@ -99,18 +102,21 @@ void onTranslationFetched(@Nullable InputStream inputStream, @NonNull String loc
/**
* Creates a CDSHandler instance.
*
* @param localeCodes An array of locale codes that can be downloaded from CDS. The source
* locale can also be included.
* @param token The API token to use for connecting to the CDS.
* @param secret The API secret to use for connecting to the CDS.
* @param csdHost The host of the Content Delivery Service.
* @param localeCodes An array of locale codes that can be downloaded from CDS. The source
* locale can also be included.
* @param token The API token to use for connecting to the CDS.
* @param secret The API secret to use for connecting to the CDS.
* @param csdHost The host of the Content Delivery Service.
* @param customAuthorizationHeaderKey A custom HTTP header name used to pass the token to CDS.
*/
public CDSHandler(@Nullable String[] localeCodes,
@NonNull String token, @Nullable String secret, @NonNull String csdHost) {
@NonNull String token, @Nullable String secret, @NonNull String csdHost,
String customAuthorizationHeaderKey) {
mLocaleCodes = localeCodes;
mToken = token;
mSecret = secret;
mCdsHost = csdHost;
mCustomAuthorizationHeaderKey = customAuthorizationHeaderKey;

mGson = new Gson();
}
Expand Down Expand Up @@ -547,11 +553,13 @@ private void addHeaders(@NonNull HttpURLConnection connection, boolean withSecre

connection.addRequestProperty("Content-type", "application/json; charset=utf-8");

String authorizationKey = (mCustomAuthorizationHeaderKey != null) ?
mCustomAuthorizationHeaderKey : "Authorization";
if (withSecret) {
connection.addRequestProperty("Authorization", "Bearer " + mToken + ":" + mSecret);
connection.addRequestProperty(authorizationKey, "Bearer " + mToken + ":" + mSecret);
}
else {
connection.addRequestProperty("Authorization", "Bearer " + mToken);
connection.addRequestProperty(authorizationKey, "Bearer " + mToken);
}

connection.addRequestProperty("x-native-sdk", "mobile/android/" + BuildProperties.getSDKVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void onFailure(@NonNull Exception exception) {
@Test
public void testFetchTranslationsCallback_badURL() {
String[] localeCodes = new String[]{"el", "es"};
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, "invalidHostURL");
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, "invalidHostURL", null);

DummyFetchCallback callback = new DummyFetchCallback();

Expand All @@ -88,7 +88,7 @@ public void testFetchTranslationsCallback_normalResponse() {
cdsMock.getServer().setDispatcher(CDSMockHelper.getElEsDispatcher());

String[] localeCodes = new String[]{"el", "es"};
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl(), null);

DummyFetchCallback callback = new DummyFetchCallback() {

Expand Down Expand Up @@ -136,12 +136,38 @@ public void onTranslationFetched(@Nullable InputStream inputStream, @NonNull Str
assertThat(recordedRequest.getHeader("Accept-version")).isEqualTo("v2");
}

@Test
public void testFetchTranslationsCallback_customAuthorizationHeaderKey() {
cdsMock.getServer().setDispatcher(CDSMockHelper.getElEsDispatcher());

String[] localeCodes = new String[]{"el", "es"};
String customAuthorizationHeaderKey = "CustomKey";
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl(), customAuthorizationHeaderKey);

DummyFetchCallback callback = new DummyFetchCallback();

cdsHandler.fetchTranslations(null, null, callback);

assertThat(callback.onFetchingTranslationsCalled).isTrue();
assertThat(callback.onFetchingTranslationsCalled).isTrue();
assertThat(callback.onFailureCalled).isFalse();

RecordedRequest recordedRequest = null;
try {
recordedRequest = cdsMock.getServer().takeRequest();
} catch (InterruptedException ignored) {
}
assertThat(recordedRequest).isNotNull();
assertThat(recordedRequest.getMethod()).isEqualTo("GET");
assertThat(recordedRequest.getHeader(customAuthorizationHeaderKey)).isEqualTo("Bearer token");
}

@Test
public void testFetchTranslationsCallback_onlyElInResponse() {
cdsMock.getServer().setDispatcher(CDSMockHelper.getElDispatcher());

String[] localeCodes = new String[]{"el", "es"};
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl(), null);

DummyFetchCallback callback = new DummyFetchCallback() {

Expand Down Expand Up @@ -193,7 +219,7 @@ public void onTranslationFetched(@Nullable InputStream inputStream, @NonNull Str
@Test
public void testFetchTranslations_badURL() {
String[] localeCodes = new String[]{"el", "es"};
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, "invalidHostURL");
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, "invalidHostURL", null);

LocaleData.TranslationMap map = cdsHandler.fetchTranslations(null, null);
assertThat(map).isNotNull();
Expand All @@ -205,7 +231,7 @@ public void testFetchTranslations_normalResponse() {
cdsMock.getServer().setDispatcher(CDSMockHelper.getElEsDispatcher());

String[] localeCodes = new String[]{"el", "es"};
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl(), null);

LocaleData.TranslationMap map = cdsHandler.fetchTranslations(null, null);

Expand Down Expand Up @@ -241,7 +267,7 @@ public void testFetchTranslations_onlyElInResponse() {
cdsMock.getServer().setDispatcher(CDSMockHelper.getElDispatcher());

String[] localeCodes = new String[]{"el", "es"};
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl(), null);

LocaleData.TranslationMap map = cdsHandler.fetchTranslations(null, null);
assertThat(map).isNotNull();
Expand All @@ -259,7 +285,7 @@ public void testFetchTranslations_specifyLocale_normalResponse() {
cdsMock.getServer().setDispatcher(CDSMockHelper.getElEsDispatcher());

String[] localeCodes = new String[]{"el", "es"};
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl(), null);

LocaleData.TranslationMap map = cdsHandler.fetchTranslations("el", null);
assertThat(map).isNotNull();
Expand All @@ -277,7 +303,7 @@ public void testFetchTranslations_specifyTags_normalResponse() {
cdsMock.getServer().setDispatcher(CDSMockHelper.getElEsWithTagsDispatcher());

String[] localeCodes = new String[]{"el", "es"};
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl(), null);

HashSet<String> tags = new HashSet<>(Arrays.asList("tag a", "tag b"));
LocaleData.TranslationMap map = cdsHandler.fetchTranslations(null, tags);
Expand All @@ -302,7 +328,7 @@ public void testFetchTranslations_first202_thenNormalResponse() {
cdsMock.getServer().setDispatcher(CDSMockHelper.getElEs202OnceDispatcher(10));

String[] localeCodes = new String[]{"el", "es"};
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl(), null);

LocaleData.TranslationMap map = cdsHandler.fetchTranslations(null, null);
assertThat(map).isNotNull();
Expand All @@ -326,7 +352,7 @@ public void testFetchTranslations_only202() {
cdsMock.getServer().setDispatcher(CDSMockHelper.getElEs202OnceDispatcher(30));

String[] localeCodes = new String[]{"el", "es"};
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl(), null);

LocaleData.TranslationMap map = cdsHandler.fetchTranslations(null, null);
assertThat(map).isNotNull();
Expand All @@ -338,7 +364,7 @@ public void testFetchTranslations_onlyElInResponse_badJSONFormatting() {
cdsMock.getServer().setDispatcher(CDSMockHelper.getElDispatcherBadJsonFormatting());

String[] localeCodes = new String[]{"el"};
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl(), null);

LocaleData.TranslationMap map = cdsHandler.fetchTranslations(null, null);
assertThat(map).isNotNull();
Expand All @@ -347,7 +373,7 @@ public void testFetchTranslations_onlyElInResponse_badJSONFormatting() {

@Test
public void testPushSourceStrings_badURL() {
CDSHandler cdsHandler = new CDSHandler(null, "token", "secret", "invalidHostURL");
CDSHandler cdsHandler = new CDSHandler(null, "token", "secret", "invalidHostURL", null);

LocaleData.TxPostData postData = getPostData();
LocaleData.TxJobStatus jobStatus = null;
Expand All @@ -362,7 +388,7 @@ public void testPushSourceStrings_badURL() {
public void testPushSourceStrings_normal() {
cdsMock.getServer().setDispatcher(CDSMockHelper.getPostDispatcher());

CDSHandler cdsHandler = new CDSHandler(null, "token", "secret", cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(null, "token", "secret", cdsMock.getBaseUrl(), null);

LocaleData.TxPostData postData = getPostData();
LocaleData.TxJobStatus jobStatus = null;
Expand Down Expand Up @@ -398,7 +424,7 @@ public void testPushSourceStrings_normal() {
public void testPushSourceStrings_CDSRespondsWith409_returnNull() {
cdsMock.getServer().setDispatcher(CDSMockHelper.getPostWith409Dispatcher());

CDSHandler cdsHandler = new CDSHandler(null, "token", "secret", cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(null, "token", "secret", cdsMock.getBaseUrl(), null);

LocaleData.TxPostData postData = getPostData();
LocaleData.TxJobStatus jobStatus = null;
Expand All @@ -414,7 +440,7 @@ public void testPushSourceStrings_CDSRespondsWith409_returnNull() {
public void testPushSourceStrings_CDSRespondsWithFailedJob_returnErrorInResponse() {
cdsMock.getServer().setDispatcher(CDSMockHelper.getPostWithFailedJobDispatcher());

CDSHandler cdsHandler = new CDSHandler(null, "token", "secret", cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(null, "token", "secret", cdsMock.getBaseUrl(), null);

LocaleData.TxPostData postData = getPostData();
LocaleData.TxJobStatus jobStatus = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void Teardown() {
@Test
public void testSaveTranslations_dirDoesNotExist() {
String[] localeCodes = new String[]{"el", "es"};
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl(), null);
TranslationsDownloader downloader = new TranslationsDownloader(cdsHandler);

HashMap<String, File> translationFiles = downloader.downloadTranslations(null, null, tempDir.getFile(), "strings.txt");
Expand All @@ -63,7 +63,7 @@ public void testSaveTranslations_normalResponse() {
assertThat(tempDirCreated).isTrue();

String[] localeCodes = new String[]{"el", "es"};
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl(), null);
TranslationsDownloader downloader = new TranslationsDownloader(cdsHandler);

HashMap<String, File> translationFiles = downloader.downloadTranslations(null, null, tempDir.getFile(), "strings.txt");
Expand Down Expand Up @@ -108,7 +108,7 @@ public void testSaveTranslations_onlyElInResponse() {
assertThat(tempDirCreated).isTrue();

String[] localeCodes = new String[]{"el", "es"};
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl(), null);
TranslationsDownloader downloader = new TranslationsDownloader(cdsHandler);

HashMap<String, File> translationFiles = downloader.downloadTranslations(null, null, tempDir.getFile(), "strings.txt");
Expand All @@ -132,7 +132,7 @@ public void testSaveTranslations_specifyLocale_normalResponse() {
assertThat(tempDirCreated).isTrue();

String[] localeCodes = new String[]{"el", "es"};
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl(), null);
TranslationsDownloader downloader = new TranslationsDownloader(cdsHandler);

HashMap<String, File> translationFiles = downloader.downloadTranslations("el", null, tempDir.getFile(), "strings.txt");
Expand All @@ -156,7 +156,7 @@ public void testSaveTranslations_specifyTags_normalResponse() {
assertThat(tempDirCreated).isTrue();

String[] localeCodes = new String[]{"el", "es"};
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl(), null);
TranslationsDownloader downloader = new TranslationsDownloader(cdsHandler);

Set<String> tags = new HashSet<>(Arrays.asList("tag a", "tag b"));
Expand Down Expand Up @@ -198,7 +198,7 @@ public void testSaveTranslations_overwriteExistingFile() {
} catch (IOException ignored) {}

String[] localeCodes = new String[]{"el"};
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl(), null);
TranslationsDownloader downloader = new TranslationsDownloader(cdsHandler);

HashMap<String, File> translationFiles = downloader.downloadTranslations(null, null, tempDir.getFile(), "strings.txt");
Expand Down Expand Up @@ -231,7 +231,7 @@ public void testSaveTranslations_skipExistingFileIfError() {
} catch (IOException ignored) {}

String[] localeCodes = new String[]{"es"};
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl());
CDSHandler cdsHandler = new CDSHandler(localeCodes, "token", null, cdsMock.getBaseUrl(), null);
TranslationsDownloader downloader = new TranslationsDownloader(cdsHandler);

HashMap<String, File> translationFiles = downloader.downloadTranslations(null, null, tempDir.getFile(), "strings.txt");
Expand Down
Loading
Loading