mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Yannick Lecaillez
21.44.2016 b59e161bd2d26df6023efea77353c8f3f61dd37b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2016 ForgeRock AS.
 */
package org.forgerock.opendj.reactive;
 
import static com.forgerock.reactive.RxJavaStreams.*;
import static org.forgerock.util.Reject.checkNotNull;
 
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
 
import org.forgerock.opendj.io.AbstractLDAPMessageHandler;
import org.forgerock.opendj.io.LDAP;
import org.forgerock.opendj.ldap.DecodeException;
import org.forgerock.opendj.ldap.DecodeOptions;
import org.forgerock.opendj.ldap.LDAPConnectionFactory;
import org.forgerock.opendj.ldap.requests.AbandonRequest;
import org.forgerock.opendj.ldap.requests.AddRequest;
import org.forgerock.opendj.ldap.requests.GenericBindRequest;
import org.forgerock.opendj.ldap.requests.ModifyDNRequest;
import org.forgerock.opendj.ldap.requests.ModifyRequest;
import org.forgerock.opendj.ldap.requests.Request;
import org.forgerock.opendj.ldap.requests.SearchRequest;
import org.forgerock.opendj.ldap.requests.UnbindRequest;
import org.forgerock.opendj.ldap.responses.Response;
import org.forgerock.opendj.ldap.spi.LdapMessages.LdapRawMessage;
import org.forgerock.util.Function;
import org.reactivestreams.Publisher;
 
import com.forgerock.reactive.ReactiveFilter;
import com.forgerock.reactive.ReactiveFilter.SimpleReactiveFilter;
import com.forgerock.reactive.ReactiveHandler;
import com.forgerock.reactive.RxJavaStreams;
import com.forgerock.reactive.Single;
import com.forgerock.reactive.Stream;
 
import io.reactivex.Flowable;
import io.reactivex.Scheduler;
import io.reactivex.schedulers.Schedulers;
 
/** Maintains a set of standard {@link ReactiveHandler} / {@link ReactiveFilter} which can be used in ldap endpoint. */
public final class Components {
 
    /**
     * Routes incoming request to dedicated handler.
     *
     * @param <CTX>
     *            Context type in which request are processed
     * @param <REQ>
     *            Type of routed request
     * @param <REP>
     *            Type of routed response
     * @param routes
     *            {@link Route} where request will be forwarded to
     * @param defaultRoute
     *            If no route can be applied for a specific request, it'll be forwarded to the defaultRoute
     * @return A new {@link ReactiveHandler} routing incoming requests
     */
    public static <CTX, REQ, REP> ReactiveHandler<CTX, REQ, REP> routeTo(final Iterable<Route<CTX, REQ, REP>> routes,
            final ReactiveHandler<CTX, REQ, REP> defaultRoute) {
        /** Routes requests. */
        final class RouterHandler implements ReactiveHandler<CTX, REQ, REP> {
            @Override
            public Single<REP> handle(final CTX context, final REQ request) throws Exception {
                for (final Route<CTX, REQ, REP> route : routes) {
                    if (route.predicate.matches(request, context)) {
                        return route.handler.handle(context, request);
                    }
                }
                return defaultRoute.handle(context, request);
            }
        }
        return new RouterHandler();
    }
 
    /**
     * Allows to transform all aspects of a {@link Request}.
     *
     * @param <CTX>
     *            Context type in which request are processed
     * @param requestTransformer
     *            Function in charge of performing the {@link Request} transformation
     * @param responseTransformer
     *            Function in charge of performing the {@link Response} transformation
     * @return A new policy performing the {@link Request} and {@link Response} transformation.
     */
    public static <CTX> SimpleReactiveFilter<CTX, Request, Stream<Response>> transform(
            final Function<Request, Request, Exception> requestTransformer,
            final Function<Response, Response, Exception> responseTransformer) {
        /** Transforms {@link Request} and {@link Response}. */
        final class TransformFilter extends SimpleReactiveFilter<CTX, Request, Stream<Response>> {
            @Override
            public Single<Stream<Response>> filter(final CTX context, final Request request,
                    final ReactiveHandler<CTX, Request, Stream<Response>> next) throws Exception {
                return next.handle(context, requestTransformer.apply(request))
                        .map(new Function<Stream<Response>, Stream<Response>, Exception>() {
                            @Override
                            public Stream<Response> apply(Stream<Response> responses) throws Exception {
                                return responses.map(new Function<Response, Response, Exception>() {
                                    @Override
                                    public Response apply(Response value) throws Exception {
                                        return responseTransformer.apply(value);
                                    }
                                });
                            }
                        });
            }
        }
        return new TransformFilter();
    }
 
    /**
     * Forward {@link Request} to the provided {@link LDAPConnectionFactory}.
     *
     * @param connectionFactory
     *            The {@link LDAPConnectionFactory} used to send the forwarded {@link Request}
     * @return a {@link ReactiveHandler} Forwarding {@link Request} to the {@link LDAPConnectionFactory}
     */
    public static ReactiveHandler<LDAPClientConnection2, Request, Stream<Response>> proxyTo(
            LDAPConnectionFactory connectionFactory) {
        return new ProxyToHandler(connectionFactory);
    }
 
    /**
     * {@link ReactiveHandler} responding with the provided Response for all incoming requests.
     *
     * @param <CTX>
     *            Context type in which request are processed
     * @param response
     *            The {@link Response} to send as response for all {@link Request}
     * @return a {@link ReactiveHandler} replying {@link Response} for all {@link Request}
     */
    public static <CTX> ReactiveHandler<CTX, Request, Stream<Response>> respondWith(final Response response) {
        return new ReactiveHandler<CTX, Request, Stream<Response>>() {
            @Override
            public Single<Stream<Response>> handle(final CTX context, final Request request) {
                if (request instanceof UnbindRequest) {
                    return singleFrom(RxJavaStreams.<Response>emptyStream());
                }
                return singleFrom(streamFrom(response));
            }
        };
    }
 
    /**
     * Decodes incoming {@link LdapRawMessage} into a {@link Request}.
     *
     * @param decodeOptions
     *            {@link DecodeOptions} used during {@link Request} decoding
     * @return a {@link ReactiveFilter} decoding {@link LdapRawMessage} into {@link Request}
     */
    public static ReactiveFilter<LDAPClientConnection2, LdapRawMessage, Stream<Response>,
                                                        Request, Stream<Response>>
        decodeRequest(final DecodeOptions decodeOptions) {
        return new ReactiveFilter<LDAPClientConnection2, LdapRawMessage, Stream<Response>,
                                                         Request, Stream<Response>>() {
            @Override
            public Single<Stream<Response>> filter(final LDAPClientConnection2 context,
                    final LdapRawMessage encodedRequestMessage,
                    final ReactiveHandler<LDAPClientConnection2, Request, Stream<Response>> next) throws Exception {
                return newSingle(new Single.Emitter<Request>() {
                    @Override
                    public void subscribe(final Single.Subscriber<Request> subscriber) throws Exception {
                        LDAP.getReader(encodedRequestMessage.getContent(), decodeOptions)
                                .readMessage(new AbstractLDAPMessageHandler() {
                            @Override
                            public void abandonRequest(final int messageID, final AbandonRequest request)
                                    throws DecodeException, IOException {
                                subscriber.onComplete(request);
                            }
 
                            @Override
                            public void addRequest(int messageID, AddRequest request)
                                    throws DecodeException, IOException {
                                subscriber.onComplete(request);
                            }
 
                            @Override
                            public void bindRequest(int messageID, int version, GenericBindRequest request)
                                    throws DecodeException, IOException {
                                subscriber.onComplete(request);
                            }
 
                            @Override
                            public void modifyDNRequest(int messageID, ModifyDNRequest request)
                                    throws DecodeException, IOException {
                                subscriber.onComplete(request);
                            }
 
                            @Override
                            public void modifyRequest(int messageID, ModifyRequest request)
                                    throws DecodeException, IOException {
                                subscriber.onComplete(request);
                            }
 
                            @Override
                            public void searchRequest(int messageID, SearchRequest request)
                                    throws DecodeException, IOException {
                                subscriber.onComplete(request);
                            }
 
                            @Override
                            public void unbindRequest(int messageID, UnbindRequest request)
                                    throws DecodeException, IOException {
                                subscriber.onComplete(request);
                            }
                        });
                    }
                }).flatMap(new Function<Request, Single<Stream<Response>>, Exception>() {
                    @Override
                    public Single<Stream<Response>> apply(final Request request) throws Exception {
                        return next.handle(context, request);
                    }
                });
            }
        };
    }
 
    /**
     * Invoke the following {@link ReactiveFilter} from the given {@link Executor}.
     *
     * @param <CTX>
     *            Context type in which request are processed
     * @param <REQ>
     *            Type of dispatched request
     * @param <REP>
     *            Type of dispatched response
     * @param executor
     *            The {@link Executor} used to forward the request
     * @return A {@link ReactiveFilter} fowarding {@link Request} through the {@link Executor}
     */
    public static <CTX, REQ, REP> ReactiveFilter<CTX, REQ, REP, REQ, REP> dispatch(final Executor executor) {
        /** Dispatches request into an {@link Executor}. */
        final class DispatchFilter extends SimpleReactiveFilter<CTX, REQ, REP> {
            private final Scheduler executor;
 
            DispatchFilter(final Executor executor) {
                this.executor = Schedulers.from(checkNotNull(executor, "executor must not be null"));
            }
 
            @Override
            public Single<REP> filter(final CTX context, final REQ request,
                    final ReactiveHandler<CTX, REQ, REP> next) {
                return singleFromPublisher(Flowable.defer(new Callable<Publisher<REP>>() {
                    @Override
                    public Publisher<REP> call() throws Exception {
                        return next.handle(context, request);
                    }
                }).subscribeOn(executor));
            }
        }
        return new DispatchFilter(executor);
    }
 
    private Components() {
        // Prevent instantiation
    }
}