Search in sources :

Example 41 with ResourceReference

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

the class Home method getImage5Resource.

/**
 * @return Gets shared image component
 */
public ResourceReference getImage5Resource() {
    return new ResourceReference(Home.class, "image5") {

        @Override
        public IResource getResource() {
            final BufferedDynamicImageResource resource = new BufferedDynamicImageResource();
            final BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
            drawCircle((Graphics2D) image.getGraphics());
            resource.setImage(image);
            return resource;
        }
    };
}
Also used : SharedResourceReference(org.apache.wicket.request.resource.SharedResourceReference) PackageResourceReference(org.apache.wicket.request.resource.PackageResourceReference) ResourceReference(org.apache.wicket.request.resource.ResourceReference) BufferedDynamicImageResource(org.apache.wicket.markup.html.image.resource.BufferedDynamicImageResource) BufferedImage(java.awt.image.BufferedImage)

Example 42 with ResourceReference

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

Example 43 with ResourceReference

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

the class PushHeaderItem method push.

/**
 * Creates a URL and pushes the resource to the client - this is only supported if http2 is
 * enabled
 *
 * @param pushItems
 *            a list of items to be pushed to the client
 * @return the current push header item
 */
@SuppressWarnings("unchecked")
public PushHeaderItem push(List<PushItem> pushItems) {
    RequestCycle requestCycle = RequestCycle.get();
    if (isHttp2(getContainerRequest(requestCycle.getRequest())))
        for (PushItem pushItem : pushItems) {
            Object object = pushItem.getObject();
            PageParameters parameters = pushItem.getPageParameters();
            if (object == null) {
                throw new WicketRuntimeException("Please provide an object to the items to be pushed, so that the url can be created for the given resource.");
            }
            CharSequence url = null;
            if (object instanceof ResourceReference) {
                url = requestCycle.urlFor((ResourceReference) object, parameters);
            } else if (Page.class.isAssignableFrom(object.getClass())) {
                url = requestCycle.urlFor((Class<? extends Page>) object, parameters);
            } else if (object instanceof IRequestHandler) {
                url = requestCycle.urlFor((IRequestHandler) object);
            } else if (pushItem.getUrl() != null) {
                url = pushItem.getUrl();
            } else {
                Url encoded = new PageParametersEncoder().encodePageParameters(parameters);
                String queryString = encoded.getQueryString();
                url = object.toString() + (queryString != null ? "?" + queryString : "");
            }
            if (url.toString().equals(".")) {
                url = "/";
            } else if (url.toString().startsWith(".")) {
                url = url.toString().substring(1);
            }
            // The context path and the filter have to be applied to the URL, because otherwise
            // the resource is not pushed correctly
            StringBuilder partialUrl = new StringBuilder();
            String contextPath = WebApplication.get().getServletContext().getContextPath();
            partialUrl.append(contextPath);
            if (!"/".equals(contextPath)) {
                partialUrl.append('/');
            }
            String filterPath = WebApplication.get().getWicketFilter().getFilterPath();
            if ("/".equals(filterPath)) {
                filterPath = "";
            } else if (filterPath.endsWith("/")) {
                filterPath = filterPath.substring(0, filterPath.length() - 1);
            }
            partialUrl.append(filterPath);
            partialUrl.append(url.toString());
            // Set the url the resource is going to be pushed with
            pushItem.setUrl(partialUrl.toString());
            // Apply the push item to be used during the push process
            this.pushItems.add(pushItem);
        }
    return this;
}
Also used : IRequestHandler(org.apache.wicket.request.IRequestHandler) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) Page(org.apache.wicket.Page) WebPage(org.apache.wicket.markup.html.WebPage) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) Url(org.apache.wicket.request.Url) ResourceReference(org.apache.wicket.request.resource.ResourceReference) PageParametersEncoder(org.apache.wicket.request.mapper.parameter.PageParametersEncoder)

Example 44 with ResourceReference

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

the class WicketWebSocketJQueryResourceReference method getDependencies.

@Override
public List<HeaderItem> getDependencies() {
    final ResourceReference wicketAjaxReference;
    if (Application.exists()) {
        wicketAjaxReference = Application.get().getJavaScriptLibrarySettings().getWicketAjaxReference();
    } else {
        wicketAjaxReference = WicketAjaxJQueryResourceReference.get();
    }
    List<HeaderItem> dependencies = super.getDependencies();
    dependencies.add(JavaScriptHeaderItem.forReference(wicketAjaxReference));
    return dependencies;
}
Also used : JavaScriptResourceReference(org.apache.wicket.request.resource.JavaScriptResourceReference) WicketAjaxJQueryResourceReference(org.apache.wicket.ajax.WicketAjaxJQueryResourceReference) ResourceReference(org.apache.wicket.request.resource.ResourceReference) HeaderItem(org.apache.wicket.markup.head.HeaderItem) JavaScriptHeaderItem(org.apache.wicket.markup.head.JavaScriptHeaderItem)

Example 45 with ResourceReference

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

the class WebSocketMessageBroadcastHandler method respond.

@Override
public void respond(IRequestCycle requestCycle) {
    final Application application = Application.get();
    final Runnable action = new Runnable() {

        @Override
        public void run() {
            if (pageId != AbstractWebSocketProcessor.NO_PAGE_ID) {
                Page page = (Page) Session.get().getPageManager().getPage(pageId);
                page.send(application, Broadcast.BREADTH, payload);
            } else {
                ResourceReference reference = new SharedResourceReference(resourceName);
                IResource resource = reference.getResource();
                if (resource instanceof WebSocketResource) {
                    WebSocketResource wsResource = (WebSocketResource) resource;
                    wsResource.onPayload(payload);
                } else {
                    throw new IllegalStateException(String.format("Shared resource with name '%s' is not a %s but %s", resourceName, WebSocketResource.class.getSimpleName(), Classes.name(resource.getClass())));
                }
            }
        }
    };
    WebSocketSettings webSocketSettings = WebSocketSettings.Holder.get(application);
    webSocketSettings.getSendPayloadExecutor().run(action);
}
Also used : SharedResourceReference(org.apache.wicket.request.resource.SharedResourceReference) Page(org.apache.wicket.Page) WebSocketSettings(org.apache.wicket.protocol.ws.WebSocketSettings) SharedResourceReference(org.apache.wicket.request.resource.SharedResourceReference) 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