Пример #1
0
  public void load(RecordStruct info) {
    this.info = info;

    this.obfuscator =
        DomainInfo.prepDomainObfuscator(
            info.getFieldAsString("ObscureClass"), info.getFieldAsString("ObscureSeed"));

    this.reloadSettings();
  }
Пример #2
0
  @Override
  public void start() {
    super.start();

    OperationContext.useNewRoot();

    FuncResult<RecordStruct> ldres = Updater.loadDeployed();

    if (ldres.hasErrors()) {
      OperationContext.get().error("Error reading deployed.json file: " + ldres.getMessage());
      return;
    }

    RecordStruct deployed = ldres.getResult();

    this.version = deployed.getFieldAsString("Version");
    this.app = deployed.getFieldAsString("PackagePrefix");
  }
Пример #3
0
  @Override
  public void execute(DatabaseInterface conn, DatabaseTask task, OperationResult log) {
    if (task.isReplicating()) return;

    RecordStruct params = task.getParamsAsRecord();
    RecordStruct fields = params.getFieldAsRecord("Fields");

    RecordStruct uname = fields.getFieldAsRecord("dcUsername");

    if (uname == null) {
      log.error("Username required to insert a user.");
      return;
    }

    TablesAdapter db = new TablesAdapter(conn, task);

    try {
      for (FieldStruct fs : uname.getFields()) {
        RecordStruct rec = (RecordStruct) fs.getValue();

        if (rec.isFieldEmpty("Data")) {
          log.error("Username required to insert a user.");
          return;
        }

        Object userid =
            db.firstInIndex(
                "dcUser",
                "dcUsername",
                rec.getFieldAsString("Data"),
                BigDateTime.nowDateTime(),
                false);

        if (userid != null) {
          log.error("Username must be unique, this username (email) already in use.");
          return;
        }
      }
    } catch (Exception x) {
      log.error("Insert User: Failed to read Index: " + x);
    }
  }
Пример #4
0
  @Override
  public void execute(DatabaseInterface conn, DatabaseTask task, OperationResult log) {
    RecordStruct params = task.getParamsAsRecord();

    // System.out.println("NT 1 Got: " + params.toPrettyString());

    // TODO replicating
    // if (task.isReplicating())

    String title = params.getFieldAsString("Title");

    if (StringUtil.isNotEmpty(title)) title = title.trim();

    /* this is a fine concept, but maybe not the right place for it - TODO
    String hash = null;

    if (StringUtil.isNotEmpty(title)) {
    	title = title.trim();

    	hash = HashUtil.getSha256(title);

    	Object tid = db.firstInIndex("dcmThread", "dcmHash", hash, when, false);

    	if (tid == null) {
    		String titleb = title.toLowerCase();

    		if (titleb.startsWith("re:")) {
    			titleb = titleb.substring(3).trim();
    			hash = HashUtil.getSha256(title);

    			tid = db.firstInIndex("dcmThread", "dcmHash", hash, when, false);
    		}
    	}

    	String threadid = tid.toString();
    }
    */

    boolean trackTitle = params.getFieldAsBooleanOrFalse("TrackTitle");

    // TODO figure out how to send to future date (target date vs modified)

    String uuid = Session.nextUUId();
    String hash = HashUtil.getSha256((trackTitle && StringUtil.isNotEmpty(title)) ? title : uuid);
    DateTime now = new DateTime();
    DateTime target = params.getFieldAsDateTime("TargetDate");

    if (target == null) target = now;

    String originator =
        !params.isFieldEmpty("Originator")
            ? params.getFieldAsString("Originator")
            : log.getContext().getUserContext().getUserId();

    DbRecordRequest req =
        new InsertRecordRequest()
            .withTable("dcmThread")
            .withUpdateField("dcmUuid", uuid)
            .withUpdateField("dcmHash", hash)
            .withUpdateField("dcmCreated", now)
            .withUpdateField("dcmModified", now)
            .withUpdateField("dcmOriginator", originator)
            .withConditionallyUpdateFields(params, "Title", "dcmTitle", "EndDate", "dcmEndDate");

    if (target != null) req.withUpdateField("dcmTargetDate", target);

    ListStruct lbs = params.getFieldAsList("Labels");

    if ((lbs != null) && !lbs.isEmpty())
      req.withUpdateField("dcmLabels", "|" + StringUtil.join(lbs.toStringList(), "|") + "|");

    ListStruct parties = params.getFieldAsList("Parties");

    if (!parties.isEmpty()) {
      for (int i = 0; i < parties.getSize(); i++) {
        RecordStruct party = parties.getItemAsRecord(i);

        String pident = party.getFieldAsString("Party");

        req.withUpdateField("dcmParty", pident, pident)
            .withUpdateField("dcmFolder", pident, party.getFieldAsString("Folder"));

        lbs = party.getFieldAsList("PartyLabels");

        if ((lbs != null) && !lbs.isEmpty())
          req.withUpdateField(
              "dcmPartyLabels", pident, "|" + StringUtil.join(lbs.toStringList(), "|") + "|");
      }
    }

    RecordStruct cnt = params.getFieldAsRecord("Content");
    String content = cnt.getFieldAsString("Content");
    String stamp = TimeUtil.stampFmt.print(new DateTime());

    req.withUpdateField("dcmContent", stamp, content)
        .withUpdateField("dcmContentHash", stamp, HashUtil.getSha256(content))
        .withUpdateField("dcmContentType", stamp, cnt.getFieldAsString("ContentType"));

    if (!cnt.isFieldEmpty("Source"))
      req.withUpdateField("dcmSource", stamp, cnt.getFieldAsString("Source"));

    if (!cnt.isFieldEmpty("Attributes"))
      req.withUpdateField("dcmAttributes", stamp, cnt.getFieldAsRecord("Attributes"));

    task.getDbm().submit(req, task.getResult());
  }
Пример #5
0
  @Override
  public void handle(TaskRun request) {
    Message msg = (Message) request.getTask().getParams();

    String feature = msg.getFieldAsString("Feature");
    String op = msg.getFieldAsString("Op");

    if ("Echo".equals(feature)) {
      if ("Test".equals(op)) {

        /*
        try {
        	Thread.sleep(5000);
        }
        catch (InterruptedException x) {
        }
        */

        request.debug("Echo got: " + msg.getField("Body"));

        request.setResult(msg.getField("Body"));
        request.complete();
        return;
      }
    } else if ("Tickle".equals(feature)) {
      if ("Test".equals(op)) {
        System.out.println("got message: " + msg);
        request.complete();
        return;
      }
    } else if ("Translate".equals(feature)) {
      /* TODO
      RecordStruct rec = msg.getFieldAsRecord("Body");

      String original = rec.getFieldAsString("Content");
      String from = rec.getFieldAsString("From");
      String to = rec.getFieldAsString("To");

      if ("Text".equals(op)) {
      	// TODO support From and To - for now it is en to x-pig-latin
      	return MessageUtil.successAlt(TranslateLang.translateText(original, from, to));
      }
      else if ("Xml".equals(op)) {
      	// TODO support From and To - for now it is en to x-pig-latin
      	return MessageUtil.successAlt(TranslateLang.translateXml(original, from, to));
      }
      */
    } else if ("Info".equals(feature)) {
      if ("Test".equals(op)) {
        OperationContext tc = OperationContext.get();

        tc.verify(
            new FuncCallback<UserContext>() {
              @Override
              public void callback() {
                UserContext uc = this.getResult();

                if (uc != null) {
                  OperationContext.use(uc, OperationContext.get().toBuilder());

                  if (!uc.isVerified()) request.error(1, "Invalid auth token.");
                  else
                    request.setResult(new RecordStruct(new FieldStruct("UserId", uc.getUserId())));
                } else request.error(1, "User context.");

                request.complete();
              }
            });

        return;
      } else if ("HubStatus".equals(op)) {
        RecordStruct rec = new RecordStruct();

        Logger.info("Status check");

        rec.setField("ServerIdled", Hub.instance.isIdled());

        rec.setField("WorkPool", Hub.instance.getWorkPool().toStatusReport());

        ListStruct sessions = new ListStruct();

        for (Session sess : Hub.instance.getSessions().list())
          sessions.addItem(sess.toStatusReport());

        rec.setField("Sessions", sessions);

        rec.setField("WorkQueue", Hub.instance.getWorkQueue().list());

        request.setResult(rec);
        request.complete();
        return;
      } else if ("TaskStatus".equals(op)) {
        ListStruct requests = msg.getFieldAsList("Body");
        ListStruct results = new ListStruct();

        for (Struct struct : requests.getItems()) {
          RecordStruct req = (RecordStruct) struct;

          // try to get the info locally
          RecordStruct trec =
              Hub.instance
                  .getWorkPool()
                  .status(req.getFieldAsString("TaskId"), req.getFieldAsString("WorkId"));

          if ((trec != null) && trec.isEmpty()) System.out.println("empty from pool");

          // else look for it in database
          if (trec == null)
            trec =
                Hub.instance
                    .getWorkQueue()
                    .status(req.getFieldAsString("TaskId"), req.getFieldAsString("WorkId"));

          if ((trec != null) && trec.isEmpty()) System.out.println("empty from queue");

          if (trec != null) results.addItem(trec);
        }

        request.setResult(results);
        request.complete();
        return;
      } else if ("Version".equals(op)) {
        request.setResult(
            new RecordStruct(
                new FieldStruct("Version", this.version), new FieldStruct("App", this.app)));
        request.complete();
        return;
      }
    } else if ("Management".equals(feature)) {
      if ("Idle".equals(op)) {
        RecordStruct rec = msg.getFieldAsRecord("Body");
        boolean idle = rec.getFieldAsBoolean("Idle");
        Hub.instance.setIdled(idle);

        request.complete();
        return;
      }
    }

    request.error("Feature or operation not supported");
    request.complete();
  }
Пример #6
0
  @Override
  public void execute(DatabaseInterface conn, DatabaseTask task, OperationResult log) {
    if (task.isReplicating()) {
      // TODO what should happen during a replicate?
      task.complete();
      return;
    }

    RecordStruct params = task.getParamsAsRecord();
    TablesAdapter db = new TablesAdapter(conn, task);
    BigDateTime when = BigDateTime.nowDateTime();

    String password = params.getFieldAsString("Password");
    String uname = params.getFieldAsString("Username");

    // TODO part of Trust monitoring -- boolean suspect =
    // if (AddUserRequest.meetsPasswordPolicy(password, true).hasLogLevel(DebugLevel.Warn))
    //	params.withField("Suspect", true);

    String uid = null;

    Object userid = db.firstInIndex("dcUser", "dcUsername", uname, when, false);

    if (userid != null) uid = userid.toString();

    // fail right away if not a valid user
    if (StringUtil.isEmpty(uid)) {
      log.errorTr(123);
      task.complete();
      return;
    }

    String ckey = params.getFieldAsString("ClientKeyPrint");

    // find out if this is a master key
    if (StringUtil.isNotEmpty(ckey)) {
      System.out.println("sign in client key: " + ckey);

      task.pushDomain(Constants.DB_GLOBAL_ROOT_DOMAIN);

      Object mk =
          db.getStaticList("dcDomain", Constants.DB_GLOBAL_ROOT_DOMAIN, "dcMasterKeys", ckey);

      Object mpp =
          (mk == null)
              ? null
              : db.getStaticScalar(
                  "dcDomain", Constants.DB_GLOBAL_ROOT_DOMAIN, "dcMasterPasswordPattern");

      task.popDomain();

      // if master key is present for the client key then check the password pattern
      if (mk != null) {
        boolean passcheck = false;

        if (StringUtil.isEmpty((String) mpp)) {
          passcheck = true;
        } else {
          Pattern pp = Pattern.compile((String) mpp);
          Matcher pm = pp.matcher(password);
          passcheck = pm.matches();
        }

        if (passcheck) {
          this.signIn(conn, task, db, log, when, uid);
          return;
        }
      }
    }

    if (StringUtil.isNotEmpty(password)) {
      password = password.trim();

      Object fndpass = db.getDynamicScalar("dcUser", uid, "dcPassword", when);

      System.out.println("local password: "******"try local password: "******"root")) {
        task.pushDomain(Constants.DB_GLOBAL_ROOT_DOMAIN);

        Object gp =
            db.getStaticScalar("dcDomain", Constants.DB_GLOBAL_ROOT_DOMAIN, "dcGlobalPassword");

        task.popDomain();

        System.out.println("global password: "******"try global password: "******"dcUser", uid, "dcConfirmCode");

      if (password.equals(fndpass)) {
        Object ra = db.getStaticScalar("dcUser", uid, "dcRecoverAt");

        if (ra == null) {
          // if code matches then good login
          this.signIn(conn, task, db, log, when, uid);
          return;
        }

        if (ra != null) {
          DateTime radt = Struct.objectToDateTime(ra);
          DateTime pastra = new DateTime().minusHours(2);

          if (!pastra.isAfter(radt)) {
            // if code matches and has not expired then good login
            this.signIn(conn, task, db, log, when, uid);
            return;
          }
        }
      }
    }

    log.errorTr(123);
    task.complete();
  }