Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.models.api</artifactId>
<version>1.3.6</version>
<version>1.3.9-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- OSGi annotations -->
Expand Down Expand Up @@ -217,5 +217,10 @@
<version>1.0.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.2</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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.serializer;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.ExternalizePath;
import org.apache.sling.models.annotations.ExternalizePathProvider;
import org.jetbrains.annotations.NotNull;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/** Fallback Implementation of the Externalized Path Provider that uses the Resource Resolver's map function **/
@Component(
property = Constants.SERVICE_RANKING + ":Integer=1",
immediate = true,
service = {
ExternalizePathProvider.class
}
)
public class DefaultExternalizePathProvider
implements ExternalizePathProvider
{
@Override
public String externalize(@NotNull Object model, ExternalizePath annotation, String sourcePath) {
String answer = sourcePath;
ResourceResolver resourceResolver = getResourceResolver(model, annotation);
if (sourcePath != null && !sourcePath.isEmpty() && resourceResolver != null) {
answer = resourceResolver.map(sourcePath);
}
return answer;
}

/**
* Obtains the Resource from the Model in order to Externalize
* @param model
* @param annotation
* @return
*/
private ResourceResolver getResourceResolver(Object model, ExternalizePath annotation) {
Resource answer = null;
// Get Resource from specified Resource method
String resourceMethodName = annotation.resourceMethod();
if(!resourceMethodName.isEmpty()) {
try {
Method getResourceMethod = model.getClass().getMethod(resourceMethodName);
if(getResourceMethod.getReturnType().isAssignableFrom(Resource.class)) {
answer = (Resource) getResourceMethod.invoke(model, null);
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
// If not found then we cannot use Externalize Path but then we just send the original value
}
}
if(answer == null) {
// Get Resource from specified Resource Field
String resourceFieldName = annotation.resourceField();
if (!resourceFieldName.isEmpty()) {
try {
Field resourceField = FieldUtils.getField(model.getClass(), resourceFieldName, true);
if(resourceField != null && resourceField.getType().isAssignableFrom(Resource.class)) {
answer = (Resource) resourceField.get(model);
}
} catch (IllegalAccessException e) {
// If not found then we cannot use Externalize Path but then we just send the original value
}
}
}
if(answer == null) {
// Get Resource from default location (getResource())
try {
Method getResourceMethod = model.getClass().getMethod("getResource");
if(getResourceMethod.getReturnType().isAssignableFrom(Resource.class)) {
answer = (Resource) getResourceMethod.invoke(model, null);
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
// If not found then we cannot use Externalize Path but then we just send the original value
}
}
if(answer == null) {
// Get Resource from default Resource Field (resource)
try {
Field resourceField = FieldUtils.getField(model.getClass(), "resource", true);
if(resourceField != null && resourceField.getType().isAssignableFrom(Resource.class)) {
answer = (Resource) resourceField.get(model);
}
} catch (IllegalAccessException e) {
// If not found then we cannot use Externalize Path but then we just send the original value
}
}
return answer == null ? null : answer.getResourceResolver();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.serializer;

import org.apache.sling.models.annotations.ExternalizePathProvider;

/** Service that maintains a list of All Externalize Path Providers and returns the best fitting one depending on its implementation **/
public interface ExternalizePathProviderManager {

/** @return The best fitting Provider that is managed here at the moment **/
ExternalizePathProvider getExternalizedPathProvider();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.serializer;

import org.apache.sling.commons.osgi.Order;
import org.apache.sling.commons.osgi.RankedServices;
import org.apache.sling.models.annotations.ExternalizePathProvider;
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.util.Map;

/**
* Simple Implementation of the Externalize Path Provider Manager service
* which just binds them and then selects the highest one (first one as the
* order is descending).
*/
@Component(
service={
ExternalizePathProviderManager.class
}
)
public class ExternalizePathProviderManagerService
implements ExternalizePathProviderManager
{
private RankedServices<ExternalizePathProvider> providers = new RankedServices<>(Order.DESCENDING);

@Reference(
cardinality = ReferenceCardinality.MULTIPLE,
policy = ReferencePolicy.DYNAMIC
)
protected void bindExternalizePathProvider(final ExternalizePathProvider provider, final Map<String, Object> props) {
providers.bind(provider, props);
}

protected void unbindExternalizePathProvider(final ExternalizePathProvider provider, final Map<String, Object> props) {
providers.unbind(provider, props);
}

@Override
public ExternalizePathProvider getExternalizedPathProvider() {
return providers.getList().get(0);
}
}
Loading