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 */
016package org.forgerock.openig.filter;
017
018import java.io.IOException;
019
020import org.forgerock.openig.handler.Handler;
021import org.forgerock.openig.handler.HandlerException;
022import org.forgerock.openig.heap.HeapException;
023import org.forgerock.openig.http.Exchange;
024import org.forgerock.openig.script.AbstractScriptableHeapObject;
025import org.forgerock.openig.script.Script;
026
027/**
028 * A scriptable filter. This filter acts as a simple wrapper around the
029 * scripting engine. Scripts are provided with the following variable bindings:
030 * <ul>
031 * <li>{@link java.util.Map globals} - the Map of global variables which persist across
032 * successive invocations of the script
033 * <li>{@link Exchange exchange} - the HTTP exchange
034 * <li>{@link org.forgerock.openig.http.HttpClient http} - an OpenIG HTTP client which may be used for
035 * performing outbound HTTP requests
036 * <li>{@link org.forgerock.openig.ldap.LdapClient ldap} - an OpenIG LDAP client which may be used for
037 * performing LDAP requests such as LDAP authentication
038 * <li>{@link org.forgerock.openig.log.Logger logger} - the OpenIG logger
039 * <li>{@link Handler next} - the next handler in the filter chain.
040 * </ul>
041 * Like Java based filters, scripts are free to choose whether or not they
042 * forward the request to the next handler or, instead, return a response
043 * immediately.
044 * <p>
045 * <b>NOTE:</b> at the moment only Groovy is supported.
046 */
047public class ScriptableFilter extends AbstractScriptableHeapObject implements Filter {
048
049    /**
050     * Creates and initializes a scriptable filter in a heap environment.
051     */
052    public static class Heaplet extends AbstractScriptableHeaplet {
053        @Override
054        public ScriptableFilter newInstance(Script script) throws HeapException {
055            return new ScriptableFilter(script);
056        }
057    }
058
059    ScriptableFilter(final Script compiledScript) {
060        super(compiledScript);
061    }
062
063    @Override
064    public void filter(final Exchange exchange, final Handler next) throws HandlerException,
065            IOException {
066        // Delegates filtering to the script.
067        runScript(exchange, next);
068    }
069}