use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.
the class ModuleManagerImpl method writeConfiguration.
public void writeConfiguration(Element body, List<ModuleConfigurationCallback> callbacks) {
Element element = body.element("script", "type", "text/javascript");
// Build it each time because we don't know if the client supports GZip or not, and
// (in development mode) URLs for some referenced assets could change (due to URLs
// containing a checksum on the resource content).
element.raw(buildRequireJSConfig(callbacks));
}
use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.
the class ModuleManagerImpl method resolveModuleNameToResource.
private Resource resolveModuleNameToResource(String moduleName) {
Resource resource = shimModuleNameToResource.get(moduleName);
if (resource != null) {
return resource;
}
// Tack on a fake extension; otherwise modules whose name includes a '.' get mangled
// by Resource.withExtension().
String baseName = String.format("/META-INF/modules/%s.EXT", moduleName);
Resource baseResource = classpathRoot.forFile(baseName);
for (String extension : extensions) {
resource = baseResource.withExtension(extension);
if (resource.exists()) {
return resource;
}
}
// Return placeholder for null:
return classpathRoot;
}
use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.
the class ModuleManagerImpl method findResourceForModule.
public Resource findResourceForModule(String moduleName) {
Resource resource = cache.get(moduleName);
if (resource == null) {
resource = resolveModuleNameToResource(moduleName);
cache.put(moduleName, resource);
}
return resource == classpathRoot ? null : resource;
}
use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.
the class CSSURLRewriter method replaceURLs.
/**
* Replaces any relative URLs in the content for the resource and returns the content with
* the URLs expanded.
*
* @param input
* content of the resource
* @param baseResource
* resource used to resolve relative URLs
* @return replacement content, or null if no relative URLs in the content
*/
private String replaceURLs(String input, Resource baseResource) {
boolean didReplace = false;
StringBuffer output = new StringBuffer(input.length());
Matcher matcher = urlPattern.matcher(input);
while (matcher.find()) {
// the string inside the quotes
String url = matcher.group(2);
// When the URL starts with a slash or a scheme (e.g. http: or data:) , there's no need
// to rewrite it (this is actually rare in Tapestry as you want to use relative URLs to
// leverage the asset pipeline.
Matcher completeURLMatcher = completeURLPattern.matcher(url);
boolean matchFound = completeURLMatcher.find();
boolean isAssetUrl = matchFound && "asset:".equals(completeURLMatcher.group(1));
if (matchFound && !isAssetUrl) {
String queryParameters = matcher.group(3);
if (queryParameters != null) {
url = url + queryParameters;
}
// This may normalize single quotes, or missing quotes, to double quotes, but is not
// considered a real change, since all such variations are valid.
appendReplacement(matcher, output, url);
continue;
}
if (isAssetUrl) {
// strip away the "asset:" prefix
url = url.substring(6);
}
Asset asset;
// TAP5-2656
try {
asset = assetSource.getAsset(baseResource, url, null);
} catch (AssetNotFoundException e) {
asset = null;
}
if (asset != null) {
String assetURL = asset.toClientURL();
String queryParameters = matcher.group(3);
if (queryParameters != null) {
assetURL += queryParameters;
}
appendReplacement(matcher, output, assetURL);
didReplace = true;
} else {
final String message = String.format("URL %s, referenced in file %s, doesn't exist.", url, baseResource.toURL(), baseResource);
if (strictCssUrlRewriting) {
throw new RuntimeException(message);
} else if (logger.isWarnEnabled()) {
logger.warn(message);
}
}
}
if (!didReplace) {
return null;
}
matcher.appendTail(output);
return output.toString();
}
use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.
the class ClasspathAssetRequestHandler method handleAssetRequest.
public boolean handleAssetRequest(Request request, Response response, String extraPath) throws IOException {
ChecksumPath path = new ChecksumPath(streamer, baseFolder, extraPath);
final boolean handled;
if (classpathAssetProtectionRule.block(path.resourcePath) && !path.resourcePath.equals(ChecksumPath.NON_EXISTING_RESOURCE)) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("Blocked request for classpath asset '" + path.resourcePath + "'. Contribute a new ClasspathAssetProtectionRule if you need this asset to be publicly accessible.");
}
handled = false;
} else {
Resource resource = assetSource.resourceForPath(path.resourcePath);
handled = path.stream(resource);
}
return handled;
}
Aggregations