aea1864d414adda34e9ff45d6359fb3d3becad71
[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.restconf.common.context.WriterParameters;
13 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
14 import org.opendaylight.restconf.common.errors.RestconfError;
15 import org.opendaylight.yangtools.yang.common.ErrorType;
16
17 public final class QueryParametersParser {
18
19     private enum UriParameters {
20         PRETTY_PRINT("prettyPrint"),
21         DEPTH("depth");
22
23         private final String uriParameterName;
24
25         UriParameters(final String uriParameterName) {
26             this.uriParameterName = uriParameterName;
27         }
28
29         @Override
30         public String toString() {
31             return this.uriParameterName;
32         }
33     }
34
35     private QueryParametersParser() {
36
37     }
38
39     public static WriterParameters parseWriterParameters(final UriInfo info) {
40         return parseParams(info, false);
41     }
42
43     public static WriterParameters parseWriterParameters(final UriInfo uriInfo, final boolean tagged) {
44         return parseParams(uriInfo, tagged);
45     }
46
47     private static WriterParameters parseParams(final UriInfo info, final boolean tagged) {
48         final WriterParameters.WriterParametersBuilder wpBuilder = new WriterParameters.WriterParametersBuilder();
49         wpBuilder.setTagged(tagged);
50         if (info == null) {
51             return wpBuilder.build();
52         }
53
54         String param = info.getQueryParameters(false).getFirst(UriParameters.DEPTH.toString());
55         if (!Strings.isNullOrEmpty(param) && !"unbounded".equals(param)) {
56             try {
57                 final int depth = Integer.parseInt(param);
58                 if (depth < 1) {
59                     throw new RestconfDocumentedException(
60                             new RestconfError(ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
61                             "Invalid depth parameter: " + depth, null,
62                             "The depth parameter must be an integer > 1 or \"unbounded\""));
63                 }
64                 wpBuilder.setDepth(depth);
65             } catch (final NumberFormatException e) {
66                 throw new RestconfDocumentedException(e, new RestconfError(
67                         ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
68                         "Invalid depth parameter: " + e.getMessage(), null,
69                         "The depth parameter must be an integer > 1 or \"unbounded\""));
70             }
71         }
72         param = info.getQueryParameters(false).getFirst(UriParameters.PRETTY_PRINT.toString());
73         wpBuilder.setPrettyPrint("true".equals(param));
74         return wpBuilder.build();
75     }
76
77 }