Commit 26ebda44 authored by Tiago Cunha's avatar Tiago Cunha

Checks for the server version

parent cab83399
......@@ -2,11 +2,14 @@ package chat.rocket.android.fragment.server_config;
import android.support.design.widget.Snackbar;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONObject;
import chat.rocket.android.R;
import chat.rocket.android.RocketChatCache;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.helper.OkHttpHelper;
import chat.rocket.android.helper.ServerHelper;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.realm_helper.RealmObjectObserver;
......@@ -37,11 +40,42 @@ public class InputHostnameFragment extends AbstractServerConfigFragment {
}
private void handleConnect() {
final String hostname = ServerHelper.enforceHostname(getHostname());
ServerHelper.isApiVersionValid(OkHttpHelper.getClientForUploadFile(), hostname,
new ServerHelper.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 String hostname =
TextUtils.or(TextUtils.or(editor.getText(), editor.getHint()), "").toString();
return TextUtils.or(TextUtils.or(editor.getText(), editor.getHint()), "").toString();
}
private void onServerValid(String hostname) {
RocketChatCache.get(getContext()).edit()
.putString(RocketChatCache.KEY_SELECTED_SERVER_CONFIG_ID, serverConfigId)
.apply();
......@@ -55,17 +89,6 @@ public class InputHostnameFragment extends AbstractServerConfigFragment {
.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) {
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 ServerHelper {
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 e) {
// 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.valueOf(versionParts[1]) >= 49;
}
public interface Callback {
void isValid();
void isNotValid();
}
}
......@@ -39,4 +39,6 @@
<string name="image_upload_message_spec_title">Attach image</string>
<string name="audio_upload_message_spec_title">Attach audio</string>
<string name="video_upload_message_spec_title">Attach video</string>
<string name="input_hostname_invalid_server_message">Invalid server version</string>
</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