6b3bb545def9aec74788125cad771de46007fa0a
[so.git] / common / src / main / java / org / onap / so / client / aai / AAIErrorFormatter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.onap.so.client.aai;
22
23 import java.util.List;
24
25 import org.onap.so.client.aai.entities.AAIError;
26 import org.onap.so.client.aai.entities.ServiceException;
27
28 public class AAIErrorFormatter {
29
30         private final AAIError error;
31         public AAIErrorFormatter(AAIError error) {
32                 this.error = error;
33         }
34         
35         public String getMessage() {
36                 if (error.getRequestError() != null && 
37                         error.getRequestError().getServiceException() != null) {
38                         ServiceException serviceException = error.getRequestError().getServiceException();
39                         return this.fillInTemplate(serviceException.getText(), serviceException.getVariables());
40                 }
41                 
42                 return "no parsable error message found";
43         }
44         
45         protected String fillInTemplate(String text, List<String> variables) {
46                 for (int i = 0; i < variables.size(); i++) {
47                         variables.set(i, this.format(variables.get(i), variables));
48                 }
49                 
50                 return format(text, variables);
51         }
52         
53         protected String format(String s, List<String> variables) {
54                 return String.format(s.replaceAll("%(\\d+)", "%$1\\$s"), variables.toArray());
55         }
56 }