2 * Copyright (c) 2015 Cisco Systems, Inc. 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.netconf.sal.restconf.impl;
10 import com.google.common.base.Strings;
11 import javax.ws.rs.core.UriInfo;
12 import org.opendaylight.restconf.common.context.WriterParameters;
13 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
14 import org.opendaylight.restconf.common.errors.RestconfError;
16 public class QueryParametersParser {
18 private enum UriParameters {
19 PRETTY_PRINT("prettyPrint"),
22 private final String uriParameterName;
24 UriParameters(final String uriParameterName) {
25 this.uriParameterName = uriParameterName;
29 public String toString() {
30 return this.uriParameterName;
34 public static WriterParameters parseWriterParameters(final UriInfo info) {
35 return parseParams(info, false);
38 public static WriterParameters parseWriterParameters(final UriInfo uriInfo, final boolean tagged) {
39 return parseParams(uriInfo, tagged);
42 private static WriterParameters parseParams(final UriInfo info, final boolean tagged) {
43 final WriterParameters.WriterParametersBuilder wpBuilder = new WriterParameters.WriterParametersBuilder();
44 wpBuilder.setTagged(tagged);
46 return wpBuilder.build();
49 String param = info.getQueryParameters(false).getFirst(UriParameters.DEPTH.toString());
50 if (!Strings.isNullOrEmpty(param) && !"unbounded".equals(param)) {
52 final int depth = Integer.valueOf(param);
54 throw new RestconfDocumentedException(
55 new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
56 "Invalid depth parameter: " + depth, null,
57 "The depth parameter must be an integer > 1 or \"unbounded\""));
59 wpBuilder.setDepth(depth);
60 } catch (final NumberFormatException e) {
61 throw new RestconfDocumentedException(new RestconfError(
62 RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
63 "Invalid depth parameter: " + e.getMessage(), null,
64 "The depth parameter must be an integer > 1 or \"unbounded\""));
67 param = info.getQueryParameters(false).getFirst(UriParameters.PRETTY_PRINT.toString());
68 wpBuilder.setPrettyPrint("true".equals(param));
69 return wpBuilder.build();