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