2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalsdk.core.logging.format;
22 import java.text.MessageFormat;
25 import org.openecomp.portalsdk.core.util.SystemProperties;
27 public class AuditLogFormatter {
29 private static AuditLogFormatter instance = new AuditLogFormatter();
31 public static AuditLogFormatter getInstance() {
36 public String createMessage(String protocol,String set,
37 String loginId, String message) {
39 Object[] securityMessageArgs = prepareFormatArgs(
45 return MessageFormat.format(SystemProperties.SECURITY_LOG_TEMPLATE, securityMessageArgs);
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.
53 * @param entry the entry
54 * @param defaultValue The default value in case the entry is empty
55 * @return String (formatted)
57 private String formatEntry(Object entry, String defaultValue) {
58 return (entry!=null && !entry.toString().isEmpty()) ? addSingleQuotes(entry.toString()): defaultValue;
62 private String addSingleQuotes(String s) {
63 if (null!=s && !s.isEmpty()) {
64 s = SystemProperties.SINGLE_QUOTE+s+SystemProperties.SINGLE_QUOTE;
71 * This method prepares an Object array of arguments that would be passed
72 * to the MessageFormat.format() method, to format the security log.
77 * @param accessingClient
82 private Object[] prepareFormatArgs(String protocol,String set,
83 String loginId, String message) {
85 Object[] messageFormatArgs = {
86 formatEntry(protocol, SystemProperties.NA),
87 formatEntry(set, SystemProperties.NA),
88 formatEntry(loginId, SystemProperties.UNKNOWN),
91 return messageFormatArgs;
95 public String createMessage(Map<String, String> logArgsMap) {
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)
104 return MessageFormat.format(SystemProperties.SECURITY_LOG_TEMPLATE, securityMessageArgs);