本文介绍了自定义sling的userManager的JSON呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用sling的userManager REST接口构建应用程序的管理UI,但我想自定义json呈现.例如,我想要获取组" 仅在请求者是成员的情况下才包括成员.

I am trying to build my application's admin UI using sling's userManager REST interface, but I would like to customize the json rendering. For example, I would like the response of "Get group" to include the members only if the requestor is a member.

我从添加libs/sling/group/json.esp开始,但是我不明白如何获得默认响应并对其进行自定义.即使我必须从头开始查询并形成json,在哪里可以找到有关可用于从JCR/Sling获取此数据的API的信息?

I started by adding libs/sling/group/json.esp but I don't understand how I can get hold of the default response and customize it. Even if I had to query and form the json from scratch, where can I find information about APIs available to get this data from JCR/Sling?

我发现我可以使用ResourceTraversor以json格式转储资源对象,但可以在其中使用新的Packages.org.apache.sling.servlets.get.impl.helpers.ResourceTraversor(-1,10000,resource,true). esp引发错误

I found that I could use ResourceTraversor to dump the resource object in json form but using new Packages.org.apache.sling.servlets.get.impl.helpers.ResourceTraversor(-1, 10000, resource, true) in the esp throws up an error

推荐答案

这里有几点需要注意.

首先,应避免将代码放在libs目录下.您的应用程序代码应位于apps目录下.尝试为URI解析servlet时,Sling将在检查libs之前先检查apps,因此,如果您需要完全覆盖Sling附带的功能,则可以将代码放在apps中.

First, you should avoid putting your code under the libs directory. Your app code should live under the apps directory. When attempting to resolve a servlet for a URI, Sling will check apps before it checks libs so if you need to completely override functionality delivered with Sling, you would place your code in apps.

第二,当您请求http://localhost:8080/system/userManager/group/administrators.tidy.1.json时发生的事情(可能取决于您的设置方式)是该请求正在由Sling的默认GET servlet处理,因为它找不到其他适用的脚本或servlet.出于研究目的,可能有必要查看默认get servlet org.apache.sling.servlets.get.impl.DefaultGetServlet的代码,以查看其用于呈现JSON的方式.如果您需要以不同于默认GET Servlet的方式来处理用户组的呈现,则需要创建一个Servlet,该Servlet侦听类型为sling/group的资源的请求.为此可能理想的是创建一个servlet并在OSGI中注册它. http://sling.apache.org/site/servlets.html 提供了各种属性您需要进行设置以确保servlet解析器找到您的servlet.然后,您的servlet将处理该请求,因此可以直接轻松地访问所请求的资源.

Second, what is (probably, depending on how you have things setup) happening when you request http://localhost:8080/system/userManager/group/administrators.tidy.1.json is the request is being handled by Sling's default GET servlet, because it finds no other script or servlet which is applicable. For research purposes it might be worth looking at the code for the default get servlet, org.apache.sling.servlets.get.impl.DefaultGetServlet, to see what it's using to render JSON. If you need to handle the rendering of a user group in a manner different than what the default GET servlet is doing, then you would need to create a servlet which is listening for requests for resources of type sling/group. It would probably be ideal to create a servlet for this purpose and register it with OSGI. http://sling.apache.org/site/servlets.html provides the various properties you would need to set to ensure the servlet resolver finds your servlet. Your servlet then would handle the request and as such would have direct and easy access to the requested resource.

第三,您指定的特定需求是您不希望组成员呈现,除非发出请求的用户是所请求的组的成员.与渲染问题相比,这更多是访问控制问题. Sling和Jackrabbit开箱即用,对您可能希望如何设置应用程序的假设尽可能少.在这种情况下,您需要建立适用于您特定用例的访问控制. Jackrabbit Wiki中有关访问控制的Wiki帖子( http://wiki.apache.org/jackrabbit/AccessControl )在某种程度上涉及到这一点.

Third, the particular need you specified is that you do not want the group members to render unless the requesting user is a member of the group requested. This is more of an access control issue than a rendering issue. Sling and Jackrabbit, out of the box, make as few assumptions as possible concerning how you might want your application to be setup. That being the case, you need to establish the access controls that are applicable for your particular use case. The wiki post on Access Control in the Jackrabbit wiki ( http://wiki.apache.org/jackrabbit/AccessControl ) goes into this to an extent.

这篇关于自定义sling的userManager的JSON呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:12