Bug 2153 - pretty printer
[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 parseKnownWriterParameters(final UriInfo info) {
32         boolean prettyPrint;
33         int depth;
34         String param = info.getQueryParameters(false).getFirst(UriParameters.DEPTH.toString());
35         if (Strings.isNullOrEmpty(param) || "unbounded".equals(param)) {
36             depth = Integer.MAX_VALUE;
37         } else {
38             try {
39                 depth = Integer.valueOf(param);
40                 if (depth < 1) {
41                     throw new RestconfDocumentedException(new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
42                             "Invalid depth parameter: " + depth, null,
43                             "The depth parameter must be an integer > 1 or \"unbounded\""));
44                 }
45             } catch (final NumberFormatException e) {
46                 throw new RestconfDocumentedException(new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
47                         "Invalid depth parameter: " + e.getMessage(), null,
48                         "The depth parameter must be an integer > 1 or \"unbounded\""));
49             }
50         }
51         param = info.getQueryParameters(false).getFirst(UriParameters.PRETTY_PRINT.toString());
52         prettyPrint = "true".equals(param);
53         return new WriterParameters(prettyPrint, depth);
54     }
55
56 }