1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 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 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package com.att.dmf.mr.utils;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.text.DecimalFormat;
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29 import java.util.Enumeration;
30 import java.util.LinkedList;
31 import java.util.List;
32 import java.util.Properties;
34 import javax.servlet.http.HttpServletRequest;
36 import com.att.dmf.mr.backends.kafka.KafkaPublisher;
37 import com.att.dmf.mr.beans.DMaaPContext;
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
41 * This is an utility class for various operations for formatting
42 * @author nilanjana.maity
47 private static final String DATE_FORMAT = "dd-MM-yyyy::hh:mm:ss:SSS";
48 public static final String CAMBRIA_AUTH_HEADER = "X-CambriaAuth";
49 private static final String BATCH_ID_FORMAT = "000000";
50 private static final EELFLogger log = EELFManager.getInstance().getLogger(Utils.class);
59 * @return date or null
61 public static String getFormattedDate(Date date) {
62 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
64 return sdf.format(date);
69 * to get the details of User Api Key
71 * @return authkey or null
73 public static String getUserApiKey(HttpServletRequest request) {
74 final String auth = request.getHeader(CAMBRIA_AUTH_HEADER);
76 final String[] splittedAuthKey = auth.split(":");
77 return splittedAuthKey[0];
78 }else if (null!=request.getHeader("Authorization")){
80 * AAF implementation enhancement
82 String user= request.getUserPrincipal().getName().toString();
83 return user.substring(0, user.lastIndexOf("@"));
88 * to format the batch sequence id
92 public static String getFromattedBatchSequenceId(Long batchId) {
93 DecimalFormat format = new DecimalFormat(BATCH_ID_FORMAT);
94 return format.format(batchId);
98 * to get the message length in bytes
102 public static long messageLengthInBytes(String message) {
103 if (null != message) {
104 return message.getBytes().length;
109 * To get transaction id details
110 * @param transactionId
111 * @return transactionId or null
113 public static String getResponseTransactionId(String transactionId) {
114 if (null != transactionId && !transactionId.isEmpty()) {
115 return transactionId.substring(0, transactionId.lastIndexOf("::"));
121 * get the thread sleep time
122 * @param ratePerMinute
123 * @return ratePerMinute or 0
125 public static long getSleepMsForRate ( double ratePerMinute )
127 if ( ratePerMinute <= 0.0 ) return 0;
128 return Math.max ( 1000, Math.round ( 60 * 1000 / ratePerMinute ) );
131 public static String getRemoteAddress(DMaaPContext ctx)
133 String reqAddr = ctx.getRequest().getRemoteAddr();
134 String fwdHeader = getFirstHeader("X-Forwarded-For",ctx);
135 return ((fwdHeader != null) ? fwdHeader : reqAddr);
137 public static String getFirstHeader(String h,DMaaPContext ctx)
139 List l = getHeader(h,ctx);
140 return ((l.size() > 0) ? (String)l.iterator().next() : null);
142 public static List<String> getHeader(String h,DMaaPContext ctx)
144 LinkedList list = new LinkedList();
145 Enumeration e = ctx.getRequest().getHeaders(h);
146 while (e.hasMoreElements())
148 list.add(e.nextElement().toString());
153 public static String getKafkaproperty(){
154 InputStream input = new Utils().getClass().getResourceAsStream("/kafka.properties");
155 Properties props = new Properties();
158 } catch (IOException e) {
159 log.error("failed to read kafka.properties");
161 return props.getProperty("key");
166 public static boolean isCadiEnabled(){
167 boolean enableCadi=false;
168 if(System.getenv("enableCadi")!=null){
169 enableCadi=Boolean.getBoolean(System.getenv("enableCadi"));