use of org.apache.felix.utils.json.JSONParser in project sling by apache.
the class JSONReporterTest method getJSON.
private static Map<String, Object> getJSON(MetricRegistry registry) throws IOException {
StringWriter sw = new StringWriter();
JSONReporter reporter = JSONReporter.forRegistry(registry).outputTo(new PrintStream(new WriterOutputStream(sw))).build();
reporter.report();
reporter.close();
return new JSONParser(sw.toString()).getParsed();
}
use of org.apache.felix.utils.json.JSONParser in project sling by apache.
the class SmokeIT method verifyAllBundlesStarted.
@Test
public void verifyAllBundlesStarted() throws Exception {
try (CloseableHttpClient client = newClient()) {
HttpGet get = new HttpGet("http://localhost:" + LAUNCHPAD_PORT + "/system/console/bundles.json");
try (CloseableHttpResponse response = client.execute(get)) {
if (response.getStatusLine().getStatusCode() != 200) {
fail("Unexpected status line " + response.getStatusLine());
}
Header contentType = response.getFirstHeader("Content-Type");
assertThat("Content-Type header", contentType.getValue(), CoreMatchers.startsWith("application/json"));
Map<String, Object> obj = new JSONParser(response.getEntity().getContent()).getParsed();
@SuppressWarnings("unchecked") List<Object> status = (List<Object>) obj.get("s");
@SuppressWarnings("unchecked") List<Object> bundles = (List<Object>) obj.get("data");
if (bundles.size() < EXPECTED_BUNDLES_COUNT) {
fail("Expected at least " + EXPECTED_BUNDLES_COUNT + " bundles, got " + bundles.size());
}
BundleStatus bs = new BundleStatus(status);
if (bs.resolvedBundles != 0 || bs.installedBundles != 0) {
StringBuilder out = new StringBuilder();
out.append("Expected all bundles to be active, but instead got ").append(bs.resolvedBundles).append(" resolved bundles, ").append(bs.installedBundles).append(" installed bundlles: ");
for (int i = 0; i < bundles.size(); i++) {
@SuppressWarnings("unchecked") Map<String, Object> bundle = (Map<String, Object>) bundles.get(i);
String bundleState = (String) bundle.get("state");
String bundleSymbolicName = (String) bundle.get("symbolicName");
String bundleVersion = (String) bundle.get("version");
switch(bundleState) {
case "Active":
case "Fragment":
continue;
default:
out.append("\n- ").append(bundleSymbolicName).append(" ").append(bundleVersion).append(" is in state ").append(bundleState);
}
}
fail(out.toString());
}
}
}
}
use of org.apache.felix.utils.json.JSONParser in project felix by apache.
the class WebConsolePlugin method doPost.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// $NON-NLS-1$
resp.setContentType("application/json");
// $NON-NLS-1$
resp.setCharacterEncoding("UTF-8");
final PrintWriter out = resp.getWriter();
final JSONWriter jw = new JSONWriter(out);
// $NON-NLS-1$
final String action = req.getParameter("action");
// $NON-NLS-1$
final String role = req.getParameter("role");
// $NON-NLS-1$
final String group = req.getParameter("group");
try {
if ("addMember".equals(action)) {
// $NON-NLS-1$
final Role xrole = userAdmin.getRole(role);
final Group xgroup = (Group) userAdmin.getRole(group);
xgroup.addMember(xrole);
toJSON(jw, xgroup, false);
} else if ("addRequiredMember".equals(action)) {
// $NON-NLS-1$
final Role xrole = userAdmin.getRole(role);
final Group xgroup = (Group) userAdmin.getRole(group);
xgroup.addRequiredMember(xrole);
toJSON(jw, xgroup, false);
} else if ("removeMember".equals(action)) {
// $NON-NLS-1$
final Role xrole = userAdmin.getRole(role);
final Group xgroup = (Group) userAdmin.getRole(group);
xgroup.removeMember(xrole);
toJSON(jw, xgroup, false);
} else if ("toggleMembership".equals(action)) {
// $NON-NLS-1$
final Role xrole = userAdmin.getRole(role);
final Group xgroup = (Group) userAdmin.getRole(group);
if (// if required
contains(xgroup.getRequiredMembers(), xrole)) {
xgroup.removeMember(xrole);
// add as basic
xgroup.addMember(xrole);
} else {
xgroup.removeMember(xrole);
// add as required
xgroup.addRequiredMember(xrole);
}
toJSON(jw, xgroup, false);
} else if ("getDigestAlgorithms".equals(action)) {
// $NON-NLS-1$
getMessageDigestAlgorithms(jw);
} else if ("digest".equals(action)) {
// $NON-NLS-1$
// $NON-NLS-1$
final String dataRaw = req.getParameter("data");
// $NON-NLS-1$
final String algorithm = req.getParameter("algorithm");
final MessageDigest digest = MessageDigest.getInstance(algorithm);
final byte[] encoded = digest.digest(dataRaw.getBytes());
jw.object();
// $NON-NLS-1$
jw.key("encoded");
jw.value(encoded);
jw.endObject();
} else if ("del".equals(action)) {
// $NON-NLS-1$
out.print(userAdmin.removeRole(role));
} else if ("get".equals(action)) {
// $NON-NLS-1$
final Role xrole = userAdmin.getRole(role);
toJSON(jw, xrole, true);
} else if ("set".equals(action)) {
// $NON-NLS-1$
// $NON-NLS-1$
final String dataRaw = req.getParameter("data");
JSONParser parser = new JSONParser(dataRaw);
final Map<String, Object> data = parser.getParsed();
// $NON-NLS-1$
Role xrole = userAdmin.getRole((String) data.get("name"));
if (null == xrole) {
xrole = //
userAdmin.createRole(// $NON-NLS-1$
(String) data.get("name"), // $NON-NLS-1$
(int) (long) (Long) data.get("type"));
}
doSetData(xrole, data);
out.print(true);
} else // list all roles without details
{
Role[] roles = userAdmin.getRoles(null);
toJSON(jw, roles, false);
}
jw.flush();
} catch (Exception e) {
throw new ServletException(e);
}
}
use of org.apache.felix.utils.json.JSONParser in project karaf by apache.
the class SyncopeBackingEngine method listRolesSyncope2.
private List<RolePrincipal> listRolesSyncope2(Principal principal) {
List<RolePrincipal> result = new ArrayList<>();
HttpGet request = new HttpGet(address + "/users/" + principal.getName());
request.setHeader("Content-Type", "application/json");
try {
HttpResponse httpResponse = client.execute(request);
String response = EntityUtils.toString(httpResponse.getEntity());
if (response != null && !response.isEmpty()) {
JSONParser parser = new JSONParser(response);
List<String> roles = (List<String>) parser.getParsed().get("roles");
for (String role : roles) {
result.add(new RolePrincipal(role));
}
}
} catch (Exception e) {
throw new RuntimeException("Error listing roles", e);
}
return result;
}
use of org.apache.felix.utils.json.JSONParser in project karaf by apache.
the class SyncopeBackingEngine method listUsersSyncope2.
private List<UserPrincipal> listUsersSyncope2() {
List<UserPrincipal> users = new ArrayList<>();
HttpGet request = new HttpGet(address + "/users");
request.setHeader("Content-Type", "application/json");
try {
HttpResponse httpResponse = client.execute(request);
String response = EntityUtils.toString(httpResponse.getEntity());
JSONParser parser = new JSONParser(response);
List<Map<String, Object>> results = (List<Map<String, Object>>) parser.getParsed().get("result");
for (Map<String, Object> result : results) {
users.add(new UserPrincipal((String) result.get("username")));
}
} catch (Exception e) {
throw new RuntimeException("Error listing users", e);
}
return users;
}
Aggregations