2cb336e28e82a0886e0c0ea5a1ae793e9b61f35e
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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  * ================================================================================
19  */
20 package org.openecomp.portalsdk.core.logging.format;
21
22 import java.text.MessageFormat;
23 import java.util.Map;
24
25 import org.openecomp.portalsdk.core.util.SystemProperties;
26
27 public class AuditLogFormatter {
28         //Singleton
29         private static AuditLogFormatter instance = new AuditLogFormatter();
30         
31         public static AuditLogFormatter getInstance() {
32                 
33                 return instance;
34         }
35         
36         public String createMessage(String protocol,String set, 
37                         String loginId, String message) {
38         
39         Object[] securityMessageArgs = prepareFormatArgs(
40                         protocol,
41                         set,
42                         loginId,
43                         message );
44         
45                 return MessageFormat.format(SystemProperties.SECURITY_LOG_TEMPLATE, securityMessageArgs);
46         }
47         
48         /**
49          * A method for normalizing the security log field - returns 
50          * the @Param defaultValue in case the entry is null or empty.
51          * If the @param entry is not empty, a single quotation is added to it.
52          * 
53          * @param entry the entry
54          * @param defaultValue The default value in case the entry is empty
55          * @return String (formatted)
56          */
57         private String formatEntry(Object entry, String defaultValue) {
58                 return  (entry!=null && !entry.toString().isEmpty()) ? addSingleQuotes(entry.toString()): defaultValue;         
59
60         }
61         
62         private String addSingleQuotes(String s) {
63                 if (null!=s && !s.isEmpty()) {
64                         s =  SystemProperties.SINGLE_QUOTE+s+SystemProperties.SINGLE_QUOTE;
65                 }
66                 return s;
67         }
68         
69         
70         /**
71          * This method prepares an Object array of arguments that would be passed
72          * to the MessageFormat.format() method, to format the security log.
73          * 
74          * @param protocol
75          * @param set
76          * @param loginId
77          * @param accessingClient
78          * @param isSuccess
79          * @param message
80          * @return
81          */
82         private Object[] prepareFormatArgs(String protocol,String set, 
83                         String loginId, String message) {
84                 
85                 Object[] messageFormatArgs = {
86                                 formatEntry(protocol, SystemProperties.NA),
87                                 formatEntry(set, SystemProperties.NA),
88                                 formatEntry(loginId, SystemProperties.UNKNOWN),
89                                 message
90                                 };
91                 return messageFormatArgs;
92         }
93
94
95         public String createMessage(Map<String, String> logArgsMap) {
96                                 
97                 Object[] securityMessageArgs = prepareFormatArgs(
98                                 logArgsMap.get(SystemProperties.PROTOCOL),
99                                 logArgsMap.get(SystemProperties.SECURIRY_EVENT_TYPE),
100                                 logArgsMap.get(SystemProperties.LOGIN_ID),
101                                 logArgsMap.get(SystemProperties.ADDITIONAL_INFO) 
102                         );
103                 
104                 return MessageFormat.format(SystemProperties.SECURITY_LOG_TEMPLATE, securityMessageArgs);
105         }
106 }