Commit 8af0b01f authored by Tiago Cunha's avatar Tiago Cunha Committed by GitHub

Merge pull request #110 from RocketChat/feature/server-api-verification

Checks for the server version
parents cab83399 67de14f4
...@@ -2,11 +2,14 @@ package chat.rocket.android.fragment.server_config; ...@@ -2,11 +2,14 @@ package chat.rocket.android.fragment.server_config;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONObject; import org.json.JSONObject;
import chat.rocket.android.R; import chat.rocket.android.R;
import chat.rocket.android.RocketChatCache; import chat.rocket.android.RocketChatCache;
import chat.rocket.android.helper.LogcatIfError; import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.helper.OkHttpHelper;
import chat.rocket.android.helper.ServerPolicyHelper;
import chat.rocket.android.helper.TextUtils; import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.ServerConfig; import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.realm_helper.RealmObjectObserver; import chat.rocket.android.realm_helper.RealmObjectObserver;
...@@ -37,11 +40,42 @@ public class InputHostnameFragment extends AbstractServerConfigFragment { ...@@ -37,11 +40,42 @@ public class InputHostnameFragment extends AbstractServerConfigFragment {
} }
private void handleConnect() { private void handleConnect() {
final String hostname = ServerPolicyHelper.enforceHostname(getHostname());
ServerPolicyHelper.isApiVersionValid(OkHttpHelper.getClientForUploadFile(), hostname,
new ServerPolicyHelper.Callback() {
@Override
public void isValid() {
getActivity().runOnUiThread(() -> onServerValid(hostname));
}
@Override
public void isNotValid() {
getActivity().runOnUiThread(() ->
Toast.makeText(getActivity(), R.string.input_hostname_invalid_server_message,
Toast.LENGTH_SHORT).show());
}
});
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onDestroyView() {
serverConfigObserver.unsub();
super.onDestroyView();
}
private String getHostname() {
final TextView editor = (TextView) rootView.findViewById(R.id.editor_hostname); final TextView editor = (TextView) rootView.findViewById(R.id.editor_hostname);
final String hostname = return TextUtils.or(TextUtils.or(editor.getText(), editor.getHint()), "").toString();
TextUtils.or(TextUtils.or(editor.getText(), editor.getHint()), "").toString(); }
private void onServerValid(String hostname) {
RocketChatCache.get(getContext()).edit() RocketChatCache.get(getContext()).edit()
.putString(RocketChatCache.KEY_SELECTED_SERVER_CONFIG_ID, serverConfigId) .putString(RocketChatCache.KEY_SELECTED_SERVER_CONFIG_ID, serverConfigId)
.apply(); .apply();
...@@ -55,17 +89,6 @@ public class InputHostnameFragment extends AbstractServerConfigFragment { ...@@ -55,17 +89,6 @@ public class InputHostnameFragment extends AbstractServerConfigFragment {
.put("state", ServerConfig.STATE_READY))).continueWith(new LogcatIfError()); .put("state", ServerConfig.STATE_READY))).continueWith(new LogcatIfError());
} }
@Override
public void onResume() {
super.onResume();
}
@Override
public void onDestroyView() {
serverConfigObserver.unsub();
super.onDestroyView();
}
private void showError(String errString) { private void showError(String errString) {
Snackbar.make(rootView, errString, Snackbar.LENGTH_LONG).show(); Snackbar.make(rootView, errString, Snackbar.LENGTH_LONG).show();
} }
......
package chat.rocket.android.helper;
import android.support.annotation.NonNull;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
public class ServerPolicyHelper {
private static final String DEFAULT_HOST = ".rocket.chat";
private static final String API_INFO_PATH = "/api/info";
private static final String VERSION_PROPERTY = "version";
public static String enforceHostname(String hostname) {
if (hostname == null) {
return "demo.rocket.chat";
}
return removeProtocol(enforceDefaultHost(hostname));
}
public static void isApiVersionValid(@NonNull OkHttpClient client, @NonNull String host,
@NonNull Callback callback) {
Request request;
try {
request = new Request.Builder()
.url("https://" + host + API_INFO_PATH)
.get()
.build();
} catch (Exception e) {
callback.isNotValid();
return;
}
client.newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException exception) {
// some connection error
callback.isNotValid();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful() || !isValid(response.body())) {
callback.isNotValid();
return;
}
callback.isValid();
}
});
}
@NonNull
private static String enforceDefaultHost(String hostname) {
if (hostname.indexOf('.') == -1) {
hostname = hostname + DEFAULT_HOST;
}
return hostname;
}
@NonNull
private static String removeProtocol(String hostname) {
// yep. cheap.
return hostname.replace("http://", "").replace("https://", "");
}
private static boolean isValid(ResponseBody body) {
if (body == null || body.contentLength() == 0) {
return false;
}
try {
final JSONObject jsonObject = new JSONObject(body.string());
return jsonObject.has(VERSION_PROPERTY)
&& isVersionValid(jsonObject.getString(VERSION_PROPERTY));
} catch (Exception e) {
return false;
}
}
private static boolean isVersionValid(String version) {
if (version == null || version.length() == 0) {
return false;
}
String[] versionParts = version.split("\\.");
return versionParts.length >= 3 && Integer.parseInt(versionParts[1]) >= 49;
}
public interface Callback {
void isValid();
void isNotValid();
}
}
...@@ -39,4 +39,6 @@ ...@@ -39,4 +39,6 @@
<string name="image_upload_message_spec_title">Attach image</string> <string name="image_upload_message_spec_title">Attach image</string>
<string name="audio_upload_message_spec_title">Attach audio</string> <string name="audio_upload_message_spec_title">Attach audio</string>
<string name="video_upload_message_spec_title">Attach video</string> <string name="video_upload_message_spec_title">Attach video</string>
<string name="input_hostname_invalid_server_message">Invalid server version</string>
</resources> </resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment