Search in sources :

Example 1 with FileCleaner

use of org.apache.wicket.util.file.FileCleaner in project wicket by apache.

the class FileUploadTest method getInputStream.

/**
 * Test that when getting an input stream a new input stream is returned every time.
 *
 * Also test that the inputstream is saved internally for later closing.
 *
 * @throws Exception
 */
@Test
public void getInputStream() throws Exception {
    final IFileCleaner fileUploadCleaner = new FileCleaner();
    DiskFileItemFactory itemFactory = new DiskFileItemFactory() {

        @Override
        public FileCleaningTracker getFileCleaningTracker() {
            return new FileCleanerTrackerAdapter(fileUploadCleaner);
        }
    };
    FileItem fileItem = itemFactory.createItem("dummyFieldName", "text/java", false, "FileUploadTest.java");
    // Initialize the upload
    fileItem.getOutputStream();
    // Get the internal list out
    Field inputStreamsField = FileUpload.class.getDeclaredField("inputStreamsToClose");
    inputStreamsField.setAccessible(true);
    FileUpload fileUpload = new FileUpload(fileItem);
    List<?> inputStreams = (List<?>) inputStreamsField.get(fileUpload);
    assertNull(inputStreams);
    InputStream is1 = fileUpload.getInputStream();
    inputStreams = (List<?>) inputStreamsField.get(fileUpload);
    assertEquals(1, inputStreams.size());
    InputStream is2 = fileUpload.getInputStream();
    inputStreams = (List<?>) inputStreamsField.get(fileUpload);
    assertEquals(2, inputStreams.size());
    assertNotSame(is1, is2);
    // Ok lets close all the streams
    try {
        fileUpload.closeStreams();
    } catch (Exception e) {
        fail();
    }
    inputStreams = (List<?>) inputStreamsField.get(fileUpload);
    assertNull(inputStreams);
    fileUploadCleaner.destroy();
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) Field(java.lang.reflect.Field) FileCleanerTrackerAdapter(org.apache.wicket.util.file.FileCleanerTrackerAdapter) InputStream(java.io.InputStream) List(java.util.List) IFileCleaner(org.apache.wicket.util.file.IFileCleaner) FileCleaner(org.apache.wicket.util.file.FileCleaner) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) IFileCleaner(org.apache.wicket.util.file.IFileCleaner) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with FileCleaner

use of org.apache.wicket.util.file.FileCleaner 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

FileCleaner (org.apache.wicket.util.file.FileCleaner)2 IFileCleaner (org.apache.wicket.util.file.IFileCleaner)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 List (java.util.List)1 FileItem (org.apache.commons.fileupload.FileItem)1 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)1 AjaxRequestHandler (org.apache.wicket.ajax.AjaxRequestHandler)1 WebApplicationPath (org.apache.wicket.core.util.file.WebApplicationPath)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 WebPageRenderer (org.apache.wicket.request.handler.render.WebPageRenderer)1 HttpSessionStore (org.apache.wicket.session.HttpSessionStore)1 FileCleanerTrackerAdapter (org.apache.wicket.util.file.FileCleanerTrackerAdapter)1 Path (org.apache.wicket.util.file.Path)1 Test (org.junit.Test)1