use of javax.json.JsonException in project sling by apache.
the class AnnouncementRegistryImpl method addAllExcept.
@Override
public synchronized void addAllExcept(final Announcement target, final ClusterView clusterView, final AnnouncementFilter filter) {
ResourceResolver resourceResolver = null;
try {
resourceResolver = resourceResolverFactory.getServiceResourceResolver(null);
final Resource clusterInstancesResource = ResourceHelper.getOrCreateResource(resourceResolver, config.getClusterInstancesPath());
final Iterator<Resource> it0 = clusterInstancesResource.getChildren().iterator();
Resource announcementsResource;
while (it0.hasNext()) {
final Resource aClusterInstanceResource = it0.next();
final String instanceId = aClusterInstanceResource.getName();
//TODO: add ClusterView.contains(instanceSlingId) for convenience to next api change
if (!contains(clusterView, instanceId)) {
// (corresponds to earlier expiry-handling)
continue;
}
announcementsResource = aClusterInstanceResource.getChild("announcements");
if (announcementsResource == null) {
continue;
}
Iterator<Resource> it = announcementsResource.getChildren().iterator();
while (it.hasNext()) {
Resource anAnnouncement = it.next();
if (logger.isDebugEnabled()) {
logger.debug("addAllExcept: anAnnouncement=" + anAnnouncement);
}
Announcement topologyAnnouncement;
topologyAnnouncement = Announcement.fromJSON(anAnnouncement.adaptTo(ValueMap.class).get("topologyAnnouncement", String.class));
if (filter != null && !filter.accept(aClusterInstanceResource.getName(), topologyAnnouncement)) {
continue;
}
target.addIncomingTopologyAnnouncement(topologyAnnouncement);
}
}
// even before SLING-3389 this method only did read operations,
// hence no commit was ever necessary. The close happens in the finally block
} catch (LoginException e) {
logger.error("handleEvent: could not log in administratively: " + e, e);
throw new RuntimeException("Could not log in to repository (" + e + ")", e);
} catch (PersistenceException e) {
logger.error("handleEvent: got a PersistenceException: " + e, e);
throw new RuntimeException("Exception while talking to repository (" + e + ")", e);
} catch (JsonException e) {
logger.error("handleEvent: got a JSONException: " + e, e);
throw new RuntimeException("Exception while converting json (" + e + ")", e);
} finally {
if (resourceResolver != null) {
resourceResolver.close();
}
}
}
use of javax.json.JsonException in project sling by apache.
the class OverrideStringParser method parse.
/**
* Parses a list of override strings from a override provider.
* @param overrideStrings Override strings
* @return Override objects
*/
public static Collection<OverrideItem> parse(Collection<String> overrideStrings) {
List<OverrideItem> result = new ArrayList<>();
for (String overrideString : overrideStrings) {
// check if override generic pattern is matched
Matcher matcher = OVERRIDE_PATTERN.matcher(StringUtils.defaultString(overrideString));
if (!matcher.matches()) {
log.warn("Ignore config override string - invalid syntax: {}", overrideString);
continue;
}
// get single parts
String path = StringUtils.trim(matcher.group(2));
String configName = StringUtils.trim(matcher.group(3));
String value = StringUtils.trim(StringUtils.defaultString(matcher.group(4)));
OverrideItem item;
try {
// check if value is JSON = defines whole parameter map for a config name
JsonObject json = toJson(value);
if (json != null) {
item = new OverrideItem(path, configName, toMap(json), true);
} else {
// otherwise it defines a key/value pair in a single line
String propertyName = StringUtils.substringAfterLast(configName, "/");
if (StringUtils.isEmpty(propertyName)) {
log.warn("Ignore config override string - missing property name: {}", overrideString);
continue;
}
configName = StringUtils.substringBeforeLast(configName, "/");
Map<String, Object> props = new HashMap<>();
props.put(propertyName, convertJsonValue(value));
item = new OverrideItem(path, configName, props, false);
}
} catch (JsonException ex) {
log.warn("Ignore config override string - invalid JSON syntax ({}): {}", ex.getMessage(), overrideString);
continue;
}
// validate item
if (!isValid(item, overrideString)) {
continue;
}
// if item does not contain a full property set try to merge with existing one
if (!item.isAllProperties()) {
boolean foundMatchingItem = false;
for (OverrideItem existingItem : result) {
if (!existingItem.isAllProperties() && StringUtils.equals(item.getPath(), existingItem.getPath()) && StringUtils.equals(item.getConfigName(), existingItem.getConfigName())) {
existingItem.getProperties().putAll(item.getProperties());
foundMatchingItem = true;
break;
}
}
if (foundMatchingItem) {
continue;
}
}
// add item to result
result.add(item);
}
return result;
}
use of javax.json.JsonException in project sling by apache.
the class ServletJsonUtils method writeJson.
public static void writeJson(SlingHttpServletResponse response, int status, String message, @Nullable Map<String, String> kv) throws IOException {
JsonObjectBuilder json = Json.createObjectBuilder();
try {
json.add("message", message);
if (kv != null && kv.size() > 0) {
for (Map.Entry<String, String> entry : kv.entrySet()) {
json.add(entry.getKey(), entry.getValue());
}
}
} catch (JsonException e) {
log.error("Cannot write json", e);
}
response.setStatus(status);
append(json.build(), response.getWriter());
}
use of javax.json.JsonException in project sling by apache.
the class JsonPipe method getOutput.
/**
* in case there is no successful retrieval of some JSON data, we cut the pipe here
* @return input resource of the pipe, can be reouputed N times in case output json binding is an array of
* N element (output binding would be here each time the Nth element of the array)
*/
public Iterator<Resource> getOutput() {
Iterator<Resource> output = EMPTY_ITERATOR;
binding = null;
String jsonString = retrieveJSONString();
if (StringUtils.isNotBlank(jsonString)) {
try {
JsonStructure json;
try {
json = JsonUtil.parse(jsonString);
} catch (JsonException ex) {
json = null;
}
if (json == null) {
binding = jsonString.trim();
output = super.getOutput();
} else if (json.getValueType() != ValueType.ARRAY) {
binding = JsonUtil.unbox(json);
output = super.getOutput();
} else {
binding = array = (JsonArray) json;
index = 0;
output = new Iterator<Resource>() {
@Override
public boolean hasNext() {
return index < array.size();
}
@Override
public Resource next() {
try {
binding = JsonUtil.unbox(array.get(index));
} catch (Exception e) {
logger.error("Unable to retrieve {}nth item of jsonarray", index, e);
}
index++;
return getInput();
}
};
}
} catch (JsonException e) {
logger.error("unable to parse JSON {} ", jsonString, e);
}
}
return output;
}
use of javax.json.JsonException in project sling by apache.
the class ServletJsonUtils method writeJson.
public static void writeJson(SlingHttpServletResponse response, DistributionResponse distributionResponse) throws IOException {
JsonObjectBuilder json = Json.createObjectBuilder();
try {
json.add("success", distributionResponse.isSuccessful());
json.add("state", distributionResponse.getState().name());
json.add("message", distributionResponse.getMessage());
} catch (JsonException e) {
log.error("Cannot write json", e);
}
switch(distributionResponse.getState()) {
case DISTRIBUTED:
response.setStatus(200);
break;
case DROPPED:
response.setStatus(400);
break;
case ACCEPTED:
response.setStatus(202);
break;
default:
// TODO
break;
}
append(json.build(), response.getWriter());
}
Aggregations