简体   繁体   English

nvelocity是否有URLResourceLoader类的模拟

[英]Is there an analog for URLResourceLoader class in nvelocity

I want to provide external resource to VelocityEnngine that located on external server by using url path, in Apache velocity there is URLResourceLoader class but i see that in NVelocity there is only FileRecourceLoader... 我想通过使用URL路径向位于外部服务器上的VelocityEnngine提供外部资源,在Apache Velocity中有URLResourceLoader类,但是我看到在NVelocity中只有FileRecourceLoader ...

Is there a way to provide external (url) resource ExtendedProperty in NVelocity 有没有办法在NVelocity中提供外部(url)资源ExtendedProperty

Thanks in advance. 提前致谢。

Implemented the interface by my self: 我自己实现了该接口:

public class UrlResourceLoader : ResourceLoader
{
    protected ArrayList paths;
    protected Hashtable templatePaths;

    public UrlResourceLoader()
    {
        templatePaths = new Hashtable();
    }


    public override void Init(ExtendedProperties configuration)
    {
        paths = configuration.GetVector("path");
    }

    public override Stream GetResourceStream(string templateName)
    {
        lock (this)
        {
            int size = paths.Count;
            if (string.IsNullOrEmpty(templateName))
            {
                throw;
            }

            for (int i = 0; i < size; i++)
            {
                var path = (string) paths[i];
                var uri = new Uri(path + templateName);
                Stream inputStream = FindTemplate(uri);
                if (inputStream != null)
                {
                    SupportClass.PutElement(templatePaths, templateName, path);
                    return inputStream;
                }
            }
            throw;
        }
    }

    private Stream FindTemplate(Uri requestUri)
    {
        try
        {
            var request = (HttpWebRequest)WebRequest.Create(requestUri);
            request.Method = "GET";

            var response = (HttpWebResponse) request.GetResponse();
            if (HttpStatusCode.OK != response.StatusCode)
            {
                throw;
            }

            return response.GetResponseStream();
        }
        catch (Exception ex)
        {
            throw;
        }
    }

    public override bool IsSourceModified(Resource resource)
    {
        var path = (string)templatePaths[resource.Name];
        var uri = new Uri(path + resource.Name);

        var request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = "GET";

        using (var response = (HttpWebResponse)request.GetResponse())
        {
            return response.LastModified.Ticks != resource.LastModified;
        }
    }

    public override long GetLastModified(Resource resource)
    {
        var path = (string)templatePaths[resource.Name];
        var uri = new Uri(path + resource.Name);

        var request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = "GET";

        using (var response = (HttpWebResponse)request.GetResponse())
        {
            return response.LastModified.Ticks;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM