098c53bc9ef219143698a19c42f4b292e48d44df
[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 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;
17
18 public final class QueryParametersParser {
19
20     private enum UriParameters {
21         PRETTY_PRINT("prettyPrint"),
22         DEPTH("depth");
23
24         private final String uriParameterName;
25
26         UriParameters(final String uriParameterName) {
27             this.uriParameterName = uriParameterName;
28         }
29
30         @Override
31         public String toString() {
32             return uriParameterName;
33         }
34     }
35
36     private QueryParametersParser() {
37
38     }
39
40     public static WriterParameters parseWriterParameters(final UriInfo info) {
41         final WriterParameters.WriterParametersBuilder wpBuilder = new WriterParameters.WriterParametersBuilder();
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.parseInt(param);
50                 if (depth < 1) {
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\""));
55                 }
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\""));
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 }