Search in sources :

Example 31 with ResourceReference

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

the class BasicResourceReferenceMapper method mapHandler.

@Override
public Url mapHandler(IRequestHandler requestHandler) {
    if (requestHandler instanceof ResourceReferenceRequestHandler) {
        ResourceReferenceRequestHandler referenceRequestHandler = (ResourceReferenceRequestHandler) requestHandler;
        ResourceReference reference = referenceRequestHandler.getResourceReference();
        Url url;
        while (reference instanceof ResourceBundleReference) {
            // unwrap the bundle to render the url for the actual reference
            reference = ((ResourceBundleReference) reference).getBundleReference();
        }
        if (reference instanceof MetaInfStaticResourceReference) {
            url = ((MetaInfStaticResourceReference) reference).mapHandler(referenceRequestHandler);
            // if running on Servlet 3.0 engine url is not null
            if (url != null) {
                return url;
            }
        // otherwise it has to be served by the standard wicket way
        }
        if (reference.canBeRegistered()) {
            ResourceReferenceRegistry resourceReferenceRegistry = getContext().getResourceReferenceRegistry();
            resourceReferenceRegistry.registerResourceReference(reference);
        }
        url = new Url();
        List<String> segments = url.getSegments();
        segments.add(getContext().getNamespace());
        segments.add(getContext().getResourceIdentifier());
        segments.add(getClassName(reference.getScope()));
        // setup resource parameters
        PageParameters parameters = new PageParameters(referenceRequestHandler.getPageParameters());
        // need to remove indexed parameters otherwise the URL won't be able to decode
        parameters.clearIndexed();
        ResourceUtil.encodeResourceReferenceAttributes(url, reference);
        StringTokenizer tokens = new StringTokenizer(reference.getName(), "/");
        while (tokens.hasMoreTokens()) {
            String token = tokens.nextToken();
            // on the last component of the resource path
            if (tokens.hasMoreTokens() == false && Strings.isEmpty(token) == false) {
                final IResource resource = reference.getResource();
                // is resource supposed to be cached?
                if (resource instanceof IStaticCacheableResource) {
                    final IStaticCacheableResource cacheable = (IStaticCacheableResource) resource;
                    // is caching enabled?
                    if (cacheable.isCachingEnabled()) {
                        // apply caching scheme to resource url
                        final ResourceUrl resourceUrl = new ResourceUrl(token, parameters);
                        getCachingStrategy().decorateUrl(resourceUrl, cacheable);
                        token = resourceUrl.getFileName();
                        Checks.notEmpty(token, "Caching strategy returned empty name for '%s'", resource);
                    }
                }
            }
            segments.add(token);
        }
        if (parameters.isEmpty() == false) {
            url = encodePageParameters(url, parameters, pageParametersEncoder);
        }
        return url;
    }
    return null;
}
Also used : IStaticCacheableResource(org.apache.wicket.request.resource.caching.IStaticCacheableResource) ResourceReferenceRequestHandler(org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler) ResourceReferenceRegistry(org.apache.wicket.request.resource.ResourceReferenceRegistry) ResourceBundleReference(org.apache.wicket.resource.bundles.ResourceBundleReference) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) ResourceUrl(org.apache.wicket.request.resource.caching.ResourceUrl) Url(org.apache.wicket.request.Url) StringTokenizer(java.util.StringTokenizer) MetaInfStaticResourceReference(org.apache.wicket.request.resource.MetaInfStaticResourceReference) MetaInfStaticResourceReference(org.apache.wicket.request.resource.MetaInfStaticResourceReference) ResourceReference(org.apache.wicket.request.resource.ResourceReference) IResource(org.apache.wicket.request.resource.IResource) ResourceUrl(org.apache.wicket.request.resource.caching.ResourceUrl)

Example 32 with ResourceReference

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

the class OnEventHeaderItem method getDependencies.

@Override
public List<HeaderItem> getDependencies() {
    JavaScriptLibrarySettings ajaxSettings = Application.get().getJavaScriptLibrarySettings();
    ResourceReference wicketAjaxReference = ajaxSettings.getWicketAjaxReference();
    List<HeaderItem> dependencies = super.getDependencies();
    dependencies.add(JavaScriptHeaderItem.forReference(wicketAjaxReference));
    return dependencies;
}
Also used : JavaScriptLibrarySettings(org.apache.wicket.settings.JavaScriptLibrarySettings) ResourceReference(org.apache.wicket.request.resource.ResourceReference)

Example 33 with ResourceReference

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

the class OnLoadHeaderItem method getDependencies.

@Override
public List<HeaderItem> getDependencies() {
    JavaScriptLibrarySettings ajaxSettings = Application.get().getJavaScriptLibrarySettings();
    ResourceReference wicketAjaxReference = ajaxSettings.getWicketAjaxReference();
    List<HeaderItem> dependencies = super.getDependencies();
    dependencies.add(JavaScriptHeaderItem.forReference(wicketAjaxReference));
    return dependencies;
}
Also used : JavaScriptLibrarySettings(org.apache.wicket.settings.JavaScriptLibrarySettings) ResourceReference(org.apache.wicket.request.resource.ResourceReference)

Example 34 with ResourceReference

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

the class ResourceAggregator method preserveCssDetails.

/**
 * Preserves the resource reference details for CSS resource replacements.
 *
 * For example if CSS resource with media <em>screen</em> is replaced with
 * {@link org.apache.wicket.protocol.http.WebApplication#addResourceReplacement(org.apache.wicket.request.resource.CssResourceReference, org.apache.wicket.request.resource.ResourceReference)} then the replacement will
 * will inherit the media attribute
 *
 * @param item   The replaced header item
 * @param bundle The bundle that represents the replacement
 * @return the bundle with the preserved details
 */
protected HeaderItem preserveCssDetails(CssReferenceHeaderItem item, CssReferenceHeaderItem bundle) {
    HeaderItem resultBundle;
    ResourceReference bundleReference = bundle.getReference();
    if (bundleReference instanceof ReplacementResourceBundleReference) {
        resultBundle = CssHeaderItem.forReference(bundleReference, item.getPageParameters(), item.getMedia(), item.getCondition());
    } else {
        resultBundle = bundle;
    }
    return resultBundle;
}
Also used : ReplacementResourceBundleReference(org.apache.wicket.resource.bundles.ReplacementResourceBundleReference) ResourceReference(org.apache.wicket.request.resource.ResourceReference)

Example 35 with ResourceReference

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

the class LocalizedImageResource method newImage.

/**
 * Generates an image resource based on the attribute values on tag
 *
 * @param value
 *            The value to parse
 */
private void newImage(final CharSequence value) {
    // Parse value
    final ImageValueParser valueParser = new ImageValueParser(value);
    // Does value match parser?
    if (valueParser.matches()) {
        final String imageReferenceName = valueParser.getImageReferenceName();
        final String specification = Strings.replaceHtmlEscapeNumber(valueParser.getSpecification());
        final String factoryName = valueParser.getFactoryName();
        final Application application = component.getApplication();
        // Do we have a reference?
        if (!Strings.isEmpty(imageReferenceName)) {
            // Is resource already available via the application?
            if (application.getResourceReferenceRegistry().getResourceReference(Application.class, imageReferenceName, locale, style, variation, true, false) == null) {
                // Resource not available yet, so create it with factory and
                // share via Application
                final IResource imageResource = getResourceFactory(application, factoryName).newResource(specification, locale, style, variation);
                ResourceReference ref = new SimpleStaticResourceReference(Application.class, imageReferenceName, locale, style, variation, imageResource);
                application.getResourceReferenceRegistry().registerResourceReference(ref);
            }
            // Create resource reference
            resourceReference = new PackageResourceReference(Application.class, imageReferenceName, locale, style, variation);
        } else {
            resource = getResourceFactory(application, factoryName).newResource(specification, locale, style, variation);
        }
    } else {
        throw new WicketRuntimeException("Could not generate image for value attribute '" + value + "'.  Was expecting a value attribute of the form \"[resourceFactoryName]:[resourceReferenceName]?:[factorySpecification]\".");
    }
}
Also used : PackageResourceReference(org.apache.wicket.request.resource.PackageResourceReference) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) PackageResourceReference(org.apache.wicket.request.resource.PackageResourceReference) ResourceReference(org.apache.wicket.request.resource.ResourceReference) Application(org.apache.wicket.Application) IResource(org.apache.wicket.request.resource.IResource)

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