Search in sources :

Example 11 with Application

use of org.apache.wicket.Application in project wicket by apache.

the class WicketFilterTest method notModifiedResponseIncludesExpiresHeader.

/**
 * @throws IOException
 * @throws ServletException
 * @throws ParseException
 */
@Test
public void notModifiedResponseIncludesExpiresHeader() throws IOException, ServletException, ParseException {
    try {
        application = new MockApplication();
        WicketFilter filter = new WicketFilter();
        filter.init(new FilterTestingConfig());
        ThreadContext.setApplication(application);
        DynamicImageResource resource = new DynamicImageResource() {

            private static final long serialVersionUID = 1L;

            @Override
            protected byte[] getImageData(Attributes attributes) {
                throw new UnsupportedOperationException("Not implemented");
            }

            @Override
            protected ResourceResponse newResourceResponse(Attributes attributes) {
                ResourceResponse response = super.newResourceResponse(attributes);
                response.setCacheDurationToMaximum();
                return response;
            }
        };
        application.getSharedResources().add("foo.gif", resource);
        MockHttpServletRequest request = new MockHttpServletRequest(application, null, null);
        request.setURL(request.getContextPath() + request.getServletPath() + "/wicket/resource/" + Application.class.getName() + "/foo.gif");
        setIfModifiedSinceToNextWeek(request);
        MockHttpServletResponse response = new MockHttpServletResponse(request);
        filter.doFilter(request, response, new FilterChain() {

            @Override
            public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            }
        });
        assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getStatus());
        String responseExpiresHeader = response.getHeader("Expires");
        assertNotNull("Expires header must be set on not modified response", responseExpiresHeader);
        Date responseExpires = headerDateFormat.parse(responseExpiresHeader);
        assertTrue("Expected later than current date but was " + responseExpires, responseExpires.after(new Date()));
    } finally {
        ThreadContext.detach();
    }
}
Also used : DynamicImageResource(org.apache.wicket.request.resource.DynamicImageResource) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockApplication(org.apache.wicket.mock.MockApplication) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) IOException(java.io.IOException) Date(java.util.Date) ServletException(javax.servlet.ServletException) MockApplication(org.apache.wicket.mock.MockApplication) Application(org.apache.wicket.Application) MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse) Test(org.junit.Test)

Example 12 with Application

use of org.apache.wicket.Application in project wicket by apache.

the class WicketMetrics method getSettings.

/**
 * Gets the wicket metrics settings
 *
 * @return the wicket metrics settings
 */
public static synchronized WicketMetricsSettings getSettings() {
    Application application = Application.get(getFilterName());
    if (application == null) {
        throw new WicketRuntimeException(APPLICATION_ERROR);
    }
    WicketMetricsSettings wicketMetricsSettings = application.getMetaData(METRIC_SETTINGS);
    if (wicketMetricsSettings == null) {
        wicketMetricsSettings = new WicketMetricsSettings();
        wicketMetricsSettings.setPrefix(getFilterName());
        application.setMetaData(METRIC_SETTINGS, wicketMetricsSettings);
    }
    return wicketMetricsSettings;
}
Also used : WicketRuntimeException(org.apache.wicket.WicketRuntimeException) Application(org.apache.wicket.Application)

Example 13 with Application

use of org.apache.wicket.Application in project wicket by apache.

the class WicketMetrics method getMetricRegistry.

/**
 * Gets the metric registry
 *
 * @return the metric registry
 */
public static synchronized MetricRegistry getMetricRegistry() {
    Application application = Application.get(getFilterName());
    if (application == null) {
        throw new WicketRuntimeException(APPLICATION_ERROR);
    }
    MetricRegistry metricRegistry = application.getMetaData(METRIC_REGISTRY);
    if (metricRegistry == null) {
        metricRegistry = new MetricRegistry();
        application.setMetaData(METRIC_REGISTRY, metricRegistry);
    }
    return metricRegistry;
}
Also used : WicketRuntimeException(org.apache.wicket.WicketRuntimeException) MetricRegistry(com.codahale.metrics.MetricRegistry) Application(org.apache.wicket.Application)

Example 14 with Application

use of org.apache.wicket.Application in project wicket by apache.

the class ComponentRenderer method renderPage.

/**
 * Collects the Html generated by the rendering a page.
 * <p>
 * Important note: Must be called on a thread bound to an application's {@link ThreadContext}!
 *
 * @param pageProvider
 *            the provider of the page class/instance and its parameters
 * @return the html rendered by a page
 *
 * @see ThreadContext
 */
public static CharSequence renderPage(final PageProvider pageProvider) {
    Application application = Application.get();
    RequestCycle originalRequestCycle = RequestCycle.get();
    BufferedWebResponse tempResponse = new BufferedWebResponse(null);
    RequestCycle tempRequestCycle = application.createRequestCycle(originalRequestCycle.getRequest(), tempResponse);
    try {
        ThreadContext.setRequestCycle(tempRequestCycle);
        pageProvider.getPageInstance().renderPage();
    } finally {
        ThreadContext.setRequestCycle(originalRequestCycle);
    }
    return tempResponse.getText();
}
Also used : BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) MockApplication(org.apache.wicket.mock.MockApplication) Application(org.apache.wicket.Application) WebApplication(org.apache.wicket.protocol.http.WebApplication)

Example 15 with Application

use of org.apache.wicket.Application 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

Application (org.apache.wicket.Application)21 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)5 WebApplication (org.apache.wicket.protocol.http.WebApplication)5 IOException (java.io.IOException)4 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)4 MockApplication (org.apache.wicket.mock.MockApplication)3 MockHttpServletRequest (org.apache.wicket.protocol.http.mock.MockHttpServletRequest)3 FilterChain (javax.servlet.FilterChain)2 ServletException (javax.servlet.ServletException)2 ServletRequest (javax.servlet.ServletRequest)2 ServletResponse (javax.servlet.ServletResponse)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpSession (javax.servlet.http.HttpSession)2 IApplication (org.apache.openmeetings.IApplication)2 Page (org.apache.wicket.Page)2 WebSocketSettings (org.apache.wicket.protocol.ws.WebSocketSettings)2 IKey (org.apache.wicket.protocol.ws.api.registry.IKey)2 IRequestHandler (org.apache.wicket.request.IRequestHandler)2 WebResponse (org.apache.wicket.request.http.WebResponse)2