001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2014 ForgeRock AS.
015 */
016
017package org.forgerock.openig.decoration.helper;
018
019import org.forgerock.json.fluent.JsonValue;
020import org.forgerock.openig.decoration.Context;
021import org.forgerock.openig.decoration.Decorator;
022import org.forgerock.openig.filter.Filter;
023import org.forgerock.openig.handler.Handler;
024import org.forgerock.openig.heap.HeapException;
025
026/**
027 * An AbstractHandlerAndFilterDecorator is the base implementation for decorators working only on {@link Filter} and
028 * {@link Handler}.
029 * <p>
030 * Implementors just have to implement the dedicated {@link #decorateFilter(Filter, JsonValue, Context)} and {@link
031 * #decorateHandler(Handler, JsonValue, Context)} for decorating Filter and Handler respectively.
032 */
033public abstract class AbstractHandlerAndFilterDecorator implements Decorator {
034
035    @Override
036    public boolean accepts(final Class<?> type) {
037        return Filter.class.isAssignableFrom(type) || Handler.class.isAssignableFrom(type);
038    }
039
040    @Override
041    public Object decorate(final Object delegate, final JsonValue decoratorConfig, final Context context)
042            throws HeapException {
043        // Only intercept if needed
044        if (delegate instanceof Handler) {
045            return decorateHandler((Handler) delegate, decoratorConfig, context);
046        } else if (delegate instanceof Filter) {
047            return decorateFilter((Filter) delegate, decoratorConfig, context);
048        }
049        return delegate;
050    }
051
052    /**
053     * Decorates the provided {@code delegate} {@link Filter} instance with the provided {@code decoratorConfig}
054     * configuration.
055     *
056     * @param delegate
057     *         Filter instance to be decorated
058     * @param decoratorConfig
059     *         the decorator configuration to apply
060     * @param context
061     *         contextual information of the decorated instance
062     * @return a decorated filter instance (or original filter delegate)
063     * @throws HeapException
064     *         when decoration fails
065     */
066    protected abstract Filter decorateFilter(final Filter delegate,
067                                             final JsonValue decoratorConfig,
068                                             final Context context) throws HeapException;
069
070    /**
071     * Decorates the provided {@code delegate} {@link Handler} instance with the provided {@code decoratorConfig}
072     * configuration.
073     *
074     * @param delegate
075     *         Handler instance to be decorated
076     * @param decoratorConfig
077     *         the decorator configuration to apply
078     * @param context
079     *         contextual information of the decorated instance
080     * @return a decorated handler instance (or original handler delegate)
081     * @throws HeapException
082     *         when decoration fails
083     */
084    protected abstract Handler decorateHandler(final Handler delegate,
085                                               final JsonValue decoratorConfig,
086                                               final Context context) throws HeapException;
087}