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