ConfigObjectService.java revision 331c327f500d45223e8ad933fd4472740a75aeea
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2011-2015 ForgeRock AS. All Rights Reserved
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*/
/**
* Provides access to OSGi configuration
*
*/
name = "org.forgerock.openidm.config.manage",
immediate = true,
)
@Properties({
})
final static QueryFilterVisitor<QueryFilter<JsonPointer>, Object, JsonPointer> VISITOR = new ConfigQueryFilterVisitor<Object>();
enum ConfigAction {
}
/**
* The ClusterManagementService used for sending and receiving cluster events
*/
}
public void unbindClusterManagementService(final ClusterManagementService clusterManagementService) {
this.clusterManagementService = null;
}
/** The Connection Factory */
protected ConnectionFactory connectionFactory;
/** Enhanced configuration service. */
protected EnhancedConfig enhancedConfig;
private ConfigCrypto configCrypto;
public Promise<ResourceResponse, ResourceException> handleRead(final Context context, final ReadRequest request) {
try {
return newResultPromise(newResourceResponse(request.getResourcePath(), null, new JsonValue(read(request.getResourcePathObject()))));
} catch (ResourceException e) {
return newExceptionPromise(e);
} catch (Exception e) {
}
}
/**
* Gets an object from the object set by identifier.
* <p/>
* The object may contain metadata properties, including object identifier {@code _id},
* and object version {@code _rev} to enable optimistic concurrency supported by OpenIDM.
*
* @param resourceName the identifier of the resource to retrieve from the object set.
* @return the requested object.
* @throws NotFoundException if the specified object could not be found.
* @throws ForbiddenException if access to the object is forbidden.
* @throws BadRequestException if the passed identifier is invalid
*/
@SuppressWarnings("rawtypes")
try {
if (resourceName.isEmpty()) {
// List all configurations
if (null != rawConfigs) {
if (properties != null) {
}
// If there is an alias for factory config is available, make a nicer ID then the internal PID
: pid;
}
}
} else {
}
}
} catch (ResourceException ex) {
throw ex;
throw new InternalServerErrorException("Failure to load configuration for " + resourceName + ": " + ex.getMessage(), ex);
}
return result;
}
public Promise<ResourceResponse, ResourceException> handleCreate(Context context, CreateRequest request) {
try {
// Create and send the ClusterEvent for the created configuration
sendClusterEvent(ConfigAction.CREATE, request.getResourcePathObject(), request.getNewResourceId(), content.asMap());
} catch (ResourceException e) {
return newExceptionPromise(e);
} catch (Exception e) {
}
}
/**
* Creates a new object in the object set.
* <p/>
* This method sets the {@code _id} property to the assigned identifier for the object,
* and the {@code _rev} property to the revised object version (For optimistic concurrency)
*
* @param resourceName for multi-instance config, the factory pid to use
* @param id the client-generated identifier to use, or {@code null} if server-generated identifier is requested.
* @param obj the contents of the object to create in the object set.
* @throws NotFoundException if the specified id could not be resolved.
* @throws ForbiddenException if access to the object or object set is forbidden.
* @throws PreconditionFailedException if an object with the same ID already exists.
* @throws BadRequestException if the passed identifier is invalid
*/
public void create(ResourcePath resourceName, String id, Map<String, Object> obj, boolean allowExisting) throws ResourceException {
logger.debug("Invoking create configuration {} {} {}", new Object[] { resourceName.toString(), id, obj });
throw new BadRequestException("The passed identifier to create is null");
}
try {
if (parsedId.isFactoryConfig()) {
throw new PreconditionFailedException("Can not create a new factory configuration with ID "
+ parsedId + ", configuration for this ID already exists.");
}
throw new BadRequestException("router config can not be factory config");
}
} else {
}
throw new PreconditionFailedException("Can not create a new configuration with ID "
+ parsedId + ", configuration for this ID already exists.");
}
Dictionary dict = configCrypto.encrypt(parsedId.getPidOrFactoryPid(), parsedId.instanceAlias, null, new JsonValue(obj));
if (parsedId.isFactoryConfig()) {
dict.put(JSONConfigInstaller.SERVICE_FACTORY_PID_ALIAS, parsedId.instanceAlias); // The alias for the PID as understood by fileinstall
}
} catch (ResourceException ex) {
throw ex;
} catch (JsonValueException ex) {
logger.warn("Invalid configuration provided for {}:" + ex.getMessage(), resourceName.toString(), ex);
throw new BadRequestException("Invalid configuration provided for " + resourceName.toString() + ": " + ex.getMessage(), ex);
} catch (WaitForMetaData ex) {
logger.info("No meta-data provider available yet to create and encrypt configuration for {}, retry later.", parsedId.toString(), ex);
throw new InternalServerErrorException("No meta-data provider available yet to create and encrypt configuration for "
throw new InternalServerErrorException("Failure to create configuration for " + parsedId.toString() + ": " + ex.getMessage(), ex);
}
}
public Promise<ResourceResponse, ResourceException> handleUpdate(Context context, UpdateRequest request) {
try {
// Create and send the ClusterEvent for the updated configuration
} catch (ResourceException e) {
return newExceptionPromise(e);
} catch (Exception e) {
}
}
/**
* Updates the specified object in the object set.
* <p/>
* This implementation requires MVCC and hence enforces that clients state what revision they expect
* to be updating
* <p/>
* If successful, this method updates metadata properties within the passed object,
* including: a new {@code _rev} value for the revised object's version
*
* @param resourceName the identifier of the resource to be updated
* @param rev the version of the object to update; or {@code null} if not provided.
* @param obj the contents of the object to put in the object set.
* @throws ConflictException if version is required but is {@code null}.
* @throws ForbiddenException if access to the object is forbidden.
* @throws NotFoundException if the specified object could not be found.
* @throws PreconditionFailedException if version did not match the existing object in the set.
* @throws BadRequestException if the passed identifier is invalid
*/
@SuppressWarnings("rawtypes")
public void update(ResourcePath resourceName, String rev, Map<String, Object> obj) throws ResourceException {
if (resourceName.isEmpty()) {
throw new BadRequestException("The passed identifier to update is empty");
}
throw new BadRequestException("The passed identifier to update has more than two parts");
}
try {
if (existingConfig == null) {
throw new NotFoundException("No existing configuration found for " + resourceName.toString() + ", can not update the configuration.");
}
existingConfig = configCrypto.encrypt(parsedId.getPidOrFactoryPid(), parsedId.instanceAlias, existingConfig, new JsonValue(obj));
logger.debug("Updated existing configuration for {} with {}", resourceName.toString(), existingConfig);
} catch (ResourceException ex) {
throw ex;
} catch (JsonValueException ex) {
logger.warn("Invalid configuration provided for {}:" + ex.getMessage(), resourceName.toString(), ex);
throw new BadRequestException("Invalid configuration provided for " + resourceName.toString() + ": " + ex.getMessage(), ex);
} catch (WaitForMetaData ex) {
logger.info("No meta-data provider available yet to update and encrypt configuration for {}, retry later.", parsedId.toString(), ex);
throw new InternalServerErrorException("No meta-data provider available yet to create and encrypt configuration for "
throw new InternalServerErrorException("Failure to update configuration for " + resourceName.toString() + ": " + ex.getMessage(), ex);
}
}
public Promise<ResourceResponse, ResourceException> handleDelete(Context context, DeleteRequest request) {
try {
// Create and send the ClusterEvent for the deleted configuration
} catch (ResourceException e) {
return newExceptionPromise(e);
} catch (Exception e) {
}
}
/**
* Deletes the specified object from the object set.
*
* @param resourceName the identifier of the resource to be deleted.
* @param rev the version of the object to delete or {@code null} if not provided.
* @return the deleted object.
* @throws NotFoundException if the specified object could not be found.
* @throws ForbiddenException if access to the object is forbidden.
* @throws ConflictException if version is required but is {@code null}.
* @throws PreconditionFailedException if version did not match the existing object in the set.
*/
@SuppressWarnings("rawtypes")
if (resourceName.isEmpty()) {
throw new BadRequestException("The passed identifier to delete is null");
}
try {
throw new NotFoundException("No existing configuration found for " + resourceName.toString() + ", can not delete the configuration.");
}
if (existingConfig == null) {
throw new NotFoundException("No existing configuration found for " + resourceName.toString() + ", can not delete the configuration.");
}
return value;
} catch (ResourceException ex) {
throw ex;
throw new InternalServerErrorException("Failure to delete configuration for " + resourceName.toString() + ": " + ex.getMessage(), ex);
}
}
public Promise<ResourceResponse, ResourceException> handlePatch(Context context, PatchRequest request) {
}
public Promise<QueryResponse, ResourceException> handleQuery(final Context context, final QueryRequest request, final QueryResourceHandler handler) {
}
try {
QueryResponse response = connectionFactory.getConnection().query(context, queryRequest, new QueryResourceHandler() {
return true;
}
});
return newResultPromise(response);
} catch (ResourceException e) {
return newExceptionPromise(e);
}
}
public Promise<ActionResponse, ResourceException> handleAction(Context context, ActionRequest request) {
}
/**
* Locate an existing configuration based on its id, which can be
* a pid or for factory configurations the <factory pid>/<alias>
* pids can be qualified or if they use the default openidm prefix unqualified
*
* @param fullId the id
* @return the configuration if found, null if not
* @throws IOException
* @throws InvalidSyntaxException
*/
Configuration findExistingConfiguration(ParsedId parsedId) throws IOException, InvalidSyntaxException, BadRequestException {
if (parsedId.isFactoryConfig()) {
filter = "(&(" + ConfigurationAdmin.SERVICE_FACTORYPID + "=" + factoryPid + ")(" + JSONConfigInstaller.SERVICE_FACTORY_PID_ALIAS + "=" + parsedId.instanceAlias + "))";
} else {
}
return configurations[0];
} else {
return null;
}
}
}
}
case CUSTOM:
try {
String id = details.get(EVENT_RESOURCE_ID).isNull() ? null : details.get(EVENT_RESOURCE_ID).asString();
Map<String, Object> obj = details.get(EVENT_RESOURCE_OBJECT).isNull() ? null : details.get(EVENT_RESOURCE_OBJECT).asMap();
switch (action) {
case CREATE:
break;
case UPDATE:
break;
case DELETE:
break;
}
} catch (Exception e) {
return false;
}
default:
return true;
}
}
/**
* Creates and sends a ClusterEvent representing a config operation for a specified resource
*
* @param action The action that was performed on the resource (create, update, or delete)
* @param name The resource name
* @param id The new resource id (used for create)
* @param obj The resource object (used for create and update)
*/
private void sendClusterEvent(ConfigAction action, ResourcePath name, String id, Map<String, Object> obj) {
details);
}
}
}
}
}
/**
* A {@link QueryFilterVisitor} implementation which modifies the {@link JsonPointer} fields by prepending them
* with the appropriate key where the full config object is located.
*/
private static class ConfigQueryFilterVisitor<P> implements QueryFilterVisitor<QueryFilter<JsonPointer>, P, JsonPointer> {
public QueryFilter<JsonPointer> visitAndFilter(P parameter, List<QueryFilter<JsonPointer>> subFilters) {
}
}
public QueryFilter<JsonPointer> visitContainsFilter(P parameter, JsonPointer field, Object valueAssertion) {
}
public QueryFilter<JsonPointer> visitEqualsFilter(P parameter, JsonPointer field, Object valueAssertion) {
}
public QueryFilter<JsonPointer> visitExtendedMatchFilter(P parameter, JsonPointer field, String operator, Object valueAssertion) {
}
public QueryFilter<JsonPointer> visitGreaterThanFilter(P parameter, JsonPointer field, Object valueAssertion) {
}
public QueryFilter<JsonPointer> visitGreaterThanOrEqualToFilter(P parameter, JsonPointer field, Object valueAssertion) {
}
public QueryFilter<JsonPointer> visitLessThanFilter(P parameter, JsonPointer field, Object valueAssertion) {
}
public QueryFilter<JsonPointer> visitLessThanOrEqualToFilter(P parameter, JsonPointer field, Object valueAssertion) {
}
}
public QueryFilter<JsonPointer> visitOrFilter(P parameter, List<QueryFilter<JsonPointer>> subFilters) {
}
}
public QueryFilter<JsonPointer> visitStartsWithFilter(P parameter, JsonPointer field, Object valueAssertion) {
}
/**
* Visits each {@link QueryFilter} in a list of filters and returns a list of the
* visited filters.
*
* @param subFilters a list of the filters to visit
* @return a list of visited filters
*/
private List<QueryFilter<JsonPointer>> visitQueryFilters(List<QueryFilter<JsonPointer>> subFilters) {
}
return visitedFilters;
}
/**
* Prepends the fields in the configuration object with the key where the object is stored.
* Will not modify fields outside of the configuration object.
*
* @param field a {@link JsonPointer} representing the field to modify.
* @return a {@link JsonPointer} representing the modified field
*/
return field;
}
}
}
/**
* In order to use findExistingConfiguration() you must have a ParsedId. This method provides that as
* a package-private and avoids the "containing class" error.
*
* @param name
* @param id
* @return
* @throws BadRequestException
*/
}
/**
* A class for converting resource names and IDs to qualified PIDs that represent managed services.
*/
class ParsedId {
public String factoryPid;
public String instanceAlias;
// OSGi pid with spaces is disallowed; replace any spaces we get to be kind
: resourceName;
case 2:
break;
case 1:
break;
default:
throw new BadRequestException("The passed resourceName has more than two parts");
}
if (null != factoryPid) {
} else {
}
}
if (resourceName.isEmpty()) {
// single-instance config
} else {
// multi-instance config
instanceAlias = id;
}
}
/**
* @return is this ID represents a managed factory configuration, or false if it is a managed service configuraiton
*/
public boolean isFactoryConfig() {
return (instanceAlias != null);
}
/**
* Get the qualified pid of the managed service or managed factory depending on the configuration represented
* Some APIs do not distinguish between single managed service PID and managed factory PID
*
* @return the qualified pid if this ID represents a managed service configuration, or the managed factory PID
* if it represents a managed factory configuration
*/
public String getPidOrFactoryPid() {
return isFactoryConfig()
}
return isFactoryConfig()
: pid;
}
}
}