}
@Override
public Representation represent(Variant variant)
{
StringRepresentation representation = null;
String clusterName = getValue("clusterName");
String constraintTypeStr = getValue("constraintType").toUpperCase();
String constraintId = getValue("constraintId");
try {
ConstraintType constraintType = ConstraintType.valueOf(constraintTypeStr);
ZkClient zkClient =
(ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
// ClusterSetup setupTool = new ClusterSetup(zkClient);
HelixAdmin admin = new ZKHelixAdmin(zkClient); // setupTool.getClusterManagementTool();
ZNRecord record = admin.getConstraints(clusterName, constraintType).getRecord();
if (constraintId == null) {
// get all message constraints
representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(record),
MediaType.APPLICATION_JSON);
} else {
// get a specific constraint
Map<String, String> constraint = record.getMapField(constraintId);
if (constraint == null) {
representation = new StringRepresentation("No constraint of type: "
+ constraintType + " associated with id: " + constraintId, MediaType.APPLICATION_JSON);
} else {
ZNRecord subRecord = new ZNRecord(record.getId());
subRecord.setMapField(constraintId, constraint);
representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(subRecord),
MediaType.APPLICATION_JSON);
}
}
}
catch (IllegalArgumentException e) {
representation = new StringRepresentation("constraint-type: " + constraintTypeStr + " not recognized.",
MediaType.APPLICATION_JSON);
}
catch (Exception e)
{
String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
representation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
LOG.error("", e);
}
return representation;
}