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;
15 import org.opendaylight.yangtools.yang.common.ErrorTag;
16 import org.opendaylight.yangtools.yang.common.ErrorType;
18 public final class QueryParametersParser {
20 private enum UriParameters {
21 PRETTY_PRINT("prettyPrint"),
24 private final String uriParameterName;
26 UriParameters(final String uriParameterName) {
27 this.uriParameterName = uriParameterName;
31 public String toString() {
32 return this.uriParameterName;
36 private QueryParametersParser() {
40 public static WriterParameters parseWriterParameters(final UriInfo info) {
41 return parseParams(info, false);
44 public static WriterParameters parseWriterParameters(final UriInfo uriInfo, final boolean tagged) {
45 return parseParams(uriInfo, tagged);
48 private static WriterParameters parseParams(final UriInfo info, final boolean tagged) {
49 final WriterParameters.WriterParametersBuilder wpBuilder = new WriterParameters.WriterParametersBuilder();
50 wpBuilder.setTagged(tagged);
52 return wpBuilder.build();
55 String param = info.getQueryParameters(false).getFirst(UriParameters.DEPTH.toString());
56 if (!Strings.isNullOrEmpty(param) && !"unbounded".equals(param)) {
58 final int depth = Integer.parseInt(param);
60 throw new RestconfDocumentedException(
61 new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
62 "Invalid depth parameter: " + depth, null,
63 "The depth parameter must be an integer > 1 or \"unbounded\""));
65 wpBuilder.setDepth(depth);
66 } catch (final NumberFormatException e) {
67 throw new RestconfDocumentedException(e, new RestconfError(
68 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
69 "Invalid depth parameter: " + e.getMessage(), null,
70 "The depth parameter must be an integer > 1 or \"unbounded\""));
73 param = info.getQueryParameters(false).getFirst(UriParameters.PRETTY_PRINT.toString());
74 wpBuilder.setPrettyPrint("true".equals(param));
75 return wpBuilder.build();