    public String getLink(Resource resource, boolean absolute)
        throws FilesException
    {
        HttpContext httpContext = HttpContext.getHttpContext(context);
        if(!(resource instanceof FileResource))
        {
            throw new FilesException("Resource is not the instance of cms.files.file class");
        }
        FileResource file = (FileResource)resource;
        RootDirectoryResource rootDirectory = null;
        for(Resource parent = file.getParent(); parent != null; parent = parent.getParent())
        {
            if(parent instanceof RootDirectoryResource)
            {
                rootDirectory = ((RootDirectoryResource)parent);
                break;
            }
        }
        if(rootDirectory.getExternal())
        {
            String path = "";
            for(Resource parent = file; parent != null; parent = parent.getParent())
            {
                if(parent instanceof RootDirectoryResource)
                {
                    break;
                }
                else
                {
                    try
                    {
                        path = "/"+URLEncoder.encode(parent.getName(), "UTF-8")+path;
                    }
                    catch(UnsupportedEncodingException e)
                    {
                        throw new FilesException("really should not happen", e);
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            if(absolute)
            {
                if(!httpContext.getRequest().isSecure())
                {
                    sb.append("http://");
                    sb.append(httpContext.getRequest().getServerName());
                    if(httpContext.getRequest().getServerPort() != 80)
                    {
                        sb.append(":");
                        sb.append(httpContext.getRequest().getServerPort());
                    }                
                }
                else
                {
                    sb.append("https://");
                    sb.append(httpContext.getRequest().getServerName());
                    if(httpContext.getRequest().getServerPort() != 443)
                    {
                        sb.append(":");
                        sb.append(httpContext.getRequest().getServerPort());
                    }                
                }
            }
            sb.append(httpContext.getRequest().getContextPath());
            sb.append("/files/");
            sb.append(rootDirectory.getParent().getParent().getName());
            sb.append("/");
            sb.append(rootDirectory.getName());
            sb.append(path);
            return sb.toString();
        }
        else
        {
            TemplatingContext tContext = (TemplatingContext)
                context.getAttribute(TemplatingContext.class);
            LinkTool link = (LinkTool)tContext.get("link");
            if(absolute)
            {
                link = link.absolute();
            }
            link = link.view("files.Download").pathInfoSuffix(file.getName()).set(
                "file_id", file.getIdString());
            if(absolute)
            {
                link = link.absolute();
            }
            return link.toString();
        }
    }

    public String getLink(Resource resource, boolean absolute)
        throws FilesException
    {
        HttpContext httpContext = HttpContext.getHttpContext(context);
        if(!(resource instanceof FileResource))
        {
            throw new FilesException("Resource is not the instance of cms.files.file class");
        }
        FileResource file = (FileResource)resource;
        RootDirectoryResource rootDirectory = null;
        for(Resource parent = file.getParent(); parent != null; parent = parent.getParent())
        {
            if(parent instanceof RootDirectoryResource)
            {
                rootDirectory = ((RootDirectoryResource)parent);
                break;
            }
        }
        if(rootDirectory.getExternal())
        {
            String path = "";
            for(Resource parent = file; parent != null; parent = parent.getParent())
            {
                if(parent instanceof RootDirectoryResource)
                {
                    break;
                }
                else
                {
                    try
                    {
                        path = "/"+URLEncoder.encode(parent.getName(), "UTF-8")+path;
                        path = path.replace("+", "%20");
                    }
                    catch(UnsupportedEncodingException e)
                    {
                        throw new FilesException("really should not happen", e);
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            if(absolute)
            {
                if(!httpContext.getRequest().isSecure())
                {
                    sb.append("http://");
                    sb.append(httpContext.getRequest().getServerName());
                    if(httpContext.getRequest().getServerPort() != 80)
                    {
                        sb.append(":");
                        sb.append(httpContext.getRequest().getServerPort());
                    }                
                }
                else
                {
                    sb.append("https://");
                    sb.append(httpContext.getRequest().getServerName());
                    if(httpContext.getRequest().getServerPort() != 443)
                    {
                        sb.append(":");
                        sb.append(httpContext.getRequest().getServerPort());
                    }                
                }
            }
            sb.append(httpContext.getRequest().getContextPath());
            sb.append("/files/");
            sb.append(rootDirectory.getParent().getParent().getName());
            sb.append("/");
            sb.append(rootDirectory.getName());
            sb.append(path);
            return sb.toString();
        }
        else
        {
            TemplatingContext tContext = (TemplatingContext)
                context.getAttribute(TemplatingContext.class);
            LinkTool link = (LinkTool)tContext.get("link");
            if(absolute)
            {
                link = link.absolute();
            }
            link = link.view("files.Download").pathInfoSuffix(file.getName()).set(
                "file_id", file.getIdString());
            if(absolute)
            {
                link = link.absolute();
            }
            return link.toString();
        }
    }
