Search in sources :

Example 1 with WebApplicationPath

use of org.apache.wicket.core.util.file.WebApplicationPath in project wicket by apache.

the class WebApplicationPathTest method doNotServeResourcesFromWebInf.

@Test
public void doNotServeResourcesFromWebInf() throws Exception {
    URL webUrl = new URL("file://dummyFile");
    ServletContext context = Mockito.mock(ServletContext.class);
    Mockito.when(context.getResource(Matchers.any(String.class))).thenReturn(webUrl);
    WebApplicationPath path = new WebApplicationPath(context, "");
    IResourceStream resourceStream = path.find(String.class, "WEB-INF/web.xml");
    assertNull(resourceStream);
    IResourceStream otherResourceStream = path.find(String.class, "any/other/resource");
    assertNotNull(otherResourceStream);
}
Also used : IResourceStream(org.apache.wicket.util.resource.IResourceStream) ServletContext(javax.servlet.ServletContext) WebApplicationPath(org.apache.wicket.core.util.file.WebApplicationPath) URL(java.net.URL) Test(org.junit.Test)

Example 2 with WebApplicationPath

use of org.apache.wicket.core.util.file.WebApplicationPath in project wicket by apache.

the class Initializer method getVelocityProperties.

private Properties getVelocityProperties(final Application application) {
    String velocityPropertiesFile = "velocity.properties";
    if (application instanceof WebApplication) {
        WebApplication webapp = (WebApplication) application;
        ServletContext servletContext = webapp.getServletContext();
        String propertiesFolder = servletContext.getInitParameter("velocityPropertiesFolder");
        String propsFile = servletContext.getInitParameter("velocity.properties");
        if (null != propsFile) {
            velocityPropertiesFile = propsFile;
        }
        if (null != propertiesFolder) {
            WebApplicationPath webPath = new WebApplicationPath(servletContext, propertiesFolder);
            IResourceStream stream = webPath.find(Initializer.class, velocityPropertiesFile);
            InputStream is;
            try {
                is = stream.getInputStream();
                Properties props = new Properties();
                props.load(is);
                return props;
            } catch (IOException | ResourceStreamNotFoundException e) {
                throw new WicketRuntimeException(e);
            } finally {
                try {
                    IOUtils.close(stream);
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }
    // if it's not a web app, load from the package
    InputStream is = Initializer.class.getResourceAsStream("velocity.properties");
    try {
        Properties props = new Properties();
        props.load(is);
        return props;
    } catch (Exception e) {
        throw new WicketRuntimeException(e);
    } finally {
        try {
            IOUtils.close(is);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }
}
Also used : IResourceStream(org.apache.wicket.util.resource.IResourceStream) InputStream(java.io.InputStream) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) ServletContext(javax.servlet.ServletContext) WebApplicationPath(org.apache.wicket.core.util.file.WebApplicationPath) IOException(java.io.IOException) WebApplication(org.apache.wicket.protocol.http.WebApplication) Properties(java.util.Properties) ResourceStreamNotFoundException(org.apache.wicket.util.resource.ResourceStreamNotFoundException) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) IOException(java.io.IOException) ResourceStreamNotFoundException(org.apache.wicket.util.resource.ResourceStreamNotFoundException)

Example 3 with WebApplicationPath

use of org.apache.wicket.core.util.file.WebApplicationPath in project wicket by apache.

the class WebApplication method internalInit.

/**
 * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL IT.
 *
 * Internal initialization. First determine the deployment mode. First check the system property
 * -Dwicket.configuration. If it does not exist check the servlet init parameter (
 * <code>&lt;init-param&gt&lt;param-name&gt;configuration&lt;/param-name&gt;</code>). If not
 * found check the servlet context init parameter
 * <code>&lt;context-param&gt&lt;param-name6gt;configuration&lt;/param-name&gt;</code>). If the
 * parameter is "development" (which is default), settings appropriate for development are set.
 * If it's "deployment" , deployment settings are used. If development is specified and a
 * "sourceFolder" init parameter is also set, then resources in that folder will be polled for
 * changes.
 */
@Override
protected void internalInit() {
    super.internalInit();
    getResourceSettings().getResourceFinders().add(new WebApplicationPath(getServletContext(), ""));
    getResourceSettings().getResourceFinders().add(new ClassPathResourceFinder(META_INF_RESOURCES));
    // Set default error pages for HTML markup
    getApplicationSettings().setPageExpiredErrorPage(PageExpiredErrorPage.class);
    getApplicationSettings().setInternalErrorPage(InternalErrorPage.class);
    getApplicationSettings().setAccessDeniedPage(AccessDeniedPage.class);
    // Add resolver for automatically resolving HTML links
    getPageSettings().addComponentResolver(new AutoLinkResolver());
    getPageSettings().addComponentResolver(new AutoLabelResolver());
    getPageSettings().addComponentResolver(new AutoLabelTextResolver());
    getResourceSettings().setFileCleaner(new FileCleaner());
    if (getConfigurationType() == RuntimeConfigurationType.DEVELOPMENT) {
        // Add optional sourceFolder for resources.
        String resourceFolder = getInitParameter("sourceFolder");
        if (resourceFolder != null) {
            getResourceSettings().getResourceFinders().add(new Path(resourceFolder));
        }
    }
    setPageRendererProvider(WebPageRenderer::new);
    setSessionStoreProvider(HttpSessionStore::new);
    setAjaxRequestTargetProvider(AjaxRequestHandler::new);
    getAjaxRequestTargetListeners().add(new AjaxEnclosureListener());
    // Configure the app.
    configure();
}
Also used : Path(org.apache.wicket.util.file.Path) WebApplicationPath(org.apache.wicket.core.util.file.WebApplicationPath) AutoLabelResolver(org.apache.wicket.markup.html.form.AutoLabelResolver) AjaxRequestHandler(org.apache.wicket.ajax.AjaxRequestHandler) WebApplicationPath(org.apache.wicket.core.util.file.WebApplicationPath) AutoLabelTextResolver(org.apache.wicket.markup.html.form.AutoLabelTextResolver) AutoLinkResolver(org.apache.wicket.markup.resolver.AutoLinkResolver) WebPageRenderer(org.apache.wicket.request.handler.render.WebPageRenderer) HttpSessionStore(org.apache.wicket.session.HttpSessionStore) ClassPathResourceFinder(org.apache.wicket.core.util.resource.ClassPathResourceFinder) IFileCleaner(org.apache.wicket.util.file.IFileCleaner) FileCleaner(org.apache.wicket.util.file.FileCleaner)

Aggregations

WebApplicationPath (org.apache.wicket.core.util.file.WebApplicationPath)3 ServletContext (javax.servlet.ServletContext)2 IResourceStream (org.apache.wicket.util.resource.IResourceStream)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 Properties (java.util.Properties)1 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1 AjaxRequestHandler (org.apache.wicket.ajax.AjaxRequestHandler)1 ClassPathResourceFinder (org.apache.wicket.core.util.resource.ClassPathResourceFinder)1 AutoLabelResolver (org.apache.wicket.markup.html.form.AutoLabelResolver)1 AutoLabelTextResolver (org.apache.wicket.markup.html.form.AutoLabelTextResolver)1 AutoLinkResolver (org.apache.wicket.markup.resolver.AutoLinkResolver)1 WebApplication (org.apache.wicket.protocol.http.WebApplication)1 WebPageRenderer (org.apache.wicket.request.handler.render.WebPageRenderer)1 HttpSessionStore (org.apache.wicket.session.HttpSessionStore)1 FileCleaner (org.apache.wicket.util.file.FileCleaner)1 IFileCleaner (org.apache.wicket.util.file.IFileCleaner)1 Path (org.apache.wicket.util.file.Path)1 ResourceStreamNotFoundException (org.apache.wicket.util.resource.ResourceStreamNotFoundException)1