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.selfservice.example;
018
019import static org.forgerock.http.routing.RouteMatchers.requestUriMatcher;
020import static org.forgerock.json.resource.ResourcePath.resourcePath;
021import static org.forgerock.json.resource.Resources.newInternalConnectionFactory;
022import static org.forgerock.json.resource.Router.uriTemplate;
023
024import com.fasterxml.jackson.databind.ObjectMapper;
025import org.forgerock.http.Client;
026import org.forgerock.http.Handler;
027import org.forgerock.http.HttpApplication;
028import org.forgerock.http.HttpApplicationException;
029import org.forgerock.http.handler.HttpClientHandler;
030import org.forgerock.http.io.Buffer;
031import org.forgerock.http.routing.RoutingMode;
032import org.forgerock.json.JsonPointer;
033import org.forgerock.json.JsonValue;
034import org.forgerock.json.resource.ConnectionFactory;
035import org.forgerock.json.resource.MemoryBackend;
036import org.forgerock.json.resource.RequestHandler;
037import org.forgerock.json.resource.ResourceException;
038import org.forgerock.json.resource.Resources;
039import org.forgerock.json.resource.Router;
040import org.forgerock.json.resource.http.CrestHttp;
041import org.forgerock.selfservice.core.UserUpdateService;
042import org.forgerock.selfservice.json.JsonAnonymousProcessServiceBuilder;
043import org.forgerock.util.Factory;
044
045import java.util.Map;
046
047/**
048 * Basic http application which initialises the user self service service.
049 *
050 * @since 0.1.0
051 */
052public final class ExampleSelfServiceApplication implements HttpApplication {
053
054    private ConnectionFactory crestConnectionFactory;
055    private Router crestRouter;
056    private JsonValue appConfig;
057    private Client httpClient;
058
059    @Override
060    public Handler start() throws HttpApplicationException {
061        try {
062
063            appConfig = JsonReader.jsonFileToJsonValue("/config.json");
064            httpClient = new Client(new HttpClientHandler());
065
066            crestRouter = new Router();
067            crestConnectionFactory = newInternalConnectionFactory(crestRouter);
068
069            registerCRESTServices();
070
071            org.forgerock.http.routing.Router chfRouter = new org.forgerock.http.routing.Router();
072            chfRouter.addRoute(requestUriMatcher(RoutingMode.STARTS_WITH, "internal"),
073                    CrestHttp.newHttpHandler(crestConnectionFactory));
074            chfRouter.addRoute(requestUriMatcher(RoutingMode.STARTS_WITH, "reset"), registerResetHandler());
075            chfRouter.addRoute(requestUriMatcher(RoutingMode.STARTS_WITH, "username"), registerUsernameHandler());
076            chfRouter.addRoute(requestUriMatcher(RoutingMode.STARTS_WITH, "registration"),
077                    registerRegistrationHandler());
078            chfRouter.addRoute(requestUriMatcher(RoutingMode.STARTS_WITH, "user"), registerUserKBAUpdateHandler());
079            return chfRouter;
080        } catch (Exception e) {
081            throw new HttpApplicationException("Some error starting", e);
082        }
083    }
084
085    private void registerCRESTServices() throws ResourceException {
086        crestRouter.addRoute(uriTemplate("users"), new MemoryBackend());
087        crestRouter.addRoute(uriTemplate("email"), new ExampleEmailService(appConfig.get("mailserver")));
088    }
089
090    private Handler buildHandler(String configResource) throws Exception {
091        ObjectMapper mapper = new ObjectMapper();
092        JsonValue json = new JsonValue(mapper.readValue(getClass().getResource(configResource), Map.class));
093
094        RequestHandler service = JsonAnonymousProcessServiceBuilder.newBuilder()
095                .withJsonConfig(json)
096                .withProgressStageProvider(new ExampleProgressStageProvider(crestConnectionFactory, httpClient))
097                .withTokenHandlerFactory(new ExampleTokenHandlerFactory())
098                .withProcessStore(new SimpleInMemoryStore())
099                .build();
100
101        return CrestHttp.newHttpHandler(Resources.newInternalConnectionFactory(service));
102    }
103
104    private Handler registerResetHandler() throws Exception {
105        return buildHandler("/reset.json");
106    }
107
108    private Handler registerUsernameHandler() throws Exception {
109        return buildHandler("/username.json");
110    }
111
112    private Handler registerRegistrationHandler() throws Exception {
113        return buildHandler("/registration.json");
114    }
115
116    private Handler registerUserKBAUpdateHandler() {
117        return CrestHttp.newHttpHandler(Resources.newInternalConnectionFactory(Resources.newCollection(
118                new UserUpdateService(crestConnectionFactory, resourcePath("users"), new JsonPointer("kbaInfo")))));
119    }
120
121    @Override
122    public Factory<Buffer> getBufferFactory() {
123        // Do nothing
124        return null;
125    }
126
127    @Override
128    public void stop() {
129        // Do nothing
130    }
131
132}