Search in sources :

Example 1 with JSONParser

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();
}
Also used : PrintStream(java.io.PrintStream) StringWriter(java.io.StringWriter) JSONParser(org.apache.felix.utils.json.JSONParser) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream)

Example 2 with JSONParser

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());
            }
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpGet(org.apache.http.client.methods.HttpGet) Header(org.apache.http.Header) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JSONParser(org.apache.felix.utils.json.JSONParser) List(java.util.List) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap) Test(org.junit.Test)

Example 3 with JSONParser

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);
    }
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) Group(org.osgi.service.useradmin.Group) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Role(org.osgi.service.useradmin.Role) ServletException(javax.servlet.ServletException) JSONParser(org.apache.felix.utils.json.JSONParser) MessageDigest(java.security.MessageDigest) PrintWriter(java.io.PrintWriter)

Example 4 with JSONParser

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;
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) JSONParser(org.apache.felix.utils.json.JSONParser) ArrayList(java.util.ArrayList) List(java.util.List) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal)

Example 5 with JSONParser

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;
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) JSONParser(org.apache.felix.utils.json.JSONParser) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Aggregations

JSONParser (org.apache.felix.utils.json.JSONParser)5 List (java.util.List)3 HttpGet (org.apache.http.client.methods.HttpGet)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 HttpResponse (org.apache.http.HttpResponse)2 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 MessageDigest (java.security.MessageDigest)1 ServletException (javax.servlet.ServletException)1 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)1 JSONWriter (org.apache.felix.utils.json.JSONWriter)1 Header (org.apache.http.Header)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 RolePrincipal (org.apache.karaf.jaas.boot.principal.RolePrincipal)1 UserPrincipal (org.apache.karaf.jaas.boot.principal.UserPrincipal)1 Test (org.junit.Test)1