2 * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.restconf.api;
10 import com.google.common.collect.ImmutableMap;
11 import java.util.Arrays;
12 import java.util.Collection;
13 import java.util.List;
15 import java.util.Map.Entry;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.restconf.api.query.RestconfQueryParam;
19 import org.opendaylight.yangtools.concepts.Immutable;
22 * Query parameters of a RESTCONF request URI. Individual parameters can be looked up by
23 * {@link RestconfQueryParam#paramName()} via {@link #lookup(String)}. All parameters are accessible via
24 * {@link #asCollection()}, where each {@link RestconfQueryParam#paramName()} is guaranteed to be encountered at most
28 public interface QueryParameters extends Immutable {
30 Collection<? extends Entry<String, String>> asCollection();
32 @Nullable String lookup(String paramName);
34 static QueryParameters of() {
35 return ImmutableQueryParameters.EMPTY;
38 static QueryParameters of(final String paramName, final String paramValue) {
39 return new ImmutableQueryParameters(ImmutableMap.of(paramName, paramValue));
42 static QueryParameters of(final Entry<String, String> entry) {
43 return of(entry.getKey(), entry.getValue());
46 static QueryParameters of(final RestconfQueryParam<?> param) {
47 return of(param.paramName(), param.paramValue());
50 static QueryParameters of(final RestconfQueryParam<?>... params) {
51 return switch (params.length) {
53 case 1 -> of(params[0]);
54 default -> new ImmutableQueryParameters(Arrays.asList(params));
58 static QueryParameters of(final Collection<RestconfQueryParam<?>> params) {
59 return params instanceof List ? of((List<RestconfQueryParam<?>>) params) : switch (params.size()) {
61 case 1 -> of(params.iterator().next());
62 default -> new ImmutableQueryParameters(params);
66 static QueryParameters of(final List<RestconfQueryParam<?>> params) {
67 return switch (params.size()) {
69 case 1 -> of(params.get(0));
70 default -> new ImmutableQueryParameters(params);
74 static QueryParameters of(final Map<String, String> params) {
75 return params.isEmpty() ? of() : new ImmutableQueryParameters(ImmutableMap.copyOf(params));
79 * Normalize query parameters from an map containing zero or more values for each parameter value, such as coming
80 * from JAX-RS's {@code UriInfo}.
82 * @param multiParams Input map
83 * @return An {@link ImmutableQueryParameters} instance
84 * @throws NullPointerException if {@code uriInfo} is {@code null}
85 * @throws IllegalArgumentException if there are multiple values for a parameter
87 static QueryParameters ofMultiValue(final Map<String, List<String>> multiParams) {
88 if (multiParams.isEmpty()) {
92 final var builder = ImmutableMap.<String, String>builder();
93 for (var entry : multiParams.entrySet()) {
94 final var values = entry.getValue();
95 switch (values.size()) {
99 case 1 -> builder.put(entry.getKey(), values.get(0));
100 default -> throw new IllegalArgumentException(
101 "Parameter " + entry.getKey() + " can appear at most once in request URI");
105 final var params = builder.build();
106 return params.isEmpty() ? of() : new ImmutableQueryParameters(params);