Refactor ServerRequest
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / api / ServerRequest.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.restconf.server.api;
9
10 import java.util.function.Function;
11 import org.eclipse.jdt.annotation.NonNullByDefault;
12 import org.opendaylight.restconf.api.QueryParameters;
13 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
14
15 /**
16  * A request to {@link RestconfServer}. It contains state and binding established by whoever is performing binding to
17  * HTTP transport layer. This includes:
18  * <ul>
19  *   <li>HTTP request {@link #queryParameters() query parameters},</li>
20  * </ul>
21  * It notably does <b>not</b> hold the HTTP request path, nor the request body. Those are passed as separate arguments
22  * to server methods as implementations of those methods are expected to act on them.
23  *
24  * @param <T> type of reported result
25  */
26 @NonNullByDefault
27 public sealed interface ServerRequest<T> permits AbstractServerRequest, TransformedServerRequest {
28
29     QueryParameters queryParameters();
30
31     void completeWith(T result);
32
33     // FIXME: NETCONF-1188: remove in favor of ServerException
34     void completeWith(RestconfDocumentedException failure);
35
36     default void completeWith(final ServerException failure) {
37         completeWith(failure.toLegacy());
38     }
39
40     default <O> ServerRequest<O> transform(final Function<O, T> function) {
41         return new TransformedServerRequest<>(this, function);
42     }
43 }