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