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.filter; 018 019import java.util.Arrays; 020import java.util.List; 021 022import org.forgerock.services.context.Context; 023import org.forgerock.http.Filter; 024import org.forgerock.http.Handler; 025import org.forgerock.http.session.SessionManager; 026import org.forgerock.http.handler.Handlers; 027import org.forgerock.http.protocol.Request; 028import org.forgerock.http.protocol.Response; 029import org.forgerock.util.promise.NeverThrowsException; 030import org.forgerock.util.promise.Promise; 031 032/** 033 * Utility methods for creating common types of filters. 034 */ 035public final class Filters { 036 037 private Filters() { 038 // Prevent instantiation. 039 } 040 041 /** 042 * Creates a {@link Filter} which handles HTTP OPTIONS method requests. 043 * 044 * @param allowedMethods The allowed HTTP methods of the endpoint. 045 * @return A {@code Filter}. 046 */ 047 public static Filter newOptionsFilter(String... allowedMethods) { 048 return new OptionsFilter(allowedMethods); 049 } 050 051 /** 052 * Creates a session {@link Filter} that will use the provided 053 * {@link SessionManager} to manage the users session. 054 * 055 * @param sessionManager The {@code SessionManager}. 056 * @return A session {@code Filter}. 057 */ 058 public static Filter newSessionFilter(SessionManager sessionManager) { 059 return new SessionFilter(sessionManager); 060 } 061 062 /** 063 * Creates a {@link Filter} which encapsulates the provided {@literal filters} 064 * into a single {@code Filter}. 065 * 066 * @param filters The list of filters to be invoked, in order. 067 * @return A {@code Filter}. 068 * @see #chainOf(List) 069 */ 070 public static Filter chainOf(final Filter... filters) { 071 return chainOf(Arrays.asList(filters)); 072 } 073 074 /** 075 * Creates a {@link Filter} which encapsulates the provided {@literal filters} 076 * into a single {@code Filter}. 077 * 078 * @param filters The list of filters to be invoked, in order. 079 * @return A {@code Filter}. 080 * @see #chainOf(Filter...) 081 */ 082 public static Filter chainOf(final List<Filter> filters) { 083 return new Filter() { 084 @Override 085 public Promise<Response, NeverThrowsException> filter(Context context, Request request, Handler next) { 086 return Handlers.chainOf(next, filters).handle(context, request); 087 } 088 }; 089 } 090}