removed code smells
[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.security.Principal;
27 import java.text.DecimalFormat;
28 import java.text.SimpleDateFormat;
29 import java.util.Date;
30 import java.util.Enumeration;
31 import java.util.LinkedList;
32 import java.util.List;
33 import java.util.Properties;
34
35 import javax.servlet.http.HttpServletRequest;
36 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39 /**
40  * This is an utility class for various operations for formatting
41  * @author nilanjana.maity
42  *
43  */
44 public class Utils {
45
46         private static final String DATE_FORMAT = "dd-MM-yyyy::hh:mm:ss:SSS";
47         public static final String CAMBRIA_AUTH_HEADER = "X-CambriaAuth";
48         private static final String AUTH_HEADER = "Authorization";
49         private static final String BATCH_ID_FORMAT = "000000";
50         private static final String X509_ATTR = "javax.servlet.request.X509Certificate";
51         private static final EELFLogger log = EELFManager.getInstance().getLogger(Utils.class);
52
53         private Utils() {
54                 super();
55         }
56
57         /**
58          * Formatting the date 
59          * @param date
60          * @return date or null
61          */
62         public static String getFormattedDate(Date date) {
63                 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
64                 if (null != date){
65                         return sdf.format(date);
66                 }
67                 return null;
68         }
69         /**
70          * to get the details of User Api Key
71          * @param request
72          * @return authkey or null
73          */
74         public static String getUserApiKey(HttpServletRequest request) {
75                 final String auth = request.getHeader(CAMBRIA_AUTH_HEADER);
76                 if (null != auth) {
77                         final String[] splittedAuthKey = auth.split(":");
78                         return splittedAuthKey[0];
79                 }else if (null != request.getHeader(AUTH_HEADER) || null != request.getAttribute(X509_ATTR)){
80                         /**
81                          * AAF implementation enhancement
82                          */
83                         Principal principal = request.getUserPrincipal();
84                         if(principal != null){
85                                 String name = principal.getName();
86                                 return name.substring(0, name.lastIndexOf('@'));
87                         }
88                         log.warn("No principal has been provided on HTTP request");
89                 }
90                 return null;
91         }
92
93
94         /**
95          * to format the batch sequence id
96          * @param batchId
97          * @return batchId
98          */
99         public static String getFromattedBatchSequenceId(Long batchId) {
100                 DecimalFormat format = new DecimalFormat(BATCH_ID_FORMAT);
101                 return format.format(batchId);
102         }
103
104         /**
105          * to get the message length in bytes
106          * @param message
107          * @return bytes or 0
108          */
109         public static long messageLengthInBytes(String message) {
110                 if (null != message) {
111                         return message.getBytes().length;
112                 }
113                 return 0;
114         }
115         /**
116          * To get transaction id details
117          * @param transactionId
118          * @return transactionId or null
119          */
120         public static String getResponseTransactionId(String transactionId) {
121                 if (null != transactionId && !transactionId.isEmpty()) {
122                         return transactionId.substring(0, transactionId.lastIndexOf("::"));
123                 }
124                 return null;
125         }
126
127         /**
128          * get the thread sleep time
129          * @param ratePerMinute
130          * @return ratePerMinute or 0
131          */
132         public static long getSleepMsForRate ( double ratePerMinute )
133         {
134                 if ( ratePerMinute <= 0.0 ) 
135                 {
136                         return 0;
137                 }
138                 return Math.max ( 1000, Math.round ( 60 * 1000 / ratePerMinute ) );
139         }
140
141           public static String getRemoteAddress(DMaaPContext ctx)
142           {
143             String reqAddr = ctx.getRequest().getRemoteAddr();
144             String fwdHeader = getFirstHeader("X-Forwarded-For",ctx);
145             return ((fwdHeader != null) ? fwdHeader : reqAddr);
146           }
147           public static String getFirstHeader(String h,DMaaPContext ctx)
148           {
149             List l = getHeader(h,ctx);
150             return ((l.size() > 0) ? (String)l.iterator().next() : null);
151           }
152           public static List<String> getHeader(String h,DMaaPContext ctx)
153           {
154             LinkedList list = new LinkedList();
155             Enumeration e = ctx.getRequest().getHeaders(h);
156             while (e.hasMoreElements())
157             {
158               list.add(e.nextElement().toString());
159             }
160             return list;
161           }
162           
163           public static String getKafkaproperty(){
164                   InputStream input = new Utils().getClass().getResourceAsStream("/kafka.properties");
165                         Properties props = new Properties();
166                         try {
167                                 props.load(input);
168                         } catch (IOException e) {
169                                 log.error("failed to read kafka.properties");
170                         }
171                         return props.getProperty("key");
172                         
173                   
174           }
175           
176           public static boolean isCadiEnabled(){
177                   boolean enableCadi=false;
178                   if(System.getenv("enableCadi")!=null&&System.getenv("enableCadi").equals("true")){
179                           enableCadi=true;
180                         }
181                   
182                   return enableCadi;
183           }
184                   
185 }