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 2015 ForgeRock AS.
015 */
016
017package org.forgerock.http.handler;
018
019import java.util.Arrays;
020import java.util.List;
021
022import org.forgerock.http.Filter;
023import org.forgerock.http.Handler;
024
025/**
026 * Utility methods for creating common types of handlers.
027 */
028public final class Handlers {
029
030    private Handlers() {
031        // Prevent instantiation.
032    }
033
034    /**
035     * Creates a {@link Handler} which wraps the provided {@literal filters}
036     * around the provided target {@literal handler}.
037     *
038     * @param handler The target handler which will be invoked once
039     *                processing has reached the end of the filter chain.
040     * @param filters The list of filters to be processed before invoking the
041     *                target.
042     * @return A {@code Handler}.
043     * @see #chainOf(Handler, List)
044     */
045    public static Handler chainOf(final Handler handler, final Filter... filters) {
046        return chainOf(handler, Arrays.asList(filters));
047    }
048
049    /**
050     * Creates a {@link Handler} which wraps the provided {@literal filters}
051     * around the provided target {@literal handler}.
052     *
053     * @param handler The target handler which will be invoked once
054     *                processing has reached the end of the filter chain.
055     * @param filters The list of filters to be processed before invoking the
056     *                target.
057     * @return A {@code Handler}.
058     * @see #chainOf(Handler, Filter...)
059     */
060    public static Handler chainOf(final Handler handler, final List<Filter> filters) {
061        return new Chain(handler, filters, 0);
062    }
063
064}