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.ErrorType;
17 public final class QueryParametersParser {
19 private enum UriParameters {
20 PRETTY_PRINT("prettyPrint"),
23 private final String uriParameterName;
25 UriParameters(final String uriParameterName) {
26 this.uriParameterName = uriParameterName;
30 public String toString() {
31 return this.uriParameterName;
35 private QueryParametersParser() {
39 public static WriterParameters parseWriterParameters(final UriInfo info) {
40 return parseParams(info, false);
43 public static WriterParameters parseWriterParameters(final UriInfo uriInfo, final boolean tagged) {
44 return parseParams(uriInfo, tagged);
47 private static WriterParameters parseParams(final UriInfo info, final boolean tagged) {
48 final WriterParameters.WriterParametersBuilder wpBuilder = new WriterParameters.WriterParametersBuilder();
49 wpBuilder.setTagged(tagged);
51 return wpBuilder.build();
54 String param = info.getQueryParameters(false).getFirst(UriParameters.DEPTH.toString());
55 if (!Strings.isNullOrEmpty(param) && !"unbounded".equals(param)) {
57 final int depth = Integer.parseInt(param);
59 throw new RestconfDocumentedException(
60 new RestconfError(ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
61 "Invalid depth parameter: " + depth, null,
62 "The depth parameter must be an integer > 1 or \"unbounded\""));
64 wpBuilder.setDepth(depth);
65 } catch (final NumberFormatException e) {
66 throw new RestconfDocumentedException(e, new RestconfError(
67 ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
68 "Invalid depth parameter: " + e.getMessage(), null,
69 "The depth parameter must be an integer > 1 or \"unbounded\""));
72 param = info.getQueryParameters(false).getFirst(UriParameters.PRETTY_PRINT.toString());
73 wpBuilder.setPrettyPrint("true".equals(param));
74 return wpBuilder.build();