re base code
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / exception / AbstractSdncException.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.sdc.exception;
22
23 import org.openecomp.sdc.common.log.wrappers.Logger;
24
25 import java.util.Arrays;
26 import java.util.Formatter;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30 public abstract class AbstractSdncException {
31
32         private String messageId;
33
34         private String text;
35
36         private String[] variables;
37
38         private static Logger log = Logger.getLogger(AbstractSdncException.class.getName());
39
40         private final static Pattern ERROR_PARAM_PATTERN = Pattern.compile("%\\d");
41
42         public AbstractSdncException() {
43         }
44
45         public AbstractSdncException(String messageId, String text, String[] variables) {
46                 super();
47                 this.messageId = messageId;
48                 this.text = text;
49                 this.variables = validateParameters(messageId, text, variables);
50         }
51
52         private String[] validateParameters(String messageId, String text, String[] variables) {
53                 String[] res = null;
54                 Matcher m = ERROR_PARAM_PATTERN.matcher(text);
55                 int expectedParamsNum = 0;
56                 while (m.find()) {
57                         expectedParamsNum += 1;
58                 }
59                 int actualParamsNum = (variables != null) ? variables.length : 0;
60                 if (actualParamsNum < expectedParamsNum) {
61                         log.warn(
62                                         "Received less parameters than expected for error with messageId {}, expected: {}, actual: {}. Missing parameters are padded with null values.",
63                                         messageId, expectedParamsNum, actualParamsNum);
64                 } else if (actualParamsNum > expectedParamsNum) {
65                         log.warn(
66                                         "Received more parameters than expected for error with messageId {}, expected: {}, actual: {}. Extra parameters are ignored.",
67                                         messageId, expectedParamsNum, actualParamsNum);
68                 }
69                 if (variables != null) {
70                         res = Arrays.copyOf(variables, expectedParamsNum);
71                 }
72
73                 return res;
74         }
75
76         public String getMessageId() {
77                 return this.messageId;
78         }
79
80         public String getText() {
81                 return text;
82         }
83
84         public String[] getVariables() {
85                 return variables;
86         }
87
88         public void setMessageId(String messageId) {
89                 this.messageId = messageId;
90         }
91
92         public void setText(String text) {
93                 this.text = text;
94         }
95
96         public void setVariables(String[] variables) {
97                 this.variables = variables;
98         }
99
100         public String getFormattedErrorMessage() {
101                 String res;
102                 if (variables != null && variables.length > 0) {
103                         Formatter formatter = new Formatter();
104                         try {
105                                 res = formatter.format(this.text.replaceAll("%\\d", "%s"), (Object[]) this.variables).toString();
106                         } finally {
107                                 formatter.close();
108                         }
109                 } else {
110                         res = this.text;
111                 }
112                 return res;
113         }
114
115 }