Search in sources :

Example 26 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class OverrideStringParser method toMap.

/**
     * Convert JSON object to map.
     * @param json JSON object
     * @return Map (keys/values are not validated)
     */
private static Map<String, Object> toMap(JsonObject json) {
    Map<String, Object> props = new HashMap<>();
    Iterator<String> keys = json.keySet().iterator();
    while (keys.hasNext()) {
        String key = keys.next();
        props.put(key, convertJsonValue(json.get(key)));
    }
    return props;
}
Also used : HashMap(java.util.HashMap) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString)

Example 27 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class Announcement method asClusterView.

/** create a clusterview from json **/
private static ClusterView asClusterView(final String localClusterViewJSON) {
    JsonObject obj = jsonReaderFactory.createReader(new StringReader(localClusterViewJSON)).readObject();
    DefaultClusterView clusterView = new DefaultClusterView(obj.getString("id"));
    JsonArray instancesObj = obj.getJsonArray("instances");
    for (int i = 0; i < instancesObj.size(); i++) {
        JsonObject anInstance = instancesObj.getJsonObject(i);
        clusterView.addInstanceDescription(asInstance(anInstance));
    }
    return clusterView;
}
Also used : JsonArray(javax.json.JsonArray) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject)

Example 28 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class Announcement method fromJSON.

/** Create an announcement form json **/
public static Announcement fromJSON(final String topologyAnnouncementJSON) {
    JsonObject announcement = jsonReaderFactory.createReader(new StringReader(topologyAnnouncementJSON)).readObject();
    final String ownerId = announcement.getString("ownerId");
    final int protocolVersion;
    if (!announcement.containsKey("protocolVersion")) {
        protocolVersion = -1;
    } else {
        protocolVersion = announcement.getInt("protocolVersion");
    }
    final Announcement result = new Announcement(ownerId, protocolVersion);
    if (announcement.containsKey("created")) {
        result.originallyCreatedAt = announcement.getJsonNumber("created").longValue();
    }
    if (announcement.containsKey("backoffInterval")) {
        long backoffInterval = announcement.getJsonNumber("backoffInterval").longValue();
        result.backoffInterval = backoffInterval;
    }
    if (announcement.containsKey("resetBackoff")) {
        boolean resetBackoff = announcement.getBoolean("resetBackoff");
        result.resetBackoff = resetBackoff;
    }
    if (announcement.containsKey("loop") && announcement.getBoolean("loop")) {
        result.setLoop(true);
        return result;
    }
    if (announcement.containsKey("localClusterView")) {
        final String localClusterViewJSON = asJSON(announcement.getJsonObject("localClusterView"));
        final ClusterView localClusterView = asClusterView(localClusterViewJSON);
        result.setLocalCluster(localClusterView);
    }
    if (announcement.containsKey("inherited")) {
        final Boolean inherited = announcement.getBoolean("inherited");
        result.inherited = inherited;
    }
    if (announcement.containsKey("serverInfo")) {
        String serverInfo = announcement.getString("serverInfo");
        result.serverInfo = serverInfo;
    }
    final JsonArray subAnnouncements = announcement.getJsonArray("topologyAnnouncements");
    for (int i = 0; i < subAnnouncements.size(); i++) {
        String subAnnouncementJSON = subAnnouncements.getString(i);
        result.addIncomingTopologyAnnouncement(fromJSON(subAnnouncementJSON));
    }
    return result;
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) JsonArray(javax.json.JsonArray) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject)

Example 29 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class Announcement method correspondsTo.

/**
     * Compare this Announcement with another one, ignoring the 'created'
     * property - which gets added to the JSON object automatically due
     * to SLING-3389 wire-backwards-compatibility - and backoffInterval
     * introduced as part of SLING-3382
     */
public boolean correspondsTo(Announcement announcement) {
    final JsonObject myJson = asJSONObject(true);
    final JsonObject otherJson = announcement.asJSONObject(true);
    return asJSON(myJson).equals(asJSON(otherJson));
}
Also used : JsonObject(javax.json.JsonObject)

Example 30 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class DiscoveryLiteDescriptor method getDescriptorFrom.

/**
     * {"seq":8,"final":true,"id":"aae34e9a-b08d-409e-be10-9ff4106e5387","me":4,"active":[4],"deactivating":[],"inactive":[1,2,3]}
     */
public static DiscoveryLiteDescriptor getDescriptorFrom(ResourceResolver resourceResolver) throws Exception {
    Session session = resourceResolver.adaptTo(Session.class);
    if (session == null) {
        throw new Exception("Could not adapt resourceResolver to session: " + resourceResolver);
    }
    String descriptorStr = session.getRepository().getDescriptor(DiscoveryLiteDescriptor.OAK_DISCOVERYLITE_CLUSTERVIEW);
    if (descriptorStr == null) {
        throw new Exception("No value available for descriptor " + OAK_DISCOVERYLITE_CLUSTERVIEW);
    }
    JsonObject descriptor = jsonReaderFactory.createReader(new StringReader(descriptorStr)).readObject();
    return new DiscoveryLiteDescriptor(descriptor);
}
Also used : StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) Session(javax.jcr.Session)

Aggregations

JsonObject (javax.json.JsonObject)214 Test (org.junit.Test)101 JsonArray (javax.json.JsonArray)58 StringReader (java.io.StringReader)52 Credentials (org.apache.commons.httpclient.Credentials)52 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)52 ArrayList (java.util.ArrayList)51 HashMap (java.util.HashMap)47 NameValuePair (org.apache.commons.httpclient.NameValuePair)43 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)41 JsonString (javax.json.JsonString)31 JsonReader (javax.json.JsonReader)25 HashSet (java.util.HashSet)16 Map (java.util.Map)12 Response (javax.ws.rs.core.Response)12 JsonObjectBuilder (javax.json.JsonObjectBuilder)11 NameValuePairList (org.apache.sling.commons.testing.integration.NameValuePairList)11 StringWriter (java.io.StringWriter)10 JsonException (javax.json.JsonException)10 JsonArrayBuilder (javax.json.JsonArrayBuilder)9