ClientState.java 4.64 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.http.Cookie
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 */
package com.adobe.granite.ui.components;

import com.adobe.granite.ui.components.AttrBuilder;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.Cookie;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;

@Deprecated
public class ClientState {
    private final String COOKIE_NAME = "cui-state";
    private final String STATE_GLOBAL_NAMESPACE = "global";
    private JSONObject json = null;

    public ClientState(SlingHttpServletRequest slingRequest) {
        Cookie cookie = slingRequest.getCookie("cui-state");
        try {
            String cookieVal = URLDecoder.decode(cookie.getValue(), "UTF-8");
            this.json = new JSONObject(cookieVal);
        }
        catch (Exception ex) {
            this.json = null;
        }
    }

    private void addAttribute(AttrBuilder attr, String attribute, JSONObject attributeJson) throws JSONException {
        if ("class".equals(attribute)) {
            String[] attrClassList;
            String attrClass = attributeJson.optString("val");
            for (String anAttrClassList : attrClassList = attrClass.split("\\s+")) {
                attr.addClass(anAttrClassList);
            }
        } else if ("rel".equals(attribute)) {
            String attrRel = attributeJson.optString("val");
            attr.addRel(attrRel);
        } else if ("href".equals(attribute)) {
            String attrHref = attributeJson.optString("val");
            attr.addHref(attribute, attrHref);
        } else if (attribute.matches("^data-.+")) {
            String attrData = attributeJson.optString("val");
            attr.addOther(attribute.replaceFirst("^data-.+", ""), attrData);
        } else if (attributeJson.optBoolean("val")) {
            attr.addBoolean(attribute, attributeJson.getBoolean("val"));
        } else {
            String attrString = attributeJson.optString("val");
            attr.add(attribute, attrString);
        }
    }

    public JSONObject getState(String selector) {
        return this.getState(selector, "global");
    }

    public JSONObject getState(String selector, String namespace) {
        try {
            JSONObject namespaceJson = this.json.getJSONObject(namespace);
            JSONObject selectorJson = namespaceJson.getJSONObject(selector);
            return selectorJson;
        }
        catch (Exception ex) {
            return null;
        }
    }

    public JSONObject getState(String selector, String[] attributes) {
        return this.getState(selector, attributes, "global");
    }

    public JSONObject getState(String selector, String[] attributes, String namespace) {
        try {
            List<String> attrList = null;
            if (attributes != null) {
                attrList = Arrays.asList(attributes);
            }
            JSONObject selectorJson = this.getState(selector, namespace);
            Iterator keys = selectorJson.keys();
            while (keys.hasNext()) {
                String attribute = (String)keys.next();
                if (attrList == null || attrList.contains(attribute)) continue;
                selectorJson.remove(attribute);
            }
            return selectorJson;
        }
        catch (Exception ex) {
            return null;
        }
    }

    public boolean restoreState(AttrBuilder attr, String selector) {
        return this.restoreState(attr, selector, "global");
    }

    public boolean restoreState(AttrBuilder attr, String selector, String namespace) {
        return this.restoreState(attr, selector, null, namespace);
    }

    public boolean restoreState(AttrBuilder attr, String selector, String[] attributes) {
        return this.restoreState(attr, selector, attributes, "global");
    }

    public boolean restoreState(AttrBuilder attr, String selector, String[] attributes, String namespace) {
        try {
            JSONObject selectorJson = this.getState(selector, attributes, namespace);
            Iterator keys = selectorJson.keys();
            while (keys.hasNext()) {
                String attribute = (String)keys.next();
                JSONObject attributeJson = selectorJson.getJSONObject(attribute);
                this.addAttribute(attr, attribute, attributeJson);
            }
            return true;
        }
        catch (Exception ex) {
            return false;
        }
    }
}