| | |
| | | */ |
| | | package org.forgerock.opendj.rest2ldap; |
| | | |
| | | import static org.forgerock.opendj.rest2ldap.Rest2Ldap.DECODE_OPTIONS; |
| | | import static org.forgerock.opendj.rest2ldap.Rest2ldapMessages.ERR_UNRECOGNIZED_SUB_RESOURCE_TYPE; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | import org.forgerock.http.routing.UriRouterContext; |
| | | import org.forgerock.i18n.LocalizableMessage; |
| | | import org.forgerock.i18n.LocalizedIllegalArgumentException; |
| | | import org.forgerock.json.resource.BadRequestException; |
| | |
| | | import org.forgerock.json.resource.ResourceException; |
| | | import org.forgerock.json.resource.Router; |
| | | import org.forgerock.opendj.ldap.DN; |
| | | import org.forgerock.opendj.ldap.schema.Schema; |
| | | import org.forgerock.services.context.Context; |
| | | import org.forgerock.util.Function; |
| | | |
| | |
| | | * </ul> |
| | | */ |
| | | public abstract class SubResource { |
| | | private static final Pattern TEMPLATE_KEY_RE = Pattern.compile("\\{([^}]+)\\}"); |
| | | |
| | | private final String resourceId; |
| | | private final List<String> dnTemplateVariables = new ArrayList<>(); |
| | | private String dnTemplateFormatString; |
| | | private DnTemplate dnTemplate; |
| | | |
| | | String urlTemplate = ""; |
| | | String dnTemplate = ""; |
| | | String dnTemplateString = ""; |
| | | boolean isReadOnly = false; |
| | | Rest2Ldap rest2Ldap; |
| | | Resource resource; |
| | |
| | | if (resource == null) { |
| | | throw new LocalizedIllegalArgumentException(ERR_UNRECOGNIZED_SUB_RESOURCE_TYPE.get(parent, resourceId)); |
| | | } |
| | | this.dnTemplateFormatString = formatTemplate(dnTemplate, dnTemplateVariables); |
| | | } |
| | | |
| | | // Parse the template keys and replace them with %s for formatting. |
| | | private String formatTemplate(final String template, final List<String> templateVariables) { |
| | | final Matcher matcher = TEMPLATE_KEY_RE.matcher(template); |
| | | final StringBuffer buffer = new StringBuffer(template.length()); |
| | | while (matcher.find()) { |
| | | matcher.appendReplacement(buffer, "%s"); |
| | | templateVariables.add(matcher.group(1)); |
| | | } |
| | | matcher.appendTail(buffer); |
| | | return buffer.toString(); |
| | | this.dnTemplate = DnTemplate.compileRelative(dnTemplateString); |
| | | } |
| | | |
| | | abstract Router addRoutes(Router router); |
| | |
| | | } |
| | | |
| | | final DN dnFrom(final Context context) { |
| | | final DN baseDn = context.containsContext(RoutingContext.class) |
| | | ? context.asContext(RoutingContext.class).getDn() : DN.rootDN(); |
| | | |
| | | final Schema schema = rest2Ldap.getOptions().get(DECODE_OPTIONS).getSchemaResolver().resolveSchema(dnTemplate); |
| | | if (dnTemplateVariables.isEmpty()) { |
| | | final DN relativeDn = DN.valueOf(dnTemplate, schema); |
| | | return baseDn.child(relativeDn); |
| | | } else { |
| | | final UriRouterContext uriRouterContext = context.asContext(UriRouterContext.class); |
| | | final Map<String, String> uriTemplateVariables = uriRouterContext.getUriTemplateVariables(); |
| | | final String[] values = new String[dnTemplateVariables.size()]; |
| | | for (int i = 0; i < values.length; i++) { |
| | | final String key = dnTemplateVariables.get(i); |
| | | values[i] = uriTemplateVariables.get(key); |
| | | } |
| | | final DN relativeDn = DN.format(dnTemplateFormatString, schema, (Object[]) values); |
| | | return baseDn.child(relativeDn); |
| | | } |
| | | return dnTemplate.format(context); |
| | | } |
| | | |
| | | final RequestHandler subResourceRouterFrom(final RoutingContext context) { |