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;
}
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();
}
}
}
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);
}
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();
}
}
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;
}
Aggregations