Search in sources :

Example 1 with Context

use of org.apache.tapestry5.http.services.Context in project flowlogix by flowlogix.

the class PeriodicUpdater method afterRender.

@AfterRender
void afterRender() {
    final String id = zone.getClientId();
    Link link;
    if (context == null) {
        link = resources.createEventLink(event);
    } else {
        link = resources.createEventLink(event, context);
    }
    final JSONObject spec = new JSONObject();
    spec.put("period", period);
    spec.put("elementId", id);
    spec.put("uri", link.toAbsoluteURI());
    jsSupport.addInitializerCall("periodicUpdater", spec);
}
Also used : JSONObject(org.apache.tapestry5.json.JSONObject) Link(org.apache.tapestry5.Link) AfterRender(org.apache.tapestry5.annotations.AfterRender)

Example 2 with Context

use of org.apache.tapestry5.http.services.Context in project tapestry-5 by apache.

the class ResourceStreamerImpl method streamResource.

public boolean streamResource(Resource resource, StreamableResource streamable, String providedChecksum, Set<Options> options) throws IOException {
    assert streamable != null;
    assert providedChecksum != null;
    assert options != null;
    String actualChecksum = streamable.getChecksum();
    if (providedChecksum.length() > 0 && !providedChecksum.equals(actualChecksum)) {
        // TAP5-2185: Trying to find the wrongly-checksummed resource in the classpath and context,
        // so we can create an Asset with the correct checksum and redirect to it.
        Asset asset = null;
        if (resource != null) {
            asset = findAssetInsideWebapp(resource);
        }
        if (asset != null) {
            response.sendRedirect(asset.toClientURL());
            return true;
        }
        return false;
    }
    // ETag should be surrounded with quotes.
    String token = QUOTE + actualChecksum + QUOTE;
    // Even when sending a 304, we want the ETag associated with the request.
    // In most cases (except JavaScript modules), the checksum is also embedded into the URL.
    // However, E-Tags are also useful for enabling caching inside intermediate servers, CDNs, etc.
    response.setHeader("ETag", token);
    // If the client can send the correct ETag token, then its cache already contains the correct
    // content.
    String providedToken = request.getHeader("If-None-Match");
    if (token.equals(providedToken)) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return true;
    }
    long lastModified = streamable.getLastModified();
    long ifModifiedSince;
    try {
        ifModifiedSince = request.getDateHeader(IF_MODIFIED_SINCE_HEADER);
    } catch (IllegalArgumentException ex) {
        // Simulate the header being missing if it is poorly formatted.
        ifModifiedSince = -1;
    }
    if (ifModifiedSince > 0 && ifModifiedSince >= lastModified) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return true;
    }
    // Prevent the upstream code from compressing when we don't want to.
    response.disableCompression();
    response.setDateHeader("Last-Modified", lastModified);
    if (productionMode && !options.contains(Options.OMIT_EXPIRATION)) {
        // Starting in 5.4, this is a lot less necessary; any change to a Resource will result
        // in a new asset URL with the changed checksum incorporated into the URL.
        response.setDateHeader("Expires", lastModified + InternalConstants.TEN_YEARS);
    }
    // mostly result in quick SC_NOT_MODIFIED responses.
    if (options.contains(Options.OMIT_EXPIRATION)) {
        response.setHeader("Cache-Control", omitExpirationCacheControlHeader);
    }
    if (streamable.getCompression() == CompressionStatus.COMPRESSED) {
        response.setHeader(TapestryHttpInternalConstants.CONTENT_ENCODING_HEADER, TapestryHttpInternalConstants.GZIP_CONTENT_ENCODING);
    }
    ResponseCustomizer responseCustomizer = streamable.getResponseCustomizer();
    if (responseCustomizer != null) {
        responseCustomizer.customizeResponse(streamable, response);
    }
    if (!request.getMethod().equals("HEAD")) {
        response.setContentLength(streamable.getSize());
        OutputStream os = response.getOutputStream(streamable.getContentType().toString());
        streamable.streamTo(os);
        os.close();
    }
    return true;
}
Also used : OutputStream(java.io.OutputStream) Asset(org.apache.tapestry5.Asset)

Example 3 with Context

use of org.apache.tapestry5.http.services.Context in project tapestry-5 by apache.

the class ComponentEventImplTest method matches_on_event_type.

@Test
public void matches_on_event_type() {
    ComponentEventCallback handler = mockComponentEventHandler();
    EventContext context = mockEventContext();
    Logger logger = mockLogger();
    ComponentModel model = mockComponentModel();
    train_isDebugEnabled(logger, false);
    train_getCount(context, 0);
    replay();
    ComponentEvent event = new ComponentEventImpl("eventType", "someId", context, handler, null, false, model, logger);
    assertTrue(event.matches("eventType", "someId", 0));
    assertFalse(event.matches("foo", "someId", 0));
    verify();
}
Also used : EventContext(org.apache.tapestry5.EventContext) ComponentModel(org.apache.tapestry5.model.ComponentModel) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Logger(org.slf4j.Logger) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback) Test(org.testng.annotations.Test)

Example 4 with Context

use of org.apache.tapestry5.http.services.Context in project tapestry-5 by apache.

the class ComponentEventImplTest method matches_on_component_id.

@Test
public void matches_on_component_id() {
    ComponentEventCallback handler = mockComponentEventHandler();
    EventContext context = mockEventContext();
    Logger logger = mockLogger();
    ComponentModel model = mockComponentModel();
    train_isDebugEnabled(logger, false);
    train_getCount(context, 0);
    replay();
    ComponentEvent event = new ComponentEventImpl("eventType", "someId", context, handler, null, false, model, logger);
    assertTrue(event.matches("eventType", "someId", 0));
    assertFalse(event.matches("eventtype", "bar", 0));
    verify();
}
Also used : EventContext(org.apache.tapestry5.EventContext) ComponentModel(org.apache.tapestry5.model.ComponentModel) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Logger(org.slf4j.Logger) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback) Test(org.testng.annotations.Test)

Example 5 with Context

use of org.apache.tapestry5.http.services.Context in project tapestry-5 by apache.

the class ComponentEventImplTest method coerce_context.

@Test
public void coerce_context() {
    ComponentEventCallback handler = mockComponentEventHandler();
    ComponentPageElementResources resources = mockComponentPageElementResources();
    EventContext context = mockEventContext();
    Integer value = new Integer(27);
    Logger logger = mockLogger();
    ComponentModel model = mockComponentModel();
    train_isDebugEnabled(logger, false);
    train_toClass(resources, "java.lang.Integer", Integer.class);
    train_getCount(context, 2);
    train_get(context, Integer.class, 0, value);
    replay();
    ComponentEvent event = new ComponentEventImpl("eventType", "someId", context, handler, resources, false, model, logger);
    assertSame(event.coerceContext(0, "java.lang.Integer"), value);
    verify();
}
Also used : EventContext(org.apache.tapestry5.EventContext) ComponentModel(org.apache.tapestry5.model.ComponentModel) ComponentPageElementResources(org.apache.tapestry5.internal.structure.ComponentPageElementResources) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Logger(org.slf4j.Logger) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)29 Context (org.apache.tapestry5.http.services.Context)18 Link (org.apache.tapestry5.http.Link)16 EventContext (org.apache.tapestry5.EventContext)12 ComponentEventCallback (org.apache.tapestry5.ComponentEventCallback)9 Request (org.apache.tapestry5.http.services.Request)9 Response (org.apache.tapestry5.http.services.Response)9 ComponentModel (org.apache.tapestry5.model.ComponentModel)9 Logger (org.slf4j.Logger)9 RequestFilter (org.apache.tapestry5.http.services.RequestFilter)8 RequestHandler (org.apache.tapestry5.http.services.RequestHandler)8 ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)8 ServletContext (javax.servlet.ServletContext)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 PageRenderRequestParameters (org.apache.tapestry5.services.PageRenderRequestParameters)7 ComponentResources (org.apache.tapestry5.ComponentResources)6 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)6 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)6 JSONObject (org.apache.tapestry5.json.JSONObject)6 LinkCreationListener2 (org.apache.tapestry5.services.LinkCreationListener2)5