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