Merge "Use yangtools ParserStreamUtils.findSchemaNodeByNameAndNamespace() method"
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / netconf / sal / restconf / impl / QueryParametersParser.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.sal.restconf.impl;
9
10 import com.google.common.base.Strings;
11 import javax.ws.rs.core.UriInfo;
12
13 public class QueryParametersParser {
14
15     private enum UriParameters {
16         PRETTY_PRINT("prettyPrint"),
17         DEPTH("depth");
18
19         private final String uriParameterName;
20
21         UriParameters(final String uriParameterName) {
22             this.uriParameterName = uriParameterName;
23         }
24
25         @Override
26         public String toString() {
27             return this.uriParameterName;
28         }
29     }
30
31     public static WriterParameters parseWriterParameters(final UriInfo info) {
32         return parseParams(info, false);
33     }
34
35     private static WriterParameters parseParams(final UriInfo info, final boolean tagged) {
36         final WriterParameters.WriterParametersBuilder wpBuilder = new WriterParameters.WriterParametersBuilder();
37         wpBuilder.setTagged(tagged);
38         if(info == null) {
39             return wpBuilder.build();
40         }
41
42         String param = info.getQueryParameters(false).getFirst(UriParameters.DEPTH.toString());
43         if (!Strings.isNullOrEmpty(param) && !"unbounded".equals(param)) {
44             try {
45                 final int depth = Integer.valueOf(param);
46                 if (depth < 1) {
47                     throw new RestconfDocumentedException(new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
48                             "Invalid depth parameter: " + depth, null,
49                             "The depth parameter must be an integer > 1 or \"unbounded\""));
50                 }
51                 wpBuilder.setDepth(depth);
52             } catch (final NumberFormatException e) {
53                 throw new RestconfDocumentedException(new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
54                         "Invalid depth parameter: " + e.getMessage(), null,
55                         "The depth parameter must be an integer > 1 or \"unbounded\""));
56             }
57         }
58         param = info.getQueryParameters(false).getFirst(UriParameters.PRETTY_PRINT.toString());
59         wpBuilder.setPrettyPrint("true".equals(param));
60         return wpBuilder.build();
61     }
62
63     public static WriterParameters parseWriterParameters(final UriInfo uriInfo, final boolean tagged) {
64         return parseParams(uriInfo, tagged);
65     }
66
67 }