1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.forgerock.selfservice.example;
18
19 import static org.forgerock.http.routing.RouteMatchers.requestUriMatcher;
20 import static org.forgerock.json.resource.ResourcePath.resourcePath;
21 import static org.forgerock.json.resource.Resources.newInternalConnectionFactory;
22 import static org.forgerock.json.resource.Router.uriTemplate;
23
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.forgerock.http.Client;
26 import org.forgerock.http.Handler;
27 import org.forgerock.http.HttpApplication;
28 import org.forgerock.http.HttpApplicationException;
29 import org.forgerock.http.handler.HttpClientHandler;
30 import org.forgerock.http.io.Buffer;
31 import org.forgerock.http.routing.RoutingMode;
32 import org.forgerock.json.JsonPointer;
33 import org.forgerock.json.JsonValue;
34 import org.forgerock.json.resource.ConnectionFactory;
35 import org.forgerock.json.resource.MemoryBackend;
36 import org.forgerock.json.resource.RequestHandler;
37 import org.forgerock.json.resource.ResourceException;
38 import org.forgerock.json.resource.Resources;
39 import org.forgerock.json.resource.Router;
40 import org.forgerock.json.resource.http.CrestHttp;
41 import org.forgerock.selfservice.core.UserUpdateService;
42 import org.forgerock.selfservice.json.JsonAnonymousProcessServiceBuilder;
43 import org.forgerock.util.Factory;
44
45 import java.util.Map;
46
47
48
49
50
51
52 public final class ExampleSelfServiceApplication implements HttpApplication {
53
54 private ConnectionFactory crestConnectionFactory;
55 private Router crestRouter;
56 private JsonValue appConfig;
57 private Client httpClient;
58
59 @Override
60 public Handler start() throws HttpApplicationException {
61 try {
62
63 appConfig = JsonReader.jsonFileToJsonValue("/config.json");
64 httpClient = new Client(new HttpClientHandler());
65
66 crestRouter = new Router();
67 crestConnectionFactory = newInternalConnectionFactory(crestRouter);
68
69 registerCRESTServices();
70
71 org.forgerock.http.routing.Router chfRouter = new org.forgerock.http.routing.Router();
72 chfRouter.addRoute(requestUriMatcher(RoutingMode.STARTS_WITH, "internal"),
73 CrestHttp.newHttpHandler(crestConnectionFactory));
74 chfRouter.addRoute(requestUriMatcher(RoutingMode.STARTS_WITH, "reset"), registerResetHandler());
75 chfRouter.addRoute(requestUriMatcher(RoutingMode.STARTS_WITH, "username"), registerUsernameHandler());
76 chfRouter.addRoute(requestUriMatcher(RoutingMode.STARTS_WITH, "registration"),
77 registerRegistrationHandler());
78 chfRouter.addRoute(requestUriMatcher(RoutingMode.STARTS_WITH, "user"), registerUserKBAUpdateHandler());
79 return chfRouter;
80 } catch (Exception e) {
81 throw new HttpApplicationException("Some error starting", e);
82 }
83 }
84
85 private void registerCRESTServices() throws ResourceException {
86 crestRouter.addRoute(uriTemplate("users"), new MemoryBackend());
87 crestRouter.addRoute(uriTemplate("email"), new ExampleEmailService(appConfig.get("mailserver")));
88 }
89
90 private Handler buildHandler(String configResource) throws Exception {
91 ObjectMapper mapper = new ObjectMapper();
92 JsonValue json = new JsonValue(mapper.readValue(getClass().getResource(configResource), Map.class));
93
94 RequestHandler service = JsonAnonymousProcessServiceBuilder.newBuilder()
95 .withJsonConfig(json)
96 .withProgressStageProvider(new ExampleProgressStageProvider(crestConnectionFactory, httpClient))
97 .withTokenHandlerFactory(new ExampleTokenHandlerFactory())
98 .withProcessStore(new SimpleInMemoryStore())
99 .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
124 return null;
125 }
126
127 @Override
128 public void stop() {
129
130 }
131
132 }