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 2015 ForgeRock AS.
015 */
016
017package org.forgerock.openig.decoration.baseuri;
018
019import static org.forgerock.openig.decoration.helper.LazyReference.newReference;
020import static org.forgerock.openig.heap.Keys.LOGSINK_HEAP_KEY;
021import static org.forgerock.openig.util.JsonValues.asExpression;
022
023import org.forgerock.http.Filter;
024import org.forgerock.http.Handler;
025import org.forgerock.json.JsonValue;
026import org.forgerock.openig.decoration.Context;
027import org.forgerock.openig.decoration.Decorator;
028import org.forgerock.openig.decoration.helper.AbstractHandlerAndFilterDecorator;
029import org.forgerock.openig.decoration.helper.DecoratorHeaplet;
030import org.forgerock.openig.decoration.helper.LazyReference;
031import org.forgerock.openig.heap.Heap;
032import org.forgerock.openig.heap.HeapException;
033import org.forgerock.openig.heap.Name;
034import org.forgerock.openig.log.LogSink;
035import org.forgerock.openig.log.Logger;
036
037/**
038 * The {@literal baseURI} decorator can decorate both {@link Filter} and {@link Handler} instances.
039 * <p>
040 * It has to be declared inside of the heap objects section:
041 * <pre>
042 *     {@code
043 *     {
044 *       "name": "myBaseUri",
045 *       "type": "BaseUriDecorator"
046 *     }
047 *     }
048 * </pre>
049 * To decorate a component, just add the decorator declaration next to the {@code config} element:
050 * <pre>
051 *     {@code
052 *     {
053 *       "type": "...",
054 *       "myBaseUri": "http://www.example.com",
055 *       "config": { ... }
056 *     }
057 *     }
058 * </pre>
059 * <p>
060 * The {@literal baseURI} has to be a string otherwise, the decoration will be ignored.
061 * <p>
062 * N.B: The Gateway Servlet creates a default BaseUriDecorator named "baseURI" at startup time.
063 */
064public class BaseUriDecorator extends AbstractHandlerAndFilterDecorator {
065
066    private final LazyReference<LogSink> reference;
067
068    /**
069     * Builds a new base uri decorator with a null sink reference.
070     */
071    public BaseUriDecorator() {
072        this(null);
073    }
074
075    /**
076     * Builds a new base uri decorator with the given sink reference (possibly
077     * {@code null}).
078     *
079     * @param reference
080     *            Log Sink reference for message capture (may be {@code null})
081     */
082    public BaseUriDecorator(final LazyReference<LogSink> reference) {
083        this.reference = reference;
084    }
085
086    @Override
087    protected Filter decorateFilter(final Filter delegate, final JsonValue decoratorConfig, final Context context)
088            throws HeapException {
089        if (decoratorConfig.isString()) {
090            return new BaseUriFilter(delegate, asExpression(decoratorConfig, String.class), getLogger(context));
091        }
092        return delegate;
093    }
094
095    @Override
096    protected Handler decorateHandler(final Handler delegate, final JsonValue decoratorConfig, final Context context)
097            throws HeapException {
098        if (decoratorConfig.isString()) {
099            return new BaseUriHandler(delegate, asExpression(decoratorConfig, String.class), getLogger(context));
100        }
101        return delegate;
102    }
103
104    private Logger getLogger(final Context context) throws HeapException {
105        LogSink sink = reference != null ? reference.get() : null;
106        if (sink == null) {
107            // Use the sink of the decorated component
108            final Heap heap = context.getHeap();
109            sink = heap.resolve(context.getConfig().get("logSink").defaultTo(LOGSINK_HEAP_KEY), LogSink.class);
110        }
111        final Name name = context.getName();
112        return new Logger(sink, name.decorated("BaseUri"));
113    }
114
115    /** Creates and initializes a baseUri in a heap environment. */
116    public static class Heaplet extends DecoratorHeaplet {
117        @Override
118        public Decorator create() throws HeapException {
119            final LazyReference<LogSink> reference = newReference(heap,
120                                                                  config.get("logSink"),
121                                                                  LogSink.class,
122                                                                  true);
123            return new BaseUriDecorator(reference);
124        }
125    }
126}