Search in sources :

Example 1 with JSONSimpleProperty

use of org.exist.util.serializer.json.JSONSimpleProperty in project exist by eXist-db.

the class RESTServer method writeResultJSON.

private void writeResultJSON(final HttpServletResponse response, final DBBroker broker, final Txn transaction, final Sequence results, int howmany, int start, final Properties outputProperties, final boolean wrap, final long compilationTime, final long executionTime) throws BadRequestException {
    // calculate number of results to return
    final int rlen = results.getItemCount();
    if (!results.isEmpty()) {
        if ((start < 1) || (start > rlen)) {
            throw new BadRequestException("Start parameter out of range");
        }
        // FD : correct bound evaluation
        if (((howmany + start) > rlen) || (howmany <= 0)) {
            howmany = rlen - start + 1;
        }
    } else {
        howmany = 0;
    }
    final Serializer serializer = broker.borrowSerializer();
    outputProperties.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
    try {
        serializer.setProperties(outputProperties);
        try (Writer writer = new OutputStreamWriter(response.getOutputStream(), outputProperties.getProperty(OutputKeys.ENCODING))) {
            final JSONObject root = new JSONObject();
            root.addObject(new JSONSimpleProperty("start", Integer.toString(start), true));
            root.addObject(new JSONSimpleProperty("count", Integer.toString(howmany), true));
            root.addObject(new JSONSimpleProperty("hits", Integer.toString(results.getItemCount()), true));
            if (outputProperties.getProperty(Serializer.PROPERTY_SESSION_ID) != null) {
                root.addObject(new JSONSimpleProperty("session", outputProperties.getProperty(Serializer.PROPERTY_SESSION_ID)));
            }
            root.addObject(new JSONSimpleProperty("compilationTime", Long.toString(compilationTime), true));
            root.addObject(new JSONSimpleProperty("executionTime", Long.toString(executionTime), true));
            final JSONObject data = new JSONObject("data");
            root.addObject(data);
            Item item;
            for (int i = --start; i < start + howmany; i++) {
                item = results.itemAt(i);
                if (Type.subTypeOf(item.getType(), Type.NODE)) {
                    final NodeValue value = (NodeValue) item;
                    JSONValue json;
                    if ("json".equals(outputProperties.getProperty("method", "xml"))) {
                        json = new JSONValue(serializer.serialize(value), false);
                        json.setSerializationDataType(JSONNode.SerializationDataType.AS_LITERAL);
                    } else {
                        json = new JSONValue(serializer.serialize(value));
                        json.setSerializationType(JSONNode.SerializationType.AS_ARRAY);
                    }
                    data.addObject(json);
                } else {
                    final JSONValue json = new JSONValue(item.getStringValue());
                    json.setSerializationType(JSONNode.SerializationType.AS_ARRAY);
                    data.addObject(json);
                }
            }
            root.serialize(writer, true);
            writer.flush();
        }
    } catch (final IOException | XPathException | SAXException e) {
        throw new BadRequestException("Error while serializing xml: " + e.toString(), e);
    } finally {
        broker.returnSerializer(serializer);
    }
}
Also used : SAXException(org.xml.sax.SAXException) JSONValue(org.exist.util.serializer.json.JSONValue) JSONObject(org.exist.util.serializer.json.JSONObject) JSONSimpleProperty(org.exist.util.serializer.json.JSONSimpleProperty) XQuerySerializer(org.exist.util.serializer.XQuerySerializer) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer)

Aggregations

Serializer (org.exist.storage.serializers.Serializer)1 SAXSerializer (org.exist.util.serializer.SAXSerializer)1 XQuerySerializer (org.exist.util.serializer.XQuerySerializer)1 JSONObject (org.exist.util.serializer.json.JSONObject)1 JSONSimpleProperty (org.exist.util.serializer.json.JSONSimpleProperty)1 JSONValue (org.exist.util.serializer.json.JSONValue)1 SAXException (org.xml.sax.SAXException)1