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