Fix NPE under MDCSetup for response code 422
[logging-analytics.git] / reference / logging-filter / logging-filter-base / src / main / java / org / onap / logging / filter / base / CustomResponseStatus.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - Logging
4  * ================================================================================
5  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.logging.filter.base;
22
23 public enum CustomResponseStatus {
24     PROCESSING(102, "Processing"),
25     MULTI_STATUS(207, "Multi-Status"),
26     ALREADY_REPORTED(208, "Already Reported"),
27     UNPROCESSABLE_ENTITY(422, "Unprocessable Entity"),
28     LOCKED(423, "Locked"),
29     FAILED_DEPENDENCY(424, "Failed Dependency"),
30     INSUFFICIENT_STORAGE(508, "Insufficient Storage"),
31     LOOP_DETECTED(508, "Loop Detected");
32
33     private final int code;
34     private final String reason;
35
36     CustomResponseStatus(int statusCode, String reasonPhrase) {
37         this.code = statusCode;
38         this.reason = reasonPhrase;
39     }
40
41     public static CustomResponseStatus fromStatusCode(int statusCode) {
42         for (CustomResponseStatus s : values()) {
43             if (s.code == statusCode) {
44                 return s;
45             }
46         }
47
48         return null;
49     }
50
51     public int getStatusCode() {
52         return this.code;
53     }
54
55     public String getReasonPhrase() {
56         return this.toString();
57     }
58
59     public String toString() {
60         return this.reason;
61     }
62 }