Search in sources :

Example 26 with ResourceReference

use of org.apache.wicket.request.resource.ResourceReference in project sandbox by irof.

the class ImagePage method onInitialize.

@Override
protected void onInitialize() {
    super.onInitialize();
    add(new Image("hoge1", new ResourceReference("hoge") {

        @Override
        public IResource getResource() {
            try {
                URL resource = this.getClass().getClassLoader().getResource("hoge_black.png");
                Path path = Paths.get(resource.toURI());
                byte[] bytes = Files.readAllBytes(path);
                return new ByteArrayResource("application/jpeg", bytes);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }));
}
Also used : Path(java.nio.file.Path) ResourceReference(org.apache.wicket.request.resource.ResourceReference) ByteArrayResource(org.apache.wicket.request.resource.ByteArrayResource) Image(org.apache.wicket.markup.html.image.Image) URL(java.net.URL)

Example 27 with ResourceReference

use of org.apache.wicket.request.resource.ResourceReference in project sandbox by irof.

the class LinkPage method onInitialize.

@Override
protected void onInitialize() {
    super.onInitialize();
    // AbstractLink
    // そのまま使っても面白みのない何も起こらないリンク
    add(new AbstractLink("abstract") {
    });
    // a, link, area タグの時に disableLink を呼ぶと span タグに変わる
    // buttonとかinputだと disabled になる
    add(new AbstractLink("abstract.disable") {

        @Override
        protected void onComponentTag(ComponentTag tag) {
            super.onComponentTag(tag);
            disableLink(tag);
        }
    });
    // 通常のリンク
    add(new Link<Void>("link") {

        @Override
        public void onClick() {
            setResponsePage(LinkPage.class);
        }
    });
    // ブックマーク可能なURLへのリンク
    add(new BookmarkablePageLink<LinkPage>("bookmarkable", LinkPage.class));
    // PageParameterを渡すこともできる
    PageParameters params = new PageParameters();
    params.set("fizz", "buzz");
    add(new BookmarkablePageLink<LinkPage>("bookmarkable.withParams", LinkPage.class, params));
    // ステートレスなリンク
    // ステートレスなPageの場合はこっちを使うか、Link#setStatelessHint が true を返すようにする。
    // 他に通常のリンクとの違ってコンストラクタでモデルを受け取らないけど、 setModel は普通に呼べるぽい。
    // リンク時の
    add(new StatelessLink<Void>("stateless") {

        @Override
        public void onClick() {
            setResponsePage(LinkPage.class);
        }
    });
    // 外部リンクは特筆することはありません。
    add(new ExternalLink("external", "http://irof.hateblo.jp/"));
    // ajax なリンク
    // AjaxRequestTarget で一部を書き換えるのが普通の使い方
    add(new AjaxLink<Void>("ajax") {

        @Override
        public void onClick(AjaxRequestTarget target) {
            labelModel.setObject(labelModel.getObject() + 1);
            target.add(ajaxTarget);
        }
    });
    // onload で javascript:self.close() やるHTMLに行く便利機能。
    add(new PopupCloseLink<Void>("popup"));
    // ファイルを直接ダウンロードさせるリンク
    // ファイルじゃなかったら ResourceLink を使う感じかな。
    add(new DownloadLink("download", getDownloadFile()));
    // 任意のリソースをへのリンク
    // ResourceLink 自体はたいしたことなくて、 ResourceReference が主役ぽい。
    // ResourceReference は実装クラスいっぱい。
    add(new ResourceLink<Void>("resource", new ResourceReference("fuga") {

        @Override
        public IResource getResource() {
            return new ByteArrayResource("text/plain", "piyo".getBytes(StandardCharsets.UTF_8));
        }
    }));
    // 以下ページ情報
    add(new Label("info.request", String.valueOf(getRequest())));
    add(new Label("info.path", String.valueOf(getClassRelativePath())));
    add(new Label("info.id", String.valueOf(getId())));
    add(new Label("info.parameters", String.valueOf(getPageParameters())));
}
Also used : Label(org.apache.wicket.markup.html.basic.Label) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) ByteArrayResource(org.apache.wicket.request.resource.ByteArrayResource) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) ComponentTag(org.apache.wicket.markup.ComponentTag) ResourceReference(org.apache.wicket.request.resource.ResourceReference)

Example 28 with ResourceReference

use of org.apache.wicket.request.resource.ResourceReference 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 29 with ResourceReference

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

the class Image method buildSrcAttribute.

/**
 * Builds the src attribute
 *
 * @param tag
 *            the component tag
 * @return the value of the src attribute
 */
protected String buildSrcAttribute(final ComponentTag tag) {
    final IResource resource = getImageResource();
    if (resource != null) {
        localizedImageResource.setResource(resource);
    }
    final ResourceReference resourceReference = getImageResourceReference();
    if (resourceReference != null) {
        localizedImageResource.setResourceReference(resourceReference);
    }
    localizedImageResource.setSrcAttribute(tag);
    if (shouldAddAntiCacheParameter()) {
        addAntiCacheParameter(tag);
    }
    return tag.getAttribute("src");
}
Also used : ResourceReference(org.apache.wicket.request.resource.ResourceReference) IResource(org.apache.wicket.request.resource.IResource)

Example 30 with ResourceReference

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

the class CoreLibrariesContributor method contributeAjax.

/**
 * Contributes the Ajax backing library plus wicket-event.js and wicket-ajax.js implementations.
 * Additionally if Ajax debug is enabled then wicket-ajax-debug.js implementation is also added.
 *
 * @param application
 *            the application instance
 * @param response
 *            the current header response
 */
public static void contributeAjax(final Application application, final IHeaderResponse response) {
    JavaScriptLibrarySettings jsLibrarySettings = application.getJavaScriptLibrarySettings();
    final DebugSettings debugSettings = application.getDebugSettings();
    if (debugSettings.isAjaxDebugModeEnabled()) {
        response.render(JavaScriptHeaderItem.forReference(jsLibrarySettings.getWicketAjaxDebugReference()));
        response.render(JavaScriptHeaderItem.forScript("Wicket.Ajax.DebugWindow.enabled=true;", "wicket-ajax-debug-enable"));
    } else {
        ResourceReference wicketAjaxReference = jsLibrarySettings.getWicketAjaxReference();
        response.render(JavaScriptHeaderItem.forReference(wicketAjaxReference));
    }
}
Also used : DebugSettings(org.apache.wicket.settings.DebugSettings) JavaScriptLibrarySettings(org.apache.wicket.settings.JavaScriptLibrarySettings) ResourceReference(org.apache.wicket.request.resource.ResourceReference)

Aggregations

ResourceReference (org.apache.wicket.request.resource.ResourceReference)46 IResource (org.apache.wicket.request.resource.IResource)10 PackageResourceReference (org.apache.wicket.request.resource.PackageResourceReference)8 SharedResourceReference (org.apache.wicket.request.resource.SharedResourceReference)7 Test (org.junit.Test)7 ResourceReferenceRequestHandler (org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler)6 HeaderItem (org.apache.wicket.markup.head.HeaderItem)5 Url (org.apache.wicket.request.Url)5 JavaScriptResourceReference (org.apache.wicket.request.resource.JavaScriptResourceReference)5 JavaScriptLibrarySettings (org.apache.wicket.settings.JavaScriptLibrarySettings)5 PageParameters (org.apache.wicket.request.mapper.parameter.PageParameters)4 Page (org.apache.wicket.Page)3 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)3 JavaScriptHeaderItem (org.apache.wicket.markup.head.JavaScriptHeaderItem)3 Image (org.apache.wicket.markup.html.image.Image)3 Properties (java.util.Properties)2 Application (org.apache.wicket.Application)2 StringResourceModel (org.apache.wicket.model.StringResourceModel)2 ByteArrayResource (org.apache.wicket.request.resource.ByteArrayResource)2 CssResourceReference (org.apache.wicket.request.resource.CssResourceReference)2