Bug 3999: Create internal service to access restconf
[netconf.git] / opendaylight / 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 uriParameterName;
28         }
29     }
30
31     public static WriterParameters parseWriterParameters(final UriInfo info) {
32         WriterParameters.WriterParametersBuilder wpBuilder = new WriterParameters.WriterParametersBuilder();
33         if(info == null) {
34             return wpBuilder.build();
35         }
36
37         String param = info.getQueryParameters(false).getFirst(UriParameters.DEPTH.toString());
38         if (!Strings.isNullOrEmpty(param) && !"unbounded".equals(param)) {
39             try {
40                 final int depth = Integer.valueOf(param);
41                 if (depth < 1) {
42                     throw new RestconfDocumentedException(new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
43                             "Invalid depth parameter: " + depth, null,
44                             "The depth parameter must be an integer > 1 or \"unbounded\""));
45                 }
46                 wpBuilder.setDepth(depth);
47             } catch (final NumberFormatException e) {
48                 throw new RestconfDocumentedException(new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
49                         "Invalid depth parameter: " + e.getMessage(), null,
50                         "The depth parameter must be an integer > 1 or \"unbounded\""));
51             }
52         }
53         param = info.getQueryParameters(false).getFirst(UriParameters.PRETTY_PRINT.toString());
54         wpBuilder.setPrettyPrint("true".equals(param));
55         return wpBuilder.build();
56     }
57
58 }