summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/att/dmf/mr/utils/Utils.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/att/dmf/mr/utils/Utils.java')
-rw-r--r--src/main/java/com/att/dmf/mr/utils/Utils.java145
1 files changed, 145 insertions, 0 deletions
diff --git a/src/main/java/com/att/dmf/mr/utils/Utils.java b/src/main/java/com/att/dmf/mr/utils/Utils.java
new file mode 100644
index 0000000..70691cf
--- /dev/null
+++ b/src/main/java/com/att/dmf/mr/utils/Utils.java
@@ -0,0 +1,145 @@
+/*******************************************************************************
+ * ============LICENSE_START=======================================================
+ * org.onap.dmaap
+ * ================================================================================
+ * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+*
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ *
+ * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ *
+ *******************************************************************************/
+package com.att.dmf.mr.utils;
+
+import java.text.DecimalFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Enumeration;
+import java.util.LinkedList;
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+
+import com.att.dmf.mr.beans.DMaaPContext;
+/**
+ * This is an utility class for various operations for formatting
+ * @author nilanjana.maity
+ *
+ */
+public class Utils {
+
+ private static final String DATE_FORMAT = "dd-MM-yyyy::hh:mm:ss:SSS";
+ public static final String CAMBRIA_AUTH_HEADER = "X-CambriaAuth";
+ private static final String BATCH_ID_FORMAT = "000000";
+
+ private Utils() {
+ super();
+ }
+
+ /**
+ * Formatting the date
+ * @param date
+ * @return date or null
+ */
+ public static String getFormattedDate(Date date) {
+ SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
+ if (null != date){
+ return sdf.format(date);
+ }
+ return null;
+ }
+ /**
+ * to get the details of User Api Key
+ * @param request
+ * @return authkey or null
+ */
+ public static String getUserApiKey(HttpServletRequest request) {
+ final String auth = request.getHeader(CAMBRIA_AUTH_HEADER);
+ if (null != auth) {
+ final String[] splittedAuthKey = auth.split(":");
+ return splittedAuthKey[0];
+ }else if (null!=request.getHeader("Authorization")){
+ /**
+ * AAF implementation enhancement
+ */
+ String user= request.getUserPrincipal().getName().toString();
+ return user.substring(0, user.lastIndexOf("@"));
+ }
+ return null;
+ }
+ /**
+ * to format the batch sequence id
+ * @param batchId
+ * @return batchId
+ */
+ public static String getFromattedBatchSequenceId(Long batchId) {
+ DecimalFormat format = new DecimalFormat(BATCH_ID_FORMAT);
+ return format.format(batchId);
+ }
+
+ /**
+ * to get the message length in bytes
+ * @param message
+ * @return bytes or 0
+ */
+ public static long messageLengthInBytes(String message) {
+ if (null != message) {
+ return message.getBytes().length;
+ }
+ return 0;
+ }
+ /**
+ * To get transaction id details
+ * @param transactionId
+ * @return transactionId or null
+ */
+ public static String getResponseTransactionId(String transactionId) {
+ if (null != transactionId && !transactionId.isEmpty()) {
+ return transactionId.substring(0, transactionId.lastIndexOf("::"));
+ }
+ return null;
+ }
+
+ /**
+ * get the thread sleep time
+ * @param ratePerMinute
+ * @return ratePerMinute or 0
+ */
+ public static long getSleepMsForRate ( double ratePerMinute )
+ {
+ if ( ratePerMinute <= 0.0 ) return 0;
+ return Math.max ( 1000, Math.round ( 60 * 1000 / ratePerMinute ) );
+ }
+
+ public static String getRemoteAddress(DMaaPContext ctx)
+ {
+ String reqAddr = ctx.getRequest().getRemoteAddr();
+ String fwdHeader = getFirstHeader("X-Forwarded-For",ctx);
+ return ((fwdHeader != null) ? fwdHeader : reqAddr);
+ }
+ public static String getFirstHeader(String h,DMaaPContext ctx)
+ {
+ List l = getHeader(h,ctx);
+ return ((l.size() > 0) ? (String)l.iterator().next() : null);
+ }
+ public static List<String> getHeader(String h,DMaaPContext ctx)
+ {
+ LinkedList list = new LinkedList();
+ Enumeration e = ctx.getRequest().getHeaders(h);
+ while (e.hasMoreElements())
+ {
+ list.add(e.nextElement().toString());
+ }
+ return list;
+ }
+}