-
Notifications
You must be signed in to change notification settings - Fork 31
SLING-8655 - Updated dependency on API, provided Externalized Path In… #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
bb865a6
d9b9a6d
06aee3b
31bcf93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.sling.models.impl.injectors; | ||
|
|
||
| import org.apache.commons.lang3.ObjectUtils; | ||
| import org.apache.sling.api.resource.ResourceResolver; | ||
| import org.apache.sling.api.resource.ValueMap; | ||
| import org.apache.sling.models.annotations.ExternalizePath; | ||
| import org.apache.sling.models.annotations.injectorspecific.ExternalizedPathProvider; | ||
| import org.apache.sling.models.spi.DisposalCallbackRegistry; | ||
| import org.apache.sling.models.spi.Injector; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.osgi.framework.Constants; | ||
| import org.osgi.service.component.annotations.Component; | ||
| import org.osgi.service.component.annotations.Reference; | ||
| import org.osgi.service.component.annotations.ReferenceCardinality; | ||
| import org.osgi.service.component.annotations.ReferencePolicy; | ||
|
|
||
| import java.lang.reflect.AnnotatedElement; | ||
| import java.lang.reflect.Type; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
|
|
||
| @Component( | ||
| property=Constants.SERVICE_RANKING+":Integer=1000", | ||
| service={ | ||
| Injector.class | ||
| } | ||
| ) | ||
| public class ExternalizedPathInjector | ||
| extends AbstractInjector | ||
| implements Injector | ||
| { | ||
| List<ExternalizedPathProvider> providerList = new ArrayList<>(); | ||
|
|
||
| @Reference(policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.OPTIONAL) | ||
| void bindExternalizedPathProvider(ExternalizedPathProvider provider) { | ||
| providerList.add(provider); | ||
| // The providers are sorted so that the one with the highest priority is the first entry | ||
| Collections.sort( | ||
| providerList, | ||
| Comparator.comparingInt(ExternalizedPathProvider::getPriority).reversed() | ||
| ); | ||
| } | ||
|
|
||
| void unbindExternalizedPathProvider(ExternalizedPathProvider provider) { | ||
| providerList.remove(provider); | ||
| } | ||
|
|
||
| public ExternalizedPathInjector() { | ||
| bindExternalizedPathProvider(new DefaultExternalizedPathProvider()); | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull String getName() { | ||
| return "externalize-path"; | ||
| } | ||
|
|
||
| @Override | ||
| public Object getValue(@NotNull Object adaptable, String name, @NotNull Type type, @NotNull AnnotatedElement element, | ||
| @NotNull DisposalCallbackRegistry callbackRegistry) { | ||
| if (adaptable == ObjectUtils.NULL) { | ||
| return null; | ||
| } | ||
| if (element.isAnnotationPresent(ExternalizePath.class)) { | ||
| ValueMap properties = getValueMap(adaptable); | ||
| if(properties != ObjectUtils.NULL) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpicky formatting on this
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed that |
||
| String imagePath = properties.get(name, String.class); | ||
| if(imagePath != null) { | ||
| ExternalizedPathProvider provider = providerList.get(0); | ||
| return provider.externalize(adaptable, imagePath); | ||
| } | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** Fallback Implementation of the Externalized Path Provider that uses the Resource Resolver's map function **/ | ||
| private class DefaultExternalizedPathProvider | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be at least a
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved this to be an regular OSGi Component |
||
| implements ExternalizedPathProvider | ||
| { | ||
| @Override | ||
| public int getPriority() { | ||
| return FALLBACK_PRIORITY; | ||
| } | ||
|
|
||
| @Override | ||
| public String externalize(@NotNull Object adaptable, String sourcePath) { | ||
| String answer = sourcePath; | ||
| ResourceResolver resourceResolver = getResourceResolver(adaptable); | ||
| if(sourcePath != null && resourceResolver != null) { | ||
| answer = resourceResolver.map(sourcePath); | ||
| } | ||
| return answer; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package org.apache.sling.models.impl.injectors; | ||
|
|
||
| import org.apache.sling.api.resource.Resource; | ||
| import org.apache.sling.api.resource.ResourceResolver; | ||
| import org.apache.sling.api.resource.ValueMap; | ||
| import org.apache.sling.models.annotations.ExternalizePath; | ||
| import org.apache.sling.models.annotations.injectorspecific.ExternalizedPathProvider; | ||
| import org.apache.sling.models.spi.DisposalCallbackRegistry; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.junit.Test; | ||
|
|
||
| import java.lang.reflect.AnnotatedElement; | ||
| import java.lang.reflect.Type; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.mockito.Matchers.eq; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| public class ExternalizedPathInjectorTest { | ||
|
|
||
| @Test | ||
| public void testNoResolveInjection() { | ||
| String imagePath = "/content/test/image/test-image.jpg"; | ||
|
|
||
| ExternalizedPathInjector injector = new ExternalizedPathInjector(); | ||
| Resource adaptable = mock(Resource.class); | ||
| ValueMap valueMap = mock(ValueMap.class); | ||
| when(adaptable.adaptTo(eq(ValueMap.class))).thenReturn(valueMap); | ||
| String name = "imagePath"; | ||
| when(valueMap.get(eq(name), eq(String.class))).thenReturn(imagePath); | ||
| Type type = String.class; | ||
| AnnotatedElement element = mock(AnnotatedElement.class); | ||
| when(element.isAnnotationPresent(eq(ExternalizePath.class))).thenReturn(true); | ||
| DisposalCallbackRegistry callbackRegistry = mock(DisposalCallbackRegistry.class); | ||
| ResourceResolver resourceResolver = mock(ResourceResolver.class); | ||
| when(adaptable.getResourceResolver()).thenReturn(resourceResolver); | ||
| when(resourceResolver.map(imagePath)).thenReturn(imagePath); | ||
|
|
||
| Object value = injector.getValue(adaptable, name, type, element, callbackRegistry); | ||
| assertEquals("No Mapping was expected", imagePath, value); | ||
| } | ||
|
|
||
| @Test | ||
| public void testResolveInjection() { | ||
| String imagePath = "/content/test/image/test-image.jpg"; | ||
| String mappedImagePath = "/image/test-image.jpg"; | ||
|
|
||
| ExternalizedPathInjector injector = new ExternalizedPathInjector(); | ||
| Resource adaptable = mock(Resource.class); | ||
| ValueMap valueMap = mock(ValueMap.class); | ||
| when(adaptable.adaptTo(eq(ValueMap.class))).thenReturn(valueMap); | ||
| String name = "imagePath"; | ||
| when(valueMap.get(eq(name), eq(String.class))).thenReturn(imagePath); | ||
| Type type = String.class; | ||
| AnnotatedElement element = mock(AnnotatedElement.class); | ||
| when(element.isAnnotationPresent(eq(ExternalizePath.class))).thenReturn(true); | ||
| DisposalCallbackRegistry callbackRegistry = mock(DisposalCallbackRegistry.class); | ||
| ResourceResolver resourceResolver = mock(ResourceResolver.class); | ||
| when(adaptable.getResourceResolver()).thenReturn(resourceResolver); | ||
| when(resourceResolver.map(imagePath)).thenReturn(mappedImagePath); | ||
|
|
||
| Object value = injector.getValue(adaptable, name, type, element, callbackRegistry); | ||
| assertEquals("Mapping was expected", mappedImagePath, value); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCustomProviderInjection() { | ||
| String imagePath = "/content/test/image/test-image.jpg"; | ||
| String from = "/content/test/image/"; | ||
| String to1 = "/image1/"; | ||
| String to2 = "/image2/"; | ||
| String to3 = "/image3/"; | ||
| String mappedImagePath = "/image/test-image.jpg"; | ||
| String mappedImagePath1 = "/image1/test-image.jpg"; | ||
| String mappedImagePath2 = "/image2/test-image.jpg"; | ||
| String mappedImagePath3 = "/image3/test-image.jpg"; | ||
|
|
||
| ExternalizedPathInjector injector = new ExternalizedPathInjector(); | ||
| Resource adaptable = mock(Resource.class); | ||
| ValueMap valueMap = mock(ValueMap.class); | ||
| when(adaptable.adaptTo(eq(ValueMap.class))).thenReturn(valueMap); | ||
| String name = "imagePath"; | ||
| when(valueMap.get(eq(name), eq(String.class))).thenReturn(imagePath); | ||
| Type type = String.class; | ||
| AnnotatedElement element = mock(AnnotatedElement.class); | ||
| when(element.isAnnotationPresent(eq(ExternalizePath.class))).thenReturn(true); | ||
| DisposalCallbackRegistry callbackRegistry = mock(DisposalCallbackRegistry.class); | ||
| ResourceResolver resourceResolver = mock(ResourceResolver.class); | ||
| when(adaptable.getResourceResolver()).thenReturn(resourceResolver); | ||
| when(resourceResolver.map(imagePath)).thenReturn(mappedImagePath); | ||
|
|
||
| TestExternalizedPathProvider provider1 = new TestExternalizedPathProvider(100, from, to1); | ||
| injector.bindExternalizedPathProvider(provider1); | ||
| TestExternalizedPathProvider provider3 = new TestExternalizedPathProvider(300, from, to3); | ||
| injector.bindExternalizedPathProvider(provider3); | ||
| TestExternalizedPathProvider provider2 = new TestExternalizedPathProvider(200, from, to2); | ||
| injector.bindExternalizedPathProvider(provider2); | ||
|
|
||
| Object value = injector.getValue(adaptable, name, type, element, callbackRegistry); | ||
| assertEquals("Wrong Provider was selected", mappedImagePath3, value); | ||
| } | ||
|
|
||
| private class TestExternalizedPathProvider | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to above, when you are not using the enclosing class data, the nested class should be
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| implements ExternalizedPathProvider | ||
| { | ||
| private int priority; | ||
| private String from = "/"; | ||
| private String to = "/"; | ||
|
|
||
| public TestExternalizedPathProvider(int priority, String from, String to) { | ||
| this.priority = priority; | ||
| this.from = from; | ||
| this.to = to; | ||
| } | ||
| @Override | ||
| public int getPriority() { return priority; } | ||
|
|
||
| @Override | ||
| public String externalize(@NotNull Object adaptable, String sourcePath) { | ||
| String answer = sourcePath; | ||
| if(sourcePath.startsWith(from)) { | ||
| answer = to + sourcePath.substring(from.length()); | ||
| } | ||
| return answer; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of sorting based on a new field called
priority, you should probably useservice.rankingof the implementations. Perhaps you want to look at how ModelAdapterFactory uses ranked services.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Used RankedServices as suggested