Search in sources :

Example 6 with JsonString

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);
    }
}
Also used : StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonString(javax.json.JsonString) IOException(java.io.IOException) ParseException(org.apache.sling.jcr.contentparser.ParseException) JsonParsingException(javax.json.stream.JsonParsingException)

Example 7 with JsonString

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());
    }
}
Also used : JsonArray(javax.json.JsonArray) Calendar(java.util.Calendar) JsonNumber(javax.json.JsonNumber) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString) ParseException(org.apache.sling.jcr.contentparser.ParseException)

Example 8 with JsonString

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);
    }
}
Also used : JsonException(javax.json.JsonException) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FileNotFoundException(java.io.FileNotFoundException) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) LineIterator(org.apache.commons.io.LineIterator) FilenameFilter(java.io.FilenameFilter) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) FileReader(java.io.FileReader) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) JsonValue(javax.json.JsonValue) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) JsonArray(javax.json.JsonArray) ScheduleOptions(org.apache.sling.commons.scheduler.ScheduleOptions) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 9 with JsonString

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;
}
Also used : JsonException(javax.json.JsonException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with JsonString

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;
}
Also used : JsonString(javax.json.JsonString)

Aggregations

JsonString (javax.json.JsonString)21 JsonObject (javax.json.JsonObject)18 StringReader (java.io.StringReader)14 JsonReader (javax.json.JsonReader)12 Test (org.junit.Test)10 JsonArray (javax.json.JsonArray)5 HashMap (java.util.HashMap)4 JsonException (javax.json.JsonException)4 JsonValue (javax.json.JsonValue)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ParseException (org.apache.sling.jcr.contentparser.ParseException)2 TableRef (com.torodb.core.TableRef)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 FilenameFilter (java.io.FilenameFilter)1 StringWriter (java.io.StringWriter)1 Calendar (java.util.Calendar)1