有一个静态的"子网站"在Wicket(即:在Wicket中安装目录)

时间:2016-07-04 13:30:26

标签: java html wicket wicket-6

我正在使用Wicket开发一个Web应用程序。

虽然大多数网站是通过检票口动态生成的,但我需要将网站的一部分设为正常的静态网站。 html网站。基本上是一个小型的子网站"在主网站内部根本不是由wicket管理的,但相反,它只是静态内容的集合(html页面,CSS,图像)。

可以这样做吗?这个想法将是'#34; mount"指向子站点的某个子路径,但我不知道这是否可能,因为mountResource()方法需要资源作为输入。

编辑:我需要一个允许我直接在文件系统上修改静态html文件的解决方案,因此我试图安装目录"通过检票口。我不能简单地将这些页面放在我的webapp文件夹中,因为这样他们最终会进入应用程序的WAR文件,并且每次对静态页面的修改都需要每次都进行完全部署。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

好吧,我最后使用动态资源自己实现了这一点。我不是Wicket的专家,所以这可能是一个"坏"解决方案出于某种原因,但似乎有效。在此处发布代码,以便其他人可以根据需要使用它:

我所做的是创建此资源:

public class DirectoryResolverResource implements IResource {
    private static final long serialVersionUID = 1L;

    private File servedDirectory;
    private String urlPrefix;

    //served directory is the directory you want to mount as a static sub-site
    //urlPrefix is the mountpoint where you're going to mount this resource, without the leading "/". E.g.: if you mount your directory in "/help" so that the sub-site URL is www.yoursite.com/pages/help the urlPrefix value must be "help"
    public DirectoryResolverResource(File servedDirectory, String urlPrefix) {
        super();
        if (servedDirectory == null || !servedDirectory.isDirectory()) {
            throw new IllegalArgumentException("Directory is null or doesn't exist");
        }
        this.servedDirectory = servedDirectory;
        this.urlPrefix = urlPrefix;
    }

    @Override
    public void respond(Attributes attributes) {
        Url url = attributes.getRequest().getUrl();
        String subPath = "";
        try {
            //we decode the URL by reversing the percent-encoding, so that filenames are properly resolved
            subPath = URLDecoder.decode(url.toString(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_BAD_REQUEST, "Encoding is invalid");
        }

        if (subPath.startsWith(urlPrefix)) {
            subPath = subPath.substring(urlPrefix.length());
        } else {
            throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Url is invalid");
        }

        File file = new File(servedDirectory.getAbsolutePath() + (subPath.startsWith("/") ? "" : "/") + subPath);
        if (file.isDirectory()) {
            // In case of a directory, redirect to the path ending in "/", otherwise browsers will fail to resolve relative paths in the page
            if (!subPath.endsWith("/")) {
                throw new RedirectToUrlException("." + (subPath.isEmpty() ? "/" + urlPrefix : subPath) + "/", HttpServletResponse.SC_MOVED_PERMANENTLY);
            }
            // no specific file specified, try to return index.html
            file = new File(file.getAbsolutePath(), "index.html");
        }
        if (!file.exists() || file.isDirectory()) {
            // file not found
            throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, "Resource not found");
        }

        if (!FSManager.isInSubDirectory(servedDirectory, file)) {
            // Security check: user is trying to escape the served directory via a non-canonical path
            throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_FORBIDDEN, "Access to this resource is forbidden");
        }

        // Serve the file
        FileResourceStream fileResourceStream = new FileResourceStream(file);
        ResourceStreamResource resource = new ResourceStreamResource(fileResourceStream);
        resource.respond(attributes);
    }

}

您可以像这样安装此资源:

mountResource("/help", new ResourceReference("helpres") {
                private static final long serialVersionUID = 1L;

                @Override
                public IResource getResource() {
                    return new DirectoryResolverResource(helpDir, "help");
                }
            });

希望这对某人有帮助。 任何改进/评论/更正/建设性批评都非常感谢!

注意:isInSubDirectory()方法只检查文件是否在某个目录树中。不会让您了解详细信息,您可以在此处找到此类方法的实现:Check if file is in (sub)directory