Search in sources :

Example 36 with ResourceReferenceRequestHandler

use of org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler in project wicket by apache.

the class CryptoMapperTest method resourceReferenceWithMoreSegments.

/**
 * Relative ResourceReferences, WICKET-3514
 */
@Test
public void resourceReferenceWithMoreSegments() {
    PackageResourceReference resource = new PackageResourceReference(getClass(), "crypt/crypt.txt");
    Url url = mapper.mapHandler(new ResourceReferenceRequestHandler(resource));
    url.getSegments().remove(url.getSegments().size() - 1);
    url.getSegments().add("more");
    url.getSegments().add("more-crypt.txt");
    Request request = getRequest(url);
    IRequestHandler requestHandler = mapper.mapRequest(request);
    assertNotNull(requestHandler);
    requestHandler = unwrapRequestHandlerDelegate(requestHandler);
    ResourceReferenceRequestHandler handler = (ResourceReferenceRequestHandler) requestHandler;
    assertEquals(getClass(), handler.getResourceReference().getScope());
    assertEquals("crypt/more/more-crypt.txt", handler.getResourceReference().getName());
}
Also used : ResourceReferenceRequestHandler(org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler) IRequestHandler(org.apache.wicket.request.IRequestHandler) PackageResourceReference(org.apache.wicket.request.resource.PackageResourceReference) Request(org.apache.wicket.request.Request) Url(org.apache.wicket.request.Url) Test(org.junit.Test)

Example 37 with ResourceReferenceRequestHandler

use of org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler in project wicket by apache.

the class AjaxDownloadBehavior method initiate.

/**
 * Call this method to initiate the download.
 *
 * @param target
 *            the initiating Ajax target
 */
public void initiate(AjaxRequestTarget target) {
    if (getComponent() == null) {
        throw new WicketRuntimeException("not bound to a component");
    }
    ((WebResponse) RequestCycle.get().getResponse()).clearCookie(cookie(getName()));
    CharSequence url;
    if (resourceBehavior == null) {
        if (resourceReference.canBeRegistered()) {
            getComponent().getApplication().getResourceReferenceRegistry().registerResourceReference(resourceReference);
        }
        PageParameters parameters = new PageParameters();
        if (resourceParameters != null) {
            parameters.mergeWith(resourceParameters);
        }
        parameters.set(RESOURCE_PARAMETER_NAME, getName());
        url = getComponent().getRequestCycle().urlFor(new ResourceReferenceRequestHandler(resourceReference, parameters));
    } else {
        url = resourceBehavior.getUrl();
    }
    JSONObject settings = new JSONObject();
    settings.put("attributes", new JSONFunction(renderAjaxAttributes(getComponent())));
    settings.put("name", getName());
    settings.put("downloadUrl", url);
    settings.put("method", getLocation().name().toLowerCase(Locale.ENGLISH));
    target.appendJavaScript(String.format("Wicket.AjaxDownload.initiate(%s);", settings));
    onBeforeDownload(target);
}
Also used : ResourceReferenceRequestHandler(org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler) WebResponse(org.apache.wicket.request.http.WebResponse) JSONObject(com.github.openjson.JSONObject) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) JSONFunction(org.apache.wicket.ajax.json.JSONFunction)

Example 38 with ResourceReferenceRequestHandler

use of org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler 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 39 with ResourceReferenceRequestHandler

use of org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler in project wicket by apache.

the class RequestCycle method renderUrl.

private String renderUrl(Url url, IRequestHandler handler) {
    if (url != null) {
        boolean shouldEncodeStaticResource = Application.exists() && Application.get().getResourceSettings().isEncodeJSessionId();
        String renderedUrl = getUrlRenderer().renderUrl(url);
        if (handler instanceof ResourceReferenceRequestHandler) {
            ResourceReferenceRequestHandler rrrh = (ResourceReferenceRequestHandler) handler;
            IResource resource = rrrh.getResource();
            if (resource != null && !(resource instanceof IStaticCacheableResource) || shouldEncodeStaticResource) {
                renderedUrl = getOriginalResponse().encodeURL(renderedUrl);
            }
        } else if (handler instanceof ResourceRequestHandler) {
            ResourceRequestHandler rrh = (ResourceRequestHandler) handler;
            IResource resource = rrh.getResource();
            if (resource != null && !(resource instanceof IStaticCacheableResource) || shouldEncodeStaticResource) {
                renderedUrl = getOriginalResponse().encodeURL(renderedUrl);
            }
        } else {
            renderedUrl = getOriginalResponse().encodeURL(renderedUrl);
        }
        return renderedUrl;
    } else {
        return null;
    }
}
Also used : IStaticCacheableResource(org.apache.wicket.request.resource.caching.IStaticCacheableResource) ResourceReferenceRequestHandler(org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler) ResourceRequestHandler(org.apache.wicket.request.handler.resource.ResourceRequestHandler) IResource(org.apache.wicket.request.resource.IResource)

Example 40 with ResourceReferenceRequestHandler

use of org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler in project wicket by apache.

the class RequestCycleUrlForTest method urlForResourceReferenceWithNonStaticResource.

/**
 * ResourceReference with non-IStaticCacheableResource should not have the jsessionid encoded in the url
 *
 * @throws Exception
 */
@Test
public void urlForResourceReferenceWithNonStaticResource() throws Exception {
    final IResource resource = mock(IResource.class);
    ResourceReference reference = new ResourceReference("dummy") {

        @Override
        public IResource getResource() {
            return resource;
        }
    };
    ResourceReferenceRequestHandler handler = new ResourceReferenceRequestHandler(reference);
    CharSequence url = requestCycle.urlFor(handler);
    assertEquals("./" + RES_REF_URL + JSESSIONID, url);
}
Also used : ResourceReferenceRequestHandler(org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler) ResourceReference(org.apache.wicket.request.resource.ResourceReference) IResource(org.apache.wicket.request.resource.IResource) Test(org.junit.Test)

Aggregations

ResourceReferenceRequestHandler (org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler)41 Url (org.apache.wicket.request.Url)33 Test (org.junit.Test)31 ResourceUrl (org.apache.wicket.request.resource.caching.ResourceUrl)26 IRequestHandler (org.apache.wicket.request.IRequestHandler)20 PageParameters (org.apache.wicket.request.mapper.parameter.PageParameters)9 PackageResourceReference (org.apache.wicket.request.resource.PackageResourceReference)6 ResourceReference (org.apache.wicket.request.resource.ResourceReference)6 Request (org.apache.wicket.request.Request)5 Locale (java.util.Locale)3 IResource (org.apache.wicket.request.resource.IResource)3 IStaticCacheableResource (org.apache.wicket.request.resource.caching.IStaticCacheableResource)3 ContextRelativeResource (org.apache.wicket.request.resource.ContextRelativeResource)2 MetaInfStaticResourceReference (org.apache.wicket.request.resource.MetaInfStaticResourceReference)2 UrlResourceReference (org.apache.wicket.request.resource.UrlResourceReference)2 ResourceBundleReference (org.apache.wicket.resource.bundles.ResourceBundleReference)2 JSONObject (com.github.openjson.JSONObject)1 StringTokenizer (java.util.StringTokenizer)1 Conversation (javax.enterprise.context.Conversation)1 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1