可以通过三种方式在AEM中获取资源解析器。
让我们考虑一下这种情况。
这三个应该不一样吗?我的意思是快速又肮脏的代码。请让我知道您的想法。
@Component(service = Servlet.class,
property = {
Constants.SERVICE_DESCRIPTION + "=Goda Servlet for Testing Resource Resolver",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.paths=" + "/bin/resource-resolver-testing"
})
public class GodaServletResourceResolver extends SlingSafeMethodsServlet {
public static final Logger LOGGER = LoggerFactory.getLogger(GodaServletResourceResolver.class);
private static final long serialVersionUID = 1L;
@Reference
private ResourceResolverFactory resourceResolverFactory;
@Override
protected void doGet(final SlingHttpServletRequest request,
final SlingHttpServletResponse response) throws ServletException, IOException {
final Resource resource = request.getResource();
response.setContentType("text/plain");
final ResourceResolver resourceResolverFromResourceWhichIsAgainFromTheRequest = request.getResource().getResourceResolver();
LOGGER.debug("Resource Resolver from resource {}", resourceResolverFromResourceWhichIsAgainFromTheRequest);
final ResourceResolver resourceResolverFromServletRequest = request.getResourceResolver();
LOGGER.debug("Resource Resolver from request {}", resourceResolverFromServletRequest);
Map<String, Object> resolverParams = new HashMap<>();
resolverParams.put(ResourceResolverFactory.SUBSERVICE, "Goda Servlet for Testing Resource Resolver");
ResourceResolver serviceResourceResolverFromResourceResolverFactory = null;
ResourceResolver adminResourceResolverFromResourceResolverFactory = null;
try {
serviceResourceResolverFromResourceResolverFactory = resourceResolverFactory.getServiceResourceResolver(resolverParams);
adminResourceResolverFromResourceResolverFactory = resourceResolverFactory.getAdministrativeResourceResolver(null);
} catch (LoginException e) {
LOGGER.error(e.getMessage());
}
LOGGER.debug("Resource Resolver from the resource resolver factory is {}", serviceResourceResolverFromResourceResolverFactory);
LOGGER.debug("Administrative Resource Resolver from the resource resolver factory is {}", adminResourceResolverFromResourceResolverFactory);
LOGGER.debug("But are all these resource resolvers the same?");
LOGGER.debug("Lets compare");
LOGGER.debug("Is the resource based resourceResolver same as the request based one? {}",
resourceResolverFromResourceWhichIsAgainFromTheRequest == resourceResolverFromServletRequest);
LOGGER.debug("Is the resource based resourceResolver same as the ResourceResolverFactory based one? {}",
resourceResolverFromResourceWhichIsAgainFromTheRequest == serviceResourceResolverFromResourceResolverFactory);
LOGGER.debug("Is the resource based resourceResolver same as the ResourceResolverFactory based one? {}",
resourceResolverFromResourceWhichIsAgainFromTheRequest == adminResourceResolverFromResourceResolverFactory);
response.getWriter().write("Title = " + resource.getValueMap().get(JcrConstants.JCR_TITLE) + "\n");
response.getWriter().write("But are all these resource resolvers the same?" + "\n");
response.getWriter().write("Lets compare" + "\n");
response.getWriter().write("Is the resource based resourceResolver same as the request based one? " +
(resourceResolverFromResourceWhichIsAgainFromTheRequest == resourceResolverFromServletRequest) + "\n");
response.getWriter().write("Is the resource based resourceResolver same as the ResourceResolverFactory based one? " +
(resourceResolverFromResourceWhichIsAgainFromTheRequest == serviceResourceResolverFromResourceResolverFactory) + "\n");
response.getWriter().write("Is the resource based resourceResolver same as the ResourceResolverFactory (Administrative) based one? "+
(resourceResolverFromResourceWhichIsAgainFromTheRequest == adminResourceResolverFromResourceResolverFactory));
}
}