Search in sources :

Example 16 with File

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

the class PageNewReport method importReportFromFilePerformed.

private void importReportFromFilePerformed(AjaxRequestTarget target) {
    OperationResult result = new OperationResult(OPERATION_IMPORT_REPORT);
    FileUploadField file = (FileUploadField) get(createComponentPath(ID_MAIN_FORM, ID_INPUT, ID_INPUT_FILE, ID_FILE_INPUT));
    final FileUpload uploadedFile = file.getFileUpload();
    if (uploadedFile == null) {
        error(getString("PageNewReport.message.nullFile"));
        target.add(getFeedbackPanel());
        return;
    }
    InputStream stream = null;
    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();
        }
        newFile = new File(folder, uploadedFile.getClientFileName());
        // Check new file, delete if it already exists
        if (newFile.exists()) {
            newFile.delete();
        }
        // Save file
        //            Task task = createSimpleTask(OPERATION_IMPORT_FILE);
        newFile.createNewFile();
        uploadedFile.writeTo(newFile);
        InputStreamReader reader = new InputStreamReader(new FileInputStream(newFile), "utf-8");
        //            reader.
        stream = new ReaderInputStream(reader, reader.getEncoding());
        byte[] reportIn = IOUtils.toByteArray(stream);
        setResponsePage(new PageReport(new ReportDto(Base64.encodeBase64(reportIn))));
    } catch (Exception ex) {
        result.recordFatalError("Couldn't import file.", ex);
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't import file", ex);
    } finally {
        if (stream != null) {
            IOUtils.closeQuietly(stream);
        }
        if (newFile != null) {
            FileUtils.deleteQuietly(newFile);
        }
    }
    showResult(result);
    target.add(getFeedbackPanel());
}
Also used : InputStreamReader(java.io.InputStreamReader) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ReportDto(com.evolveum.midpoint.web.page.admin.reports.dto.ReportDto) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) FileInputStream(java.io.FileInputStream) FileUploadField(org.apache.wicket.markup.html.form.upload.FileUploadField) MidPointApplication(com.evolveum.midpoint.web.security.MidPointApplication) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) WebApplicationConfiguration(com.evolveum.midpoint.web.security.WebApplicationConfiguration) File(org.apache.wicket.util.file.File) FileUpload(org.apache.wicket.markup.html.form.upload.FileUpload)

Example 17 with File

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

the class PageAccounts method downloadPerformed.

private void downloadPerformed(AjaxRequestTarget target, String fileName, AjaxDownloadBehaviorFromFile downloadBehavior) {
    MidpointConfiguration config = getMidpointConfiguration();
    downloadFile = new File(config.getMidpointHome() + "/export/" + fileName);
    downloadBehavior.initiate(target);
}
Also used : MidpointConfiguration(com.evolveum.midpoint.common.configuration.api.MidpointConfiguration) AjaxDownloadBehaviorFromFile(com.evolveum.midpoint.web.component.AjaxDownloadBehaviorFromFile) File(org.apache.wicket.util.file.File)

Example 18 with File

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

the class PageAccounts method createFilesModel.

private LoadableModel<List<String>> createFilesModel() {
    return new LoadableModel<List<String>>(false) {

        @Override
        protected List<String> load() {
            String[] filesArray;
            try {
                MidpointConfiguration config = getMidpointConfiguration();
                File exportFolder = new File(config.getMidpointHome() + "/export");
                filesArray = exportFolder.list(new FilenameFilter() {

                    @Override
                    public boolean accept(java.io.File dir, String name) {
                        return name.endsWith("xml");
                    }
                });
            } catch (Exception ex) {
                LoggingUtils.logUnexpectedException(LOGGER, "Couldn't list files", ex);
                getSession().error("Couldn't list files, reason: " + ex.getMessage());
                throw new RestartResponseException(PageDashboard.class);
            }
            if (filesArray == null) {
                return new ArrayList<>();
            }
            List<String> list = Arrays.asList(filesArray);
            Collections.sort(list);
            return list;
        }
    };
}
Also used : FilenameFilter(java.io.FilenameFilter) RestartResponseException(org.apache.wicket.RestartResponseException) MidpointConfiguration(com.evolveum.midpoint.common.configuration.api.MidpointConfiguration) LoadableModel(com.evolveum.midpoint.gui.api.model.LoadableModel) ArrayList(java.util.ArrayList) PageDashboard(com.evolveum.midpoint.web.page.admin.home.PageDashboard) AjaxDownloadBehaviorFromFile(com.evolveum.midpoint.web.component.AjaxDownloadBehaviorFromFile) File(org.apache.wicket.util.file.File) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) RestartResponseException(org.apache.wicket.RestartResponseException) IOException(java.io.IOException) CommonException(com.evolveum.midpoint.util.exception.CommonException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException)

Example 19 with File

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

the class AsynchronousPageStoreTest method runTest.

private List<Metrics> runTest(int sessions, int pages, long writeMillis, long readMillis, int asyncPageStoreCapacity) throws InterruptedException {
    List<Metrics> results = new ArrayList<>();
    final CountDownLatch lock = new CountDownLatch(pages * sessions);
    // ISerializer serializer = new DummySerializer();
    ISerializer serializer = new DeflatedJavaSerializer("applicationKey");
    IDataStore dataStore = new DiskDataStore("applicationName", new File("./target"), Bytes.bytes(10000l));
    // IPageStore pageStore = new DummyPageStore(new File("target/store")) {
    IPageStore pageStore = new DefaultPageStore(serializer, dataStore, 0) {

        @Override
        public void storePage(String sessionId, IManageablePage page) {
            super.storePage(sessionId, page);
            lock.countDown();
        }
    };
    IPageStore asyncPageStore = new AsynchronousPageStore(pageStore, asyncPageStoreCapacity);
    Stopwatch stopwatch = Stopwatch.createUnstarted();
    for (int pageId = 1; pageId <= pages; pageId++) {
        for (int i = 1; i <= sessions; i++) {
            String sessionId = String.valueOf(i);
            Metrics metrics = new Metrics();
            stopwatch.reset();
            DummyPage page = new DummyPage(pageId, around(writeMillis), around(readMillis), sessionId);
            stopwatch.start();
            asyncPageStore.storePage(sessionId, page);
            metrics.storedPage = page;
            metrics.storingMillis = stopwatch.elapsed(TimeUnit.MILLISECONDS);
            stopwatch.reset();
            stopwatch.start();
            metrics.restoredPage = DummyPage.class.cast(asyncPageStore.getPage(sessionId, pageId));
            metrics.restoringMillis = stopwatch.elapsed(TimeUnit.MILLISECONDS);
            results.add(metrics);
        }
    }
    lock.await(pages * sessions * (writeMillis + readMillis), TimeUnit.MILLISECONDS);
    return results;
}
Also used : ArrayList(java.util.ArrayList) Stopwatch(com.google.common.base.Stopwatch) CountDownLatch(java.util.concurrent.CountDownLatch) 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)

Example 20 with File

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

the class AsynchronousPageStoreTest method storeReturnsRestoredInstanceOnDistantPageRequest.

/**
 * Store returns the restored page instance from wrapped store when there is a distant request
 * for it back again.
 *
 * @throws InterruptedException
 */
@Test
public void storeReturnsRestoredInstanceOnDistantPageRequest() 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(1500);
    IManageablePage pageBack = asyncPageStore.getPage(sessionId, pageId);
    verify(pageStore, times(1)).getPage(sessionId, pageId);
    assertNotEquals(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)

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