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
017// --- JCite ---
018package org.forgerock.openig.doc;
019
020import org.forgerock.services.context.Context;
021import org.forgerock.http.Filter;
022import org.forgerock.http.Handler;
023import org.forgerock.http.protocol.Request;
024import org.forgerock.http.protocol.Response;
025import org.forgerock.openig.heap.GenericHeapObject;
026import org.forgerock.openig.heap.GenericHeaplet;
027import org.forgerock.openig.heap.HeapException;
028import org.forgerock.util.promise.NeverThrowsException;
029import org.forgerock.util.promise.Promise;
030import org.forgerock.util.promise.ResultHandler;
031
032/**
033 * Filter to set a header in the incoming request and in the outgoing response.
034 */
035public class SampleFilter extends GenericHeapObject implements Filter {
036
037    /** Header name. */
038    String name;
039
040    /** Header value. */
041    String value;
042
043    /**
044     * Set a header in the incoming request and in the outgoing response.
045     * A configuration example looks something like the following.
046     *
047     * <pre>
048     * {
049     *     "name": "SampleFilter",
050     *     "type": "SampleFilter",
051     *     "config": {
052     *         "name": "X-Greeting",
053     *         "value": "Hello world"
054     *     }
055     * }
056     * </pre>
057     *
058     * @param context           Execution context.
059     * @param request           HTTP Request.
060     * @param next              Next filter or handler in the chain.
061     * @return A {@code Promise} representing the response to be returned to the client.
062     */
063    @Override
064    public Promise<Response, NeverThrowsException> filter(final Context context,
065                                                          final Request request,
066                                                          final Handler next) {
067
068        // Set header in the request.
069        request.getHeaders().put(name, value);
070
071        // Pass to the next filter or handler in the chain.
072        return next.handle(context, request)
073                // When it has been successfully executed, execute the following callback
074                .thenOnResult(new ResultHandler<Response>() {
075                    @Override
076                    public void handleResult(final Response response) {
077                        // Set header in the response.
078                        response.getHeaders().put(name, value);
079                    }
080                });
081    }
082
083    /**
084     * Create and initialize the filter, based on the configuration.
085     * The filter object is stored in the heap.
086     */
087    public static class Heaplet extends GenericHeaplet {
088
089        /**
090         * Create the filter object in the heap,
091         * setting the header name and value for the filter,
092         * based on the configuration.
093         *
094         * @return                  The filter object.
095         * @throws HeapException    Failed to create the object.
096         */
097        @Override
098        public Object create() throws HeapException {
099
100            SampleFilter filter = new SampleFilter();
101            filter.name  = config.get("name").required().asString();
102            filter.value = config.get("value").required().asString();
103
104            return filter;
105        }
106    }
107}
108// --- JCite ---