Skip to content

Commit

Permalink
update network configuration to be changed in runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
2math committed Nov 1, 2020
1 parent f061740 commit f5deff5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,20 @@
/build
/captures
.externalNativeBuild
/.idea/.gitignore
/app/dev/release/app-dev-release.apk
/.idea/caches/build_file_checksums.ser
/.idea/codeStyles/codeStyleConfig.xml
/.idea/compiler.xml
/.idea/encodings.xml
/.idea/gradle.xml
/.idea/caches/gradle_models.ser
/.idea/jarRepositories.xml
/key.jks
/.idea/misc.xml
/app/dev/release/output.json
/.idea/codeStyles/Project.xml
/.idea/runConfigurations.xml
/.idea/dictionaries/SmartIntr.xml
/app/test_key.jks
/.idea/vcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,34 @@
import com.futurist_labs.android.base_library.repository.persistence.BaseJsonParser;
import com.futurist_labs.android.base_library.utils.LogUtils;

import javax.net.ssl.*;
import java.io.*;
import java.net.*;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


/**
* Created by Galeen on 18.1.2016 г..
Expand Down Expand Up @@ -140,11 +158,11 @@ static NetworkResponse sendPost(String endpoint, String params, String authToken

private static void addMainHeaders(final String authToken, final HttpURLConnection conn) {
if (authToken != null) {
conn.setRequestProperty(NetConstants.HEADER_AUTHORIZATION, authToken);
conn.setRequestProperty(BaseLibraryConfiguration.getInstance().getHeaderAuthorizationFieldName(), authToken);
}

if (NetConstants.SHOULD_ADD_LOCALE) {
conn.setRequestProperty(NetConstants.HEADER_LOCALE_FIELD, BaseLibraryConfiguration.getInstance().getHeaderLocaleFieldValue());
if (BaseLibraryConfiguration.getInstance().isAddLocale()) {
conn.setRequestProperty(BaseLibraryConfiguration.getInstance().getHeaderLocaleFieldName(), BaseLibraryConfiguration.getInstance().getHeaderLocaleFieldValue());
}

if (BaseLibraryConfiguration.getInstance().getHeaderOS() != null) {
Expand Down Expand Up @@ -231,10 +249,10 @@ static NetworkResponse sendAuthenticatedGet(String serverUrl, String token, Map<
headers = new HashMap<>();
}
if (token != null) {
headers.put(NetConstants.HEADER_AUTHORIZATION, token);
headers.put(BaseLibraryConfiguration.getInstance().getHeaderAuthorizationFieldName(), token);
}
if (NetConstants.SHOULD_ADD_LOCALE) {
headers.put(NetConstants.HEADER_LOCALE_FIELD,
if (BaseLibraryConfiguration.getInstance().isAddLocale()) {
headers.put(BaseLibraryConfiguration.getInstance().getHeaderLocaleFieldName(),
BaseLibraryConfiguration.getInstance().getHeaderLocaleFieldValue());
}
return sendGet(serverUrl, params, headers);
Expand Down Expand Up @@ -336,9 +354,9 @@ static NetworkResponse postFile(String urlLink, File uploadFile, String token, M
httpUrlConnection.setRequestProperty(
"Content-Type", "multipart/form-data;boundary=" + boundary);
// if (token != null)
// httpUrlConnection.setRequestProperty(NetConstants.HEADER_AUTHORIZATION, token);
if (NetConstants.SHOULD_ADD_LOCALE) {
httpUrlConnection.setRequestProperty(NetConstants.HEADER_LOCALE_FIELD,
// httpUrlConnection.setRequestProperty(BaseLibraryConfiguration.getInstance().getHeaderAuthorizationFieldName(), token);
if (BaseLibraryConfiguration.getInstance().isAddLocale()) {
httpUrlConnection.setRequestProperty(BaseLibraryConfiguration.getInstance().getHeaderLocaleFieldName(),
BaseLibraryConfiguration.getInstance().getHeaderLocaleFieldValue());
}
DataOutputStream request = null;
Expand Down Expand Up @@ -419,7 +437,7 @@ static NetworkResponse multipartRequest(String urlTo, Map<String, String> params
connection.setUseCaches(false);

if (token != null) {
connection.setRequestProperty(NetConstants.HEADER_AUTHORIZATION, token);
connection.setRequestProperty(BaseLibraryConfiguration.getInstance().getHeaderAuthorizationFieldName(), token);
}
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
Expand Down Expand Up @@ -503,7 +521,7 @@ static NetworkResponse multipartRequest(String urlTo, Map<String, String> params
connection.setUseCaches(false);

if (token != null) {
connection.setRequestProperty(NetConstants.HEADER_AUTHORIZATION, token);
connection.setRequestProperty(BaseLibraryConfiguration.getInstance().getHeaderAuthorizationFieldName(), token);
}
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
Expand Down Expand Up @@ -585,7 +603,7 @@ static NetworkResponse downloadFile(String fileURL, File file, String authToken,
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
if (authToken != null) {
httpConn.setRequestProperty(NetConstants.HEADER_AUTHORIZATION, authToken);
httpConn.setRequestProperty(BaseLibraryConfiguration.getInstance().getHeaderAuthorizationFieldName(), authToken);
}

if (headers != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected NetworkResponse doInBackground(Action... params) {
}

if (res != null && res.isResponsePositive()) {
if (!action.isCheckServerUrl || (res.url != null && res.url.startsWith(NetConstants.SERVER_ADDRESS))) {
if (!action.isCheckServerUrl || (res.url != null && res.url.startsWith(BaseLibraryConfiguration.getInstance().getServerUrl()))) {
//we can use the url from action too
if (mCallback != null){
mCallback.inTheEndOfDoInBackground(res);
Expand Down Expand Up @@ -112,7 +112,7 @@ public static NetworkResponse doServerCall(final Action action, String token) {
try {
tracker.startTracker();
if (!action.isFullUrl)
action.endpoint = NetConstants.SERVER_ADDRESS + action.endpoint;
action.endpoint = BaseLibraryConfiguration.getInstance().getServerUrl() + action.endpoint;
tracker.logPrettyJson(action, token);
switch (action.operation) {
case Action.PUT:
Expand Down

0 comments on commit f5deff5

Please sign in to comment.