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