Java Code Examples for org.apache.sling.commons.json.JSONObject#toString()

The following examples show how to use org.apache.sling.commons.json.JSONObject#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MockURLStreamHandler.java    From development with Apache License 2.0 6 votes vote down vote up
private static String respStacks() {
    try {
        JSONObject response = new JSONObject();
        JSONObject state = new JSONObject();
        state.put("label", "OK");
        state.put("id", "OK");
        response.put("state", state);
        response.put("id", "1234");
        response.put("stack", "1234");
        response.put("name", "Appliance1");
        response.put("projectUri",
                "http://test.com/cloud/api/projects/54346");
        JSONObject stack = new JSONObject();
        stack.put("id", "idValue");
        response.put("stack", stack);
        return response.toString();
    } catch (JSONException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 2
Source File: JsonMessageFormatter.java    From otroslogviewer with Apache License 2.0 6 votes vote down vote up
@Override
public String format(String message) {
  final ArrayList<SubText> jsonFragments = jsonFinder.findJsonFragments(message);
  StringBuilder sb = new StringBuilder();
  int lastEnd = 0;
  for (SubText jsonFragment : jsonFragments) {
    sb.append(message.substring(lastEnd, jsonFragment.getStart()));
    final String group = jsonFragment.subString(message);
    String toAppend = group;
    try {
      JSONObject o = new JSONObject(group);
      final String jsonFormatted = o.toString(2);
      toAppend = jsonFormatted;
    } catch (JSONException e) {
      LOGGER.debug("There is no need to format {}", group);
    }
    if (!sb.toString().endsWith("\n")) {
      sb.append("\n");
    }
    sb.append(toAppend).append("\n");
    lastEnd = jsonFragment.getEnd();
  }
  sb.append(message.substring(lastEnd));
  return sb.toString().trim();
}
 
Example 3
Source File: MockURLStreamHandler.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * @param string
 * @return
 */
public static String respFlavor(int id, String flavorName) {
    try {
        JSONObject response = new JSONObject();
        JSONObject flavor = new JSONObject();
        flavor.put("id", id);
        flavor.put("name", flavorName);

        response.put("flavor", flavor);
        return response.toString();
    } catch (JSONException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 4
Source File: MockURLStreamHandler.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * @return
 */
private static String repServers(List<String> serverNames) {
    try {
        JSONObject response = new JSONObject();
        JSONArray servers = new JSONArray();
        for (int i = 0; i < serverNames.size(); i++) {
            JSONObject server = new JSONObject();
            JSONArray links = new JSONArray();
            JSONObject linkSelf = new JSONObject();
            JSONObject linkBookmark = new JSONObject();
            String instanceId = Integer.toString(i) + "-Instance-"
                    + serverNames.get(i);
            server.put("id", instanceId);
            linkSelf.put("href",
                    "http://novaendpoint/v2/servers/" + instanceId);
            linkSelf.put("rel", "self");
            links.put(linkSelf);
            linkBookmark.put("href",
                    "http://novaendpoint/servers/" + instanceId);
            linkBookmark.put("rel", "self");
            links.put(linkBookmark);
            server.put("links", links);
            server.put("name", serverNames.get(i));
            servers.put(server);
        }
        response.put("servers", servers);
        return response.toString();
    } catch (JSONException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 5
Source File: MockURLStreamHandler.java    From development with Apache License 2.0 5 votes vote down vote up
public static String respStacksInstanceName(StackStatus status,
        boolean withStatusReason, String... stackStatusReason) {
    String reason;
    if (stackStatusReason == null || stackStatusReason.length == 0) {
        reason = "SSR";
    } else {
        reason = Arrays.toString(stackStatusReason);
    }
    try {
        JSONObject response = new JSONObject();
        JSONObject stack = new JSONObject();
        response.put("stack", stack);
        stack.put("stack_name", "SN");
        stack.put("id", "ID");
        stack.put("stack_status",
                status == null ? "bullshit" : status.name());
        if (withStatusReason) {
            stack.put("stack_status_reason", reason);
        }
        JSONArray outputs = new JSONArray();
        JSONObject output = new JSONObject();
        output.put("output_key", "OK");
        output.put("output_value", "OV");
        outputs.put(output);
        stack.put("outputs", outputs);
        return response.toString();
    } catch (JSONException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 6
Source File: MockURLStreamHandler.java    From development with Apache License 2.0 5 votes vote down vote up
public static String respStacksResources(List<String> serverNames,
        String resourceType) {
    try {
        JSONObject response = new JSONObject();
        JSONArray resources = new JSONArray();
        response.put("resources", resources);

        JSONObject volume = new JSONObject();
        volume.put("resource_name", "sys-vol");
        volume.put("physical_resource_id", "12345");
        volume.put("resource_type", "OS::Cinder::Volume");
        resources.put(volume);

        for (int i = 0; i < serverNames.size(); i++) {
            JSONObject server = new JSONObject();
            server.put("resource_name", serverNames.get(i));
            server.put("physical_resource_id", Integer.toString(i)
                    + "-Instance-" + serverNames.get(i));
            server.put("resource_type", resourceType);
            resources.put(server);
        }

        return response.toString();
    } catch (JSONException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 7
Source File: MockURLStreamHandler.java    From development with Apache License 2.0 5 votes vote down vote up
public static String respServer(String serverName, String serverId) {
    try {
        JSONObject response = new JSONObject();
        JSONObject server = new JSONObject();
        response.put("server", server);
        server.put("name", serverName);
        server.put("id", serverId);

        return response.toString();
    } catch (JSONException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 8
Source File: MockURLStreamHandler.java    From development with Apache License 2.0 5 votes vote down vote up
public static String respServerDetail(String serverName, String serverId,
        ServerStatus status, String tenant_id) {
    try {
        JSONObject response = new JSONObject();
        JSONObject server = new JSONObject();
        response.put("server", server);
        server.put("name", serverName);
        server.put("id", serverId);
        server.put("status", status);
        server.put("tenant_id", tenant_id);
        server.put("accessIPv4", "192.0.2.0");
        JSONObject flavor = new JSONObject();
        flavor.put("id", 1);
        server.put("flavor", flavor);
        JSONObject addresses = new JSONObject();
        JSONArray networkDetail = new JSONArray();
        JSONObject fixedNetwork = new JSONObject();
        JSONObject floatingNetwork = new JSONObject();
        fixedNetwork.put("OS-EXT-IPS-MAC:mac_addr", "fa:16:3e:e5:b7:f8");
        fixedNetwork.put("version", "4");
        fixedNetwork.put("addr", "192.168.0.4");
        fixedNetwork.put("OS-EXT-IPS:type", "fixed");
        floatingNetwork.put("OS-EXT-IPS-MAC:mac_addr", "fa:16:3e:e5:b7:f8");
        floatingNetwork.put("version", "4");
        floatingNetwork.put("addr", "133.162.161.216");
        floatingNetwork.put("OS-EXT-IPS:type", "floating");
        networkDetail.put(fixedNetwork);
        networkDetail.put(floatingNetwork);
        addresses.put(serverName + "-network", networkDetail);
        server.put("addresses", addresses);

        return response.toString();
    } catch (JSONException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 9
Source File: MockURLStreamHandler.java    From development with Apache License 2.0 4 votes vote down vote up
public static String respTokens(boolean addPublicURL,
        boolean addPublicURLNova, boolean https) {
    try {
        JSONObject response = new JSONObject();
        JSONObject token = new JSONObject();
        JSONArray serviceCatalog = new JSONArray();

        JSONObject endpointsHeat = new JSONObject();
        JSONArray endpointsListHeat = new JSONArray();
        String httpMethod = https == true ? "https://" : "http://";
        endpointsHeat.put("endpoints", endpointsListHeat);
        if (addPublicURL) {
            JSONObject publicUrl = new JSONObject();
            publicUrl.put("name", KeystoneClient.TYPE_HEAT);
            publicUrl.put("url", httpMethod + "heatendpoint/");
            publicUrl.put("interface", "public");
            endpointsListHeat.put(publicUrl);
        }

        endpointsHeat.put("type", KeystoneClient.TYPE_HEAT);
        serviceCatalog.put(endpointsHeat);

        JSONObject endpointsNova = new JSONObject();

        JSONArray endpointsListNova = new JSONArray();
        endpointsNova.put("endpoints", endpointsListNova);
        if (addPublicURLNova) {
            JSONObject publicUrlNova = new JSONObject();
            publicUrlNova.put("name", KeystoneClient.TYPE_NOVA);
            publicUrlNova.put("url", httpMethod + "novaendpoint/");
            endpointsListNova.put(publicUrlNova);
        }

        endpointsNova.put("type", KeystoneClient.TYPE_NOVA);
        serviceCatalog.put(endpointsNova);

        token.put("id", "authId");
        token.put("catalog", serviceCatalog);

        response.put("token", token);

        return response.toString();
    } catch (JSONException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 10
Source File: MockURLStreamHandler.java    From development with Apache License 2.0 4 votes vote down vote up
public static String respServerActions() {
    JSONObject response = new JSONObject();
    return response.toString();
}
 
Example 11
Source File: MockURLStreamHandler.java    From development with Apache License 2.0 4 votes vote down vote up
private static String respStacksInstance4sIdActions() {
    JSONObject response = new JSONObject();
    return response.toString();
}
 
Example 12
Source File: MockURLStreamHandler.java    From development with Apache License 2.0 4 votes vote down vote up
public static String respStackDeleted() {
    JSONObject response = new JSONObject();
    return response.toString();
}