Java Code Examples for org.codehaus.jettison.json.JSONObject#has()

The following examples show how to use org.codehaus.jettison.json.JSONObject#has() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * For a given json string, this method sets the field members
 *
 * @param json
 * @throws JSONException
 * @throws IOException
 */
private void initialize(String json) throws JSONException, IOException
{
  JSONObject jo = new JSONObject(json);
  if (jo.has(SEPARATOR)) {
    delimiterChar = ((String)jo.getString(SEPARATOR)).charAt(0);
  }
  if (jo.has(QUOTE_CHAR)) {
    quoteChar = ((String)jo.getString(QUOTE_CHAR)).charAt(0);
  }
  if (jo.has(LINE_DELIMITER)) {
    lineDelimiter = (String)jo.getString(LINE_DELIMITER);
  }

  JSONArray fieldArray = jo.getJSONArray(FIELDS);
  for (int i = 0; i < fieldArray.length(); i++) {
    JSONObject obj = fieldArray.getJSONObject(i);
    Field field = new Field(obj.getString(NAME), obj.getString(TYPE));
    fields.add(field);
    fieldNames.add(field.name);
    if (obj.has(CONSTRAINTS)) {
      JSONObject constraints = obj.getJSONObject(CONSTRAINTS);
      field.constraints = new ObjectMapper().readValue(constraints.toString(), HashMap.class);
    }
  }
}
 
Example 2
Source Project: OSTMap   File: JsonHelper.java    License: Apache License 2.0 6 votes vote down vote up
public static String generateCoordinates(String json) {
    try {
        JSONObject obj = new JSONObject(json);
        Double[] longLat = Extractor.extractLocation(obj);

        if (obj.has(KEY_COORDINATES) && obj.isNull(KEY_COORDINATES)) {
            //coordinates is empty
            JSONArray longLatArr = new JSONArray();
            if (longLat != null && longLat[0] != null && longLat[1] != null) {
                longLatArr.put(longLat[0]);
                longLatArr.put(longLat[1]);
                JSONObject coordinates1 = new JSONObject().put(KEY_COORDINATES, longLatArr);
                obj.put(KEY_COORDINATES, coordinates1);
            }
        }
        if (obj.has(KEY_PLACE)) {
            //ccordinates is set, remove place
            obj.remove(KEY_PLACE);
        }
        return obj.toString();
    } catch (JSONException e) {
        log.error("no correct JSON string:" + json);

        return null;
    }
}
 
Example 3
Source Project: attic-apex-core   File: TypeGraph.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * A utility method that tells whether a class is considered a bean.<br/>
 * For simplicity we exclude classes that have any type-args.
 *
 * @param className name of the class
 * @return true if it is a bean false otherwise.
 */
public boolean isInstantiableBean(String className) throws JSONException
{
  JSONObject classDesc = describeClass(className);
  if (classDesc.has("typeArgs")) {
    //any type with generics is not considered a bean
    return false;
  }
  JSONArray classProps = classDesc.optJSONArray("properties");
  if (classProps == null || classProps.length() == 0) {
    //no properties then cannot be a bean
    return false;
  }
  for (int p = 0; p < classProps.length(); p++) {
    JSONObject propDesc = classProps.getJSONObject(p);
    if (propDesc.optBoolean("canGet", false)) {
      return true;
    }
  }
  return false;
}
 
Example 4
/**
 * This is a helper method which retrieves the schema tags from the {@link JSONObject} if they are present.
 *
 * @param jo The {@link JSONObject} to retrieve schema tags from.
 * @return A list containing the retrieved schema tags. The list is empty if there are no schema tags present.
 */
//TODO To be removed when Malhar Library 3.3 becomes a dependency.
private List<String> getTags(JSONObject jo) throws JSONException
{
  if (jo.has(FIELD_TAGS)) {
    return getStringsFromJSONArray(jo.getJSONArray(FIELD_TAGS));
  } else {
    return Lists.newArrayList();
  }
}
 
Example 5
public static DelegationToken getDelegationTokenFromJson(JSONObject json)
    throws JSONException {
  DelegationToken ret = new DelegationToken();
  if (json.has("token")) {
    ret.setToken(json.getString("token"));
  } else if (json.has("expiration-time")) {
    ret.setNextExpirationTime(json.getLong("expiration-time"));
  }
  return ret;
}
 
Example 6
Source Project: hadoop   File: VerifyJobsUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static void verifyHsJob(JSONObject info, Job job) throws JSONException {
  assertEquals("incorrect number of elements", 25, info.length());

  // everyone access fields
  verifyHsJobGeneric(job, info.getString("id"), info.getString("user"),
      info.getString("name"), info.getString("state"),
      info.getString("queue"), info.getLong("startTime"),
      info.getLong("finishTime"), info.getInt("mapsTotal"),
      info.getInt("mapsCompleted"), info.getInt("reducesTotal"),
      info.getInt("reducesCompleted"));

  String diagnostics = "";
  if (info.has("diagnostics")) {
    diagnostics = info.getString("diagnostics");
  }

  // restricted access fields - if security and acls set
  verifyHsJobGenericSecure(job, info.getBoolean("uberized"), diagnostics,
      info.getLong("avgMapTime"), info.getLong("avgReduceTime"),
      info.getLong("avgShuffleTime"), info.getLong("avgMergeTime"),
      info.getInt("failedReduceAttempts"),
      info.getInt("killedReduceAttempts"),
      info.getInt("successfulReduceAttempts"),
      info.getInt("failedMapAttempts"), info.getInt("killedMapAttempts"),
      info.getInt("successfulMapAttempts"));

  // acls not being checked since
  // we are using mock job instead of CompletedJob
}
 
Example 7
private static void collectComponentTypes(JSONObject componentProperties,
    Set<String> componentTypes) throws JSONException {
  String componentType = componentProperties.getString("$Type");
  componentTypes.add(componentType);

  // Recursive call to collect nested components.
  if (componentProperties.has("$Components")) {
    JSONArray components = componentProperties.getJSONArray("$Components");
    for (int i = 0; i < components.length(); i++) {
      collectComponentTypes(components.getJSONObject(i), componentTypes);
    }
  }
}
 
Example 8
/**
 * Extracts a mapping from component to set of blocks used from the Form's
 * Scheme (.scm) file, which is actually a JSON dictionary contained within
 * a block comment. Any property that is expressed in the properties section
 * (i.e., not the default value) is considered used by the function.
 *
 * @param source Source contents of the Scheme file
 * @return A mapping of component type names to sets of blocks used
 */
public static Map<String, Set<String>> getComponentBlocksFromSchemeFile(String source) {
  Map<String, Set<String>> result = new HashMap<>();
  JSONObject propertiesObject = parseSourceFile(source);
  try {
    Queue<JSONObject> toProcess = new LinkedList<JSONObject>();
    toProcess.add(propertiesObject.getJSONObject("Properties"));
    while ((propertiesObject = toProcess.poll()) != null) {
      String type = propertiesObject.getString("$Type");
      if (!result.containsKey(type)) {
        result.put(type, new HashSet<String>());
      }
      Set<String> typeProps = result.get(type);
      Iterator<String> it = propertiesObject.keys();
      while (it.hasNext()) {
        String key = it.next();
        if (!key.startsWith("$")) {
          typeProps.add(key);
        }
      }
      if (propertiesObject.has("$Components")) {
        JSONArray components = propertiesObject.getJSONArray("$Components");
        for (int i = 0; i < components.length(); i++) {
          toProcess.add(components.getJSONObject(i));
        }
      }
    }
  } catch (JSONException e) {
    throw new IllegalArgumentException("Unable to parse file - invalid $JSON section syntax");
  }
  return result;
}
 
Example 9
Source Project: attic-apex-malhar   File: SchemaUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * This is a helper method which retrieves the schema tags from the {@link JSONObject} if they are present.
 *
 * @param jo The {@link JSONObject} to retrieve schema tags from.
 * @return A list containing the retrieved schema tags. The list is empty if there are no schema tags present.
 */
public static List<String> getTags(JSONObject jo) throws JSONException
{
  if (jo.has(FIELD_TAGS)) {
    return getStringsFromJSONArray(jo.getJSONArray(FIELD_TAGS));
  } else {
    return Collections.emptyList();
  }
}
 
Example 10
Source Project: batfish   File: BfCoordWorkHelper.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public String initNetwork(@Nullable String networkName, @Nullable String networkPrefix) {
  try {
    WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_INIT_NETWORK);

    @SuppressWarnings("PMD.CloseResource") // postData will close it
    MultiPart multiPart = new MultiPart();
    multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);

    addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey());
    if (networkName != null) {
      addTextMultiPart(multiPart, CoordConsts.SVC_KEY_NETWORK_NAME, networkName);
    } else {
      addTextMultiPart(multiPart, CoordConsts.SVC_KEY_NETWORK_PREFIX, networkPrefix);
    }

    JSONObject jObj = postData(webTarget, multiPart);
    if (jObj == null) {
      return null;
    }

    if (!jObj.has(CoordConsts.SVC_KEY_NETWORK_NAME)) {
      _logger.errorf("network name key not found in: %s\n", jObj);
      return null;
    }

    return jObj.getString(CoordConsts.SVC_KEY_NETWORK_NAME);
  } catch (Exception e) {
    _logger.errorf("exception: ");
    _logger.error(Throwables.getStackTraceAsString(e) + "\n");
    return null;
  }
}
 
Example 11
/**
 * 20141022.
 *
 * @param jObj
 *            the j obj
 * @param projectionStr
 *            the projection str
 * @return the FQDN value list cms
 * @throws JSONException
 *             the JSON exception
 */
static List<String> getFQDNValueListCMS(JSONObject jObj,
        String projectionStr) throws JSONException {
    final List<String> labelList = new ArrayList<String>();

    if (!jObj.has("result")) {
        logger.error("!!CMS_ERROR! result key is not in jOBJ in getFQDNValueListCMS!!: \njObj:"
                + PcStringUtils.renderJson(jObj));

        return labelList;
    }
    JSONArray jArr = (JSONArray) jObj.get("result");
    if (jArr == null || jArr.length() == 0) {
        return labelList;
    }
    for (int i = 0; i < jArr.length(); ++i) {
        JSONObject agentObj = jArr.getJSONObject(i);
        // properties can be null

        if (!agentObj.has(projectionStr)) {
            continue;
        }
        String label = (String) agentObj.get(projectionStr);

        if (label != null && !label.trim().isEmpty()) {
            labelList.add(label);
        }
    }

    return labelList;
}
 
Example 12
Source Project: incubator-atlas   File: AttributeInfo.java    License: Apache License 2.0 5 votes vote down vote up
public static AttributeDefinition fromJson(String jsonStr) throws JSONException {
    JSONObject json = new JSONObject(jsonStr);
    String reverseAttr = null;
    if (json.has("reverseAttributeName")) {
        reverseAttr = json.getString("reverseAttributeName");
    }
    return new AttributeDefinition(json.getString("name"), json.getString("dataType"),
            Multiplicity.fromJson(json.getString("multiplicity")), json.getBoolean("isComposite"),
            json.getBoolean("isUnique"), json.getBoolean("isIndexable"), reverseAttr);
}
 
Example 13
Source Project: tez   File: TaskAttemptInfo.java    License: Apache License 2.0 4 votes vote down vote up
TaskAttemptInfo(JSONObject jsonObject) throws JSONException {
  super(jsonObject);

  Preconditions.checkArgument(
      jsonObject.getString(Constants.ENTITY_TYPE).equalsIgnoreCase
          (Constants.TEZ_TASK_ATTEMPT_ID));

  taskAttemptId = StringInterner.weakIntern(jsonObject.optString(Constants.ENTITY));

  //Parse additional Info
  final JSONObject otherInfoNode = jsonObject.getJSONObject(Constants.OTHER_INFO);

  long sTime = otherInfoNode.optLong(Constants.START_TIME);
  long eTime = otherInfoNode.optLong(Constants.FINISH_TIME);
  if (eTime < sTime) {
    LOG.warn("TaskAttemptInfo has got wrong start/end values. "
        + "startTime=" + sTime + ", endTime=" + eTime + ". Will check "
        + "timestamps in DAG started/finished events");

    // Check if events TASK_STARTED, TASK_FINISHED can be made use of
    for(Event event : eventList) {
      switch (HistoryEventType.valueOf(event.getType())) {
      case TASK_ATTEMPT_STARTED:
        sTime = event.getAbsoluteTime();
        break;
      case TASK_ATTEMPT_FINISHED:
        eTime = event.getAbsoluteTime();
        break;
      default:
        break;
      }
    }

    if (eTime < sTime) {
      LOG.warn("TaskAttemptInfo has got wrong start/end values in events as well. "
          + "startTime=" + sTime + ", endTime=" + eTime);
    }
  }
  startTime = sTime;
  endTime = eTime;

  diagnostics = otherInfoNode.optString(Constants.DIAGNOSTICS);
  creationTime = otherInfoNode.optLong(Constants.CREATION_TIME);
  creationCausalTA = StringInterner.weakIntern(
      otherInfoNode.optString(Constants.CREATION_CAUSAL_ATTEMPT));
  allocationTime = otherInfoNode.optLong(Constants.ALLOCATION_TIME);
  containerId = StringInterner.weakIntern(otherInfoNode.optString(Constants.CONTAINER_ID));
  String id = otherInfoNode.optString(Constants.NODE_ID);
  nodeId = StringInterner.weakIntern((id != null) ? (id.split(":")[0]) : "");
  logUrl = otherInfoNode.optString(Constants.COMPLETED_LOGS_URL);

  status = StringInterner.weakIntern(otherInfoNode.optString(Constants.STATUS));
  container = new Container(containerId, nodeId);
  if (otherInfoNode.has(Constants.LAST_DATA_EVENTS)) {
    List<DataDependencyEvent> eventInfo = Utils.parseDataEventDependencyFromJSON(
        otherInfoNode.optJSONObject(Constants.LAST_DATA_EVENTS));
    long lastTime = 0;
    for (DataDependencyEvent item : eventInfo) {
      // check these are in time order
      Preconditions.checkState(lastTime < item.getTimestamp());
      lastTime = item.getTimestamp();
      lastDataEvents.add(item);
    }
  }
  terminationCause = StringInterner
      .weakIntern(otherInfoNode.optString(ATSConstants.TASK_ATTEMPT_ERROR_ENUM));
  executionTimeInterval = (endTime > startTime) ? (endTime - startTime) : 0;
}
 
Example 14
private String getExecutorId(final JSONObject executor) throws JSONException {
    return executor.has("id") ? executor.getString("id") : executor.getString("executor_id");
}
 
Example 15
private void verifySubQueue(JSONObject info, String q, 
    float parentAbsCapacity, float parentAbsMaxCapacity)
    throws JSONException, Exception {
  int numExpectedElements = 13;
  boolean isParentQueue = true;
  if (!info.has("queues")) {
    numExpectedElements = 25;
    isParentQueue = false;
  }
  assertEquals("incorrect number of elements", numExpectedElements, info.length());

  QueueInfo qi = isParentQueue ? new QueueInfo() : new LeafQueueInfo();
  qi.capacity = (float) info.getDouble("capacity");
  qi.usedCapacity = (float) info.getDouble("usedCapacity");
  qi.maxCapacity = (float) info.getDouble("maxCapacity");
  qi.absoluteCapacity = (float) info.getDouble("absoluteCapacity");
  qi.absoluteMaxCapacity = (float) info.getDouble("absoluteMaxCapacity");
  qi.absoluteUsedCapacity = (float) info.getDouble("absoluteUsedCapacity");
  qi.numApplications = info.getInt("numApplications");
  qi.queueName = info.getString("queueName");
  qi.state = info.getString("state");

  verifySubQueueGeneric(q, qi, parentAbsCapacity, parentAbsMaxCapacity);

  if (isParentQueue) {
    JSONArray arr = info.getJSONObject("queues").getJSONArray("queue");
    // test subqueues
    for (int i = 0; i < arr.length(); i++) {
      JSONObject obj = arr.getJSONObject(i);
      String q2 = q + "." + obj.getString("queueName");
      verifySubQueue(obj, q2, qi.absoluteCapacity, qi.absoluteMaxCapacity);
    }
  } else {
    LeafQueueInfo lqi = (LeafQueueInfo) qi;
    lqi.numActiveApplications = info.getInt("numActiveApplications");
    lqi.numPendingApplications = info.getInt("numPendingApplications");
    lqi.numContainers = info.getInt("numContainers");
    lqi.maxApplications = info.getInt("maxApplications");
    lqi.maxApplicationsPerUser = info.getInt("maxApplicationsPerUser");
    lqi.userLimit = info.getInt("userLimit");
    lqi.userLimitFactor = (float) info.getDouble("userLimitFactor");
    verifyLeafQueueGeneric(q, lqi);
    // resourcesUsed and users (per-user resources used) are checked in
    // testPerUserResource()
  }
}
 
Example 16
Source Project: hadoop   File: TestAMWebServicesJobs.java    License: Apache License 2.0 4 votes vote down vote up
public void verifyAMJob(JSONObject info, Job job) throws JSONException {

    assertEquals("incorrect number of elements", 30, info.length());

    // everyone access fields
    verifyAMJobGeneric(job, info.getString("id"), info.getString("user"),
        info.getString("name"), info.getString("state"),
        info.getLong("startTime"), info.getLong("finishTime"),
        info.getLong("elapsedTime"), info.getInt("mapsTotal"),
        info.getInt("mapsCompleted"), info.getInt("reducesTotal"),
        info.getInt("reducesCompleted"),
        (float) info.getDouble("reduceProgress"),
        (float) info.getDouble("mapProgress"));

    String diagnostics = "";
    if (info.has("diagnostics")) {
      diagnostics = info.getString("diagnostics");
    }

    // restricted access fields - if security and acls set
    verifyAMJobGenericSecure(job, info.getInt("mapsPending"),
        info.getInt("mapsRunning"), info.getInt("reducesPending"),
        info.getInt("reducesRunning"), info.getBoolean("uberized"),
        diagnostics, info.getInt("newReduceAttempts"),
        info.getInt("runningReduceAttempts"),
        info.getInt("failedReduceAttempts"),
        info.getInt("killedReduceAttempts"),
        info.getInt("successfulReduceAttempts"), info.getInt("newMapAttempts"),
        info.getInt("runningMapAttempts"), info.getInt("failedMapAttempts"),
        info.getInt("killedMapAttempts"), info.getInt("successfulMapAttempts"));

    Map<JobACL, AccessControlList> allacls = job.getJobACLs();
    if (allacls != null) {

      for (Map.Entry<JobACL, AccessControlList> entry : allacls.entrySet()) {
        String expectName = entry.getKey().getAclName();
        String expectValue = entry.getValue().getAclString();
        Boolean found = false;
        // make sure ws includes it
        if (info.has("acls")) {
          JSONArray arr = info.getJSONArray("acls");

          for (int i = 0; i < arr.length(); i++) {
            JSONObject aclInfo = arr.getJSONObject(i);
            if (expectName.matches(aclInfo.getString("name"))) {
              found = true;
              WebServicesTestUtils.checkStringMatch("value", expectValue,
                  aclInfo.getString("value"));
            }
          }
        } else {
          fail("should have acls in the web service info");
        }
        assertTrue("acl: " + expectName + " not found in webservice output",
            found);
      }
    }

  }
 
Example 17
private Message deserializeHelper(String json, Class<? extends Message> message, Object context) throws Exception
{
  JSONObject schemaJO = new JSONObject(json);

  String type = schemaJO.getString(Query.FIELD_TYPE);

  if (!type.equals(SchemaQuery.TYPE)) {
    LOG.error("The given type {} is invalid.", type);
    return null;
  }

  String id = schemaJO.getString(Query.FIELD_ID);
  Map<String, String> contextKeysMap = null;
  Map<String, String> schemaKeysMap = null;

  if (schemaJO.has(SchemaQuery.FIELD_CONTEXT)) {
    JSONObject contextJO = schemaJO.getJSONObject(SchemaQuery.FIELD_CONTEXT);

    if (contextJO.length() == 0) {
      LOG.error("The context cannot be empty");
      return null;
    }

    if (contextJO.has(SchemaQuery.FIELD_CONTEXT_KEYS)) {
      JSONObject keys = contextJO.getJSONObject(SchemaQuery.FIELD_CONTEXT_KEYS);
      contextKeysMap = SchemaUtils.extractMap(keys);

      if (contextKeysMap.isEmpty()) {
        contextKeysMap = null;
      }
    }

    if (contextJO.has(SchemaQuery.FIELD_SCHEMA_KEYS)) {
      JSONObject schemaKeys = contextJO.getJSONObject(SchemaQuery.FIELD_SCHEMA_KEYS);
      schemaKeysMap = SchemaUtils.extractMap(schemaKeys);

      if (schemaKeysMap.isEmpty()) {
        schemaKeysMap = null;
      }
    }
  }

  SchemaQuery sq = new SchemaQuery(id, schemaKeysMap, contextKeysMap);

  return sq;
}
 
Example 18
/**
 * This is a helper deserializer method.
 * @param json The JSON to deserialize.
 * @param context The context information to use when deserializing the query.
 * @return The deserialized query. If the given json contains some invalid content this
 * method may return null.
 * @throws Exception
 */
private Message deserializeHelper(String json, Object context) throws Exception
{
  JSONObject jo = new JSONObject(json);

  //Validate fields
  if (!SchemaUtils.checkValidKeys(jo, FIRST_LEVEL_FIELD_COMBINATIONS)) {
    throw new IOException("Invalid keys");
  }

  //// Query id stuff
  String id = jo.getString(DataQuerySnapshot.FIELD_ID);
  String type = jo.getString(DataQuerySnapshot.FIELD_TYPE);

  if (!type.equals(DataQuerySnapshot.TYPE)) {
    LOG.error("Found type {} in the query json, but expected type {}.", type, DataQuerySnapshot.TYPE);
    return null;
  }

  /// Countdown
  long countdown = -1L;
  boolean hasCountdown = jo.has(DataQuerySnapshot.FIELD_COUNTDOWN);

  if (hasCountdown) {
    countdown = jo.getLong(DataQuerySnapshot.FIELD_COUNTDOWN);
  }

  ////Data
  Map<String, String> schemaKeys = null;
  Set<String> fieldsSet = Sets.newHashSet();

  if (jo.has(DataQuerySnapshot.FIELD_DATA)) {
    JSONObject data = jo.getJSONObject(DataQuerySnapshot.FIELD_DATA);

    if (!SchemaUtils.checkValidKeys(data, DATA_FIELD_COMBINATIONS)) {
      LOG.error("Error validating {} field", DataQuerySnapshot.FIELD_DATA);
      throw new IOException("Invalid keys");
    }

    if (data.has(DataQuerySnapshot.FIELD_SCHEMA_KEYS)) {
      schemaKeys = SchemaUtils.extractMap(data.getJSONObject(DataQuerySnapshot.FIELD_SCHEMA_KEYS));
    }

    if (data.has(DataQuerySnapshot.FIELD_FIELDS)) {
      //// Fields
      JSONArray jArray = data.getJSONArray(DataQuerySnapshot.FIELD_FIELDS);

      for (int index = 0; index < jArray.length(); index++) {
        String field = jArray.getString(index);

        if (!fieldsSet.add(field)) {
          LOG.error("The field {} was listed more than once, this is an invalid query.", field);
        }
      }
    }
  }

  Fields fields = new Fields(fieldsSet);

  if (!hasCountdown) {
    return new DataQuerySnapshot(id, fields, schemaKeys);
  } else {
    return new DataQuerySnapshot(id, fields, countdown, schemaKeys);
  }
}
 
Example 19
Source Project: vespa   File: JsonReader.java    License: Apache License 2.0 4 votes vote down vote up
public SetRequestData getStateRequestData(HttpRequest request) throws Exception {
    JSONObject json = new JSONObject(request.getPostContent().toString());

    final boolean probe = json.has("probe") && json.getBoolean("probe");

    final SetUnitStateRequest.Condition condition;
    if (json.has("condition")) {
        condition = SetUnitStateRequest.Condition.fromString(json.getString("condition"));
    } else {
        condition = SetUnitStateRequest.Condition.FORCE;
    }

    final SetUnitStateRequest.ResponseWait responseWait = json.has("response-wait")
            ? SetUnitStateRequest.ResponseWait.fromString(json.getString("response-wait"))
            : SetUnitStateRequest.ResponseWait.WAIT_UNTIL_CLUSTER_ACKED;

    Map<String, UnitState> stateMap = new HashMap<>();
    if (!json.has("state")) {
        throw new InvalidContentException("Set state requests must contain a state object");
    }
    Object o = json.get("state");
    if (!(o instanceof JSONObject)) {
        throw new InvalidContentException("value of state is not a json object");
    }

    JSONObject state = (JSONObject) o;

    JSONArray stateTypes = state.names();
    for (int i=0; i<stateTypes.length(); ++i) {
        o = stateTypes.get(i);
        String type = (String) o;
        o = state.get(type);
        if (!(o instanceof JSONObject)) {
            throw new InvalidContentException("value of state->" + type + " is not a json object");
        }
        JSONObject userState = (JSONObject) o;
        String code = "up";
        if (userState.has("state")) {
            o = userState.get("state");
            if (!(o instanceof String)) {
                throw new InvalidContentException("value of state->" + type + "->state is not a string");
            }
            code = o.toString();
        }
        String reason = "";
        if (userState.has("reason")) {
            o = userState.get("reason");
            if (!(o instanceof String)) {
                throw new InvalidContentException("value of state->" + type + "->reason is not a string");
            }
            reason = o.toString();
        }
        stateMap.put(type, new UnitStateImpl(code, reason));
    }

    return new SetRequestData(probe, stateMap, condition, responseWait);
}
 
Example 20
private void initDatatypes(List<String> geoSpatialDatatypeConfigs) {
   
    datatypeConfigs = new ArrayList<GeoSpatialDatatypeConfiguration>();
   
    if (geoSpatialDatatypeConfigs==null)
        return; // nothing to be done

    /**
     * We expect a JSON config string of the following format (example):
     * 
     * {"config": { 
     *   "uri": "http://my.custom.datatype2.uri", 
     *   "literalSerializer": "com.bigdata.service.GeoSpatialLiteralSerializer",
     *   "fields": [ 
     *     { "valueType": "DOUBLE", "multiplier": "100000", "serviceMapping": "LATITUDE" }, 
     *     { "valueType": "DOUBLE", "multiplier": "100000", "serviceMapping": "LONGITUDE" }, 
     *     { "valueType": "LONG, "multiplier": "1", "minValue" : "0" , "serviceMapping": "TIME"  }, 
     *     { "valueType": "LONG", "multiplier": "1", "minValue" : "0" , "serviceMapping" : "COORD_SYSTEM"  } 
     *   ] 
     * }}
     */
    for (final String configStr : geoSpatialDatatypeConfigs) {
       
        if (configStr==null || configStr.isEmpty())
            continue; // skip

        try {

            // read values from JSON
            final JSONObject json = new JSONObject(configStr);
            final JSONObject topLevelNode = (JSONObject)json.get(JSON_STR_CONFIG);
            final String uri = (String)topLevelNode.get(JSON_STR_URI);
            final String literalSerializer = topLevelNode.has(JSON_STR_LITERALSERIALIZER) ?
                    (String)topLevelNode.get(JSON_STR_LITERALSERIALIZER) : null;
            final JSONArray fields = (JSONArray)topLevelNode.get(JSON_STR_FIELDS);

            // delegate to GeoSpatialDatatypeConfiguration for construction
            datatypeConfigs.add(new GeoSpatialDatatypeConfiguration(uri, literalSerializer, fields));
            
        } catch (JSONException e) {
            
            log.warn("Illegal JSON configuration: " + e.getMessage());
            throw new IllegalArgumentException(e); // forward exception
        }
       
        // validate that there are no duplicate URIs used for the datatypeConfigs
        final Set<URI> uris = new HashSet<URI>();
        for (int i=0; i<datatypeConfigs.size(); i++) {
            
            final URI curUri = datatypeConfigs.get(i).getUri();
            
            if (uris.contains(curUri)) {
                throw new IllegalArgumentException("Duplicate URI used for geospatial datatype config: " + curUri);
            }
            
            uris.add(curUri);
        }

    }
}