public IOutputAdapter pathToAdapter(boolean isPreview, CommonPath path, CacheFile cfile) { IOutputAdapter ioa = null; String filename = cfile.getPath(); HtmlMode hmode = this.domain.getHtmlMode(); if (filename.endsWith(".dcui.xml") || filename.endsWith(".dcuis.xml")) { DcuiOutputAdapter voa = new DcuiOutputAdapter(); (isPreview ? this.dynpreviewcache : this.dyncache).put(path.toString(), voa); ioa = voa; } else if (filename.endsWith(".shtml") || ((hmode == HtmlMode.Ssi) && filename.endsWith(".html"))) { ioa = new SsiOutputAdapter(); } else if (filename.endsWith(".gas")) { ioa = new GasOutputAdapter(); } // certain resource types cannot be delivered else if (filename.endsWith(".class")) { OperationContext.get().errorTr(150001); return null; } else { ioa = new StaticOutputAdapter(); } ioa.init(this, cfile, path, isPreview); return ioa; }
@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"); }
public DbRecordRequest withRetireField(String name, String subkey) { divconq.schema.DbField fld = OperationContext.get().getSchema().getDbField(this.table, name); if (fld == null) return this; FieldRequest dfld = new FieldRequest().withName(name).withRetired().withSubKey(subkey); this.withFields(dfld); return this; }
@Override public void run(final StackEntry stack) { String output = this.source.hasText() ? stack.resolveValue(this.source.getText()).toString() : null; long steps = stack.intFromSource("Steps", -1); long step = stack.intFromSource("Step", -1); long amount = stack.intFromSource("Amount", -1); long add = stack.intFromSource("Add", -1); String name = stack.stringFromSource("Name"); if (amount >= 0) OperationContext.get().setAmountCompleted((int) amount); else if (add >= 0) OperationContext.get() .setAmountCompleted(OperationContext.get().getAmountCompleted() + (int) add); if ((step >= 0) && StringUtil.isNotEmpty(name)) OperationContext.get().setCurrentStep((int) step, name); if (steps >= 0) OperationContext.get().setSteps((int) steps); if (StringUtil.isNotEmpty(output)) OperationContext.get().setProgressMessage(output); stack.setState(ExecuteState.Done); stack.resume(); }
public CacheFile findFilePath(CommonPath path, boolean isPreview) { // figure out which section we are looking in String sect = "www"; if ("files".equals(path.getName(0)) || "galleries".equals(path.getName(0))) { sect = path.getName(0); path = path.subpath(1); } if (Logger.isDebug()) Logger.debug("find file path: " + path + " in " + sect); // ===================================================== // if request has an extension do specific file lookup // ===================================================== // if we have an extension then we don't have to do the search below // never go up a level past a file (or folder) with an extension if (path.hasFileExtension()) return this.findSectionFile(sect, path.toString(), isPreview); // ===================================================== // if request does not have an extension look for files // that might match this path or one of its parents // using the special extensions // ===================================================== if (Logger.isDebug()) Logger.debug("find file path dyn: " + path + " in " + sect); // we get here if we have no extension - thus we need to look for path match with specials int pdepth = path.getNameCount(); // check file system while (pdepth > 0) { CommonPath ppath = path.subpath(0, pdepth); for (String ext : this.domain.getSpecialExtensions()) { CacheFile cfile = this.findSectionFile(sect, ppath.toString() + ext, isPreview); if (cfile != null) return cfile; } pdepth--; } OperationContext.get().errorTr(150007); return null; }
public DbRecordRequest withSetField(String name, String subkey, Object value, BigDateTime from) { if (value instanceof ConditionalValue) { if (!((ConditionalValue) value).set) return this; value = ((ConditionalValue) value).value; } divconq.schema.DbField fld = OperationContext.get().getSchema().getDbField(this.table, name); if ((fld == null) || !fld.isDynamic()) return this; FieldRequest dfld = new FieldRequest().withName(name).withValue(value).withSubKey(subkey).withFrom(from); this.withFields(dfld); return this; }
public DbRecordRequest withUpdateField(String name, Object value) { if (value instanceof ConditionalValue) { if (!((ConditionalValue) value).set) return this; value = ((ConditionalValue) value).value; } divconq.schema.DbField fld = OperationContext.get().getSchema().getDbField(this.table, name); if (fld == null) return this; FieldRequest dfld = new FieldRequest().withName(name).withValue(value).withUpdateOnly(); this.withFields(dfld); if (fld.isDynamic()) dfld.withRandomSubKey().withFrom(this.when); else if (fld.isList()) dfld.withRandomSubKey(); return this; }
public static ISettingsObfuscator prepDomainObfuscator(String obclass, String seed) { ISettingsObfuscator obfuscator = null; if (StringUtil.isEmpty(obclass)) obclass = "divconq.util.BasicSettingsObfuscator"; try { obfuscator = (ISettingsObfuscator) Hub.instance.getInstance(obclass); } catch (Exception x) { OperationContext.get().error("Bad Settings Obfuscator"); return null; } XElement clock1 = Hub.instance.getConfig().find("Clock"); String obid = (clock1 != null) ? clock1.getAttribute("Id") : null; obfuscator.init( new XElement("Clock", new XAttribute("Id", obid), new XAttribute("Feed", seed))); return obfuscator; }
public void prepDomainSchedule() { // cancel and remove any previous schedules if (this.schedulenodes.size() > 0) { OperationContext.get().info("Cancelling schedules for " + this.getAlias()); for (ISchedule sch : this.schedulenodes) { OperationContext.get().info("- schedule: " + sch.task().getTitle()); sch.cancel(); } } this.schedulenodes.clear(); if (this.overrideSettings == null) return; // now load new schedules OperationContext.get().info("Prepping schedules for " + this.getAlias()); for (XElement schedule : this.overrideSettings.selectAll("Schedules/*")) { OperationContext.get().info("- find schedule: " + schedule.getAttribute("Title")); ISchedule sched = "CommonSchedule".equals(schedule.getName()) ? new CommonSchedule() : new SimpleSchedule(); sched.init(schedule); sched.setTask( new Task() .withId(Task.nextTaskId("DomainSchedule")) .withTitle("Domain Scheduled Task: " + schedule.getAttribute("Title")) .withContext( new OperationContextBuilder() .withRootTaskTemplate() .withDomainId(this.getId()) .toOperationContext()) .withWork( trun -> { OperationContext.get() .info( "Executing schedule: " + trun.getTask().getTitle() + " for domain " + trun.getTask().getContext().getDomain().getAlias()); if (schedule.hasAttribute("MethodName") && (this.watcher != null)) this.watcher.tryExecuteMethod( schedule.getAttribute("MethodName"), new Object[] {trun}); })); OperationContext.get() .info( "- prepped schedule: " + schedule.getAttribute("Title") + " next run " + new DateTime(sched.when())); this.schedulenodes.add(sched); Hub.instance.getScheduler().addNode(sched); } }
public void collectFeedFile(Path sfile) { XElement feed = OperationContext.get().getDomain().getSettings().find("Feed"); if ((feed == null) || (sfile == null)) return; sfile = sfile.toAbsolutePath().normalize(); DomainInfo di = OperationContext.get().getDomain(); Path dmpath = di.getPath().toAbsolutePath().normalize(); Path relpath = dmpath.relativize(sfile); if (relpath.getNameCount() < 3) return; // String wwwpathf1 = preview ? "/" + area + "-preview/" + alias : "/" + area + "/" + alias; // Path wwwsrc1 = di.resolvePath(wwwpathf1).toAbsolutePath().normalize(); String area = "feed"; String alias = relpath.getName(1).toString(); String perpath = ""; // there are two special channels - Pages and Blocks for (XElement chan : feed.selectAll("Channel")) { String calias = chan.getAttribute("Alias"); if (calias == null) calias = chan.getAttribute("Name"); if (calias.equals(alias)) { perpath = chan.getAttribute("Path", ""); // or empty string break; } } System.out.println("relpath: " + relpath); relpath = relpath.subpath(2, relpath.getNameCount()); // System.out.println("abs: " + feedsrc1); // Path relpath = wwwsrc1.relativize(sfile); // System.out.println("rel: " + relpath); String cleanpath = relpath.toString(); int pos = cleanpath.indexOf('.'); if (pos != -1) cleanpath = cleanpath.substring(0, pos); // + ".dcf.xml"; String innerpath = perpath + "/" + cleanpath; FileImporter imp = new FeedImporter(); imp.setKey(innerpath); imp.setArea(area); imp.setAlias(alias); OperationContext.get().info("Indexing " + alias + " > " + innerpath); ImportWebsiteTool.this.feedpaths.put(innerpath, imp); String pubfeedpath = "/" + area + "/" + alias + "/" + cleanpath + ".dcf.xml"; Path pubfeedsrc = di.resolvePath(pubfeedpath).normalize(); if (Files.exists(pubfeedsrc)) { OperationContext.get().info("Indexing published " + alias + " > " + innerpath); imp.setFile(pubfeedsrc); } String prefeedpath = "/" + area + "-preview/" + alias + "/" + cleanpath + ".dcf.xml"; Path prefeedsrc = di.resolvePath(prefeedpath).normalize(); if (Files.exists(prefeedsrc)) { OperationContext.get().info("Indexing preview " + alias + " > " + innerpath); imp.setPreviewFile(prefeedsrc); } }
public void collectArea(String area, String alias, boolean preview, String perpath) { try { DomainInfo di = OperationContext.get().getDomain(); String wwwpathf1 = preview ? "/" + area + "-preview/" + alias : "/" + area + "/" + alias; Path wwwsrc1 = di.resolvePath(wwwpathf1).toAbsolutePath().normalize(); // System.out.println("abs: " + feedsrc1); if (Files.exists(wwwsrc1)) { Files.walkFileTree( wwwsrc1, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path sfile, BasicFileAttributes attrs) throws IOException { Path relpath = wwwsrc1.relativize(sfile); // System.out.println("rel: " + relpath); String innerpath = relpath.toString(); FileImporter imp = null; if (innerpath.endsWith(".dcui.xml")) { innerpath = perpath + "/" + innerpath.substring(0, innerpath.length() - 9); imp = new PageImporter(); } else if (innerpath.endsWith(".dcf.xml")) { innerpath = perpath + "/" + innerpath.substring(0, innerpath.length() - 8); imp = new FeedImporter(); } if ((imp != null) && !ImportWebsiteTool.this.feedpaths.containsKey(innerpath)) { imp.setKey(innerpath); imp.setArea(area); imp.setAlias(alias); if (preview) { OperationContext.get() .info("Indexing preview only " + alias + " > " + innerpath); ImportWebsiteTool.this.feedpaths.put(innerpath, imp); imp.setPreviewFile(sfile); } else { OperationContext.get().info("Indexing " + alias + " > " + innerpath); ImportWebsiteTool.this.feedpaths.put(innerpath, imp); imp.setFile(sfile); String prefeedpath = "/" + area + "-preview/" + alias + "/" + relpath; Path prefeedsrc = di.resolvePath(prefeedpath).normalize(); if (Files.exists(prefeedsrc)) { OperationContext.get() .info("Indexing preview also " + alias + " > " + innerpath); imp.setPreviewFile(prefeedsrc); } } } return FileVisitResult.CONTINUE; } }); } } catch (IOException x) { OperationContext.get().error("Error indexing " + area + ": " + alias + " : " + x); } }
public IOutputAdapter findFile(CommonPath path, boolean isPreview) { // ===================================================== // if request has an extension do specific file lookup // ===================================================== if (Logger.isDebug()) Logger.debug("find file before ext check: " + path + " - " + isPreview); // if we have an extension then we don't have to do the search below // never go up a level past a file (or folder) with an extension if (path.hasFileExtension()) { CacheFile wpath = this.findFilePath(path, isPreview); if (wpath != null) return this.pathToAdapter(isPreview, path, wpath); // TODO not found file!! OperationContext.get().errorTr(150007); return null; } // ===================================================== // if request does not have an extension look for files // that might match this path or one of its parents // using the special extensions // ===================================================== if (Logger.isDebug()) Logger.debug("find file before dyn check: " + path + " - " + isPreview); // we get here if we have no extension - thus we need to look for path match with specials int pdepth = path.getNameCount(); // check path maps first - hopefully the request has been mapped at one of the levels while (pdepth > 0) { CommonPath ppath = path.subpath(0, pdepth); if (isPreview) { IOutputAdapter ioa = this.dynpreviewcache.get(ppath.toString()); if (ioa != null) return ioa; } IOutputAdapter ioa = this.dyncache.get(ppath.toString()); if (ioa != null) return ioa; pdepth--; } if (Logger.isDebug()) Logger.debug("find file not cached: " + path + " - " + isPreview); // if not in dyncache then look on file system CacheFile wpath = this.findFilePath(path, isPreview); if (wpath == null) { OperationContext.get().errorTr(150007); return null; } if (Logger.isDebug()) Logger.debug("find file path: " + wpath + " - " + path + " - " + isPreview); return this.pathToAdapter(isPreview, path, wpath); }
@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(); }
@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(); }