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-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.protocol.Request; 026import org.forgerock.http.protocol.Response; 027import org.forgerock.http.protocol.Status; 028import org.forgerock.util.promise.NeverThrowsException; 029import org.forgerock.util.promise.Promise; 030import org.forgerock.util.promise.Promises; 031 032/** 033 * {@link Filter} which handles OPTION HTTP requests to CREST resources. 034 */ 035public final class OptionsFilter implements Filter { 036 037 /** The HTTP DELETE method. */ 038 public static final String METHOD_DELETE = "DELETE"; 039 /** The HTTP GET method. */ 040 public static final String METHOD_GET = "GET"; 041 /** The HTTP HEAD method. */ 042 public static final String METHOD_HEAD = "HEAD"; 043 /** The HTTP OPTIONS method. */ 044 public static final String METHOD_OPTIONS = "OPTIONS"; 045 /** The HTTP PATCH method. */ 046 public static final String METHOD_PATCH = "PATCH"; 047 /** The HTTP POST method. */ 048 public static final String METHOD_POST = "POST"; 049 /** The HTTP PUT method. */ 050 public static final String METHOD_PUT = "PUT"; 051 /** The HTTP TRACE method. */ 052 public static final String METHOD_TRACE = "TRACE"; 053 054 private final List<String> allowedMethods; 055 056 OptionsFilter(String... allowedMethods) { 057 this.allowedMethods = Arrays.asList(allowedMethods); 058 } 059 060 /** 061 * Handles all OPTION requests to CREST resources, all other request methods are handled by the {@link Handler}. 062 * 063 * @param context {@inheritDoc} 064 * @param request {@inheritDoc} 065 * @param next {@inheritDoc} 066 * @return {@inheritDoc} 067 */ 068 @Override 069 public Promise<Response, NeverThrowsException> filter(Context context, Request request, 070 Handler next) { 071 switch (request.getMethod()) { 072 case METHOD_OPTIONS: 073 Response response = new Response(Status.OK); 074 response.getHeaders().put("Allow", allowedMethods); 075 return Promises.newResultPromise(response); 076 default: 077 return next.handle(context, request); 078 } 079 } 080}