Exemplo n.º 1
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);
  }
Exemplo n.º 2
0
  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;
  }
Exemplo n.º 3
0
  public CacheFile findSectionFile(String section, String path, boolean isPreview) {
    if (Logger.isDebug()) Logger.debug("find section file: " + path + " in " + section);

    LocalFileStore pubfs = Hub.instance.getPublicFileStore();
    LocalFileStore prifs = Hub.instance.getPrivateFileStore();

    // for a sub-site, check first in the site folder

    if (this != this.domain.getRootSite()) {
      if (Logger.isDebug())
        Logger.debug("find section file, check site: " + path + " in " + section);

      if (prifs != null) {
        if (isPreview) {
          CacheFile cfile =
              prifs.cacheResolvePath(
                  "/dcw/"
                      + this.domain.getAlias()
                      + "/sites/"
                      + this.alias
                      + "/"
                      + section
                      + "-preview"
                      + path);

          if (cfile != null) return cfile;
        }

        CacheFile cfile =
            prifs.cacheResolvePath(
                "/dcw/" + this.domain.getAlias() + "/sites/" + this.alias + "/" + section + path);

        if (cfile != null) return cfile;
      }

      if (pubfs != null) {
        if (isPreview) {
          CacheFile cfile =
              pubfs.cacheResolvePath(
                  "/dcw/"
                      + this.domain.getAlias()
                      + "/sites/"
                      + this.alias
                      + "/"
                      + section
                      + "-preview"
                      + path);

          if (cfile != null) return cfile;
        }

        CacheFile cfile =
            pubfs.cacheResolvePath(
                "/dcw/" + this.domain.getAlias() + "/sites/" + this.alias + "/" + section + path);

        if (cfile != null) return cfile;
      }

      // if not shared then jump right to modules for resource
      if (!this.isSharedSection(section))
        return Hub.instance
            .getResources()
            .getPackages()
            .cacheLookupPath(this.domain.getPackagelist(), "/" + section + path);
    }

    // now check the root site folders

    // TODO Each site's special files (dcui, dcf, html, shtml, gas) are completely separate -
    // supplemental files like js, css, imgs, etc may be integrated depending on site settings.

    if (Logger.isDebug()) Logger.debug("find section file, check root: " + path + " in " + section);

    if (prifs != null) {
      if (isPreview) {
        CacheFile cfile =
            prifs.cacheResolvePath(
                "/dcw/" + this.domain.getAlias() + "/" + section + "-preview" + path);

        if (cfile != null) return cfile;
      }

      CacheFile cfile =
          prifs.cacheResolvePath("/dcw/" + this.domain.getAlias() + "/" + section + path);

      if (cfile != null) return cfile;
    }

    if (pubfs != null) {
      if (isPreview) {
        CacheFile cfile =
            pubfs.cacheResolvePath(
                "/dcw/" + this.domain.getAlias() + "/" + section + "-preview" + path);

        if (cfile != null) return cfile;
      }

      CacheFile cfile =
          pubfs.cacheResolvePath("/dcw/" + this.domain.getAlias() + "/" + section + path);

      if (cfile != null) return cfile;
    }

    if (Logger.isDebug()) Logger.debug("find section check packages: " + path + " in " + section);

    return Hub.instance
        .getResources()
        .getPackages()
        .cacheLookupPath(this.domain.getPackagelist(), "/" + section + path);
  }
Exemplo n.º 4
0
  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);
  }
Exemplo n.º 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();
  }