use of org.apache.tapestry5.commons.Resource 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;
}
use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.
the class MutableComponentModelImplTest method get_mixin_class_names_mixes_with_parent_model.
@Test
public void get_mixin_class_names_mixes_with_parent_model() {
Resource r = mockResource();
Logger logger = mockLogger();
replay();
MutableComponentModel parent = new MutableComponentModelImpl(CLASS_NAME, logger, r, null, false, null);
parent.addMixinClassName("Wilma");
MutableComponentModel child = new MutableComponentModelImpl(CLASS_NAME, logger, r, parent, false, null);
child.addMixinClassName("Fred");
child.addMixinClassName("Barney");
assertEquals(child.getMixinClassNames(), Arrays.asList("Barney", "Fred", "Wilma"));
verify();
}
use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.
the class MutableComponentModelImplTest method parent_handles_render_phase.
/**
* @since 5.0.19
*/
@Test
public void parent_handles_render_phase() {
Resource r = mockResource();
Logger logger = mockLogger();
replay();
MutableComponentModel parent = new MutableComponentModelImpl(CLASS_NAME, logger, r, null, false, null);
MutableComponentModel child = new MutableComponentModelImpl(CLASS_NAME, logger, r, parent, false, null);
parent.addRenderPhase(BeginRender.class);
assertTrue(child.getHandledRenderPhases().contains(BeginRender.class));
verify();
}
use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.
the class MutableComponentModelImplTest method add_duplicate_parameters_to_embedded.
@Test
public void add_duplicate_parameters_to_embedded() {
Resource r = mockResource();
Logger logger = mockLogger();
replay();
MutableComponentModel model = new MutableComponentModelImpl(CLASS_NAME, logger, r, null, false, null);
MutableEmbeddedComponentModel fred = model.addEmbeddedComponent("fred", "Fred", COMPONENT_CLASS_NAME, false, null);
fred.addParameter("city", "bedrock");
try {
fred.addParameter("city", "slateville");
unreachable();
} catch (IllegalArgumentException ex) {
assertEquals(ex.getMessage(), "A value for parameter 'city' of embedded component fred (of component class org.example.components.Foo) has already been provided.");
}
verify();
}
use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.
the class MutableComponentModelImplTest method no_persistence_defined_for_field.
@Test
public void no_persistence_defined_for_field() {
Resource r = mockResource();
Logger logger = mockLogger();
replay();
MutableComponentModel model = new MutableComponentModelImpl(CLASS_NAME, logger, r, null, false, null);
try {
model.getFieldPersistenceStrategy("someField");
unreachable();
} catch (IllegalArgumentException ex) {
assertEquals(ex.getMessage(), "No field persistence strategy has been defined for field \'someField\'.");
}
verify();
}
Aggregations