Search in sources :

Example 56 with JsonString

use of javax.json.JsonString in project activemq-artemis by apache.

the class JsonUtil method fromJsonArray.

public static Object[] fromJsonArray(final JsonArray jsonArray) throws Exception {
    Object[] array = new Object[jsonArray.size()];
    for (int i = 0; i < jsonArray.size(); i++) {
        Object val = jsonArray.get(i);
        if (val instanceof JsonArray) {
            Object[] inner = fromJsonArray((JsonArray) val);
            array[i] = inner;
        } else if (val instanceof JsonObject) {
            JsonObject jsonObject = (JsonObject) val;
            Map<String, Object> map = new HashMap<>();
            Set<String> keys = jsonObject.keySet();
            for (String key : keys) {
                Object innerVal = jsonObject.get(key);
                if (innerVal instanceof JsonArray) {
                    innerVal = fromJsonArray(((JsonArray) innerVal));
                } else if (innerVal instanceof JsonString) {
                    innerVal = ((JsonString) innerVal).getString();
                } else if (innerVal == JsonValue.FALSE) {
                    innerVal = Boolean.FALSE;
                } else if (innerVal == JsonValue.TRUE) {
                    innerVal = Boolean.TRUE;
                } else if (innerVal instanceof JsonNumber) {
                    JsonNumber jsonNumber = (JsonNumber) innerVal;
                    if (jsonNumber.isIntegral()) {
                        innerVal = jsonNumber.longValue();
                    } else {
                        innerVal = jsonNumber.doubleValue();
                    }
                } else if (innerVal instanceof JsonObject) {
                    Map<String, Object> innerMap = new HashMap<>();
                    JsonObject o = (JsonObject) innerVal;
                    Set<String> innerKeys = o.keySet();
                    for (String k : innerKeys) {
                        innerMap.put(k, o.get(k));
                    }
                    innerVal = innerMap;
                }
                if (CompositeData.class.getName().equals(key)) {
                    Object[] data = (Object[]) innerVal;
                    CompositeData[] cds = new CompositeData[data.length];
                    for (int i1 = 0; i1 < data.length; i1++) {
                        String dataConverted = convertJsonValue(data[i1], String.class).toString();
                        try (ObjectInputStreamWithClassLoader ois = new ObjectInputStreamWithClassLoader(new ByteArrayInputStream(Base64.decode(dataConverted)))) {
                            ois.setWhiteList("java.util,java.lang,javax.management");
                            cds[i1] = (CompositeDataSupport) ois.readObject();
                        }
                    }
                    innerVal = cds;
                }
                map.put(key, innerVal);
            }
            array[i] = map;
        } else if (val instanceof JsonString) {
            array[i] = ((JsonString) val).getString();
        } else if (val == JsonValue.FALSE) {
            array[i] = Boolean.FALSE;
        } else if (val == JsonValue.TRUE) {
            array[i] = Boolean.TRUE;
        } else if (val instanceof JsonNumber) {
            JsonNumber jsonNumber = (JsonNumber) val;
            if (jsonNumber.isIntegral()) {
                array[i] = jsonNumber.longValue();
            } else {
                array[i] = jsonNumber.doubleValue();
            }
        } else {
            if (val == JsonValue.NULL) {
                array[i] = null;
            } else {
                array[i] = val;
            }
        }
    }
    return array;
}
Also used : ObjectInputStreamWithClassLoader(org.apache.activemq.artemis.utils.ObjectInputStreamWithClassLoader) Set(java.util.Set) HashMap(java.util.HashMap) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonArray(javax.json.JsonArray) ByteArrayInputStream(java.io.ByteArrayInputStream) JsonNumber(javax.json.JsonNumber) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) HashMap(java.util.HashMap) Map(java.util.Map)

Example 57 with JsonString

use of javax.json.JsonString in project activemq-artemis by apache.

the class ArtemisFeatureTest method test.

@Test(timeout = 5 * 60 * 1000)
public void test() throws Throwable {
    executeCommand("bundle:list");
    withinReason(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            assertTrue("artemis bundle installed", verifyBundleInstalled("artemis-server-osgi"));
            return true;
        }
    });
    Object service = waitForService("(objectClass=org.apache.activemq.artemis.core.server.ActiveMQServer)", 30000);
    assertNotNull(service);
    LOG.info("have service " + service);
    executeCommand("service:list -n");
    Connection connection = null;
    try {
        JmsConnectionFactory factory = new JmsConnectionFactory("amqp://localhost:5672");
        connection = factory.createConnection(USER, PASSWORD);
        connection.start();
        QueueSession sess = (QueueSession) connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
        Queue queue = sess.createQueue("exampleQueue");
        MessageProducer producer = sess.createProducer(queue);
        producer.send(sess.createTextMessage("TEST"));
        // Test browsing
        try (QueueBrowser browser = sess.createBrowser(queue)) {
            Enumeration messages = browser.getEnumeration();
            while (messages.hasMoreElements()) {
                messages.nextElement();
            }
        }
        // Test management
        Queue managementQueue = sess.createQueue("activemq.management");
        QueueRequestor requestor = new QueueRequestor(sess, managementQueue);
        connection.start();
        TextMessage m = sess.createTextMessage();
        m.setStringProperty("_AMQ_ResourceName", "broker");
        m.setStringProperty("_AMQ_OperationName", "getQueueNames");
        m.setText("[\"ANYCAST\"]");
        Message reply = requestor.request(m);
        String json = ((TextMessage) reply).getText();
        JsonArray array = Json.createReader(new StringReader(json)).readArray();
        List<JsonString> queues = (List<JsonString>) array.get(0);
        assertNotNull(queues);
        assertFalse(queues.isEmpty());
        MessageConsumer consumer = sess.createConsumer(queue);
        Message msg = consumer.receive(5000);
        assertNotNull(msg);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Enumeration(java.util.Enumeration) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) JmsConnectionFactory(org.apache.qpid.jms.JmsConnectionFactory) Connection(javax.jms.Connection) JsonString(javax.json.JsonString) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) JsonArray(javax.json.JsonArray) QueueRequestor(javax.jms.QueueRequestor) StringReader(java.io.StringReader) List(java.util.List) ArrayList(java.util.ArrayList) MessageProducer(javax.jms.MessageProducer) JsonString(javax.json.JsonString) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) QueueBrowser(javax.jms.QueueBrowser) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 58 with JsonString

use of javax.json.JsonString in project activemq-artemis by apache.

the class AddressControlTest method testGetRolesAsJSON.

@Test
public void testGetRolesAsJSON() throws Exception {
    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();
    Role role = new Role(RandomUtil.randomString(), RandomUtil.randomBoolean(), RandomUtil.randomBoolean(), RandomUtil.randomBoolean(), RandomUtil.randomBoolean(), RandomUtil.randomBoolean(), RandomUtil.randomBoolean(), RandomUtil.randomBoolean(), RandomUtil.randomBoolean(), RandomUtil.randomBoolean(), RandomUtil.randomBoolean());
    session.createQueue(address, queue, true);
    AddressControl addressControl = createManagementControl(address);
    String jsonString = addressControl.getRolesAsJSON();
    Assert.assertNotNull(jsonString);
    RoleInfo[] roles = RoleInfo.from(jsonString);
    Assert.assertEquals(0, roles.length);
    Set<Role> newRoles = new HashSet<>();
    newRoles.add(role);
    server.getSecurityRepository().addMatch(address.toString(), newRoles);
    jsonString = addressControl.getRolesAsJSON();
    Assert.assertNotNull(jsonString);
    roles = RoleInfo.from(jsonString);
    Assert.assertEquals(1, roles.length);
    RoleInfo r = roles[0];
    Assert.assertEquals(role.getName(), roles[0].getName());
    Assert.assertEquals(role.isSend(), r.isSend());
    Assert.assertEquals(role.isConsume(), r.isConsume());
    Assert.assertEquals(role.isCreateDurableQueue(), r.isCreateDurableQueue());
    Assert.assertEquals(role.isDeleteDurableQueue(), r.isDeleteDurableQueue());
    Assert.assertEquals(role.isCreateNonDurableQueue(), r.isCreateNonDurableQueue());
    Assert.assertEquals(role.isDeleteNonDurableQueue(), r.isDeleteNonDurableQueue());
    Assert.assertEquals(role.isManage(), r.isManage());
    session.deleteQueue(queue);
}
Also used : Role(org.apache.activemq.artemis.core.security.Role) AddressControl(org.apache.activemq.artemis.api.core.management.AddressControl) RoleInfo(org.apache.activemq.artemis.api.core.management.RoleInfo) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) RandomUtil.randomString(org.apache.activemq.artemis.tests.util.RandomUtil.randomString) JsonString(javax.json.JsonString) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 59 with JsonString

use of javax.json.JsonString in project fql by CategoricalData.

the class XJsonToFQL method translate.

private static String translate(String in) {
    try {
        JsonReader rdr = Json.createReader(new StringReader(in));
        JsonObject obj = rdr.readObject();
        JsonObject graph = obj.getJsonObject("graph");
        JsonArray nodes = graph.getJsonArray("nodes");
        JsonArray edges = graph.getJsonArray("edges");
        JsonArray eqs = graph.getJsonArray("equations");
        Set<String> types = new HashSet<>();
        Set<String> entities = new HashSet<>();
        Set<Triple<String, String, String>> consts = new HashSet<>();
        Set<Triple<String, String, String>> fks = new HashSet<>();
        Set<Pair<List<String>, List<String>>> tfns = new HashSet<>();
        Set<Pair<List<String>, List<String>>> efns = new HashSet<>();
        Set<String> all_e = new HashSet<>();
        for (JsonObject o : nodes.getValuesAs(JsonObject.class)) {
            String id = o.getJsonString("id").toString();
            String ty = o.getJsonString("type").toString();
            if (ty.equals("\"type\"")) {
                types.add(id);
            } else {
                entities.add(id);
                all_e.add(id);
            }
        }
        for (JsonObject o : edges.getValuesAs(JsonObject.class)) {
            String id = o.getJsonString("id").toString();
            String src = o.getJsonString("source").toString();
            String dst = o.getJsonString("target").toString();
            if (!types.contains(src) && !types.contains(dst)) {
                all_e.add(id);
            }
            if (dst.equals("\"_1\"")) {
                continue;
            }
            if (types.contains(src)) {
                consts.add(new Triple<>(id, src, dst));
            } else {
                fks.add(new Triple<>(id, src, dst));
            }
        }
        outer: for (JsonObject o : eqs.getValuesAs(JsonObject.class)) {
            JsonArray lhs = o.getJsonArray("lhs");
            JsonArray rhs = o.getJsonArray("rhs");
            List<String> l = new LinkedList<>();
            List<String> r = new LinkedList<>();
            boolean isty = false;
            for (JsonString x : lhs.getValuesAs(JsonString.class)) {
                String s = x.toString();
                if (s.contains("!_")) {
                    continue outer;
                }
                if (!all_e.contains(s)) {
                    isty = true;
                }
                l.add(s);
            }
            for (JsonString x : rhs.getValuesAs(JsonString.class)) {
                String s = x.toString();
                if (s.contains("!_")) {
                    continue outer;
                }
                if (!all_e.contains(s)) {
                    isty = true;
                }
                r.add(s);
            }
            if (isty) {
                tfns.add(new Pair<>(l, r));
            } else {
                efns.add(new Pair<>(l, r));
            }
        }
        String ret = "";
        for (String ty : types) {
            if (ty.equals("\"_1\"")) {
                continue;
            }
            ret += ty + " : type\n";
        }
        ret += "\n";
        for (Triple<String, String, String> c : consts) {
            if (c.third.equals("\"_1\"")) {
                continue;
            }
            ret += c.second.equals("\"_1\"") ? c.first + " : " + c.third + "\n" : c.first + " : " + c.second + " -> " + c.third + "\n";
        }
        ret += "\n";
        Set<String> temp1 = new HashSet<>();
        for (Pair<List<String>, List<String>> x : tfns) {
            temp1.add(Util.sep(x.first, ".") + " = " + Util.sep(x.second, "."));
        }
        ret += Util.sep(temp1, ",\n");
        ret += "\n";
        ret += "S = schema {\n";
        ret += "nodes\n";
        ret += Util.sep(entities, ",\n");
        ret += ";\n";
        ret += "edges\n";
        ret += Util.sep(fks.stream().map(x -> x.first + " : " + x.second + " -> " + x.third).collect(Collectors.toList()), ",\n");
        ret += ";\n";
        ret += "equations\n";
        Set<String> temp2 = new HashSet<>();
        for (Pair<List<String>, List<String>> x : efns) {
            temp2.add(Util.sep(x.first, ".") + " = " + Util.sep(x.second, "."));
        }
        ret += Util.sep(temp2, ",\n");
        ret += ";\n";
        ret += "}\n";
        return ret;
    } catch (Exception e) {
        e.printStackTrace();
        return e.getMessage();
    }
}
Also used : JSplitPane(javax.swing.JSplitPane) JDialog(javax.swing.JDialog) SwingConstants(javax.swing.SwingConstants) GridLayout(java.awt.GridLayout) HashSet(java.util.HashSet) Json(javax.json.Json) BorderLayout(java.awt.BorderLayout) LinkedList(java.util.LinkedList) JComboBox(javax.swing.JComboBox) JFrame(javax.swing.JFrame) Pair(catdata.Pair) ScrollPaneConstants(javax.swing.ScrollPaneConstants) JsonObject(javax.json.JsonObject) JsonReader(javax.json.JsonReader) JButton(javax.swing.JButton) CodeTextPanel(catdata.ide.CodeTextPanel) JsonArray(javax.json.JsonArray) Util(catdata.Util) Set(java.util.Set) BorderFactory(javax.swing.BorderFactory) JOptionPane(javax.swing.JOptionPane) ActionEvent(java.awt.event.ActionEvent) Collectors(java.util.stream.Collectors) JsonString(javax.json.JsonString) Language(catdata.ide.Language) JScrollPane(javax.swing.JScrollPane) Dimension(java.awt.Dimension) List(java.util.List) StringReader(java.io.StringReader) JLabel(javax.swing.JLabel) Triple(catdata.Triple) Example(catdata.ide.Example) JTextArea(javax.swing.JTextArea) JPanel(javax.swing.JPanel) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonArray(javax.json.JsonArray) Triple(catdata.Triple) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) LinkedList(java.util.LinkedList) List(java.util.List) JsonString(javax.json.JsonString) HashSet(java.util.HashSet) Pair(catdata.Pair)

Example 60 with JsonString

use of javax.json.JsonString in project traccar by traccar.

the class GoogleGeocoder method parseAddress.

@Override
public Address parseAddress(JsonObject json) {
    JsonArray results = json.getJsonArray("results");
    if (!results.isEmpty()) {
        Address address = new Address();
        JsonObject result = (JsonObject) results.get(0);
        JsonArray components = result.getJsonArray("address_components");
        if (result.containsKey("formatted_address")) {
            address.setFormattedAddress(result.getString("formatted_address"));
        }
        for (JsonObject component : components.getValuesAs(JsonObject.class)) {
            String value = component.getString("short_name");
            typesLoop: for (JsonString type : component.getJsonArray("types").getValuesAs(JsonString.class)) {
                switch(type.getString()) {
                    case "street_number":
                        address.setHouse(value);
                        break typesLoop;
                    case "route":
                        address.setStreet(value);
                        break typesLoop;
                    case "locality":
                        address.setSettlement(value);
                        break typesLoop;
                    case "administrative_area_level_2":
                        address.setDistrict(value);
                        break typesLoop;
                    case "administrative_area_level_1":
                        address.setState(value);
                        break typesLoop;
                    case "country":
                        address.setCountry(value);
                        break typesLoop;
                    case "postal_code":
                        address.setPostcode(value);
                        break typesLoop;
                    default:
                        break;
                }
            }
        }
        return address;
    }
    return null;
}
Also used : JsonArray(javax.json.JsonArray) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString)

Aggregations

JsonString (javax.json.JsonString)78 JsonObject (javax.json.JsonObject)54 JsonReader (javax.json.JsonReader)36 StringReader (java.io.StringReader)34 Test (org.junit.Test)30 JsonArray (javax.json.JsonArray)24 HashMap (java.util.HashMap)17 JsonValue (javax.json.JsonValue)16 LinkedList (java.util.LinkedList)10 ArrayList (java.util.ArrayList)8 Map (java.util.Map)8 JsonNumber (javax.json.JsonNumber)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 JsonException (javax.json.JsonException)5 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)5 ProcessGroupStatus (org.apache.nifi.controller.status.ProcessGroupStatus)5 RemoteProcessGroupStatus (org.apache.nifi.controller.status.RemoteProcessGroupStatus)5 POST (javax.ws.rs.POST)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3