public String getFileURL(CoralSession coralSession, FileResource file) {
    Resource parent = file.getParent();
    while (parent != null && !(parent instanceof RootDirectoryResource)) {
      parent = parent.getParent();
    }
    if (parent == null) {
      throw new IllegalStateException("cannot determine root directory");
    }
    RootDirectoryResource rootDirectory = ((RootDirectoryResource) parent);

    while (parent != null && !(parent instanceof SiteResource)) {
      parent = parent.getParent();
    }
    if (parent == null) {
      throw new IllegalStateException("cannot determine site");
    }
    SiteResource site = (SiteResource) parent;

    if (rootDirectory.getExternal()) {
      String path = "";
      for (parent = file; parent != null; parent = parent.getParent()) {
        if (parent instanceof RootDirectoryResource) {
          break;
        } else {
          try {
            path = "/" + URLEncoder.encode(parent.getName(), LinkTool.PARAMETER_ENCODING) + path;
          } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        }
      }
      return buildPath(
          getContextURL(coralSession, site),
          "files",
          site.getName(),
          rootDirectory.getName(),
          path);
    } else {
      String path = "";
      for (parent = file; parent != null; parent = parent.getParent()) {
        if (parent instanceof RootDirectoryResource) {
          break;
        } else {
          path = "," + parent.getName() + path;
        }
      }
      path = "/" + rootDirectory.getName() + path;
      return buildPath(
          getApplicationURL(coralSession, site),
          "view/files.Download?path=" + path + "&file_id=" + file.getIdString());
    }
  }