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
017package org.forgerock.openig.handler;
018
019import org.forgerock.http.Handler;
020import org.forgerock.http.io.IO;
021import org.forgerock.http.protocol.Request;
022import org.forgerock.http.protocol.Response;
023import org.forgerock.http.protocol.Status;
024import org.forgerock.openig.heap.GenericHeapObject;
025import org.forgerock.openig.heap.GenericHeaplet;
026import org.forgerock.openig.heap.HeapException;
027import org.forgerock.services.context.Context;
028import org.forgerock.util.promise.NeverThrowsException;
029import org.forgerock.util.promise.Promise;
030import org.forgerock.util.promise.Promises;
031
032/**
033 * Creates a static response containing a simple HTML welcome page.
034 */
035public class WelcomeHandler extends GenericHeapObject implements Handler {
036
037    /**
038     * Creates a new welcome page handler.
039     */
040    public WelcomeHandler() {
041        // Nothing to do.
042    }
043
044    @Override
045    public Promise<Response, NeverThrowsException> handle(final Context context, final Request request) {
046        Response response = new Response();
047        response.setStatus(Status.OK);
048        response.getHeaders().add("Content-Type", "text/html");
049        response.setEntity(IO.newBranchingInputStream(getClass().getResourceAsStream(
050                "welcome.html"), getStorage()));
051        return Promises.newResultPromise(response);
052    }
053
054    /**
055     * Creates and initializes a static response handler in a heap environment.
056     */
057    public static class Heaplet extends GenericHeaplet {
058        @Override
059        public Object create() throws HeapException {
060            return new WelcomeHandler();
061        }
062    }
063}