70691cfceadeec3584ef6b5f038fb02f86c3023f
[dmaap/messagerouter/msgrtr.git] / src / main / java / com / att / 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 com.att.dmf.mr.utils;
23
24 import java.text.DecimalFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27 import java.util.Enumeration;
28 import java.util.LinkedList;
29 import java.util.List;
30
31 import javax.servlet.http.HttpServletRequest;
32
33 import com.att.dmf.mr.beans.DMaaPContext;
34 /**
35  * This is an utility class for various operations for formatting
36  * @author nilanjana.maity
37  *
38  */
39 public class Utils {
40
41         private static final String DATE_FORMAT = "dd-MM-yyyy::hh:mm:ss:SSS";
42         public static final String CAMBRIA_AUTH_HEADER = "X-CambriaAuth";
43         private static final String BATCH_ID_FORMAT = "000000";
44
45         private Utils() {
46                 super();
47         }
48
49         /**
50          * Formatting the date 
51          * @param date
52          * @return date or null
53          */
54         public static String getFormattedDate(Date date) {
55                 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
56                 if (null != date){
57                         return sdf.format(date);
58                 }
59                 return null;
60         }
61         /**
62          * to get the details of User Api Key
63          * @param request
64          * @return authkey or null
65          */
66         public static String getUserApiKey(HttpServletRequest request) {
67                 final String auth = request.getHeader(CAMBRIA_AUTH_HEADER);
68                 if (null != auth) {
69                         final String[] splittedAuthKey = auth.split(":");
70                         return splittedAuthKey[0];
71                 }else if (null!=request.getHeader("Authorization")){
72                         /**
73                          * AAF implementation enhancement
74                          */
75                          String user= request.getUserPrincipal().getName().toString();
76                         return user.substring(0, user.lastIndexOf("@"));
77                 }
78                 return null;
79         }
80         /**
81          * to format the batch sequence id
82          * @param batchId
83          * @return batchId
84          */
85         public static String getFromattedBatchSequenceId(Long batchId) {
86                 DecimalFormat format = new DecimalFormat(BATCH_ID_FORMAT);
87                 return format.format(batchId);
88         }
89
90         /**
91          * to get the message length in bytes
92          * @param message
93          * @return bytes or 0
94          */
95         public static long messageLengthInBytes(String message) {
96                 if (null != message) {
97                         return message.getBytes().length;
98                 }
99                 return 0;
100         }
101         /**
102          * To get transaction id details
103          * @param transactionId
104          * @return transactionId or null
105          */
106         public static String getResponseTransactionId(String transactionId) {
107                 if (null != transactionId && !transactionId.isEmpty()) {
108                         return transactionId.substring(0, transactionId.lastIndexOf("::"));
109                 }
110                 return null;
111         }
112
113         /**
114          * get the thread sleep time
115          * @param ratePerMinute
116          * @return ratePerMinute or 0
117          */
118         public static long getSleepMsForRate ( double ratePerMinute )
119         {
120                 if ( ratePerMinute <= 0.0 ) return 0;
121                 return Math.max ( 1000, Math.round ( 60 * 1000 / ratePerMinute ) );
122         }
123
124           public static String getRemoteAddress(DMaaPContext ctx)
125           {
126             String reqAddr = ctx.getRequest().getRemoteAddr();
127             String fwdHeader = getFirstHeader("X-Forwarded-For",ctx);
128             return ((fwdHeader != null) ? fwdHeader : reqAddr);
129           }
130           public static String getFirstHeader(String h,DMaaPContext ctx)
131           {
132             List l = getHeader(h,ctx);
133             return ((l.size() > 0) ? (String)l.iterator().next() : null);
134           }
135           public static List<String> getHeader(String h,DMaaPContext ctx)
136           {
137             LinkedList list = new LinkedList();
138             Enumeration e = ctx.getRequest().getHeaders(h);
139             while (e.hasMoreElements())
140             {
141               list.add(e.nextElement().toString());
142             }
143             return list;
144           }
145 }