/*
* 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 legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions copyright [year] [name of copyright owner]".
*
* Copyright 2016 ForgeRock AS.
*
*/
package org.forgerock.opendj.rest2ldap;
import static org.forgerock.opendj.rest2ldap.Rest2ldapMessages.ERR_UNRECOGNIZED_SUB_RESOURCE_TYPE;
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.LocalizedIllegalArgumentException;
import org.forgerock.json.resource.BadRequestException;
import org.forgerock.json.resource.NotFoundException;
import org.forgerock.json.resource.RequestHandler;
import org.forgerock.json.resource.ResourceException;
import org.forgerock.json.resource.Router;
import org.forgerock.opendj.ldap.DN;
import org.forgerock.services.context.Context;
import org.forgerock.util.Function;
/**
* Defines a parent-child relationship between a parent resource and one or more child resource(s). Removal of the
* parent resource implies that the children (the sub-resources) are also removed. There are two types of
* sub-resource:
*
* - {@link SubResourceSingleton} represents a one-to-one relationship supporting read, update, patch, and action
* requests
* - {@link SubResourceCollection} represents a one-to-many relationship supporting all requests.
*
*/
public abstract class SubResource {
private final String resourceId;
private DnTemplate dnTemplate;
String urlTemplate = "";
String dnTemplateString = "";
boolean isReadOnly = false;
Rest2Ldap rest2Ldap;
Resource resource;
SubResource(final String resourceId) {
this.resourceId = resourceId;
}
@Override
public final boolean equals(final Object o) {
return this == o || (o instanceof SubResource && urlTemplate.equals(((SubResource) o).urlTemplate));
}
@Override
public final int hashCode() {
return urlTemplate.hashCode();
}
@Override
public final String toString() {
return urlTemplate;
}
final Resource getResource() {
return resource;
}
final void build(final Rest2Ldap rest2Ldap, final String parent) {
this.rest2Ldap = rest2Ldap;
this.resource = rest2Ldap.getResource(resourceId);
if (resource == null) {
throw new LocalizedIllegalArgumentException(ERR_UNRECOGNIZED_SUB_RESOURCE_TYPE.get(parent, resourceId));
}
this.dnTemplate = DnTemplate.compileRelative(dnTemplateString);
}
abstract Router addRoutes(Router router);
/** A 404 indicates that this instance is not also a collection, so return a more helpful message. */
static Function convert404To400(final LocalizableMessage msg) {
return new Function() {
@Override
public T apply(final ResourceException e) throws ResourceException {
if (e instanceof NotFoundException) {
throw new BadRequestException(msg.toString());
}
throw e;
}
};
}
final RequestHandler readOnly(final RequestHandler handler) {
return isReadOnly ? new ReadOnlyRequestHandler(handler) : handler;
}
final DN dnFrom(final Context context) {
return dnTemplate.format(context);
}
final RequestHandler subResourceRouterFrom(final RoutingContext context) {
return context.getType().getSubResourceRouter();
}
}