1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package com.att.dmf.mr.utils;
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;
31 import javax.servlet.http.HttpServletRequest;
33 import com.att.dmf.mr.beans.DMaaPContext;
35 * This is an utility class for various operations for formatting
36 * @author nilanjana.maity
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";
52 * @return date or null
54 public static String getFormattedDate(Date date) {
55 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
57 return sdf.format(date);
62 * to get the details of User Api Key
64 * @return authkey or null
66 public static String getUserApiKey(HttpServletRequest request) {
67 final String auth = request.getHeader(CAMBRIA_AUTH_HEADER);
69 final String[] splittedAuthKey = auth.split(":");
70 return splittedAuthKey[0];
71 }else if (null!=request.getHeader("Authorization")){
73 * AAF implementation enhancement
75 String user= request.getUserPrincipal().getName().toString();
76 return user.substring(0, user.lastIndexOf("@"));
81 * to format the batch sequence id
85 public static String getFromattedBatchSequenceId(Long batchId) {
86 DecimalFormat format = new DecimalFormat(BATCH_ID_FORMAT);
87 return format.format(batchId);
91 * to get the message length in bytes
95 public static long messageLengthInBytes(String message) {
96 if (null != message) {
97 return message.getBytes().length;
102 * To get transaction id details
103 * @param transactionId
104 * @return transactionId or null
106 public static String getResponseTransactionId(String transactionId) {
107 if (null != transactionId && !transactionId.isEmpty()) {
108 return transactionId.substring(0, transactionId.lastIndexOf("::"));
114 * get the thread sleep time
115 * @param ratePerMinute
116 * @return ratePerMinute or 0
118 public static long getSleepMsForRate ( double ratePerMinute )
120 if ( ratePerMinute <= 0.0 ) return 0;
121 return Math.max ( 1000, Math.round ( 60 * 1000 / ratePerMinute ) );
124 public static String getRemoteAddress(DMaaPContext ctx)
126 String reqAddr = ctx.getRequest().getRemoteAddr();
127 String fwdHeader = getFirstHeader("X-Forwarded-For",ctx);
128 return ((fwdHeader != null) ? fwdHeader : reqAddr);
130 public static String getFirstHeader(String h,DMaaPContext ctx)
132 List l = getHeader(h,ctx);
133 return ((l.size() > 0) ? (String)l.iterator().next() : null);
135 public static List<String> getHeader(String h,DMaaPContext ctx)
137 LinkedList list = new LinkedList();
138 Enumeration e = ctx.getRequest().getHeaders(h);
139 while (e.hasMoreElements())
141 list.add(e.nextElement().toString());