Ejemplo n.º 1
0
  public DbRecordRequest withConditionallyUpdateFields(RecordStruct source, String... pairs) {
    for (int i = 0; i < (pairs.length - 1); i += 2) {
      if (source.hasField(pairs[i])) this.withUpdateField(pairs[i + 1], source.getField(pairs[i]));
    }

    return this;
  }
Ejemplo n.º 2
0
  public static Task createDownloadTask(
      ApiSession sess,
      String servicename,
      Path local,
      CommonPath remote,
      Struct storeParams,
      boolean allowResume) {
    DownloadFile work = new DownloadFile();
    work.session = sess;

    RecordStruct params =
        new RecordStruct(
            new FieldStruct("LocalPath", local),
            new FieldStruct("RemotePath", remote),
            new FieldStruct("ServiceName", servicename),
            new FieldStruct("TransferParams", storeParams));

    if (allowResume && Files.exists(local)) {
      try {
        params.setField("Offset", Files.size(local));
      } catch (IOException x) {
        Logger.error("Unable to get file size for: " + local);
        return null;
      }
    }

    return new Task()
        .withTitle("Download file " + local)
        .withWork(work)
        .withSubContext()
        .withParams(params)
        .withTimeout(1)
        .withDeadline(0);
  }
Ejemplo n.º 3
0
  public void load(RecordStruct info) {
    this.info = info;

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

    this.reloadSettings();
  }
Ejemplo n.º 4
0
  public DbRecordRequest withConditionallySetList(RecordStruct source, String sname, String dname) {
    if (!source.hasField(sname)) return this;

    this.sets.addItem(
        new RecordStruct()
            .withField("Field", dname)
            .withField("Values", source.getFieldAsList(sname)));

    return this;
  }
Ejemplo n.º 5
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");
  }
Ejemplo n.º 6
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);
    }
  }
Ejemplo n.º 7
0
  @Override
  public CompositeStruct buildParams() {
    RecordStruct flds = new RecordStruct();

    for (FieldRequest fld : this.fields) flds.setField(fld.getName(), fld.getParams(flds));

    RecordStruct params =
        new RecordStruct(new FieldStruct("Table", this.table), new FieldStruct("Fields", flds));

    if (StringUtil.isNotEmpty(this.id)) params.setField("Id", this.id);

    params.setField("When", this.when);

    if (this.sets.getSize() > 0) params.setField("Sets", this.sets);

    this.parameters = params;

    return this.parameters;
  }
Ejemplo n.º 8
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());
  }
Ejemplo n.º 9
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();
  }
Ejemplo n.º 10
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();
  }
Ejemplo n.º 11
0
  public void signIn(
      DatabaseInterface conn,
      DatabaseTask task,
      TablesAdapter db,
      OperationResult log,
      BigDateTime when,
      String uid) {
    ICompositeBuilder out = task.getBuilder();
    RecordStruct params = task.getParamsAsRecord();
    String did = task.getDomain();

    String token = null;

    try {
      if (StringUtil.isEmpty(uid)) {
        log.errorTr(123);
        task.complete();
        return;
      }

      if (!db.isCurrent("dcUser", uid, when, false)) {
        log.errorTr(123);
        task.complete();
        return;
      }

      if (!task.isReplicating()) {
        // TODO a confirmed login requires at least user name and a confirmation code, it might also
        // take a password
        // but the code must be present to become a confirmed user
        // i '$$get1^dcDb("dcUser",uid,"dcConfirmed") d
        // . i (code'="")&($$get1^dcDb("dcUser",uid,"dcConfirmCode")=code) s
        // Params("Confirmed")=1,confirmed=1 q
        // . d err^dcConn(124) q

        token = Session.nextSessionId();
      }

      if (log.hasErrors()) {
        task.complete();
        return;
      }

      // replication will need these later
      if (!task.isReplicating()) {
        params.setField("Token", token);
        params.setField("Uid", uid);
      }

      // both isReplicating and normal store the token

      conn.set("dcSession", token, "LastAccess", task.getStamp());
      conn.set("dcSession", token, "User", uid);
      conn.set("dcSession", token, "Domain", did);

      // if (confirmed)
      //	db.setStaticScalar("dcUser", uid, "dcConfirmed", confirmed);

      // TODO create some way to track last login that doesn't take up db space
      // or make last login an audit thing...track all logins in StaticList?

      // done with replication stuff
      if (task.isReplicating()) {
        task.complete();
        return;
      }

      // load info about the user
      ListStruct select =
          new ListStruct(
              new RecordStruct(new FieldStruct("Field", "Id"), new FieldStruct("Name", "UserId")),
              new RecordStruct(
                  new FieldStruct("Field", "dcUsername"), new FieldStruct("Name", "Username")),
              new RecordStruct(
                  new FieldStruct("Field", "dcFirstName"), new FieldStruct("Name", "FirstName")),
              new RecordStruct(
                  new FieldStruct("Field", "dcLastName"), new FieldStruct("Name", "LastName")),
              new RecordStruct(
                  new FieldStruct("Field", "dcEmail"), new FieldStruct("Name", "Email")),
              new RecordStruct(
                  new FieldStruct("Field", "dcLocale"), new FieldStruct("Name", "Locale")),
              new RecordStruct(
                  new FieldStruct("Field", "dcChronology"), new FieldStruct("Name", "Chronology")),
              // TODO we actually need group tags too - extend how this works
              new RecordStruct(
                  new FieldStruct("Field", "dcAuthorizationTag"),
                  new FieldStruct("Name", "AuthorizationTags")),
              new RecordStruct(
                  new FieldStruct("Value", token), new FieldStruct("Name", "AuthToken")));

      // out.startRecord();
      // out.field("UserInfo");

      this.writeRecord(conn, task, log, out, db, "dcUser", uid, when, select, true, false, false);

      // out.field("AdditionalTags", null);
      // out.endRecord();
    } catch (Exception x) {
      log.error("SignIn: Unable to create resp: " + x);
    }

    task.complete();
  }