Search in sources :

Example 11 with File

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

the class MultiFileUploadFieldTest method submitMultiFileUploadFields.

/**
 * FormTester supports MultiFileUploadField
 *
 * https://issues.apache.org/jira/browse/WICKET-5346
 */
@Test
public void submitMultiFileUploadFields() {
    final AtomicBoolean submitted = new AtomicBoolean(false);
    final ListModel<FileUpload> filesModel = new ListModel<>(new ArrayList<FileUpload>());
    TestPage page = new TestPage(filesModel) {

        @Override
        protected void onSubmit() {
            super.onSubmit();
            List<FileUpload> uploads = filesModel.getObject();
            assertEquals(2, uploads.size());
            for (int i = 0; i < 2; i++) {
                FileUpload fileUpload = uploads.get(i);
                String clientFileName = fileUpload.getClientFileName();
                String id = clientFileName.replaceAll(MultiFileUploadFieldTest.class.getSimpleName() + "(\\d).txt", "$1");
                try {
                    assertEquals("Test" + id, IOUtils.toString(fileUpload.getInputStream()));
                } catch (IOException e) {
                    fail("Reading file upload '" + id + "' failed: " + e.getMessage());
                }
            }
            submitted.set(true);
        }
    };
    tester.startPage(page);
    tester.assertContainsNot("disabled=\"disabled\"");
    FormTester ft = tester.newFormTester("f");
    ft.setFile("muf", new File("target/test-classes/org/apache/wicket/markup/html/form/upload/MultiFileUploadFieldTest0.txt"), "plain/text");
    ft.setFile("muf", new File("target/test-classes/org/apache/wicket/markup/html/form/upload/MultiFileUploadFieldTest1.txt"), "plain/text");
    ft.submit();
    assertEquals("The form is not submitted", true, submitted.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ListModel(org.apache.wicket.model.util.ListModel) FormTester(org.apache.wicket.util.tester.FormTester) IOException(java.io.IOException) File(org.apache.wicket.util.file.File) Test(org.junit.Test)

Example 12 with File

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

the class AsynchronousPageStoreTest method storeReturnsSameInstanceOnClosePageRequest.

/**
 * Store returns the same page instance from queue when there is a close request for it back
 * again.
 *
 * @throws InterruptedException
 */
@Test
public void storeReturnsSameInstanceOnClosePageRequest() throws InterruptedException {
    ISerializer serializer = new DeflatedJavaSerializer("applicationKey");
    // ISerializer serializer = new DummySerializer();
    IDataStore dataStore = new DiskDataStore("applicationName", new File("./target"), Bytes.bytes(10000l));
    // IPageStore pageStore = new DummyPageStore(new File("target/store"));
    IPageStore pageStore = spy(new DefaultPageStore(serializer, dataStore, 0));
    IPageStore asyncPageStore = new AsynchronousPageStore(pageStore, 100);
    int pageId = 0;
    String sessionId = "sessionId";
    DummyPage page = new DummyPage(pageId, 1000, 1000, sessionId);
    asyncPageStore.storePage(sessionId, page);
    Thread.sleep(500);
    IManageablePage pageBack = asyncPageStore.getPage(sessionId, pageId);
    verify(pageStore, never()).getPage(sessionId, pageId);
    assertEquals(page, pageBack);
}
Also used : DeflatedJavaSerializer(org.apache.wicket.serialize.java.DeflatedJavaSerializer) File(org.apache.wicket.util.file.File) IManageablePage(org.apache.wicket.page.IManageablePage) ISerializer(org.apache.wicket.serialize.ISerializer) Test(org.junit.Test)

Example 13 with File

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

the class ReloadingClassLoader method watchForModifications.

/**
 * Watch changes of a class file by locating it in the list of location URLs and adding the
 * corresponding file to the ModificationWatcher
 *
 * @param clz
 *            the class to watch
 */
private void watchForModifications(final Class<?> clz) {
    // Watch class in the future
    Iterator<URL> locationsIterator = urls.iterator();
    File clzFile = null;
    while (locationsIterator.hasNext()) {
        // FIXME only works for directories, but JARs etc could be checked
        // as well
        URL location = locationsIterator.next();
        String clzLocation = location.getFile() + clz.getName().replaceAll("\\.", "/") + ".class";
        log.debug("clzLocation=" + clzLocation);
        clzFile = new File(clzLocation);
        final File finalClzFile = clzFile;
        if (clzFile.exists()) {
            log.info("Watching changes of class " + clzFile);
            watcher.add(clzFile, new IChangeListener<IModifiable>() {

                @Override
                public void onChange(IModifiable modifiable) {
                    log.info("Class file " + finalClzFile + " has changed, reloading");
                    try {
                        listener.onChange(clz);
                    } catch (Exception e) {
                        log.error("Could not notify listener", e);
                        // If an error occurs when the listener is notified,
                        // remove the watched object to avoid rethrowing the
                        // exception at next check
                        // FIXME check if class file has been deleted
                        watcher.remove(finalClzFile);
                    }
                }
            });
            break;
        } else {
            log.debug("Class file does not exist: " + clzFile);
        }
    }
    if (clzFile != null && !clzFile.exists()) {
        log.debug("Could not locate class " + clz.getName());
    }
}
Also used : IModifiable(org.apache.wicket.util.watch.IModifiable) File(org.apache.wicket.util.file.File) URL(java.net.URL) IOException(java.io.IOException)

Example 14 with File

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

the class FileResourceStreamReference method getReference.

@Override
public FileResourceStream getReference() {
    FileResourceStream fileResourceStream = new FileResourceStream(new File(fileName));
    restoreResourceStream(fileResourceStream);
    return fileResourceStream;
}
Also used : FileResourceStream(org.apache.wicket.util.resource.FileResourceStream) File(org.apache.wicket.util.file.File)

Example 15 with File

use of org.apache.wicket.util.file.File in project midpoint by Evolveum.

the class PageImportObject method getInputDescription.

@NotNull
private InputDescription getInputDescription(boolean editor) throws Exception {
    if (editor) {
        return new InputDescription(IOUtils.toInputStream(xmlEditorModel.getObject(), StandardCharsets.UTF_8), dataLanguage);
    }
    File newFile = null;
    try {
        // Create new file
        MidPointApplication application = getMidpointApplication();
        WebApplicationConfiguration config = application.getWebApplicationConfiguration();
        File folder = new File(config.getImportFolder());
        if (!folder.exists() || !folder.isDirectory()) {
            folder.mkdir();
        }
        FileUpload uploadedFile = getUploadedFile();
        newFile = new File(folder, uploadedFile.getClientFileName());
        // Check new file, delete if it already exists
        if (newFile.exists()) {
            newFile.delete();
        }
        // Save file
        newFile.createNewFile();
        FileUtils.copyInputStreamToFile(uploadedFile.getInputStream(), newFile);
        String language = getPrismContext().detectLanguage(newFile);
        return new InputDescription(new FileInputStream(newFile), language);
    } finally {
        if (newFile != null) {
            FileUtils.deleteQuietly(newFile);
        }
    }
}
Also used : MidPointApplication(com.evolveum.midpoint.web.security.MidPointApplication) WebApplicationConfiguration(com.evolveum.midpoint.web.security.WebApplicationConfiguration) File(org.apache.wicket.util.file.File) FileUpload(org.apache.wicket.markup.html.form.upload.FileUpload) FileInputStream(java.io.FileInputStream) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

File (org.apache.wicket.util.file.File)28 Test (org.junit.Test)12 IOException (java.io.IOException)7 FileUpload (org.apache.wicket.markup.html.form.upload.FileUpload)6 AjaxDownloadBehaviorFromFile (com.evolveum.midpoint.web.component.AjaxDownloadBehaviorFromFile)5 MidPointApplication (com.evolveum.midpoint.web.security.MidPointApplication)5 WebApplicationConfiguration (com.evolveum.midpoint.web.security.WebApplicationConfiguration)5 FileInputStream (java.io.FileInputStream)5 MidpointConfiguration (com.evolveum.midpoint.common.configuration.api.MidpointConfiguration)4 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)4 RestartResponseException (org.apache.wicket.RestartResponseException)4 FormTester (org.apache.wicket.util.tester.FormTester)4 CommonException (com.evolveum.midpoint.util.exception.CommonException)3 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)3 IManageablePage (org.apache.wicket.page.IManageablePage)3 ISerializer (org.apache.wicket.serialize.ISerializer)3 DeflatedJavaSerializer (org.apache.wicket.serialize.java.DeflatedJavaSerializer)3 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2