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 */
016package org.forgerock.openig.handler;
017
018import static org.forgerock.openig.el.Bindings.bindings;
019
020import org.forgerock.http.Handler;
021import org.forgerock.http.protocol.Request;
022import org.forgerock.http.protocol.Response;
023import org.forgerock.openig.heap.Heap;
024import org.forgerock.openig.heap.HeapException;
025import org.forgerock.openig.script.AbstractScriptableHeapObject;
026import org.forgerock.openig.script.Script;
027import org.forgerock.services.context.Context;
028import org.forgerock.util.promise.NeverThrowsException;
029import org.forgerock.util.promise.Promise;
030
031/**
032 * A scriptable handler. This handler acts as a simple wrapper around the
033 * scripting engine. Scripts are provided with the following variable bindings:
034 * <ul>
035 * <li>{@link java.util.Map globals} - the Map of global variables which persist across
036 * successive invocations of the script
037 * <li>{@link org.forgerock.services.context.Context context} - the associated request context
038 * <li>{@link Request request} - the HTTP request
039 * <li>{@link org.forgerock.http.Client http} - an HTTP client which may be used for
040 * performing outbound HTTP requests
041 * <li>{@link org.forgerock.openig.ldap.LdapClient ldap} - an OpenIG LDAP client which may be used for
042 * performing LDAP requests such as LDAP authentication
043 * <li>{@link org.forgerock.openig.log.Logger logger} - the OpenIG logger.
044 * </ul>
045 * <p>Contains also easy access to {@code attributes} from the {@link org.forgerock.services.context.AttributesContext},
046 * e.g: {@code attributes.user = "jackson"}, instead of {@code contexts.attributes.attributes.user = "jackson"}.
047 * <p>In the same way, it gives access to {@code session} from the {@link org.forgerock.http.session.SessionContext},
048 * for example, you can use: {@code session.put(...)}, instead of {@code contexts.session.session.put(...)}.
049 * <p>
050 * <b>NOTE:</b> at the moment only Groovy is supported.
051 * <p><b>NOTE:</b> As of OpenIG 4.0, {@code exchange.request} and {@code exchange.response} are not set anymore.
052 */
053public class ScriptableHandler extends AbstractScriptableHeapObject implements Handler {
054
055    @Override
056    public Promise<Response, NeverThrowsException> handle(final Context context, final Request request) {
057        return runScript(bindings(context, request), null, context);
058    }
059
060    /**
061     * Creates and initializes a scriptable handler in a heap environment.
062     */
063    public static class Heaplet extends AbstractScriptableHeaplet {
064        @Override
065        public ScriptableHandler newInstance(Script script, Heap heap) throws HeapException {
066            return new ScriptableHandler(script, heap);
067        }
068    }
069
070    ScriptableHandler(final Script compiledScript, Heap heap) {
071        super(compiledScript, heap);
072    }
073}