Search in sources :

Example 6 with IResource

use of org.apache.wicket.request.resource.IResource in project wicket by apache.

the class AjaxDownloadPage method initDownloadInIframe.

private void initDownloadInIframe() {
    IResource resource = new ExampleResource("downloaded via ajax in iframe").setContentDisposition(ContentDisposition.ATTACHMENT);
    final AjaxDownloadBehavior download = new AjaxDownloadBehavior(resource) {

        @Override
        protected void onBeforeDownload(AjaxRequestTarget target) {
            downloadingContainer.setVisible(true);
            target.add(downloadingContainer);
        }

        @Override
        protected void onDownloadSuccess(AjaxRequestTarget target) {
            downloadingContainer.setVisible(false);
            target.add(downloadingContainer);
        }

        @Override
        protected void onDownloadFailed(AjaxRequestTarget target) {
            downloadingContainer.setVisible(false);
            target.add(downloadingContainer);
            target.appendJavaScript("alert('Download failed');");
        }
    };
    download.setLocation(Location.IFrame);
    add(download);
    add(new AjaxLink<Void>("downloadIframe") {

        @Override
        public void onClick(AjaxRequestTarget target) {
            download.initiate(target);
        }
    });
}
Also used : AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) IResource(org.apache.wicket.request.resource.IResource) AjaxDownloadBehavior(org.apache.wicket.extensions.ajax.AjaxDownloadBehavior)

Example 7 with IResource

use of org.apache.wicket.request.resource.IResource in project wicket by apache.

the class AjaxDownloadPage method initDownload.

private void initDownload() {
    IResource resource = new ExampleResource("downloaded via ajax").setContentDisposition(ContentDisposition.ATTACHMENT);
    final AjaxDownloadBehavior download = new AjaxDownloadBehavior(resource) {

        @Override
        protected void onBeforeDownload(AjaxRequestTarget target) {
            downloadingContainer.setVisible(true);
            target.add(downloadingContainer);
        }

        @Override
        protected void onDownloadSuccess(AjaxRequestTarget target) {
            downloadingContainer.setVisible(false);
            target.add(downloadingContainer);
        }

        @Override
        protected void onDownloadFailed(AjaxRequestTarget target) {
            downloadingContainer.setVisible(false);
            target.add(downloadingContainer);
            target.appendJavaScript("alert('Download failed');");
        }
    };
    add(download);
    add(new AjaxLink<Void>("download") {

        @Override
        public void onClick(AjaxRequestTarget target) {
            download.initiate(target);
        }
    });
}
Also used : AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) IResource(org.apache.wicket.request.resource.IResource) AjaxDownloadBehavior(org.apache.wicket.extensions.ajax.AjaxDownloadBehavior)

Example 8 with IResource

use of org.apache.wicket.request.resource.IResource in project syncope by apache.

the class SyncopeConsoleApplication method init.

@Override
protected void init() {
    super.init();
    // read console.properties
    Properties props = PropertyUtils.read(getClass(), CONSOLE_PROPERTIES, "console.directory").getLeft();
    site = props.getProperty("site");
    Args.notNull(site, "<site>");
    anonymousUser = props.getProperty("anonymousUser");
    Args.notNull(anonymousUser, "<anonymousUser>");
    anonymousKey = props.getProperty("anonymousKey");
    Args.notNull(anonymousKey, "<anonymousKey>");
    scheme = props.getProperty("scheme");
    Args.notNull(scheme, "<scheme>");
    host = props.getProperty("host");
    Args.notNull(host, "<host>");
    port = props.getProperty("port");
    Args.notNull(port, "<port>");
    rootPath = props.getProperty("rootPath");
    Args.notNull(rootPath, "<rootPath>");
    useGZIPCompression = props.getProperty("useGZIPCompression");
    Args.notNull(useGZIPCompression, "<useGZIPCompression>");
    maxUploadFileSizeMB = props.getProperty("maxUploadFileSizeMB") == null ? null : Integer.valueOf(props.getProperty("maxUploadFileSizeMB"));
    maxWaitTime = Integer.valueOf(props.getProperty("maxWaitTimeOnApplyChanges", "30"));
    String csrf = props.getProperty("csrf");
    // process page properties
    pageClasses = new HashMap<>();
    populatePageClasses(props);
    pageClasses = Collections.unmodifiableMap(pageClasses);
    // Application settings
    IBootstrapSettings settings = new BootstrapSettings();
    // set theme provider
    settings.setThemeProvider(new SingleThemeProvider(new AdminLTE()));
    // install application settings
    Bootstrap.install(this, settings);
    getResourceSettings().setUseMinifiedResources(true);
    getResourceSettings().setThrowExceptionOnMissingResource(true);
    getJavaScriptLibrarySettings().setJQueryReference(new DynamicJQueryResourceReference());
    getSecuritySettings().setAuthorizationStrategy(new MetaDataRoleAuthorizationStrategy(this));
    ClassPathScanImplementationLookup lookup = (ClassPathScanImplementationLookup) getServletContext().getAttribute(ConsoleInitializer.CLASSPATH_LOOKUP);
    lookup.getPageClasses().forEach(cls -> MetaDataRoleAuthorizationStrategy.authorize(cls, SyncopeConsoleSession.AUTHENTICATED));
    getMarkupSettings().setStripWicketTags(true);
    getMarkupSettings().setCompressWhitespace(true);
    if (BooleanUtils.toBoolean(csrf)) {
        getRequestCycleListeners().add(new CsrfPreventionRequestCycleListener());
    }
    getRequestCycleListeners().add(new SyncopeConsoleRequestCycleListener());
    mountPage("/login", getSignInPageClass());
    flowableModelerDirectory = props.getProperty("flowableModelerDirectory");
    Args.notNull(flowableModelerDirectory, "<flowableModelerDirectory>");
    try {
        reconciliationReportKey = props.getProperty("reconciliationReportKey");
    } catch (NumberFormatException e) {
        LOG.error("While parsing reconciliationReportKey", e);
    }
    Args.notNull(reconciliationReportKey, "<reconciliationReportKey>");
    mountResource("/" + FLOWABLE_MODELER_CONTEXT, new ResourceReference(FLOWABLE_MODELER_CONTEXT) {

        private static final long serialVersionUID = -128426276529456602L;

        @Override
        public IResource getResource() {
            return new FilesystemResource(FLOWABLE_MODELER_CONTEXT, flowableModelerDirectory);
        }
    });
    mountResource("/workflowDefGET", new ResourceReference("workflowDefGET") {

        private static final long serialVersionUID = -128426276529456602L;

        @Override
        public IResource getResource() {
            return new WorkflowDefGETResource();
        }
    });
    mountResource("/workflowDefPUT", new ResourceReference("workflowDefPUT") {

        private static final long serialVersionUID = -128426276529456602L;

        @Override
        public IResource getResource() {
            return new WorkflowDefPUTResource();
        }
    });
    // enable component path
    if (getDebugSettings().isAjaxDebugModeEnabled()) {
        getDebugSettings().setComponentPathAttributeName("syncope-path");
    }
}
Also used : SingleThemeProvider(de.agilecoders.wicket.core.settings.SingleThemeProvider) Properties(java.util.Properties) MetaDataRoleAuthorizationStrategy(org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy) IBootstrapSettings(de.agilecoders.wicket.core.settings.IBootstrapSettings) BootstrapSettings(de.agilecoders.wicket.core.settings.BootstrapSettings) CsrfPreventionRequestCycleListener(org.apache.wicket.protocol.http.CsrfPreventionRequestCycleListener) IBootstrapSettings(de.agilecoders.wicket.core.settings.IBootstrapSettings) FilesystemResource(org.apache.syncope.client.console.resources.FilesystemResource) ClassPathScanImplementationLookup(org.apache.syncope.client.console.init.ClassPathScanImplementationLookup) WorkflowDefGETResource(org.apache.syncope.client.console.resources.WorkflowDefGETResource) AdminLTE(org.apache.syncope.client.console.themes.AdminLTE) DynamicJQueryResourceReference(org.apache.wicket.resource.DynamicJQueryResourceReference) ResourceReference(org.apache.wicket.request.resource.ResourceReference) WorkflowDefPUTResource(org.apache.syncope.client.console.resources.WorkflowDefPUTResource) DynamicJQueryResourceReference(org.apache.wicket.resource.DynamicJQueryResourceReference) IResource(org.apache.wicket.request.resource.IResource)

Example 9 with IResource

use of org.apache.wicket.request.resource.IResource in project syncope by apache.

the class SyncopeEnduserApplication method init.

@Override
protected void init() {
    super.init();
    // read enduser.properties
    Properties props = PropertyUtils.read(getClass(), ENDUSER_PROPERTIES, "enduser.directory").getLeft();
    domain = props.getProperty("domain", SyncopeConstants.MASTER_DOMAIN);
    adminUser = props.getProperty("adminUser");
    Args.notNull(adminUser, "<adminUser>");
    anonymousUser = props.getProperty("anonymousUser");
    Args.notNull(anonymousUser, "<anonymousUser>");
    anonymousKey = props.getProperty("anonymousKey");
    Args.notNull(anonymousKey, "<anonymousKey>");
    captchaEnabled = Boolean.parseBoolean(props.getProperty("captcha"));
    Args.notNull(captchaEnabled, "<captcha>");
    xsrfEnabled = Boolean.parseBoolean(props.getProperty("xsrf"));
    Args.notNull(xsrfEnabled, "<xsrf>");
    String scheme = props.getProperty("scheme");
    Args.notNull(scheme, "<scheme>");
    String host = props.getProperty("host");
    Args.notNull(host, "<host>");
    String port = props.getProperty("port");
    Args.notNull(port, "<port>");
    String rootPath = props.getProperty("rootPath");
    Args.notNull(rootPath, "<rootPath>");
    String useGZIPCompression = props.getProperty("useGZIPCompression");
    Args.notNull(useGZIPCompression, "<useGZIPCompression>");
    maxUploadFileSizeMB = props.getProperty("maxUploadFileSizeMB") == null ? null : Integer.valueOf(props.getProperty("maxUploadFileSizeMB"));
    clientFactory = new SyncopeClientFactoryBean().setAddress(scheme + "://" + host + ":" + port + "/" + rootPath).setContentType(SyncopeClientFactoryBean.ContentType.JSON).setUseCompression(BooleanUtils.toBoolean(useGZIPCompression));
    // read customForm.json
    try (InputStream is = getClass().getResourceAsStream("/" + CUSTOM_FORM_FILE)) {
        customForm = MAPPER.readValue(is, new TypeReference<HashMap<String, CustomAttributesInfo>>() {
        });
        File enduserDir = new File(props.getProperty("enduser.directory"));
        boolean existsEnduserDir = enduserDir.exists() && enduserDir.canRead() && enduserDir.isDirectory();
        if (existsEnduserDir) {
            File customFormFile = FileUtils.getFile(enduserDir, CUSTOM_FORM_FILE);
            if (customFormFile.exists() && customFormFile.canRead() && customFormFile.isFile()) {
                customForm = MAPPER.readValue(FileUtils.openInputStream(customFormFile), new TypeReference<HashMap<String, CustomAttributesInfo>>() {
                });
            }
        }
        FileAlterationObserver observer = existsEnduserDir ? new FileAlterationObserver(enduserDir, pathname -> StringUtils.contains(pathname.getPath(), CUSTOM_FORM_FILE)) : new FileAlterationObserver(getClass().getResource("/" + CUSTOM_FORM_FILE).getFile(), pathname -> StringUtils.contains(pathname.getPath(), CUSTOM_FORM_FILE));
        FileAlterationMonitor monitor = new FileAlterationMonitor(5000);
        FileAlterationListener listener = new FileAlterationListenerAdaptor() {

            @Override
            public void onFileChange(final File file) {
                try {
                    LOG.trace("{} has changed. Reloading form customization configuration.", CUSTOM_FORM_FILE);
                    customForm = MAPPER.readValue(FileUtils.openInputStream(file), new TypeReference<HashMap<String, CustomAttributesInfo>>() {
                    });
                } catch (IOException e) {
                    e.printStackTrace(System.err);
                }
            }

            @Override
            public void onFileCreate(final File file) {
                try {
                    LOG.trace("{} has been created. Loading form customization configuration.", CUSTOM_FORM_FILE);
                    customForm = MAPPER.readValue(FileUtils.openInputStream(file), new TypeReference<HashMap<String, CustomAttributesInfo>>() {
                    });
                } catch (IOException e) {
                    e.printStackTrace(System.err);
                }
            }

            @Override
            public void onFileDelete(final File file) {
                LOG.trace("{} has been deleted. Resetting form customization configuration.", CUSTOM_FORM_FILE);
                customForm = null;
            }
        };
        observer.addListener(listener);
        monitor.addObserver(observer);
        monitor.start();
    } catch (Exception e) {
        throw new WicketRuntimeException("Could not read " + CUSTOM_FORM_FILE, e);
    }
    // mount resources
    ClassPathScanImplementationLookup classPathScanImplementationLookup = (ClassPathScanImplementationLookup) getServletContext().getAttribute(EnduserInitializer.CLASSPATH_LOOKUP);
    for (final Class<? extends AbstractResource> resource : classPathScanImplementationLookup.getResources()) {
        Resource annotation = resource.getAnnotation(Resource.class);
        if (annotation == null) {
            LOG.debug("No @Resource annotation found on {}, ignoring", resource.getName());
        } else {
            try {
                final AbstractResource instance = resource.newInstance();
                mountResource(annotation.path(), new ResourceReference(annotation.key()) {

                    private static final long serialVersionUID = -128426276529456602L;

                    @Override
                    public IResource getResource() {
                        return instance;
                    }
                });
            } catch (Exception e) {
                LOG.error("Could not instantiate {}", resource.getName(), e);
            }
        }
    }
    // mount captcha resource only if captcha is enabled
    if (captchaEnabled) {
        mountResource("/api/captcha", new ResourceReference("captcha") {

            private static final long serialVersionUID = -128426276529456602L;

            @Override
            public IResource getResource() {
                return new CaptchaResource();
            }
        });
    }
}
Also used : Page(org.apache.wicket.Page) FileAlterationObserver(org.apache.commons.io.monitor.FileAlterationObserver) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) BooleanUtils(org.apache.commons.lang3.BooleanUtils) ClassPathScanImplementationLookup(org.apache.syncope.client.enduser.init.ClassPathScanImplementationLookup) SyncopeClientFactoryBean(org.apache.syncope.client.lib.SyncopeClientFactoryBean) StringUtils(org.apache.commons.lang3.StringUtils) FileAlterationListenerAdaptor(org.apache.commons.io.monitor.FileAlterationListenerAdaptor) IResource(org.apache.wicket.request.resource.IResource) Map(java.util.Map) HomePage(org.apache.syncope.client.enduser.pages.HomePage) Response(org.apache.wicket.request.Response) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Request(org.apache.wicket.request.Request) ResourceReference(org.apache.wicket.request.resource.ResourceReference) SyncopeConstants(org.apache.syncope.common.lib.SyncopeConstants) Properties(java.util.Properties) Logger(org.slf4j.Logger) FileAlterationListener(org.apache.commons.io.monitor.FileAlterationListener) FileAlterationMonitor(org.apache.commons.io.monitor.FileAlterationMonitor) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) Args(org.apache.wicket.util.lang.Args) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AbstractResource(org.apache.wicket.request.resource.AbstractResource) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) File(java.io.File) Serializable(java.io.Serializable) PropertyUtils(org.apache.syncope.common.lib.PropertyUtils) WebApplication(org.apache.wicket.protocol.http.WebApplication) CaptchaResource(org.apache.syncope.client.enduser.resources.CaptchaResource) Session(org.apache.wicket.Session) CustomAttributesInfo(org.apache.syncope.client.enduser.model.CustomAttributesInfo) Resource(org.apache.syncope.client.enduser.annotations.Resource) EnduserInitializer(org.apache.syncope.client.enduser.init.EnduserInitializer) InputStream(java.io.InputStream) FileAlterationMonitor(org.apache.commons.io.monitor.FileAlterationMonitor) AbstractResource(org.apache.wicket.request.resource.AbstractResource) CaptchaResource(org.apache.syncope.client.enduser.resources.CaptchaResource) Properties(java.util.Properties) FileAlterationObserver(org.apache.commons.io.monitor.FileAlterationObserver) ClassPathScanImplementationLookup(org.apache.syncope.client.enduser.init.ClassPathScanImplementationLookup) SyncopeClientFactoryBean(org.apache.syncope.client.lib.SyncopeClientFactoryBean) FileAlterationListener(org.apache.commons.io.monitor.FileAlterationListener) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ResourceReference(org.apache.wicket.request.resource.ResourceReference) CustomAttributesInfo(org.apache.syncope.client.enduser.model.CustomAttributesInfo) InputStream(java.io.InputStream) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) IResource(org.apache.wicket.request.resource.IResource) AbstractResource(org.apache.wicket.request.resource.AbstractResource) CaptchaResource(org.apache.syncope.client.enduser.resources.CaptchaResource) Resource(org.apache.syncope.client.enduser.annotations.Resource) IOException(java.io.IOException) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) IOException(java.io.IOException) FileAlterationListenerAdaptor(org.apache.commons.io.monitor.FileAlterationListenerAdaptor) File(java.io.File) IResource(org.apache.wicket.request.resource.IResource)

Example 10 with IResource

use of org.apache.wicket.request.resource.IResource in project wicket by apache.

the class ResourceMapper method addCachingDecoration.

protected void addCachingDecoration(Url url, PageParameters parameters) {
    final List<String> segments = url.getSegments();
    final int lastSegmentAt = segments.size() - 1;
    final String filename = segments.get(lastSegmentAt);
    if (Strings.isEmpty(filename) == false) {
        final IResource resource = resourceReference.getResource();
        if (resource instanceof IStaticCacheableResource) {
            final IStaticCacheableResource cacheable = (IStaticCacheableResource) resource;
            if (cacheable.isCachingEnabled()) {
                final ResourceUrl cacheUrl = new ResourceUrl(filename, parameters);
                getCachingStrategy().decorateUrl(cacheUrl, cacheable);
                if (Strings.isEmpty(cacheUrl.getFileName())) {
                    if (Application.exists() && Application.get().usesDeploymentConfig()) {
                        throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, "caching strategy returned empty name for " + resource);
                    } else {
                        throw new IllegalStateException("caching strategy returned empty name for " + resource);
                    }
                }
                segments.set(lastSegmentAt, cacheUrl.getFileName());
            }
        }
    }
}
Also used : IStaticCacheableResource(org.apache.wicket.request.resource.caching.IStaticCacheableResource) AbortWithHttpErrorCodeException(org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException) IResource(org.apache.wicket.request.resource.IResource) ResourceUrl(org.apache.wicket.request.resource.caching.ResourceUrl)

Aggregations

IResource (org.apache.wicket.request.resource.IResource)24 ResourceReference (org.apache.wicket.request.resource.ResourceReference)10 Test (org.junit.Test)7 AjaxRequestTarget (org.apache.wicket.ajax.AjaxRequestTarget)4 AjaxDownloadBehavior (org.apache.wicket.extensions.ajax.AjaxDownloadBehavior)4 Application (org.apache.wicket.Application)3 ResourceReferenceRequestHandler (org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler)3 IStaticCacheableResource (org.apache.wicket.request.resource.caching.IStaticCacheableResource)3 IOException (java.io.IOException)2 Properties (java.util.Properties)2 Page (org.apache.wicket.Page)2 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)2 PageParameters (org.apache.wicket.request.mapper.parameter.PageParameters)2 PackageResourceReference (org.apache.wicket.request.resource.PackageResourceReference)2 ResourceUrl (org.apache.wicket.request.resource.caching.ResourceUrl)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 BootstrapSettings (de.agilecoders.wicket.core.settings.BootstrapSettings)1 IBootstrapSettings (de.agilecoders.wicket.core.settings.IBootstrapSettings)1 SingleThemeProvider (de.agilecoders.wicket.core.settings.SingleThemeProvider)1