Search in sources :

Example 1 with AssetAlias

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

the class ClasspathAssetAliasManagerImplTest method to_client_url.

@Test(dataProvider = "to_client_url_data")
public void to_client_url(String resourcePath, String expectedFolder, String expectedPath) throws IOException {
    Resource r = mockResource();
    expect(r.getPath()).andReturn(resourcePath);
    replay();
    ClasspathAssetAliasManager manager = new ClasspathAssetAliasManagerImpl(configuration());
    AssetAlias alias = manager.extractAssetAlias(r);
    assertEquals(alias.virtualFolder, expectedFolder);
    assertEquals(alias.path, expectedPath);
    verify();
}
Also used : Resource(org.apache.tapestry5.commons.Resource) AssetAlias(org.apache.tapestry5.services.AssetAlias) ClasspathAssetAliasManager(org.apache.tapestry5.services.ClasspathAssetAliasManager) Test(org.testng.annotations.Test)

Example 2 with AssetAlias

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

the class ClasspathAssetAliasManagerImpl method extractAssetAlias.

public AssetAlias extractAssetAlias(Resource resource) {
    String resourcePath = resource.getPath();
    for (String pathPrefix : sortedPathPrefixes) {
        if (resourcePath.startsWith(pathPrefix)) {
            if (pathPrefix.length() == resourcePath.length()) {
                throw new IllegalArgumentException(String.format("Resource path '%s' is not valid as it is mapped as virtual folder '%s'.", resourcePath, pathPrefixToAlias.get(pathPrefix)));
            }
            // Prevent matching path prefix "foo" against "foobar" ... it must match against "foo/".
            if (resourcePath.charAt(pathPrefix.length()) != '/') {
                continue;
            }
            String virtualFolder = pathPrefixToAlias.get(pathPrefix);
            // Don't want that slash seperating the folder from the rest of the path.
            String path = resourcePath.substring(pathPrefix.length() + 1);
            return new AssetAlias(virtualFolder, path);
        }
    }
    throw new UnknownValueException(String.format("Unable to create a client URL for classpath resource %s: The resource path was not within an aliased path.", resourcePath), new AvailableValues("Aliased paths", aliasToPathPrefix.values()));
}
Also used : AssetAlias(org.apache.tapestry5.services.AssetAlias) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) AvailableValues(org.apache.tapestry5.commons.util.AvailableValues)

Aggregations

AssetAlias (org.apache.tapestry5.services.AssetAlias)2 Resource (org.apache.tapestry5.commons.Resource)1 AvailableValues (org.apache.tapestry5.commons.util.AvailableValues)1 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)1 ClasspathAssetAliasManager (org.apache.tapestry5.services.ClasspathAssetAliasManager)1 Test (org.testng.annotations.Test)1