BUG 2155 - depth parameter in URI
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / 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.controller.sal.restconf.impl;
9
10 import com.google.common.base.Strings;
11 import javax.ws.rs.core.UriInfo;
12
13 public class QueryParametersParser {
14
15     private enum UriParameters {
16         PRETTY_PRINT("prettyPrint"),
17         DEPTH("depth");
18
19         private String uriParameterName;
20
21         UriParameters(final String uriParameterName) {
22             this.uriParameterName = uriParameterName;
23         }
24
25         @Override
26         public String toString() {
27             return uriParameterName;
28         }
29     }
30
31     public static WriterParameters parseWriterParameters(final UriInfo info) {
32         WriterParameters.WriterParametersBuilder wpBuilder = new WriterParameters.WriterParametersBuilder();
33         String param = info.getQueryParameters(false).getFirst(UriParameters.DEPTH.toString());
34         if (!Strings.isNullOrEmpty(param) && !"unbounded".equals(param)) {
35             try {
36                 final int depth = Integer.valueOf(param);
37                 if (depth < 1) {
38                     throw new RestconfDocumentedException(new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
39                             "Invalid depth parameter: " + depth, null,
40                             "The depth parameter must be an integer > 1 or \"unbounded\""));
41                 }
42                 wpBuilder.setDepth(depth);
43             } catch (final NumberFormatException e) {
44                 throw new RestconfDocumentedException(new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
45                         "Invalid depth parameter: " + e.getMessage(), null,
46                         "The depth parameter must be an integer > 1 or \"unbounded\""));
47             }
48         }
49         param = info.getQueryParameters(false).getFirst(UriParameters.PRETTY_PRINT.toString());
50         wpBuilder.setPrettyPrint("true".equals(param));
51         return wpBuilder.build();
52     }
53
54 }