Search in sources :

Example 91 with SMSException

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();
    }
}
Also used : ServiceConfig(com.sun.identity.sm.ServiceConfig) SMSException(com.sun.identity.sm.SMSException) TreeSet(java.util.TreeSet) JsonValue(org.forgerock.json.JsonValue) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) SSOException(com.iplanet.sso.SSOException) NotSupportedException(org.forgerock.json.resource.NotSupportedException) ServiceConfigManager(com.sun.identity.sm.ServiceConfigManager)

Example 92 with SMSException

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();
    }
}
Also used : ServiceConfig(com.sun.identity.sm.ServiceConfig) SMSException(com.sun.identity.sm.SMSException) JsonValue(org.forgerock.json.JsonValue) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) NotFoundException(org.forgerock.json.resource.NotFoundException) ServiceNotFoundException(com.sun.identity.sm.ServiceNotFoundException) SSOException(com.iplanet.sso.SSOException) ServiceConfigManager(com.sun.identity.sm.ServiceConfigManager)

Example 93 with SMSException

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();
    }
}
Also used : Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ServiceConfig(com.sun.identity.sm.ServiceConfig) SMSException(com.sun.identity.sm.SMSException) ServiceNotFoundException(com.sun.identity.sm.ServiceNotFoundException) NotFoundException(org.forgerock.json.resource.NotFoundException) ServiceNotFoundException(com.sun.identity.sm.ServiceNotFoundException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ResourceException(org.forgerock.json.resource.ResourceException) SSOException(com.iplanet.sso.SSOException) ServiceConfigManager(com.sun.identity.sm.ServiceConfigManager)

Example 94 with SMSException

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;
}
Also used : JsonException(org.forgerock.json.JsonException) SMSException(com.sun.identity.sm.SMSException) JsonValue(org.forgerock.json.JsonValue) JsonPointer(org.forgerock.json.JsonPointer) AttributeSchema(com.sun.identity.sm.AttributeSchema) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HashBiMap(org.forgerock.guava.common.collect.HashBiMap) BiMap(org.forgerock.guava.common.collect.BiMap) Pair(org.forgerock.util.Pair)

Example 95 with SMSException

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);
    }
}
Also used : JsonException(org.forgerock.json.JsonException) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) SMSException(com.sun.identity.sm.SMSException) InvalidAttributeValueException(com.sun.identity.sm.InvalidAttributeValueException) BadRequestException(org.forgerock.json.resource.BadRequestException) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) HashSet(java.util.HashSet)

Aggregations

SMSException (com.sun.identity.sm.SMSException)704 SSOException (com.iplanet.sso.SSOException)525 Set (java.util.Set)272 HashSet (java.util.HashSet)200 SSOToken (com.iplanet.sso.SSOToken)185 Map (java.util.Map)166 ServiceConfig (com.sun.identity.sm.ServiceConfig)164 HashMap (java.util.HashMap)158 CLIException (com.sun.identity.cli.CLIException)149 ServiceSchema (com.sun.identity.sm.ServiceSchema)138 Iterator (java.util.Iterator)133 ServiceSchemaManager (com.sun.identity.sm.ServiceSchemaManager)131 ServiceConfigManager (com.sun.identity.sm.ServiceConfigManager)104 IOutput (com.sun.identity.cli.IOutput)96 IdRepoException (com.sun.identity.idm.IdRepoException)86 OrganizationConfigManager (com.sun.identity.sm.OrganizationConfigManager)84 AMConsoleException (com.sun.identity.console.base.model.AMConsoleException)83 AttributeSchema (com.sun.identity.sm.AttributeSchema)66 IOException (java.io.IOException)55 List (java.util.List)51