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 */
016
017// --- JCite ---
018package org.forgerock.openig.doc;
019
020import org.forgerock.openig.filter.GenericFilter;
021import org.forgerock.openig.handler.Handler;
022import org.forgerock.openig.handler.HandlerException;
023import org.forgerock.openig.heap.GenericHeaplet;
024import org.forgerock.openig.heap.HeapException;
025import org.forgerock.openig.http.Exchange;
026
027import java.io.IOException;
028
029/**
030 * Filter to set a header in the incoming request and in the outgoing response.
031 */
032public class SampleFilter extends GenericFilter {
033
034    /** Header name. */
035    String name;
036
037    /** Header value. */
038    String value;
039
040    /**
041     * Set a header in the incoming request and in the outgoing response.
042     * A configuration example looks something like the following.
043     *
044     * <pre>
045     * {
046     *     "name": "SampleFilter",
047     *     "type": "SampleFilter",
048     *     "config": {
049     *         "name": "X-Greeting",
050     *         "value": "Hello world"
051     *     }
052     * }
053     * </pre>
054     *
055     * @param exchange          Wraps request and response.
056     * @param next              Next filter or handler in the chain.
057     * @throws HandlerException Failure when handling the exchange.
058     * @throws IOException      I/O exception when handling the exchange.
059     */
060    @Override
061    public void filter(Exchange exchange, Handler next)
062            throws HandlerException, IOException {
063
064        // Set header in the request.
065        exchange.request.getHeaders().putSingle(name, value);
066
067        // Pass to the next filter or handler in the chain.
068        next.handle(exchange);
069
070        // Set header in the response.
071        exchange.response.getHeaders().putSingle(name, value);
072    }
073
074    /**
075     * Create and initialize the filter, based on the configuration.
076     * The filter object is stored in the heap.
077     */
078    public static class Heaplet extends GenericHeaplet {
079
080        /**
081         * Create the filter object in the heap,
082         * setting the header name and value for the filter,
083         * based on the configuration.
084         *
085         * @return                  The filter object.
086         * @throws HeapException    Failed to create the object.
087         */
088        @Override
089        public Object create() throws HeapException {
090
091            SampleFilter filter = new SampleFilter();
092            filter.name  = config.get("name").required().asString();
093            filter.value = config.get("value").required().asString();
094
095            return filter;
096        }
097    }
098}
099// --- JCite ---