You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by "Piotr Rzasa (JIRA)" <ji...@apache.org> on 2016/06/22 07:10:57 UTC
[jira] [Created] (SLING-5801) There is no possible to inject Sling
Models adaptable from SlingHttpServletRequest in Sling Model object which
is adaptable from SlingHttpServletRequest as well
Piotr Rzasa created SLING-5801:
----------------------------------
Summary: There is no possible to inject Sling Models adaptable from SlingHttpServletRequest in Sling Model object which is adaptable from SlingHttpServletRequest as well
Key: SLING-5801
URL: https://issues.apache.org/jira/browse/SLING-5801
Project: Sling
Issue Type: Improvement
Reporter: Piotr Rzasa
Priority: Minor
Scenario:
{code}
@Model(adaptables = SlingHttpServletRequest.class)
public class A {
}
@Model(adaptables = SlingHttpServletRequest.class)
public class B {
@Inject
private A instanceOfB;
}
{code}
Injection of Sling model A in Sling Model B will fails, the error appears that there is no injection which can handle it.
I think it will be good to add such injector and extend Via annotation to support injection either from resource or from request.
Here is an example implementation:
{code}
@Component
@Service
@Property(name = Constants.SERVICE_RANKING, intValue = Integer.MAX_VALUE)
public class RequestInjector implements Injector {
public static final String INJECTOR_NAME = "request-injector";
@Override
public String getName() {
return INJECTOR_NAME;
}
@Override
public Object getValue(Object adaptable, String name, Type declaredType, AnnotatedElement element,
DisposalCallbackRegistry callbackRegistry) {
if (!(adaptable instanceof SlingHttpServletRequest)) {
return null;
}
Source source = element.getAnnotation(Source.class);
SlingHttpServletRequest request = (SlingHttpServletRequest) adaptable;
Class<?> clazz = getClassDeclaration(declaredType);
if (clazz != null) {
return request.adaptTo(clazz);
}
return null;
}
private Class<?> getClassDeclaration(Type declaredType) {
Class<?> result = null;
if (declaredType instanceof Class<?>) {
result = (Class<?>) declaredType;
}
return result;
}
}
{code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)