PrincipalsServlet.java
5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.i18n.I18n
* javax.jcr.AccessDeniedException
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.UnsupportedRepositoryOperationException
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* javax.servlet.http.HttpServletResponse
* org.apache.commons.lang3.math.NumberUtils
* org.apache.jackrabbit.api.JackrabbitSession
* org.apache.jackrabbit.api.security.principal.PrincipalIterator
* org.apache.jackrabbit.api.security.principal.PrincipalManager
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.osgi.framework.BundleContext
* org.slf4j.Logger
*/
package com.day.crx.delite.impl.servlets;
import com.day.cq.i18n.I18n;
import com.day.crx.delite.impl.AbstractServlet;
import com.day.crx.delite.impl.support.RequestData;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.security.Principal;
import java.security.acl.Group;
import java.util.Iterator;
import javax.jcr.AccessDeniedException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.UnsupportedRepositoryOperationException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.principal.PrincipalIterator;
import org.apache.jackrabbit.api.security.principal.PrincipalManager;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class PrincipalsServlet
extends AbstractServlet {
public PrincipalsServlet(BundleContext bc) {
super(bc);
}
@Override
protected void doService(HttpServletRequest request, HttpServletResponse res, Session session) throws ServletException, IOException {
try {
if (!(session instanceof JackrabbitSession)) {
throw new UnsupportedRepositoryOperationException(I18n.get((HttpServletRequest)request, (String)"The repository does not support principal"));
}
res.setContentType("application/json");
res.setCharacterEncoding("UTF-8");
JSONWriter writer = new JSONWriter((Writer)res.getWriter());
RequestData req = new RequestData(request);
String name = req.getParameter("name");
String[] categories = req.getParameterValues("category");
if (categories == null) {
this.printEmpty(writer);
return;
}
int start = NumberUtils.toInt((String)req.getParameter("start"));
int limit = NumberUtils.toInt((String)req.getParameter("limit"));
if (limit == 0) {
this.printEmpty(writer);
return;
}
int searchType = this.getSearchType(categories);
PrincipalManager principalMgr = ((JackrabbitSession)session).getPrincipalManager();
PrincipalIterator it = name == null || name.length() == 0 ? principalMgr.getPrincipals(searchType) : principalMgr.findPrincipals(name, searchType);
this.skip(it, start);
writer.object();
writer.key("success").value(true);
writer.key("rows").array();
int i = 0;
while (it.hasNext() && i++ < limit) {
Principal p = it.nextPrincipal();
writer.object();
writer.key("name").value((Object)p.getName());
writer.key("type").value((Object)(p instanceof Group ? "group" : "user"));
writer.endObject();
}
writer.endArray();
writer.endObject();
}
catch (UnsupportedRepositoryOperationException e) {
this.logger.info("Unsupported repository operation", (Throwable)e);
res.sendError(501, e.getMessage());
}
catch (AccessDeniedException e) {
this.logger.info("Access denied getting principal manager", (Throwable)e);
res.sendError(403);
}
catch (RepositoryException e) {
this.logger.error("Error occur getting principals", (Throwable)e);
res.sendError(500, e.getMessage());
}
catch (JSONException e) {
this.logger.error("Error occur getting principals", (Throwable)e);
res.sendError(500, e.getMessage());
}
}
private void skip(Iterator<?> it, int start) {
int i = 0;
while (it.hasNext() && i++ < start) {
it.next();
}
}
private void printEmpty(JSONWriter writer) throws JSONException {
writer.object();
writer.key("success").value(true);
writer.key("total").value(0);
writer.key("rows").array().endArray();
writer.endObject();
}
private int getSearchType(String[] categories) {
int type = -1000;
for (String c : categories) {
if (type == -1000) {
if ("user".equals(c)) {
type = 1;
continue;
}
if (!"group".equals(c)) continue;
type = 2;
continue;
}
type = 3;
}
return type;
}
}