All Downloads are FREE. Search and download functionalities are using the official Maven repository.

denominator.dynect.ResourceRecordSetsAdapter Maven / Gradle / Ivy

There is a newer version: 4.7.1
Show newest version
package denominator.dynect;

import static denominator.common.Util.peekingIterator;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;

import denominator.common.PeekingIterator;
import denominator.dynect.DynECT.Record;
import denominator.dynect.DynECTAdapters.DataAdapter;
import denominator.model.ResourceRecordSet;
import denominator.model.ResourceRecordSet.Builder;

class ResourceRecordSetsAdapter extends DataAdapter>> {

    @Override
    public Iterator> build(JsonReader reader) throws IOException {
        JsonElement data;
        try {
            data = new JsonParser().parse(reader);
        } catch (JsonIOException e) {
            if (e.getCause() != null && e.getCause() instanceof IOException) {
                throw IOException.class.cast(e.getCause());
            }
            throw e;
        }

        // there are 2 forms for record responses: an array of same type, or a
        // map per type.
        if (data.isJsonArray()) {
            return new GroupByRecordNameAndTypeIterator(data.getAsJsonArray().iterator());
        } else if (data.isJsonObject()) {
            List elements = new ArrayList();
            for (Entry entry : data.getAsJsonObject().entrySet()) {
                if (entry.getValue() instanceof JsonArray) {
                    for (JsonElement element : entry.getValue().getAsJsonArray())
                        elements.add(element);
                }
            }
            return new GroupByRecordNameAndTypeIterator(elements.iterator());
        } else {
            throw new IllegalStateException("unknown format: " + data);
        }
    }

    static class GroupByRecordNameAndTypeIterator extends PeekingIterator> {

        private final PeekingIterator peekingIterator;

        public GroupByRecordNameAndTypeIterator(Iterator sortedIterator) {
            this.peekingIterator = peekingIterator(sortedIterator);
        }

        @Override
        protected ResourceRecordSet computeNext() {
            if (!peekingIterator.hasNext())
                return endOfData();
            JsonElement current = peekingIterator.next();
            Record record = ToRecord.INSTANCE.apply(current);
            Builder> builder = ResourceRecordSet.builder().name(record.name).type(record.type)
                    .ttl(record.ttl).add(record.rdata);
            while (peekingIterator.hasNext()) {
                JsonElement next = peekingIterator.peek();
                if (next == null || next.isJsonNull())
                    continue;
                if (fqdnAndTypeEquals(current.getAsJsonObject(), next.getAsJsonObject())) {
                    peekingIterator.next();
                    builder.add(ToRecord.INSTANCE.apply(next).rdata);
                } else {
                    break;
                }
            }
            return builder.build();
        }
    }

    private static boolean fqdnAndTypeEquals(JsonObject current, JsonObject next) {
        return current.get("fqdn").equals(next.get("fqdn"))
                && current.get("record_type").equals(next.get("record_type"));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy