Search in sources :

Example 36 with Resource

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));
}
Also used : Element(org.apache.tapestry5.dom.Element)

Example 37 with Resource

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;
}
Also used : Resource(org.apache.tapestry5.commons.Resource)

Example 38 with Resource

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;
}
Also used : Resource(org.apache.tapestry5.commons.Resource)

Example 39 with 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();
}
Also used : Matcher(java.util.regex.Matcher) Asset(org.apache.tapestry5.Asset) AssetNotFoundException(org.apache.tapestry5.services.AssetNotFoundException)

Example 40 with Resource

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;
}
Also used : Resource(org.apache.tapestry5.commons.Resource)

Aggregations

Resource (org.apache.tapestry5.commons.Resource)78 Test (org.testng.annotations.Test)62 MutableComponentModel (org.apache.tapestry5.model.MutableComponentModel)38 Logger (org.slf4j.Logger)38 ClasspathResource (org.apache.tapestry5.ioc.internal.util.ClasspathResource)16 Asset (org.apache.tapestry5.Asset)14 ComponentModel (org.apache.tapestry5.model.ComponentModel)10 MutableEmbeddedComponentModel (org.apache.tapestry5.model.MutableEmbeddedComponentModel)10 ComponentTemplate (org.apache.tapestry5.internal.parser.ComponentTemplate)6 AssetFactory (org.apache.tapestry5.services.AssetFactory)6 IOException (java.io.IOException)5 AssetSource (org.apache.tapestry5.services.AssetSource)5 ComponentResourceLocator (org.apache.tapestry5.services.pageload.ComponentResourceLocator)5 Context (org.apache.tapestry5.http.services.Context)4 BeginRender (org.apache.tapestry5.annotations.BeginRender)3 Location (org.apache.tapestry5.commons.Location)3 AbstractResource (org.apache.tapestry5.ioc.internal.util.AbstractResource)3 ThreadLocale (org.apache.tapestry5.ioc.services.ThreadLocale)3 ClasspathAssetAliasManager (org.apache.tapestry5.services.ClasspathAssetAliasManager)3 StreamableResource (org.apache.tapestry5.services.assets.StreamableResource)3