SitecatalystJsonItemReader.java 5.08 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Value
 *  javax.jcr.ValueFactory
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.analytics.sitecatalyst.util;

import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SitecatalystJsonItemReader {
    private final Logger LOGGER;
    private String incrementalIndexKey;
    private int startIndex;

    public SitecatalystJsonItemReader(String incrementalIndexKey, int startIndex) {
        this.LOGGER = LoggerFactory.getLogger(this.getClass());
        this.incrementalIndexKey = "";
        this.startIndex = -1;
        this.incrementalIndexKey = incrementalIndexKey;
        this.startIndex = startIndex;
    }

    public SitecatalystJsonItemReader() {
        this.LOGGER = LoggerFactory.getLogger(this.getClass());
        this.incrementalIndexKey = "";
        this.startIndex = -1;
    }

    public void readObject(Node parent, String key, JSONObject obj) throws RepositoryException, JSONException {
        Node child = !parent.hasNode(key) ? parent.addNode(key) : parent.getNode(key);
        Iterator keyIter = obj.keys();
        while (keyIter.hasNext()) {
            String childKey = (String)keyIter.next();
            if (childKey.matches("jcr:.+")) {
                this.LOGGER.debug("skipping JCR property " + childKey);
                continue;
            }
            if (childKey.matches("(^\\/|.*?\\.\\.\\/).*?")) {
                this.LOGGER.warn("skipping path traversal sequence " + childKey);
                continue;
            }
            if (obj.isNull(childKey)) continue;
            Object value = obj.get(childKey);
            if (value instanceof JSONObject) {
                this.readObject(child, childKey, (JSONObject)value);
                continue;
            }
            if (value instanceof JSONArray) {
                this.readArray(child, childKey, (JSONArray)value);
                continue;
            }
            if (value.toString().matches("\\d+")) {
                child.setProperty(childKey, Long.parseLong(value.toString()));
                continue;
            }
            child.setProperty(childKey, value.toString());
        }
    }

    public void readArray(Node parent, String key, JSONArray array) throws RepositoryException, JSONException {
        Object[] values = new Object[array.length()];
        Node child = null;
        int useIndex = 0;
        if (this.startIndex >= 0 && StringUtils.isNotBlank((String)this.incrementalIndexKey) && this.incrementalIndexKey.equalsIgnoreCase(key)) {
            useIndex = this.startIndex;
        }
        for (int i = 0; i < array.length(); ++i) {
            if (array.isNull(i)) continue;
            String childKey = Integer.toString(i + useIndex);
            Object value = array.get(i);
            if (value instanceof JSONObject) {
                if (child == null) {
                    child = !parent.hasNode(key) ? parent.addNode(key) : parent.getNode(key);
                }
                this.readObject(child, childKey, (JSONObject)value);
                values = null;
                continue;
            }
            if (value instanceof JSONArray) {
                if (child == null) {
                    child = !parent.hasNode(key) ? parent.addNode(key) : parent.getNode(key);
                }
                this.readArray(child, childKey, (JSONArray)value);
                values = null;
                continue;
            }
            if (values == null) continue;
            values[i] = value;
        }
        if (values != null) {
            boolean isLong = true;
            for (int i2 = 0; i2 < values.length; ++i2) {
                if (values[i2].toString().matches("\\d+")) continue;
                isLong = false;
                break;
            }
            if (isLong) {
                Value[] longValues = new Value[values.length];
                for (int i3 = 0; i3 < values.length; ++i3) {
                    longValues[i3] = parent.getSession().getValueFactory().createValue(Long.parseLong(values[i3].toString()));
                }
                parent.setProperty(key, longValues);
            } else {
                String[] stringValues = new String[values.length];
                for (int i4 = 0; i4 < values.length; ++i4) {
                    stringValues[i4] = values[i4].toString();
                }
                parent.setProperty(key, stringValues);
            }
        }
    }
}