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.netconf.sal.rest.impl.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 uriParameterName;
36 private QueryParametersParser() {
40 public static WriterParameters parseWriterParameters(final UriInfo info) {
41 final WriterParameters.WriterParametersBuilder wpBuilder = new WriterParameters.WriterParametersBuilder();
43 return wpBuilder.build();
46 String param = info.getQueryParameters(false).getFirst(UriParameters.DEPTH.toString());
47 if (!Strings.isNullOrEmpty(param) && !"unbounded".equals(param)) {
49 final int depth = Integer.parseInt(param);
51 throw new RestconfDocumentedException(
52 new RestconfError(ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
53 "Invalid depth parameter: " + depth, null,
54 "The depth parameter must be an integer > 1 or \"unbounded\""));
56 wpBuilder.setDepth(depth);
57 } catch (final NumberFormatException e) {
58 throw new RestconfDocumentedException(e, new RestconfError(
59 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
60 "Invalid depth parameter: " + e.getMessage(), null,
61 "The depth parameter must be an integer > 1 or \"unbounded\""));
64 param = info.getQueryParameters(false).getFirst(UriParameters.PRETTY_PRINT.toString());
65 wpBuilder.setPrettyPrint("true".equals(param));
66 return wpBuilder.build();