2021-05-20 01:50:50 +02:00
package eu.openaire.urls_controller.util ;
2024-03-14 12:59:23 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2023-06-06 15:49:53 +02:00
import javax.servlet.http.HttpServletRequest ;
2023-05-11 02:07:55 +02:00
import java.text.SimpleDateFormat ;
import java.util.Date ;
2021-05-20 01:50:50 +02:00
public class GenericUtils {
2024-03-14 12:59:23 +01:00
private static final Logger logger = LoggerFactory . getLogger ( GenericUtils . class ) ;
2023-10-04 15:17:12 +02:00
public static final String endOfLine = " \ n " ;
2023-07-25 10:59:47 +02:00
private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat ( " yyyy-MM-dd HH:mm:ss.SSS z " ) ;
2023-05-11 02:07:55 +02:00
2023-10-04 15:17:12 +02:00
2023-05-11 02:07:55 +02:00
public static String getReadableCurrentTimeAndZone ( ) {
return ( simpleDateFormat . format ( new Date ( System . currentTimeMillis ( ) ) ) ) ;
}
2021-05-20 01:50:50 +02:00
public static int getRandomNumber ( int min , int max ) {
return ( int ) ( Math . random ( ) * ( max - min + 1 ) + min ) ;
}
2023-05-11 02:07:55 +02:00
2024-03-14 12:59:23 +01:00
public static String getSelectedStackTraceForCausedException ( Throwable thr , String firstMessage , String additionalMessage , int numOfLines )
{
// The stacktrace of the "ExecutionException" is the one of the current code and not the code which ran inside the background-task. Try to get the cause.
Throwable causedThrowable = thr . getCause ( ) ;
if ( causedThrowable = = null ) {
logger . warn ( " No cause was retrieved for the \" ExecutionException \" ! " ) ;
causedThrowable = thr ;
}
String initialMessage = firstMessage + causedThrowable . getMessage ( ) + ( ( additionalMessage ! = null ) ? additionalMessage : " " ) ;
return getSelectiveStackTrace ( causedThrowable , initialMessage , numOfLines ) ;
}
2023-05-11 02:07:55 +02:00
public static String getSelectiveStackTrace ( Throwable thr , String initialMessage , int numOfLines )
{
StackTraceElement [ ] stels = thr . getStackTrace ( ) ;
StringBuilder sb = new StringBuilder ( numOfLines * 100 ) ;
if ( initialMessage ! = null )
2024-03-13 11:15:59 +01:00
sb . append ( initialMessage ) . append ( GenericUtils . endOfLine ) . append ( " Stacktrace: " ) . append ( GenericUtils . endOfLine ) ; // This StringBuilder is thread-safe as a local-variable.
2023-05-11 02:07:55 +02:00
for ( int i = 0 ; ( i < stels . length ) & & ( i < = numOfLines ) ; + + i ) {
sb . append ( stels [ i ] ) ;
2023-10-04 15:17:12 +02:00
if ( i < numOfLines ) sb . append ( GenericUtils . endOfLine ) ;
2023-05-11 02:07:55 +02:00
}
return sb . toString ( ) ;
}
2023-06-06 15:49:53 +02:00
public static String getRequestorAddress ( HttpServletRequest request )
{
String remoteAddr = request . getHeader ( " X-FORWARDED-FOR " ) ; // This retrieves the original IP address, if the request passes through a proxy server.
if ( remoteAddr = = null )
remoteAddr = request . getRemoteAddr ( ) ;
return remoteAddr ;
}
2021-05-20 01:50:50 +02:00
}