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

denominator.dynect.DynECTAdapters Maven / Gradle / Ivy

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

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonParser;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import denominator.dynect.DynECT.Data;
import denominator.dynect.DynECT.Record;
import denominator.model.Zone;
import feign.RetryableException;

import static denominator.common.Preconditions.checkState;

/**
 * DynECT json includes an envelope called "data". These type adapters apply at that level.
 */
class DynECTAdapters {

  private static List toFirstGroup(String pattern, JsonArray elements) {
    Pattern compiled = Pattern.compile(pattern);
    List results = new ArrayList(elements.size());
    for (JsonElement in : elements) {
      Matcher matcher = compiled.matcher(in.getAsString());
      checkState(matcher.find() && matcher.groupCount() == 1, "%s didn't match %s", in, compiled);
      results.add(matcher.group(1));
    }
    return results;
  }

  static class NothingForbiddenAdapter extends DataAdapter {

    @Override
    public Boolean build(JsonReader reader) throws IOException {
      return
          new JsonParser().parse(reader).getAsJsonObject().get("forbidden").getAsJsonArray().size()
          == 0;
    }
  }

  static class TokenAdapter extends DataAdapter {

    @Override
    public String build(JsonReader reader) throws IOException {
      return new JsonParser().parse(reader).getAsJsonObject().get("token").getAsString();
    }
  }

  static class ZonesAdapter extends DataAdapter> {

    @Override
    public List build(JsonReader reader) throws IOException {
      JsonArray data = new JsonParser().parse(reader).getAsJsonArray();
      List zones = new ArrayList();
      for (String name : toFirstGroup("/REST.*/([^/]+)/?$", data)) {
        zones.add(Zone.create(name));
      }
      return zones;
    }
  }

  static class RecordIdsAdapter extends DataAdapter> {

    @Override
    public List build(JsonReader reader) throws IOException {
      JsonArray data = new JsonParser().parse(reader).getAsJsonArray();
      return toFirstGroup("/REST/([a-zA-Z]+Record/[^\"]+/[^\"]+/[0-9]+)", data);
    }
  }

  static class RecordsByNameAndTypeAdapter extends DataAdapter> {

    @Override
    public Iterator build(JsonReader reader) throws IOException {
      JsonArray data;
      data = new JsonParser().parse(reader).getAsJsonArray();
      List records = new ArrayList();
      for (JsonElement datum : data) {
        records.add(ToRecord.INSTANCE.apply(datum));
      }
      return records.iterator();
    }
  }

  static abstract class DataAdapter extends TypeAdapter> {

    protected abstract X build(JsonReader reader) throws IOException;

    @Override
    public Data read(JsonReader reader) throws IOException {
      Data data = new Data();
      String status = null;
      reader.beginObject();
      while (reader.hasNext()) {
        String nextName = reader.nextName();
        if ("data".equals(nextName) && reader.peek() != JsonToken.NULL) {
          try {
            data.data = build(reader);
          } catch (JsonIOException e) {
            if (e.getCause() != null && e.getCause() instanceof IOException) {
              throw IOException.class.cast(e.getCause());
            }
            throw e;
          }
        } else if ("status".equals(nextName)) {
          status = reader.nextString();
        } else {
          reader.skipValue();
        }
      }
      reader.endObject();
      if ("incomplete".equals(status)) {
        throw new RetryableException(status, null);
      }
      return data;
    }

    @Override
    public void write(JsonWriter out, Data value) throws IOException {
      throw new UnsupportedOperationException();
    }
  }

  private DynECTAdapters() {
    // no instances.
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy