Beispiel #1
0
  public static JsonObject invoke(JsonObject json, String area) {
    JsonObject responseData = json.getObject("ResponseData");

    if (responseData == null) {
      return new JsonObject();
    }

    if (Server.l != null) {
      Server.l.info(getLatestUpdate(json) + " - " + getExecutionTime(json));
    }

    Predicate<String> isJsonArray = name -> responseData.getField(name) instanceof JsonArray;

    Function<String, List<JsonObject>> jsonArrayToList =
        name -> {
          Stream<Object> stream =
              stream(responseData.<JsonArray>getField(name).spliterator(), false);
          Stream<JsonObject> objectStream = stream.filter(getFilterFor(area));
          return objectStream.collect(toList());
        };

    Predicate<Map.Entry<String, List<JsonObject>>> nonEmpty = entry -> !entry.getValue().isEmpty();

    Stream<Map.Entry<String, List<JsonObject>>> entryStream =
        responseData
            .getFieldNames()
            .stream()
            .filter(isJsonArray)
            .collect(toMap(String::toLowerCase, jsonArrayToList))
            .entrySet()
            .stream()
            .filter(nonEmpty);

    JsonObject result = toJsonObject(entryStream);

    setUniqueKeysOn(result.getArray("trains"));
    writeHeaderFields(result);

    return result;
  }
  /*
  Empty inboundPermitted means reject everything - this is the default.
  If at least one match is supplied and all the fields of any match match then the message inboundPermitted,
  this means that specifying one match with a JSON empty object means everything is accepted
   */
  private Match checkMatches(boolean inbound, String address, Object body) {

    if (inbound && acceptedReplyAddresses.remove(address)) {
      // This is an inbound reply, so we accept it
      return new Match(true, false);
    }

    List<JsonObject> matches = inbound ? inboundPermitted : outboundPermitted;

    for (JsonObject matchHolder : matches) {
      String matchAddress = matchHolder.getString("address");
      String matchRegex;
      if (matchAddress == null) {
        matchRegex = matchHolder.getString("address_re");
      } else {
        matchRegex = null;
      }

      boolean addressOK;
      if (matchAddress == null) {
        if (matchRegex == null) {
          addressOK = true;
        } else {
          addressOK = regexMatches(matchRegex, address);
        }
      } else {
        addressOK = matchAddress.equals(address);
      }

      if (addressOK) {
        boolean matched = true;
        // Can send message other than JSON too - in which case we can't do deep matching on
        // structure of message
        if (body instanceof JsonObject) {
          JsonObject match = matchHolder.getObject("match");
          if (match != null) {
            for (String fieldName : match.getFieldNames()) {
              if (!match.getField(fieldName).equals(((JsonObject) body).getField(fieldName))) {
                matched = false;
                break;
              }
            }
          }
        }
        if (matched) {
          Boolean b = matchHolder.getBoolean("requires_auth");
          return new Match(true, b != null && b);
        }
      }
    }
    return new Match(false, false);
  }
 @Override
 public void handleUser(Message<JsonObject> event) {
   Integer id = event.body.getInteger("id");
   JsonObject params = event.body.getObject("op");
   JsonObject state = event.body.getObject("state");
   JsonObject meta = state.getObject("meta");
   JsonObject metaColumn = state.getObject("metaColumn");
   JsonObject metaRanged = state.getObject("metaRanged");
   if (meta == null) {
     meta = new JsonObject();
   }
   if (metaColumn == null) {
     metaColumn = new JsonObject();
   }
   if (metaRanged == null) {
     metaRanged = new JsonObject();
   }
   if (params.getString("type") != null) {
     StringBuilder key = new StringBuilder();
     key.append(HandlerUtils.instance.determineKs(params, state, null));
     key.append(' ');
     key.append(HandlerUtils.instance.determineCf(params, state, null));
     key.append(' ');
     key.append(params.getString("type"));
     meta.putObject(
         key.toString(), new JsonObject().putString("clazz", params.getString("clazz")));
     state.putObject("meta", meta);
     event.reply(new JsonObject().putString(id.toString(), "OK").putObject("state", state));
   } else if (params.getField("name") != null) {
     Object o = params.getField("name");
     ByteBuffer bb = HandlerUtils.instance.byteBufferForObject(o);
     StringBuilder key = new StringBuilder();
     key.append(HandlerUtils.instance.determineKs(params, state, null));
     key.append(' ');
     key.append(HandlerUtils.instance.determineCf(params, state, null));
     key.append(' ');
     key.append(ByteBufferUtil.bytesToHex(bb));
     metaColumn.putObject(
         key.toString(), new JsonObject().putString("clazz", params.getString("clazz")));
     state.putObject("metaColumn", metaColumn);
     event.reply(new JsonObject().putString(id.toString(), "OK").putObject("state", state));
   } else if (params.getField(Operations.RANGE_START) != null) {
     Object start = params.getField(Operations.RANGE_START);
     Object end = params.getField(Operations.RANGE_END);
     ByteBuffer sbuf = HandlerUtils.instance.byteBufferForObject(start);
     ByteBuffer ebuf = HandlerUtils.instance.byteBufferForObject(end);
     StringBuilder key = new StringBuilder();
     key.append(HandlerUtils.instance.determineKs(params, state, null));
     key.append(' ');
     key.append(HandlerUtils.instance.determineCf(params, state, null));
     key.append(' ');
     key.append(ByteBufferUtil.bytesToHex(sbuf));
     JsonObject value = new JsonObject();
     value.putString("clazz", params.getString("clazz"));
     value.putString("end", ByteBufferUtil.bytesToHex(ebuf));
     metaRanged.putObject(key.toString(), value);
     state.putObject("metaRanged", metaRanged);
     event.reply(new JsonObject().putString(id.toString(), "OK").putObject("state", state));
   } else {
     throw new RuntimeException("hit bottom this is bad ok");
   }
 }