Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Capabilities.java « model « shared « notes « owncloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-notes.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b182ca6395003475b91a422f172c153e9965d986 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package it.niedermann.owncloud.notes.shared.model;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.load.HttpException;
import com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException;

import org.json.JSONException;
import org.json.JSONObject;

import static java.net.HttpURLConnection.HTTP_UNAVAILABLE;

/**
 * This entity class is used to return relevant data of the HTTP reponse.
 */
public class Capabilities {

    private static final String TAG = Capabilities.class.getSimpleName();

    private static final String JSON_OCS = "ocs";
    private static final String JSON_OCS_META = "meta";
    private static final String JSON_OCS_META_STATUSCODE = "statuscode";
    private static final String JSON_OCS_DATA = "data";
    private static final String JSON_OCS_DATA_CAPABILITIES = "capabilities";
    private static final String JSON_OCS_DATA_CAPABILITIES_NOTES = "notes";
    private static final String JSON_OCS_DATA_CAPABILITIES_NOTES_API_VERSION = "api_version";
    private static final String JSON_OCS_DATA_CAPABILITIES_THEMING = "theming";
    private static final String JSON_OCS_DATA_CAPABILITIES_THEMING_COLOR = "color";
    private static final String JSON_OCS_DATA_CAPABILITIES_THEMING_COLOR_TEXT = "color-text";

    private String apiVersion = null;
    private String color = null;
    private String textColor = null;
    @Nullable
    private String eTag;

    public Capabilities(@NonNull String response, @Nullable String eTag) throws NextcloudHttpRequestFailedException {
        this.eTag = eTag;
        final JSONObject ocs;
        try {
            ocs = new JSONObject(response).getJSONObject(JSON_OCS);
            if (ocs.has(JSON_OCS_META)) {
                final JSONObject meta = ocs.getJSONObject(JSON_OCS_META);
                if (meta.has(JSON_OCS_META_STATUSCODE)) {
                    if (meta.getInt(JSON_OCS_META_STATUSCODE) == HTTP_UNAVAILABLE) {
                        Log.i(TAG, "Capabilities Endpoint: This instance is currently in maintenance mode.");
                        throw new NextcloudHttpRequestFailedException(HTTP_UNAVAILABLE, new HttpException(HTTP_UNAVAILABLE));
                    }
                }
            }
            if (ocs.has(JSON_OCS_DATA)) {
                final JSONObject data = ocs.getJSONObject(JSON_OCS_DATA);
                if (data.has(JSON_OCS_DATA_CAPABILITIES)) {
                    final JSONObject capabilities = data.getJSONObject(JSON_OCS_DATA_CAPABILITIES);
                    if (capabilities.has(JSON_OCS_DATA_CAPABILITIES_NOTES)) {
                        final JSONObject notes = capabilities.getJSONObject(JSON_OCS_DATA_CAPABILITIES_NOTES);
                        if (notes.has(JSON_OCS_DATA_CAPABILITIES_NOTES_API_VERSION)) {
                            this.apiVersion = notes.getString(JSON_OCS_DATA_CAPABILITIES_NOTES_API_VERSION);
                        }
                    }
                    if (capabilities.has(JSON_OCS_DATA_CAPABILITIES_THEMING)) {
                        final JSONObject theming = capabilities.getJSONObject(JSON_OCS_DATA_CAPABILITIES_THEMING);
                        if (theming.has(JSON_OCS_DATA_CAPABILITIES_THEMING_COLOR)) {
                            this.color = theming.getString(JSON_OCS_DATA_CAPABILITIES_THEMING_COLOR);
                        }
                        if (theming.has(JSON_OCS_DATA_CAPABILITIES_THEMING_COLOR_TEXT)) {
                            this.textColor = theming.getString(JSON_OCS_DATA_CAPABILITIES_THEMING_COLOR_TEXT);
                        }
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public String getApiVersion() {
        return apiVersion;
    }

    public String getColor() {
        return color;
    }

    public String getTextColor() {
        return textColor;
    }

    @Nullable
    public String getETag() {
        return eTag;
    }

    @NonNull
    @Override
    public String toString() {
        return "Capabilities{" +
                "apiVersion='" + apiVersion + '\'' +
                ", color='" + color + '\'' +
                ", textColor='" + textColor + '\'' +
                '}';
    }
}