use of javax.json.JsonString in project sling by apache.
the class JsonContentParser method toJsonObjectWithJsonTicks.
private JsonObject toJsonObjectWithJsonTicks(InputStream is) {
String jsonString;
try {
jsonString = IOUtils.toString(is, CharEncoding.UTF_8);
} catch (IOException ex) {
throw new ParseException("Error getting JSON string.", ex);
}
// convert ticks to double quotes
jsonString = JsonTicksConverter.tickToDoubleQuote(jsonString);
try (JsonReader reader = jsonReaderFactory.createReader(new StringReader(jsonString))) {
return reader.readObject();
} catch (JsonParsingException ex) {
throw new ParseException("Error parsing JSON content: " + ex.getMessage(), ex);
}
}
use of javax.json.JsonString in project sling by apache.
the class JsonContentParser method convertValue.
private Object convertValue(JsonValue value) {
switch(value.getValueType()) {
case STRING:
String stringValue = ((JsonString) value).getString();
Calendar calendarValue = helper.tryParseCalendar(stringValue);
if (calendarValue != null) {
return calendarValue;
} else {
return stringValue;
}
case NUMBER:
JsonNumber numberValue = (JsonNumber) value;
if (numberValue.isIntegral()) {
return numberValue.longValue();
} else {
return numberValue.bigDecimalValue();
}
case TRUE:
return true;
case FALSE:
return false;
case NULL:
return null;
case ARRAY:
JsonArray arrayValue = (JsonArray) value;
Object[] values = new Object[arrayValue.size()];
for (int i = 0; i < values.length; i++) {
values[i] = convertValue(arrayValue.get(i));
}
return helper.convertSingleTypeArray(values);
case OBJECT:
return (JsonObject) value;
default:
throw new ParseException("Unexpected JSON value type: " + value.getValueType());
}
}
use of javax.json.JsonString in project sling by apache.
the class SimpleDistributionQueueProvider method enableQueueProcessing.
public void enableQueueProcessing(@Nonnull DistributionQueueProcessor queueProcessor, String... queueNames) {
if (checkpoint) {
// recover from checkpoints
log.debug("recovering from checkpoints if needed");
for (final String queueName : queueNames) {
log.debug("recovering for queue {}", queueName);
DistributionQueue queue = getQueue(queueName);
FilenameFilter filenameFilter = new FilenameFilter() {
@Override
public boolean accept(File file, String name) {
return name.equals(queueName + "-checkpoint");
}
};
for (File qf : checkpointDirectory.listFiles(filenameFilter)) {
log.info("recovering from checkpoint {}", qf);
try {
LineIterator lineIterator = IOUtils.lineIterator(new FileReader(qf));
while (lineIterator.hasNext()) {
String s = lineIterator.nextLine();
String[] split = s.split(" ");
String id = split[0];
String infoString = split[1];
Map<String, Object> info = new HashMap<String, Object>();
JsonReader reader = Json.createReader(new StringReader(infoString));
JsonObject jsonObject = reader.readObject();
for (Map.Entry<String, JsonValue> entry : jsonObject.entrySet()) {
if (entry.getValue().getValueType().equals(JsonValue.ValueType.ARRAY)) {
JsonArray value = jsonObject.getJsonArray(entry.getKey());
String[] a = new String[value.size()];
for (int i = 0; i < a.length; i++) {
a[i] = value.getString(i);
}
info.put(entry.getKey(), a);
} else if (JsonValue.NULL.equals(entry.getValue())) {
info.put(entry.getKey(), null);
} else {
info.put(entry.getKey(), ((JsonString) entry.getValue()).getString());
}
}
queue.add(new DistributionQueueItem(id, info));
}
log.info("recovered {} items from queue {}", queue.getStatus().getItemsCount(), queueName);
} catch (FileNotFoundException e) {
log.warn("could not read checkpoint file {}", qf.getAbsolutePath());
} catch (JsonException e) {
log.warn("could not parse info from checkpoint file {}", qf.getAbsolutePath());
}
}
}
// enable checkpointing
for (String queueName : queueNames) {
ScheduleOptions options = scheduler.NOW(-1, 15).canRunConcurrently(false).name(getJobName(queueName + "-checkpoint"));
scheduler.schedule(new SimpleDistributionQueueCheckpoint(getQueue(queueName), checkpointDirectory), options);
}
}
// enable processing
for (String queueName : queueNames) {
ScheduleOptions options = scheduler.NOW(-1, 1).canRunConcurrently(false).name(getJobName(queueName));
scheduler.schedule(new SimpleDistributionQueueProcessor(getQueue(queueName), queueProcessor), options);
}
}
use of javax.json.JsonString in project sling by apache.
the class DistributionUtils method getQueueItems.
public static List<Map<String, Object>> getQueueItems(SlingInstance instance, String queueUrl) throws IOException, JsonException {
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
JsonObject json = getResource(instance, queueUrl + ".infinity");
Iterator<String> keys = json.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
JsonObject queueItem;
Object item = json.get(key);
if (item instanceof JsonObject) {
queueItem = (JsonObject) item;
} else if (item instanceof JsonString) {
try {
queueItem = Json.createReader(new StringReader(((JsonString) item).getString())).readObject();
} catch (JsonException ex) {
queueItem = null;
}
} else {
queueItem = null;
}
if (queueItem != null && queueItem.containsKey("id")) {
Map<String, Object> itemProperties = new HashMap<String, Object>();
itemProperties.put("id", queueItem.getString("id"));
itemProperties.put("paths", JsonUtil.unbox(queueItem.get("paths")));
itemProperties.put("action", JsonUtil.unbox(queueItem.get("action")));
itemProperties.put("userid", JsonUtil.unbox(queueItem.get("userid")));
itemProperties.put("attempts", JsonUtil.unbox(queueItem.get("attempts")));
itemProperties.put("time", JsonUtil.unbox(queueItem.get("time")));
itemProperties.put("state", JsonUtil.unbox(queueItem.get("state")));
result.add(itemProperties);
}
}
return result;
}
use of javax.json.JsonString in project sling by apache.
the class JsonResponseTest method assertProperty.
@SuppressWarnings({ "unchecked" })
private static JsonString assertProperty(JsonObject obj, String key, String expected) {
JsonString res = (JsonString) assertProperty(obj, key);
assertEquals(expected, res.getString());
return res;
}
Aggregations