use of com.sun.identity.sm.SMSException in project OpenAM by OpenRock.
the class SmsCollectionProvider method queryCollection.
/**
* Queries for child instances of config. The parent config referenced by the request path is found, and
* all child config for the type is returned.
* <p>
* Note that only query filter is supported, and only a filter of value {@code true} (i.e. all values).
* Sorting and paging are not supported.
* {@inheritDoc}
*/
@Override
public Promise<QueryResponse, ResourceException> queryCollection(Context context, QueryRequest request, QueryResourceHandler handler) {
if (!"true".equals(request.getQueryFilter().toString())) {
return new NotSupportedException("Query not supported: " + request.getQueryFilter()).asPromise();
}
if (request.getPagedResultsCookie() != null || request.getPagedResultsOffset() > 0 || request.getPageSize() > 0) {
return new NotSupportedException("Query paging not currently supported").asPromise();
}
try {
ServiceConfigManager scm = getServiceConfigManager(context);
String realm = realmFor(context);
if (subSchemaPath.isEmpty()) {
Set<String> instanceNames = new TreeSet<String>(scm.getInstanceNames());
for (String instanceName : instanceNames) {
ServiceConfig config = type == SchemaType.GLOBAL ? scm.getGlobalConfig(instanceName) : scm.getOrganizationConfig(realm, instanceName);
if (config != null) {
JsonValue value = getJsonValue(realm, config);
handler.handleResource(newResourceResponse(instanceName, String.valueOf(value.hashCode()), value));
}
}
} else {
ServiceConfig config = parentSubConfigFor(context, scm);
Set<String> names = config.getSubConfigNames("*", lastSchemaNodeName());
for (String configName : names) {
JsonValue value = getJsonValue(realm, config.getSubConfig(configName));
handler.handleResource(newResourceResponse(configName, String.valueOf(value.hashCode()), value));
}
}
return newResultPromise(newQueryResponse());
} catch (SMSException e) {
debug.warning("::SmsCollectionProvider:: SMSException on query", e);
return new InternalServerErrorException("Unable to query SMS config: " + e.getMessage()).asPromise();
} catch (SSOException e) {
debug.warning("::SmsCollectionProvider:: SSOException on query", e);
return new InternalServerErrorException("Unable to query SMS config: " + e.getMessage()).asPromise();
}
}
use of com.sun.identity.sm.SMSException in project OpenAM by OpenRock.
the class SmsCollectionProvider method readInstance.
/**
* Reads a child instance of config. The parent config referenced by the request path is found, and
* the config is read using the resourceId.
* {@inheritDoc}
*/
@Override
public Promise<ResourceResponse, ResourceException> readInstance(Context context, String resourceId, ReadRequest request) {
try {
ServiceConfigManager scm = getServiceConfigManager(context);
ServiceConfig config = parentSubConfigFor(context, scm);
ServiceConfig item = checkedInstanceSubConfig(context, resourceId, config);
JsonValue result = getJsonValue(realmFor(context), item);
return newResultPromise(newResourceResponse(resourceId, String.valueOf(result.hashCode()), result));
} catch (SMSException e) {
debug.warning("::SmsCollectionProvider:: SMSException on read", e);
return new InternalServerErrorException("Unable to read SMS config: " + e.getMessage()).asPromise();
} catch (SSOException e) {
debug.warning("::SmsCollectionProvider:: SSOException on read", e);
return new InternalServerErrorException("Unable to read SMS config: " + e.getMessage()).asPromise();
} catch (NotFoundException e) {
return e.asPromise();
}
}
use of com.sun.identity.sm.SMSException in project OpenAM by OpenRock.
the class SmsCollectionProvider method deleteInstance.
/**
* Deletes a child instance of config. The parent config referenced by the request path is found, and
* the config is deleted using the resourceId.
* {@inheritDoc}
*/
@Override
public Promise<ResourceResponse, ResourceException> deleteInstance(Context context, final String resourceId, DeleteRequest request) {
try {
ServiceConfigManager scm = getServiceConfigManager(context);
ServiceConfig config = parentSubConfigFor(context, scm);
checkedInstanceSubConfig(context, resourceId, config);
if (isDefaultCreatedAuthModule(context, resourceId)) {
scm.removeOrganizationConfiguration(realmFor(context), null);
} else {
config.removeSubConfig(resourceId);
}
return awaitDeletion(context, resourceId).then(new Function<Void, ResourceResponse, ResourceException>() {
@Override
public ResourceResponse apply(Void aVoid) {
return newResourceResponse(resourceId, "0", json(object(field("success", true))));
}
});
} catch (ServiceNotFoundException e) {
debug.warning("::SmsCollectionProvider:: ServiceNotFoundException on delete", e);
return new NotFoundException("Unable to delete SMS config: " + e.getMessage()).asPromise();
} catch (SMSException e) {
debug.warning("::SmsCollectionProvider:: SMSException on delete", e);
return new InternalServerErrorException("Unable to delete SMS config: " + e.getMessage()).asPromise();
} catch (SSOException e) {
debug.warning("::SmsCollectionProvider:: SSOException on delete", e);
return new InternalServerErrorException("Unable to delete SMS config: " + e.getMessage()).asPromise();
} catch (NotFoundException e) {
return e.asPromise();
}
}
use of com.sun.identity.sm.SMSException in project OpenAM by OpenRock.
the class SmsJsonConverter method toJson.
/**
* Will validate the Map representation of the service configuration against the serviceSchema and return a
* corresponding JSON representation
*
* @param attributeValuePairs The schema attribute values.
* @param realm The realm, or null if global.
* @return Json representation of attributeValuePairs
*/
public JsonValue toJson(String realm, Map<String, Set<String>> attributeValuePairs) {
if (!initialised) {
init();
}
final boolean validAttributes;
try {
if (realm == null) {
validAttributes = schema.validateAttributes(attributeValuePairs);
} else {
validAttributes = schema.validateAttributes(attributeValuePairs, realm);
}
} catch (SMSException e) {
debug.error("schema validation threw an exception while validating the attributes: realm=" + realm + " attributes: " + attributeValuePairs, e);
throw new JsonException("Unable to validate attributes", e);
}
JsonValue parentJson = json(new HashMap<String, Object>());
if (validAttributes) {
for (String attributeName : attributeValuePairs.keySet()) {
String jsonResourceName = attributeNameToResourceName.get(attributeName);
String name;
if (jsonResourceName != null) {
name = jsonResourceName;
} else {
name = attributeName;
}
AttributeSchema attributeSchema = schema.getAttributeSchema(attributeName);
if (shouldBeIgnored(attributeName)) {
continue;
}
AttributeSchema.Type type = attributeSchema.getType();
final Set<String> object = attributeValuePairs.get(attributeName);
Object jsonAttributeValue = null;
if (type == null) {
throw new JsonException("Type not defined.");
}
AttributeSchemaConverter attributeSchemaConverter = attributeSchemaConverters.get(name);
if (isASingleValue(type)) {
if (!object.isEmpty()) {
jsonAttributeValue = attributeSchemaConverter.toJson(object.iterator().next());
}
} else if (containsMultipleValues(type)) {
if (isAMap(attributeSchema.getUIType())) {
Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> itr = object.iterator();
while (itr.hasNext()) {
Pair<String, String> entry = nameValueParser.parse(itr.next());
map.put(entry.getFirst(), attributeSchemaConverter.toJson(entry.getSecond()));
}
jsonAttributeValue = map;
} else {
List<Object> list = new ArrayList<Object>();
Iterator<String> itr = object.iterator();
while (itr.hasNext()) {
list.add(attributeSchemaConverter.toJson(itr.next()));
}
jsonAttributeValue = list;
}
}
String sectionName = attributeNameToSection.get(attributeName);
if (sectionName != null) {
parentJson.putPermissive(new JsonPointer("/" + sectionName + "/" + name), jsonAttributeValue);
} else {
parentJson.put(name, jsonAttributeValue);
}
}
} else {
throw new JsonException("Invalid attributes");
}
return parentJson;
}
use of com.sun.identity.sm.SMSException in project OpenAM by OpenRock.
the class SmsJsonConverter method fromJson.
/**
* Will validate the Json representation of the service configuration against the serviceSchema for a realm,
* and return a corresponding Map representation.
*
* @param jsonValue The request body.
* @param realm The realm, or null if global.
* @return Map representation of jsonValue
*/
public Map<String, Set<String>> fromJson(String realm, JsonValue jsonValue) throws JsonException, BadRequestException {
if (!initialised) {
init();
}
Map<String, Set<String>> result = new HashMap<>();
if (jsonValue == null || jsonValue.isNull()) {
return result;
}
Map<String, Object> translatedAttributeValuePairs = getTranslatedAttributeValuePairs(jsonValue.asMap());
for (String attributeName : translatedAttributeValuePairs.keySet()) {
// Ignore _id field used to name resource when creating
if (ResourceResponse.FIELD_CONTENT_ID.equals(attributeName)) {
continue;
}
if (shouldNotBeUpdated(attributeName)) {
throw new BadRequestException("Invalid attribute, '" + attributeName + "', specified");
}
if (shouldBeIgnored(attributeName)) {
continue;
}
final Object attributeValue = translatedAttributeValuePairs.get(attributeName);
Set<String> value = new HashSet<>();
if (attributeValue instanceof HashMap) {
final HashMap<String, Object> attributeMap = (HashMap<String, Object>) attributeValue;
for (String name : attributeMap.keySet()) {
value.add("[" + name + "]=" + convertJsonToString(attributeName, attributeMap.get(name)));
}
} else if (attributeValue instanceof List) {
List<Object> attributeArray = (ArrayList<Object>) attributeValue;
for (Object val : attributeArray) {
value.add(convertJsonToString(attributeName, val));
}
} else {
value.add(convertJsonToString(attributeName, attributeValue));
}
result.put(attributeName, value);
}
try {
if (result.isEmpty() || (realm == null && schema.validateAttributes(result)) || (realm != null && schema.validateAttributes(result, realm))) {
return result;
} else {
throw new JsonException("Invalid attributes");
}
} catch (InvalidAttributeValueException e) {
throw new BadRequestException(e.getLocalizedMessage(), e);
} catch (SMSException e) {
throw new JsonException("Unable to validate attributes", e);
}
}
Aggregations