update the package name
[dmaap/messagerouter/msgrtr.git] / src / main / java / org / onap / dmaap / dmf / mr / utils / Utils.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
11 *  
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=========================================================
18  *  
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package org.onap.dmaap.dmf.mr.utils;
23
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;
33
34 import javax.servlet.http.HttpServletRequest;
35
36 import org.onap.dmaap.dmf.mr.backends.kafka.KafkaPublisher;
37 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
40 /**
41  * This is an utility class for various operations for formatting
42  * @author nilanjana.maity
43  *
44  */
45 public class Utils {
46
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);
51
52         private Utils() {
53                 super();
54         }
55
56         /**
57          * Formatting the date 
58          * @param date
59          * @return date or null
60          */
61         public static String getFormattedDate(Date date) {
62                 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
63                 if (null != date){
64                         return sdf.format(date);
65                 }
66                 return null;
67         }
68         /**
69          * to get the details of User Api Key
70          * @param request
71          * @return authkey or null
72          */
73         public static String getUserApiKey(HttpServletRequest request) {
74                 final String auth = request.getHeader(CAMBRIA_AUTH_HEADER);
75                 if (null != auth) {
76                         final String[] splittedAuthKey = auth.split(":");
77                         return splittedAuthKey[0];
78                 }else if (null!=request.getHeader("Authorization")){
79                         /**
80                          * AAF implementation enhancement
81                          */
82                          String user= request.getUserPrincipal().getName().toString();
83                         return user.substring(0, user.lastIndexOf("@"));
84                 }
85                 return null;
86         }
87         /**
88          * to format the batch sequence id
89          * @param batchId
90          * @return batchId
91          */
92         public static String getFromattedBatchSequenceId(Long batchId) {
93                 DecimalFormat format = new DecimalFormat(BATCH_ID_FORMAT);
94                 return format.format(batchId);
95         }
96
97         /**
98          * to get the message length in bytes
99          * @param message
100          * @return bytes or 0
101          */
102         public static long messageLengthInBytes(String message) {
103                 if (null != message) {
104                         return message.getBytes().length;
105                 }
106                 return 0;
107         }
108         /**
109          * To get transaction id details
110          * @param transactionId
111          * @return transactionId or null
112          */
113         public static String getResponseTransactionId(String transactionId) {
114                 if (null != transactionId && !transactionId.isEmpty()) {
115                         return transactionId.substring(0, transactionId.lastIndexOf("::"));
116                 }
117                 return null;
118         }
119
120         /**
121          * get the thread sleep time
122          * @param ratePerMinute
123          * @return ratePerMinute or 0
124          */
125         public static long getSleepMsForRate ( double ratePerMinute )
126         {
127                 if ( ratePerMinute <= 0.0 ) return 0;
128                 return Math.max ( 1000, Math.round ( 60 * 1000 / ratePerMinute ) );
129         }
130
131           public static String getRemoteAddress(DMaaPContext ctx)
132           {
133             String reqAddr = ctx.getRequest().getRemoteAddr();
134             String fwdHeader = getFirstHeader("X-Forwarded-For",ctx);
135             return ((fwdHeader != null) ? fwdHeader : reqAddr);
136           }
137           public static String getFirstHeader(String h,DMaaPContext ctx)
138           {
139             List l = getHeader(h,ctx);
140             return ((l.size() > 0) ? (String)l.iterator().next() : null);
141           }
142           public static List<String> getHeader(String h,DMaaPContext ctx)
143           {
144             LinkedList list = new LinkedList();
145             Enumeration e = ctx.getRequest().getHeaders(h);
146             while (e.hasMoreElements())
147             {
148               list.add(e.nextElement().toString());
149             }
150             return list;
151           }
152           
153           public static String getKafkaproperty(){
154                   InputStream input = new Utils().getClass().getResourceAsStream("/kafka.properties");
155                         Properties props = new Properties();
156                         try {
157                                 props.load(input);
158                         } catch (IOException e) {
159                                 log.error("failed to read kafka.properties");
160                         }
161                         return props.getProperty("key");
162                         
163                   
164           }
165           
166           public static boolean isCadiEnabled(){
167                   boolean enableCadi=false;
168                   if(System.getenv("enableCadi")!=null&&System.getenv("enableCadi").equals("true")){
169                           enableCadi=true;
170                         }
171                   
172                   return enableCadi;
173           }
174                   
175 }