Search in sources :

Example 21 with ResourceMetadata

use of org.apache.sling.api.resource.ResourceMetadata in project sling by apache.

the class ProviderHandlerTest method testServletRegistrationAndSyntheticResources.

@SuppressWarnings("unchecked")
@Test
public void testServletRegistrationAndSyntheticResources() throws LoginException {
    final String servletpath = "/libs/a/b/GET.servlet";
    final Resource servletResource = Mockito.mock(Resource.class);
    Mockito.when(servletResource.getResourceMetadata()).then(new Answer<ResourceMetadata>() {

        @Override
        public ResourceMetadata answer(InvocationOnMock invocation) throws Throwable {
            return new ResourceMetadata();
        }
    });
    final ResourceProvider<?> leaveProvider = Mockito.mock(ResourceProvider.class);
    Mockito.when(leaveProvider.getResource(Mockito.any(ResolveContext.class), Mockito.eq(servletpath), Mockito.any(ResourceContext.class), Mockito.any(Resource.class))).thenReturn(servletResource);
    final ResourceProviderHandler h = createRPHandler(leaveProvider, "my-pid", 0, servletpath);
    ResourceResolverFactoryActivator activator = new ResourceResolverFactoryActivator();
    activator.resourceAccessSecurityTracker = new ResourceAccessSecurityTracker();
    ResourceResolver resolver = new ResourceResolverImpl(new CommonResourceResolverFactoryImpl(activator), false, null, new ResourceProviderStorageProvider() {

        @Override
        public ResourceProviderStorage getResourceProviderStorage() {
            return new ResourceProviderStorage(Arrays.asList(h));
        }
    });
    final Resource parent = resolver.getResource(ResourceUtil.getParent(servletpath));
    assertNotNull("Parent must be available", parent);
    assertTrue("Resource should be synthetic", ResourceUtil.isSyntheticResource(parent));
    final Resource servlet = resolver.getResource(servletpath);
    assertNotNull("Servlet resource must not be null", servlet);
    assertEquals(servletResource, servlet);
    assertNotNull(resolver.getResource("/libs"));
    // now check when doing a resolve()
    assertTrue(resolver.resolve("/libs") instanceof NonExistingResource);
    assertTrue(resolver.resolve(ResourceUtil.getParent(servletpath)) instanceof NonExistingResource);
    assertNotNull(resolver.resolve(servletpath));
    resolver.close();
}
Also used : ResolveContext(org.apache.sling.spi.resource.provider.ResolveContext) ResourceContext(org.apache.sling.spi.resource.provider.ResourceContext) ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) ResourceProviderStorageProvider(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorageProvider) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata) ResourceProviderStorage(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Test(org.junit.Test)

Example 22 with ResourceMetadata

use of org.apache.sling.api.resource.ResourceMetadata in project sling by apache.

the class StarResourceTest method assertSplit.

private void assertSplit(String requestPath, String path, String pathInfo) {
    final ResourceMetadata rm = StarResource.getResourceMetadata(requestPath);
    assertEquals("For requestPath=" + requestPath + ", path matches", path, rm.getResolutionPath());
    assertEquals("For requestPath=" + requestPath + ", pathInfo matches", pathInfo, rm.getResolutionPathInfo());
}
Also used : ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata)

Example 23 with ResourceMetadata

use of org.apache.sling.api.resource.ResourceMetadata in project sling by apache.

the class SightlyJavaCompilerServiceTest method testDiskCachedUseObject.

@Test
public void testDiskCachedUseObject() throws Exception {
    String pojoPath = "/apps/myproject/testcomponents/a/Pojo.java";
    String className = "apps.myproject.testcomponents.a.Pojo";
    scriptingResourceResolverProvider = Mockito.mock(ScriptingResourceResolverProvider.class);
    ResourceResolver resolver = Mockito.mock(ResourceResolver.class);
    when(scriptingResourceResolverProvider.getRequestScopedResourceResolver()).thenReturn(resolver);
    Resource pojoResource = Mockito.mock(Resource.class);
    when(pojoResource.getPath()).thenReturn(pojoPath);
    ResourceMetadata mockMetadata = Mockito.mock(ResourceMetadata.class);
    when(mockMetadata.getModificationTime()).thenReturn(1l);
    when(pojoResource.getResourceMetadata()).thenReturn(mockMetadata);
    when(pojoResource.adaptTo(InputStream.class)).thenReturn(IOUtils.toInputStream("DUMMY", "UTF-8"));
    when(resolver.getResource(pojoPath)).thenReturn(pojoResource);
    when(classLoaderWriter.getLastModified("/apps/myproject/testcomponents/a/Pojo.class")).thenReturn(2l);
    getInstancePojoTest(className);
    /*
         * assuming the compiled class has a last modified date greater than the source, then the compiler should not recompile the Use
         * object
         */
    verify(compiler, never()).compileSource(any(SourceIdentifier.class), anyString());
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) SourceIdentifier(org.apache.sling.scripting.sightly.impl.engine.compiled.SourceIdentifier) ScriptingResourceResolverProvider(org.apache.sling.scripting.api.resource.ScriptingResourceResolverProvider) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata) Test(org.junit.Test)

Example 24 with ResourceMetadata

use of org.apache.sling.api.resource.ResourceMetadata in project sling by apache.

the class SlingIOProvider method lastModified.

/**
     * Returns the value of the last modified meta data field of the resource
     * found at file name or zero if the meta data field is not set. If the
     * resource does not exist or an error occurrs finding the resource, -1 is
     * returned.
     */
public long lastModified(final String path) {
    if (path.startsWith(":")) {
        return this.classLoaderWriter.getLastModified(path.substring(1));
    }
    ResourceResolver resolver = requestResourceResolver.get();
    if (resolver != null) {
        try {
            final Resource resource = resolver.getResource(cleanPath(path, true));
            if (resource != null) {
                ResourceMetadata meta = resource.getResourceMetadata();
                long modTime = meta.getModificationTime();
                return (modTime > 0) ? modTime : 0;
            }
        } catch (final SlingException se) {
            log.error("Cannot get last modification time for " + path, se);
        }
    }
    // fallback to "non-existant" in case of problems
    return -1;
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) SlingException(org.apache.sling.api.SlingException) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata)

Example 25 with ResourceMetadata

use of org.apache.sling.api.resource.ResourceMetadata in project sling by apache.

the class ScriptableResourceTest method assertResourceMetaData.

private void assertResourceMetaData(Object metaData) throws Exception {
    if (metaData instanceof ResourceMetadata) {
        ResourceMetadata rm = (ResourceMetadata) metaData;
        rm.getResolutionPath().equals(node.getPath());
    } else {
        fail("Expected ResourceMetadata, got " + metaData);
    }
}
Also used : ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata)

Aggregations

ResourceMetadata (org.apache.sling.api.resource.ResourceMetadata)31 Resource (org.apache.sling.api.resource.Resource)15 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)7 Test (org.junit.Test)6 AbstractResourceImpl (com.adobe.acs.commons.mcp.impl.AbstractResourceImpl)5 ValueMap (org.apache.sling.api.resource.ValueMap)5 InputStream (java.io.InputStream)4 ValueMapResource (com.adobe.granite.ui.components.ds.ValueMapResource)3 HashMap (java.util.HashMap)3 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)3 BufferedInputStream (java.io.BufferedInputStream)2 ArrayList (java.util.ArrayList)2 Session (javax.jcr.Session)2 AccessControlManager (javax.jcr.security.AccessControlManager)2 Privilege (javax.jcr.security.Privilege)2 SlingException (org.apache.sling.api.SlingException)2 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)2 ScriptingResourceResolverProvider (org.apache.sling.scripting.api.resource.ScriptingResourceResolverProvider)2 SourceIdentifier (org.apache.sling.scripting.sightly.impl.engine.compiled.SourceIdentifier)2 ResolveContext (org.apache.sling.spi.resource.provider.ResolveContext)2