Search in sources :

Example 1 with ResourceProviderStorageProvider

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorageProvider in project sling by apache.

the class ResourceDecoratorTestBase method setup.

@Before
public void setup() throws LoginException {
    final ResourceDecorator d = new ResourceDecorator() {

        @Override
        public Resource decorate(Resource resource) {
            return ResourceDecoratorTestBase.this.wrapResourceForTest(resource);
        }

        @Override
        public Resource decorate(Resource resource, HttpServletRequest request) {
            throw new UnsupportedOperationException("Not supposed to be used in these tests");
        }
    };
    final ResourceDecoratorTracker t = new ResourceDecoratorTracker();
    t.bindResourceDecorator(d, null);
    final ResourceProvider<?> provider = new ResourceProvider<Object>() {

        @Override
        public QueryLanguageProvider<Object> getQueryLanguageProvider() {
            return new QueryLanguageProvider<Object>() {

                @Override
                public String[] getSupportedLanguages(ResolveContext<Object> ctx) {
                    return new String[] { QUERY_LANGUAGE };
                }

                @Override
                public Iterator<Resource> findResources(ResolveContext<Object> ctx, String query, String language) {
                    final List<Resource> found = new ArrayList<Resource>();
                    found.add(mockResource("/tmp/C"));
                    found.add(mockResource("/tmp/D"));
                    found.add(mockResource("/var/one"));
                    found.add(mockResource("/var/two"));
                    return found.iterator();
                }

                @Override
                public Iterator<ValueMap> queryResources(ResolveContext<Object> ctx, String query, String language) {
                    return null;
                }
            };
        }

        @Override
        public Resource getResource(ResolveContext<Object> ctx, String path, final ResourceContext rCtx, Resource parent) {
            if (path.equals("/") || path.startsWith("/tmp") || path.startsWith("/var")) {
                return mockResource(path);
            }
            return null;
        }

        @Override
        public Iterator<Resource> listChildren(ResolveContext<Object> ctx, Resource parent) {
            final List<Resource> children = new ArrayList<Resource>();
            if ("/".equals(parent.getPath())) {
                children.add(mockResource("/tmp"));
                children.add(mockResource("/var"));
            } else if ("/var".equals(parent.getPath())) {
                children.add(mockResource("/var/one"));
                children.add(mockResource("/var/two"));
                children.add(mockResource("/var/three"));
            } else if ("/tmp".equals(parent.getPath())) {
                children.add(mockResource("/tmp/A"));
                children.add(mockResource("/tmp/B"));
                children.add(mockResource("/tmp/C"));
                children.add(mockResource("/tmp/D"));
            }
            return children.iterator();
        }
    };
    ResourceResolverFactoryActivator activator = new ResourceResolverFactoryActivator();
    final CommonResourceResolverFactoryImpl crf = new CommonResourceResolverFactoryImpl(activator) {

        @Override
        public ResourceDecoratorTracker getResourceDecoratorTracker() {
            return t;
        }

        @Override
        public ResourceAccessSecurityTracker getResourceAccessSecurityTracker() {
            return new ResourceAccessSecurityTracker();
        }
    };
    final List<ResourceProviderHandler> list = Arrays.asList(MockedResourceResolverImplTest.createRPHandler(provider, "A-provider", 0L, "/"));
    resolver = new ResourceResolverImpl(crf, false, null, new ResourceProviderStorageProvider() {

        @Override
        public ResourceProviderStorage getResourceProviderStorage() {
            return new ResourceProviderStorage(list);
        }
    });
}
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) ValueMap(org.apache.sling.api.resource.ValueMap) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) ResourceProviderStorageProvider(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorageProvider) ResourceDecorator(org.apache.sling.api.resource.ResourceDecorator) HttpServletRequest(javax.servlet.http.HttpServletRequest) ResourceProviderStorage(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage) ResourceProvider(org.apache.sling.spi.resource.provider.ResourceProvider) ResourceDecoratorTracker(org.apache.sling.resourceresolver.impl.helper.ResourceDecoratorTracker) QueryLanguageProvider(org.apache.sling.spi.resource.provider.QueryLanguageProvider) Before(org.junit.Before)

Example 2 with ResourceProviderStorageProvider

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorageProvider 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 3 with ResourceProviderStorageProvider

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorageProvider in project sling by apache.

the class ResourceResolverControlTest method prepare.

@Before
public void prepare() throws Exception {
    BundleContext bc = MockOsgi.newBundleContext();
    Fixture fixture = new Fixture(bc);
    // sub-provider
    subProvider = Mockito.mock(ResourceProvider.class);
    ResourceProviderInfo info = fixture.registerResourceProvider(subProvider, "/some/path", AuthType.required);
    ResourceProviderHandler handler = new ResourceProviderHandler(bc, info);
    when(subProvider.getQueryLanguageProvider()).thenReturn(new SimpleQueryLanguageProvider(QL_MOCK, QL_ANOTHER_MOCK) {

        @Override
        public Iterator<ValueMap> queryResources(ResolveContext<Object> ctx, String query, String language) {
            if (query.equals(QUERY_MOCK_FIND_ALL) && language.equals(QL_MOCK)) {
                SimpleValueMapImpl valueMap = new SimpleValueMapImpl();
                valueMap.put("key", "value");
                return Collections.<ValueMap>singletonList(valueMap).iterator();
            }
            throw new UnsupportedOperationException();
        }

        @Override
        public Iterator<Resource> findResources(ResolveContext<Object> ctx, String query, String language) {
            if (query.equals(QUERY_MOCK_FIND_ALL) && language.equals(QL_MOCK)) {
                return Collections.<Resource>singletonList(newMockResource("/some/path/object")).iterator();
            }
            throw new UnsupportedOperationException();
        }
    });
    handler.activate();
    rootProvider = mock(ResourceProvider.class);
    ResourceProviderInfo rootInfo = fixture.registerResourceProvider(rootProvider, "/", AuthType.required);
    ResourceProviderHandler rootHandler = new ResourceProviderHandler(bc, rootInfo);
    when(rootProvider.getQueryLanguageProvider()).thenReturn(new SimpleQueryLanguageProvider(QL_NOOP));
    rootHandler.activate();
    // configure mock resources
    Resource root = configureResourceAt(rootProvider, "/");
    somethingResource = configureResourceAt(rootProvider, "/something");
    subProviderResource = configureResourceAt(subProvider, "/some/path/object");
    // configure query at '/'
    when(rootProvider.listChildren((ResolveContext<Object>) Mockito.anyObject(), Mockito.eq(root))).thenReturn(Collections.singleton(somethingResource).iterator());
    ResourceResolver rr = mock(ResourceResolver.class);
    ResourceAccessSecurityTracker securityTracker = Mockito.mock(ResourceAccessSecurityTracker.class);
    authInfo = getAuthInfo();
    handlers = Arrays.asList(rootHandler, handler);
    final ResourceProviderStorage storage = new ResourceProviderStorage(handlers);
    crp = new ResourceResolverControl(false, authInfo, new ResourceProviderStorageProvider() {

        @Override
        public ResourceProviderStorage getResourceProviderStorage() {
            return storage;
        }
    });
    context = new ResourceResolverContext(rr, securityTracker);
}
Also used : ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) ResourceProviderStorageProvider(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorageProvider) SimpleValueMapImpl(org.apache.sling.resourceresolver.impl.SimpleValueMapImpl) ResourceProviderInfo(org.apache.sling.resourceresolver.impl.providers.ResourceProviderInfo) ResourceProviderStorage(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage) ResourceProvider(org.apache.sling.spi.resource.provider.ResourceProvider) Iterator(java.util.Iterator) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ResourceAccessSecurityTracker(org.apache.sling.resourceresolver.impl.ResourceAccessSecurityTracker) Fixture(org.apache.sling.resourceresolver.impl.Fixture) BundleContext(org.osgi.framework.BundleContext) Before(org.junit.Before)

Aggregations

Resource (org.apache.sling.api.resource.Resource)3 ResourceProviderHandler (org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler)3 ResourceProviderStorage (org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage)3 ResourceProviderStorageProvider (org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorageProvider)3 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 ValueMap (org.apache.sling.api.resource.ValueMap)2 ResolveContext (org.apache.sling.spi.resource.provider.ResolveContext)2 ResourceContext (org.apache.sling.spi.resource.provider.ResourceContext)2 ResourceProvider (org.apache.sling.spi.resource.provider.ResourceProvider)2 Before (org.junit.Before)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ResourceDecorator (org.apache.sling.api.resource.ResourceDecorator)1 ResourceMetadata (org.apache.sling.api.resource.ResourceMetadata)1 Fixture (org.apache.sling.resourceresolver.impl.Fixture)1 ResourceAccessSecurityTracker (org.apache.sling.resourceresolver.impl.ResourceAccessSecurityTracker)1 SimpleValueMapImpl (org.apache.sling.resourceresolver.impl.SimpleValueMapImpl)1 ResourceDecoratorTracker (org.apache.sling.resourceresolver.impl.helper.ResourceDecoratorTracker)1