103d07c2f3c9489aade0a50708ea9cab381ceb84
[netconf.git] / restconf / restconf-nb-bierman02 / 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     public static WriterParameters parseWriterParameters(final UriInfo uriInfo, final boolean tagged) {
36         return parseParams(uriInfo, tagged);
37     }
38
39     private static WriterParameters parseParams(final UriInfo info, final boolean tagged) {
40         final WriterParameters.WriterParametersBuilder wpBuilder = new WriterParameters.WriterParametersBuilder();
41         wpBuilder.setTagged(tagged);
42         if (info == null) {
43             return wpBuilder.build();
44         }
45
46         String param = info.getQueryParameters(false).getFirst(UriParameters.DEPTH.toString());
47         if (!Strings.isNullOrEmpty(param) && !"unbounded".equals(param)) {
48             try {
49                 final int depth = Integer.valueOf(param);
50                 if (depth < 1) {
51                     throw new RestconfDocumentedException(
52                             new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
53                             "Invalid depth parameter: " + depth, null,
54                             "The depth parameter must be an integer > 1 or \"unbounded\""));
55                 }
56                 wpBuilder.setDepth(depth);
57             } catch (final NumberFormatException e) {
58                 throw new RestconfDocumentedException(new RestconfError(
59                         RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
60                         "Invalid depth parameter: " + e.getMessage(), null,
61                         "The depth parameter must be an integer > 1 or \"unbounded\""));
62             }
63         }
64         param = info.getQueryParameters(false).getFirst(UriParameters.PRETTY_PRINT.toString());
65         wpBuilder.setPrettyPrint("true".equals(param));
66         return wpBuilder.build();
67     }
68
69 }