89af684da5fd75cc2e83ceef36f4e8bbbe0e1649
[aai/rest-client.git] / src / main / java / org / openecomp / restclient / rest / HttpUtil.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * RestClient\r
4  * ================================================================================\r
5  * Copyright © 2017 AT&T Intellectual Property.\r
6  * Copyright © 2017 Amdocs\r
7  * All rights reserved.\r
8  * ================================================================================\r
9  * Licensed under the Apache License, Version 2.0 (the "License");\r
10  * you may not use this file except in compliance with the License.\r
11  * You may obtain a copy of the License at\r
12  *\r
13  *    http://www.apache.org/licenses/LICENSE-2.0\r
14  *\r
15  * Unless required by applicable law or agreed to in writing, software\r
16  * distributed under the License is distributed on an "AS IS" BASIS,\r
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
18  * See the License for the specific language governing permissions and\r
19  * limitations under the License.\r
20  * ============LICENSE_END=========================================================\r
21  *\r
22  * ECOMP and OpenECOMP are trademarks\r
23  * and service marks of AT&T Intellectual Property.\r
24  */\r
25 package org.openecomp.restclient.rest;\r
26 \r
27 public class HttpUtil {\r
28 \r
29   /**\r
30    * Determines if the provided HTTP response is present in the provided list of acceptable response\r
31    * codes.\r
32    *\r
33    * @param response the http response we got from our request\r
34    * @param list the list of acceptable response codes\r
35    * @return true if the http response is in the provided list\r
36    */\r
37   public static boolean isHttpResponseInList(int response, int... list) {\r
38     for (int checkCode : list) {\r
39       if (checkCode == response) {\r
40         return true;\r
41       }\r
42     }\r
43     return false;\r
44   }\r
45 \r
46   /**\r
47    * Determines if the provided http response is of the information class.\r
48    *\r
49    * @param response the http response we got from our request\r
50    * @return true if the response is of the informational class and false otherwise\r
51    */\r
52   public static boolean isHttpResponseClassInformational(int response) {\r
53     return isExpectedHttpResponseClass(response, '1');\r
54   }\r
55 \r
56   /**\r
57    * Determines if the provided http response is of the success class.\r
58    *\r
59    * @param response the http response we got from our request\r
60    * @return true if the response is of the success class and false otherwise\r
61    */\r
62   public static boolean isHttpResponseClassSuccess(int response) {\r
63     return isExpectedHttpResponseClass(response, '2');\r
64   }\r
65 \r
66   /**\r
67    * Determines if the provided http response is of the redirection class.\r
68    *\r
69    * @param response the http response we got from our request\r
70    * @return true if the response is of the redirection class and false otherwise\r
71    */\r
72   public static boolean isHttpResponseClassRedirection(int response) {\r
73     return isExpectedHttpResponseClass(response, '3');\r
74   }\r
75 \r
76   /**\r
77    * Determines if the provided http response is of the client error class.\r
78    *\r
79    * @param response the http response we got from our request\r
80    * @return true if the response is of the client error class and false otherwise\r
81    */\r
82   public static boolean isHttpResponseClassClientError(int response) {\r
83     return isExpectedHttpResponseClass(response, '4');\r
84   }\r
85 \r
86   /**\r
87    * Determines if the provided http response is of the server error class.\r
88    *\r
89    * @param response the http response we got from our request\r
90    * @return true if the response is of the server error class and false otherwise\r
91    */\r
92   public static boolean isHttpResponseClassServerError(int response) {\r
93     return isExpectedHttpResponseClass(response, '5');\r
94   }\r
95 \r
96   /**\r
97    * Helper method to determine if we have received the response class we are expecting.\r
98    *\r
99    * @param response the http response we got from our request\r
100    * @param expectedClass the expected http response class ie: 1, 2, 3, 4, 5 which maps to 1xx, 2xx,\r
101    *        3xx, 4xx, 5xx respectively\r
102    * @return true if the response if of our expected class and false if not\r
103    */\r
104   private static boolean isExpectedHttpResponseClass(int response, char expectedClass) {\r
105     if (response < 100 || response >= 600) {\r
106       return false;\r
107     }\r
108 \r
109     if (Integer.toString(response).charAt(0) == expectedClass) {\r
110       return true;\r
111     }\r
112 \r
113     return false;\r
114   }\r
115 }\r