Search in sources :

Example 1 with JsonParser

use of org.apache.jackrabbit.commons.json.JsonParser in project jackrabbit by apache.

the class JsonDiffHandler method extractValues.

private Value[] extractValues(String diffValue) throws RepositoryException, DiffException, IOException {
    ValuesHandler hndlr = new ValuesHandler();
    // surround diff value { key : } to make it parsable
    new JsonParser(hndlr).parse("{\"a\":" + diffValue + "}");
    return hndlr.getValues();
}
Also used : JsonParser(org.apache.jackrabbit.commons.json.JsonParser)

Example 2 with JsonParser

use of org.apache.jackrabbit.commons.json.JsonParser in project jackrabbit by apache.

the class JsonDiffHandler method extractValue.

private Value extractValue(String diffValue) throws RepositoryException, DiffException, IOException {
    ValueHandler hndlr = new ValueHandler();
    // surround diff value { key : } to make it parsable
    new JsonParser(hndlr).parse("{\"a\":" + diffValue + "}");
    return hndlr.getValue();
}
Also used : JsonParser(org.apache.jackrabbit.commons.json.JsonParser)

Example 3 with JsonParser

use of org.apache.jackrabbit.commons.json.JsonParser in project jackrabbit by apache.

the class JsonDiffHandler method addNode.

private void addNode(String parentPath, String nodeName, String diffValue) throws DiffException, RepositoryException {
    Item item = session.getItem(parentPath);
    if (!item.isNode()) {
        throw new ItemNotFoundException(parentPath);
    }
    Node parent = (Node) item;
    try {
        NodeHandler hndlr = new NodeHandler(parent, nodeName);
        new JsonParser(hndlr).parse(diffValue);
    } catch (IOException e) {
        if (e instanceof DiffException) {
            throw (DiffException) e;
        } else {
            throw new DiffException(e.getMessage(), e);
        }
    }
}
Also used : Item(javax.jcr.Item) Node(javax.jcr.Node) IOException(java.io.IOException) ItemNotFoundException(javax.jcr.ItemNotFoundException) JsonParser(org.apache.jackrabbit.commons.json.JsonParser)

Example 4 with JsonParser

use of org.apache.jackrabbit.commons.json.JsonParser in project jackrabbit by apache.

the class RepositoryServiceImpl method getItemInfos.

/**
     * @see RepositoryService#getItemInfos(SessionInfo, ItemId)
     */
@Override
public Iterator<? extends ItemInfo> getItemInfos(SessionInfo sessionInfo, ItemId itemId) throws RepositoryException {
    if (!itemId.denotesNode()) {
        PropertyInfo propertyInfo = getPropertyInfo(sessionInfo, (PropertyId) itemId);
        return Iterators.singleton(propertyInfo);
    } else {
        NodeId nodeId = (NodeId) itemId;
        Path path = getPath(itemId, sessionInfo);
        String uri = getURI(path, sessionInfo);
        int depth = batchReadConfig.getDepth(path, this.getNamePathResolver(sessionInfo));
        HttpGet request = new HttpGet(uri + "." + depth + ".json");
        HttpResponse response = null;
        try {
            response = executeRequest(sessionInfo, request);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == DavServletResponse.SC_OK) {
                HttpEntity entity = response.getEntity();
                if (entity.getContentLength() == 0) {
                    // no JSON response -> no such node on the server
                    throw new ItemNotFoundException("No such item " + nodeId);
                }
                NamePathResolver resolver = getNamePathResolver(sessionInfo);
                NodeInfoImpl nInfo = new NodeInfoImpl(nodeId, path);
                ItemInfoJsonHandler handler = new ItemInfoJsonHandler(resolver, nInfo, getRootURI(sessionInfo), getQValueFactory(sessionInfo), getPathFactory(), getIdFactory());
                JsonParser ps = new JsonParser(handler);
                ps.parse(entity.getContent(), ContentType.get(entity).getCharset().name());
                Iterator<? extends ItemInfo> it = handler.getItemInfos();
                if (!it.hasNext()) {
                    throw new ItemNotFoundException("No such node " + uri);
                }
                return handler.getItemInfos();
            } else {
                throw ExceptionConverter.generate(new DavException(statusCode, "Unable to retrieve NodeInfo for " + uri), request);
            }
        } catch (IOException e) {
            log.error("Internal error while retrieving NodeInfo.", e);
            throw new RepositoryException(e.getMessage());
        } finally {
            request.releaseConnection();
        }
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) NamePathResolver(org.apache.jackrabbit.spi.commons.conversion.NamePathResolver) HttpEntity(org.apache.http.HttpEntity) DavException(org.apache.jackrabbit.webdav.DavException) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) NodeId(org.apache.jackrabbit.spi.NodeId) PropertyInfo(org.apache.jackrabbit.spi.PropertyInfo) ItemNotFoundException(javax.jcr.ItemNotFoundException) JsonParser(org.apache.jackrabbit.commons.json.JsonParser)

Example 5 with JsonParser

use of org.apache.jackrabbit.commons.json.JsonParser in project sling by apache.

the class JcrResourceBundle method loadJsonDictionary.

private void loadJsonDictionary(Resource resource, final Map<String, Object> targetDictionary) {
    log.info("Loading json dictionary: {}", resource.getPath());
    // use streaming parser (we don't need the dict in memory twice)
    JsonParser parser = new JsonParser(new JsonHandler() {

        private String key;

        @Override
        public void key(String key) throws IOException {
            this.key = key;
        }

        @Override
        public void value(String value) throws IOException {
            targetDictionary.put(key, value);
        }

        @Override
        public void object() throws IOException {
        }

        @Override
        public void endObject() throws IOException {
        }

        @Override
        public void array() throws IOException {
        }

        @Override
        public void endArray() throws IOException {
        }

        @Override
        public void value(boolean value) throws IOException {
        }

        @Override
        public void value(long value) throws IOException {
        }

        @Override
        public void value(double value) throws IOException {
        }
    });
    final InputStream stream = resource.adaptTo(InputStream.class);
    if (stream != null) {
        String encoding = "utf-8";
        final ResourceMetadata metadata = resource.getResourceMetadata();
        if (metadata.getCharacterEncoding() != null) {
            encoding = metadata.getCharacterEncoding();
        }
        try {
            parser.parse(stream, encoding);
        } catch (IOException e) {
            log.warn("Could not parse i18n json dictionary {}: {}", resource.getPath(), e.getMessage());
        } finally {
            try {
                stream.close();
            } catch (IOException ignore) {
            }
        }
    } else {
        log.warn("Not a json file: {}", resource.getPath());
    }
}
Also used : InputStream(java.io.InputStream) JsonHandler(org.apache.jackrabbit.commons.json.JsonHandler) IOException(java.io.IOException) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata) JsonParser(org.apache.jackrabbit.commons.json.JsonParser)

Aggregations

JsonParser (org.apache.jackrabbit.commons.json.JsonParser)5 IOException (java.io.IOException)3 ItemNotFoundException (javax.jcr.ItemNotFoundException)2 InputStream (java.io.InputStream)1 Item (javax.jcr.Item)1 Node (javax.jcr.Node)1 RepositoryException (javax.jcr.RepositoryException)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1 JsonHandler (org.apache.jackrabbit.commons.json.JsonHandler)1 NodeId (org.apache.jackrabbit.spi.NodeId)1 Path (org.apache.jackrabbit.spi.Path)1 PropertyInfo (org.apache.jackrabbit.spi.PropertyInfo)1 NamePathResolver (org.apache.jackrabbit.spi.commons.conversion.NamePathResolver)1 DavException (org.apache.jackrabbit.webdav.DavException)1 ResourceMetadata (org.apache.sling.api.resource.ResourceMetadata)1