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