ServerPolicyHelper.java 2.94 KB
Newer Older
1 2 3 4
package chat.rocket.android.helper;

import android.support.annotation.NonNull;

5
import io.reactivex.Flowable;
6 7
import org.json.JSONObject;

Tiago Cunha's avatar
Tiago Cunha committed
8
public class ServerPolicyHelper {
9 10 11 12 13 14 15 16 17

  private static final String DEFAULT_HOST = ".rocket.chat";
  private static final String VERSION_PROPERTY = "version";

  public static String enforceHostname(String hostname) {
    if (hostname == null) {
      return "demo.rocket.chat";
    }

18
    return removeTrailingSlash(removeProtocol(enforceDefaultHost(hostname)));
19 20
  }

21
  public static Flowable<ServerValidation> isApiVersionValid(
Tiago Cunha's avatar
Tiago Cunha committed
22 23 24 25 26
      @NonNull ServerPolicyApiValidationHelper serverPolicyApiValidationHelper) {
    return serverPolicyApiValidationHelper.getApiVersion()
        .map(serverInfo ->
            new ServerValidation(isValid(serverInfo.getApiInfo()),
                serverInfo.usesSecureConnection()));
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  }

  @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://", "");
  }

43 44
  private static String removeTrailingSlash(String hostname) {
    if (hostname.charAt(hostname.length() - 1) != '/') {
Tiago Cunha's avatar
Tiago Cunha committed
45
      // no need for a regex - just return it
46 47 48 49 50 51
      return hostname;
    }

    return hostname.replaceAll("/+$", "");
  }

52 53 54 55
  private static String removeExtraInvalidChars(String hostname) {
    return hostname.replaceAll("[^\\w|\\.|\\-|/]", "");
  }

56 57
  private static boolean isValid(JSONObject jsonObject) {
    if (jsonObject == null) {
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
      return false;
    }

    try {
      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("\\.");
Tiago Cunha's avatar
Tiago Cunha committed
75
    return versionParts.length >= 3 && Integer.parseInt(versionParts[1]) >= 49;
76 77
  }

Yusuke Iwaki's avatar
Yusuke Iwaki committed
78
  public static class ServerInfoResponse {
Tiago Cunha's avatar
Tiago Cunha committed
79 80
    private final boolean secureConnection;
    private final JSONObject apiInfo;
81

Yusuke Iwaki's avatar
Yusuke Iwaki committed
82
    public ServerInfoResponse(boolean secureConnection, JSONObject apiInfo) {
Tiago Cunha's avatar
Tiago Cunha committed
83 84 85 86 87 88 89 90 91 92 93 94
      this.secureConnection = secureConnection;
      this.apiInfo = apiInfo;
    }

    public boolean usesSecureConnection() {
      return secureConnection;
    }

    public JSONObject getApiInfo() {
      return apiInfo;
    }
  }
95

Tiago Cunha's avatar
Tiago Cunha committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  public static class ServerValidation {
    private final boolean valid;
    private final boolean secureConnection;

    public ServerValidation(boolean valid, boolean secureConnection) {
      this.valid = valid;
      this.secureConnection = secureConnection;
    }

    public boolean isValid() {
      return valid;
    }

    public boolean usesSecureConnection() {
      return secureConnection;
    }
112 113
  }
}