Rework body formatting wiring
[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 static java.util.Objects.requireNonNull;
11
12 import org.eclipse.jdt.annotation.NonNullByDefault;
13 import org.opendaylight.restconf.api.FormatParameters;
14 import org.opendaylight.restconf.api.QueryParameters;
15 import org.opendaylight.restconf.api.query.PrettyPrintParam;
16
17 /**
18  * A request to {@link RestconfServer}. It contains state and binding established by whoever is performing binding to
19  * HTTP transport layer. This includes:
20  * <ul>
21  *   <li>HTTP request {@link #queryParameters() query parameters},</li>
22  *   <li>{@link #format() format parameters}, including those affected by query parameters<li>
23  * </ul>
24  * It notably does <b>not</b> hold the HTTP request path, nor the request body. Those are passed as separate arguments
25  * to server methods as implementations of those methods are expected to act on them.
26  */
27 @NonNullByDefault
28 public record ServerRequest(QueryParameters queryParameters, FormatParameters format) {
29     // TODO: this is where a binding to security principal and access control should be:
30     //       - we would like to be able to have java.security.Principal#name() for logging purposes
31     //       - we need to have a NACM-capable interface, through which we can check permissions (such as data PUT) and
32     //         establish output filters (i.e. excluding paths inaccessible path to user from a data GET a ContainerNode)
33     public ServerRequest {
34         requireNonNull(queryParameters);
35         requireNonNull(format);
36     }
37
38     private ServerRequest(final QueryParameters queryParameters, final PrettyPrintParam prettyPrint) {
39         this(queryParameters, prettyPrint.value() ? FormatParameters.PRETTY : FormatParameters.COMPACT);
40     }
41
42     public static ServerRequest of(final QueryParameters queryParameters, final PrettyPrintParam defaultPrettyPrint) {
43         final var prettyPrint = queryParameters.lookup(PrettyPrintParam.uriName, PrettyPrintParam::forUriValue);
44         return prettyPrint == null ? new ServerRequest(queryParameters, defaultPrettyPrint)
45             : new ServerRequest(queryParameters.withoutParam(PrettyPrintParam.uriName), prettyPrint);
46     }
47 }