Ejemplo n.º 1
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.º 2
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.º 3
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();
  }