diff --git a/.circleci/config.yml b/.circleci/config.yml index ed70da3..c7140c2 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -14,7 +14,6 @@ jobs: - run: name: Audit Dependencies command: ./gradlew dependencyCheckAnalyze - test: parameters: jdk-version: @@ -26,10 +25,23 @@ jobs: - image: rabbitmq:3.8.3 steps: - checkout - - run: TERM=${TERM:-dumb} ./gradlew clean test + - run: + name: Run Tests + command: TERM=${TERM:-dumb} ./gradlew clean test - store_test_results: path: build/reports - publish: + test_and_build_snapshot: + executor: docker + docker: + - image: amazoncorretto:17-alpine-jdk + working_directory: ~/sailor-jvm + steps: + - checkout + - run: | + TERM=${TERM:-dumb} ./gradlew shadowJar + - store_artifacts: + path: ~/sailor-jvm/build/libs + test_and_build_release: executor: docker docker: - image: amazoncorretto:17-alpine-jdk @@ -37,11 +49,9 @@ jobs: steps: - checkout - run: | - TERM=${TERM:-dumb} ./gradlew publish -PsonatypeUsername=$SONATYPE_USERNAME \ - -PsonatypePassword=$SONATYPE_PASSWORD \ - -PsigningPassword=${SIGNING_PSW} \ - -PsigningKeyBase64=${SIGNING_KEY} \ - -PkeyId=${SIGNING_KEY_ID} + TERM=${TERM:-dumb} ./gradlew publishToMavenLocal + - store_artifacts: + path: ~/sailor-jvm/build/libs/ workflows: nightly: triggers: @@ -54,48 +64,43 @@ workflows: jobs: - audit: name: "Audit dependencies" - test_and_publish_snapshot: + build_snapshot: jobs: - - audit: - name: "Audit dependencies" + - audit - test: matrix: parameters: - jdk-version: [ "amazoncorretto:8-alpine-jdk", "amazoncorretto:11-alpine-jdk", "amazoncorretto:17-alpine-jdk" ] - - publish: - name: "Publish SNAPSHOT version to Maven" + jdk-version: [ "amazoncorretto:11-alpine-jdk", "amazoncorretto:17-alpine-jdk" ] + - test_and_build_snapshot: requires: + - audit - test - - "Audit dependencies" filters: - tags: - ignore: /.*/ - test_and_publish_release: + branches: + ignore: master + build_release: jobs: - audit: - name: "Audit dependencies" filters: branches: ignore: /.*/ tags: only: /^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/ - test: - filters: - branches: - ignore: /.*/ - tags: - only: /^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/ matrix: parameters: - jdk-version: [ "amazoncorretto:8-alpine-jdk", "amazoncorretto:11-alpine-jdk", "amazoncorretto:17-alpine-jdk" ] - - - publish: - name: "Publish release version to Maven" + jdk-version: [ "amazoncorretto:11-alpine-jdk", "amazoncorretto:17-alpine-jdk" ] filters: branches: ignore: /.*/ tags: only: /^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/ + - test_and_build_release: requires: + - audit - test - - "Audit dependencies" \ No newline at end of file + filters: + branches: + only: master + tags: + only: /^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/ diff --git a/.env b/.env index 63fbf34..6df9292 100644 --- a/.env +++ b/.env @@ -17,4 +17,5 @@ ELASTICIO_SNAPSHOT_ROUTING_KEY=javasailor.test_exec.step_1.snapshot ELASTICIO_REBOUND_ROUTING_KEY=javasailor.test_exec.step_1.rebound ELASTICIO_SHAPSHOT_ROUTING_KEY=javasailor.test_exec.step_1.rebound ELASTICIO_MESSAGE_CRYPTO_PASSWORD=k8HO8UurPfKUNjECNAbvLRjBHWkIWz -ELASTICIO_MESSAGE_CRYPTO_IV=1gmpybK4iLRRyyu0 \ No newline at end of file +ELASTICIO_MESSAGE_CRYPTO_IV=1gmpybK4iLRRyyu0 +ELASTICIO_API_REQUEST_RETRY_ATTEMPTS=10 diff --git a/README.md b/README.md index 4da742a..308b407 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,12 @@ To build the project run in terminal - `ELASTICIO_CONSUMER_THREAD_POOL_SIZE` - if not specified (by default) it equals Prefetch Count value. Indicates the size of the thread pool for AMQP consumers. ## Development lifecycle -Prerequisites: +Prerequisites: Imagine that current sailor version is `3.5.1` and you gonna release new major version `4.0.0` -1. Create branch to implement feature. -2. During implementation specify `-SNAPHOT` suffix for the version in the `build.gradle`. The version should have next value - `4.0.0-SHAPSHOT`. -3. If you want to test new sailor version, just push changes to your feature branch. If you push any changes to the Sailor GitHub repository with X.X.X-SNAPSHOT, circle.ci will automatically upload the SNAPSHOT version to Sonatype repository. -After CI job will be done you can use `4.0.0-SHAPSHOT` version in the components. +1. Create branch to implement feature. +2. During implementation specify `-SNAPSHOT` suffix for the version in the `build.gradle`. The version should have next value - `4.0.0-SHAPSHOT`. +3. If you want to test new sailor version, just push changes to your feature branch. If you push any changes to the Sailor GitHub repository with X.X.X-SNAPSHOT, circle.ci will automatically upload the SNAPSHOT version to Sonatype repository. +After CI job will be done you can use `4.0.0-SHAPSHOT` version in the components. 4. After code changes will be reviewed and tested by qa, remove `-SNAPHOT` suffix, and merge Pull Request to **master** branch. The version should have next value - `4.0.0`. 5. To publish stable release version create GitHub release with tag **4.0.0**. This will trigger CI pipeline to publish release version to the Production maven repository. @@ -48,4 +48,4 @@ Once you finally decided that you want to publish the Sailor artefacts to Maven 11. Use the artefacts from Maven Central -Usually it takes 8 to 24 hour for the published artefacts to appear on Maven Central. Just check [here](https://search.maven.org/search?q=a:sailor-jvm) to find out if the Sailor version is finally publicly available. \ No newline at end of file +Usually it takes 8 to 24 hour for the published artefacts to appear on Maven Central. Just check [here](https://search.maven.org/search?q=a:sailor-jvm) to find out if the Sailor version is finally publicly available. diff --git a/build.gradle b/build.gradle index 1aa974f..9ad2f93 100644 --- a/build.gradle +++ b/build.gradle @@ -18,8 +18,8 @@ plugins { id 'com.github.johnrengelman.shadow' version '7.1.2' } group = 'io.elastic' -version = '4.0.3' -sourceCompatibility = '1.8' +version = '5.0.0-SNAPSHOT' +sourceCompatibility = '11' dependencyCheck { format = 'ALL' @@ -60,10 +60,10 @@ dependencies { api 'com.rabbitmq:amqp-client:5.21.0' api 'commons-codec:commons-codec:1.17.1' api 'commons-io:commons-io:2.16.1' - api 'org.apache.httpcomponents:httpclient:4.5.14' - api 'com.google.inject:guice:5.1.0' - api 'com.google.inject.extensions:guice-assistedinject:5.1.0' - api 'ch.qos.logback:logback-classic:1.2.13' + api 'org.apache.httpcomponents.client5:httpclient5:5.5' + api 'com.google.inject:guice:7.0.0' + api 'com.google.inject.extensions:guice-assistedinject:7.0.0' + api 'ch.qos.logback:logback-classic:1.5.18' api 'ch.qos.logback.contrib:logback-json-classic:0.1.5' api 'ch.qos.logback.contrib:logback-jackson:0.1.5' api 'com.fasterxml.jackson.core:jackson-databind:2.17.2' @@ -89,7 +89,7 @@ java { } wrapper { - gradleVersion = '7.5.1' + gradleVersion = '8.5' } publishing { @@ -213,3 +213,9 @@ shadowJar { archiveVersion.set(project.version.toString()) archiveClassifier.set('') } + +task printVersion { + doLast { + println "Sailor JVM version: ${project.version}" + } +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 62d4c05..d64cd49 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index ae04661..1af9e09 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,7 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip +networkTimeout=10000 +validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index fbd7c51..1aa94a4 100755 --- a/gradlew +++ b/gradlew @@ -1,7 +1,7 @@ -#!/usr/bin/env sh +#!/bin/sh # -# Copyright 2015 the original author or authors. +# Copyright © 2015-2021 the original authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -17,67 +17,99 @@ # ############################################################################## -## -## Gradle start up script for UN*X -## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# ############################################################################## # Attempt to set APP_HOME + # Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >/dev/null -APP_HOME="`pwd -P`" -cd "$SAVED" >/dev/null -APP_NAME="Gradle" -APP_BASE_NAME=`basename "$0"` - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD="maximum" +MAX_FD=maximum warn () { echo "$*" -} +} >&2 die () { echo echo "$*" echo exit 1 -} +} >&2 # OS specific support (must be 'true' or 'false'). cygwin=false msys=false darwin=false nonstop=false -case "`uname`" in - CYGWIN* ) - cygwin=true - ;; - Darwin* ) - darwin=true - ;; - MINGW* ) - msys=true - ;; - NONSTOP* ) - nonstop=true - ;; +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; esac CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar @@ -87,9 +119,9 @@ CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar if [ -n "$JAVA_HOME" ] ; then if [ -x "$JAVA_HOME/jre/sh/java" ] ; then # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" + JAVACMD=$JAVA_HOME/jre/sh/java else - JAVACMD="$JAVA_HOME/bin/java" + JAVACMD=$JAVA_HOME/bin/java fi if [ ! -x "$JAVACMD" ] ; then die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME @@ -98,88 +130,120 @@ Please set the JAVA_HOME variable in your environment to match the location of your Java installation." fi else - JAVACMD="java" - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the location of your Java installation." + fi fi # Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then - MAX_FD_LIMIT=`ulimit -H -n` - if [ $? -eq 0 ] ; then - if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then - MAX_FD="$MAX_FD_LIMIT" - fi - ulimit -n $MAX_FD - if [ $? -ne 0 ] ; then - warn "Could not set maximum file descriptor limit: $MAX_FD" - fi - else - warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" - fi +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac fi -# For Darwin, add options to specify how the application appears in the dock -if $darwin; then - GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" -fi +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. # For Cygwin or MSYS, switch paths to Windows format before running java -if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then - APP_HOME=`cygpath --path --mixed "$APP_HOME"` - CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` - - JAVACMD=`cygpath --unix "$JAVACMD"` - - # We build the pattern for arguments to be converted via cygpath - ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` - SEP="" - for dir in $ROOTDIRSRAW ; do - ROOTDIRS="$ROOTDIRS$SEP$dir" - SEP="|" - done - OURCYGPATTERN="(^($ROOTDIRS))" - # Add a user-defined pattern to the cygpath arguments - if [ "$GRADLE_CYGPATTERN" != "" ] ; then - OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" - fi +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + # Now convert the arguments - kludge to limit ourselves to /bin/sh - i=0 - for arg in "$@" ; do - CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` - CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option - - if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition - eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` - else - eval `echo args$i`="\"$arg\"" + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) fi - i=`expr $i + 1` + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg done - case $i in - 0) set -- ;; - 1) set -- "$args0" ;; - 2) set -- "$args0" "$args1" ;; - 3) set -- "$args0" "$args1" "$args2" ;; - 4) set -- "$args0" "$args1" "$args2" "$args3" ;; - 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; - esac fi -# Escape application args -save () { - for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done - echo " " -} -APP_ARGS=`save "$@"` -# Collect all arguments for the java command, following the shell quoting and substitution rules -eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat index 5093609..6689b85 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,104 +1,92 @@ -@rem -@rem Copyright 2015 the original author or authors. -@rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at -@rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. -@rem - -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Resolve any "." and ".." in APP_HOME to make it shorter. -for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto init - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:init -@rem Get command-line arguments, handling Windows variants - -if not "%OS%" == "Windows_NT" goto win9xME_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/main/java/io/elastic/sailor/Service.java b/src/main/java/io/elastic/sailor/Service.java index 71b4927..d9b2999 100644 --- a/src/main/java/io/elastic/sailor/Service.java +++ b/src/main/java/io/elastic/sailor/Service.java @@ -5,11 +5,14 @@ import com.google.inject.Injector; import com.google.inject.name.Named; import io.elastic.sailor.impl.HttpUtils; + +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import jakarta.json.Json; import jakarta.json.JsonObject; + import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; @@ -22,7 +25,14 @@ public class Service { private JsonObject configuration; private String triggerOrAction; private String metaModelName; - private int retryCount; + private final CloseableHttpClient httpClient; + + @Inject + public Service( + @Named(Constants.ENV_VAR_API_REQUEST_RETRY_ATTEMPTS) final int retryCount + ) { + this.httpClient = HttpUtils.createHttpClient(retryCount); + } protected ServiceExecutionParameters createServiceExecutionParameters() { @@ -87,7 +97,7 @@ private void createResponseAndSend(final String status, .add("data", data) .build(); - sendData(this.postResultUrl, payload, this.retryCount); + sendData(this.postResultUrl, payload); } public void executeMethod(final ServiceMethods method, final ServiceExecutionParameters params) { @@ -110,11 +120,11 @@ private void processException(Exception e) { createResponseAndSend("error", data); } - private static void sendData(String url, JsonObject payload, int retryCnt) { + private void sendData(String url, JsonObject payload) { logger.info("Sending response"); - HttpUtils.postJson(url, payload, new HttpUtils.BasicURLAuthorizationHandler(), retryCnt); + HttpUtils.postJson(url, this.httpClient, payload, new HttpUtils.BasicAuthorizationHandler(url)); logger.info("Received response from server"); } @@ -143,8 +153,4 @@ public void setTriggerOrAction(@Named(Constants.ENV_VAR_ACTION_OR_TRIGGER) final public void setMetaModelName(@Named(Constants.ENV_VAR_GET_MODEL_METHOD) final String metaModelName) { this.metaModelName = metaModelName; } - @Inject - public void setRetryCount(@Named(Constants.ENV_VAR_API_REQUEST_RETRY_ATTEMPTS) final int retryCount) { - this.retryCount = retryCount; - } } diff --git a/src/main/java/io/elastic/sailor/Utils.java b/src/main/java/io/elastic/sailor/Utils.java index 8e27b24..5f3c7db 100644 --- a/src/main/java/io/elastic/sailor/Utils.java +++ b/src/main/java/io/elastic/sailor/Utils.java @@ -3,7 +3,6 @@ import com.rabbitmq.client.AMQP; import io.elastic.api.JSON; import io.elastic.api.Message; -import io.elastic.api.Message.Builder; import io.elastic.sailor.impl.MessageEncoding; import jakarta.json.Json; @@ -206,4 +205,4 @@ public static MessageEncoding getMessageEncoding(final AMQP.BasicProperties prop return MessageEncoding.fromProtocolVersion(protocolVersion.intValue()); } -} \ No newline at end of file +} diff --git a/src/main/java/io/elastic/sailor/impl/ApiClientImpl.java b/src/main/java/io/elastic/sailor/impl/ApiClientImpl.java index c644a51..6b1d873 100644 --- a/src/main/java/io/elastic/sailor/impl/ApiClientImpl.java +++ b/src/main/java/io/elastic/sailor/impl/ApiClientImpl.java @@ -7,6 +7,8 @@ import io.elastic.sailor.Constants; import io.elastic.sailor.Step; import io.elastic.sailor.impl.HttpUtils.BasicAuthorizationHandler; + +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,8 +22,8 @@ public class ApiClientImpl implements ApiClient { private final BasicAuthorizationHandler authorizationHandler; private final String apiBaseUri; - private final int retryCount; private boolean putIncomingMessageIntoPassThrough; + private final CloseableHttpClient httpClient; @Inject public ApiClientImpl(@Named(Constants.ENV_VAR_API_URI) final String apiUri, @@ -32,8 +34,8 @@ public ApiClientImpl(@Named(Constants.ENV_VAR_API_URI) final String apiUri, this.authorizationHandler = new BasicAuthorizationHandler(apiUser, apiKey); this.apiBaseUri = String.format("%s", apiUri); - this.retryCount = retryCount; this.putIncomingMessageIntoPassThrough = putIncomingMessageIntoPassThrough; + this.httpClient = HttpUtils.createHttpClient(retryCount); } @Override @@ -43,7 +45,7 @@ public Step retrieveFlowStep(final String taskId, final String stepId) { logger.info("Retrieving step data at: {}", path); - final JsonObject step = HttpUtils.getJson(uri, authorizationHandler, this.retryCount); + final JsonObject step = HttpUtils.getJson(uri, this.httpClient, authorizationHandler); return new Step(step, uri, authorizationHandler, putIncomingMessageIntoPassThrough); } @@ -55,7 +57,7 @@ public JsonObject updateAccount(final String accountId, final JsonObject body) { logger.info("Updating account for user {} at: {}", this.authorizationHandler.getUsername(), path); - return HttpUtils.putJson(uri, body, authorizationHandler, this.retryCount); + return HttpUtils.putJson(uri, this.httpClient, body, authorizationHandler); } @@ -63,14 +65,14 @@ public JsonObject updateAccount(final String accountId, final JsonObject body) { public void storeStartupState(final String flowId, final JsonObject body) { final String uri = getStartupStateUrl(flowId); - HttpUtils.postJson(uri, body, authorizationHandler, this.retryCount); + HttpUtils.postJson(uri, this.httpClient, body, authorizationHandler); } @Override public JsonObject retrieveStartupState(final String flowId) { final String uri = getStartupStateUrl(flowId); - final JsonObject state = HttpUtils.getJson(uri, authorizationHandler, this.retryCount); + final JsonObject state = HttpUtils.getJson(uri, this.httpClient, authorizationHandler); if (state == null) { return Json.createObjectBuilder().build(); @@ -83,7 +85,7 @@ public JsonObject retrieveStartupState(final String flowId) { public void deleteStartupState(final String flowId) { final String uri = getStartupStateUrl(flowId); - HttpUtils.delete(uri, authorizationHandler, this.retryCount); + HttpUtils.delete(uri, this.httpClient, authorizationHandler); } private String getStartupStateUrl(final String flowId) { diff --git a/src/main/java/io/elastic/sailor/impl/BunyanJsonLayout.java b/src/main/java/io/elastic/sailor/impl/BunyanJsonLayout.java index fb352b0..761cb14 100644 --- a/src/main/java/io/elastic/sailor/impl/BunyanJsonLayout.java +++ b/src/main/java/io/elastic/sailor/impl/BunyanJsonLayout.java @@ -15,13 +15,18 @@ public class BunyanJsonLayout extends JsonLayout { - public static final String LEVEL = "level"; public static final String LEVEL_STRING = "level_str"; - public static final String TIME = "time"; - public static final String HOSTNAME = "hostname"; public static final String THREAD_ID = "threadId"; public static final String MESSAGE_ID = "messageId"; public static final String PARENT_MESSAGE_ID = "parentMessageId"; + + // Required fields for Bunyan format + public static final String VERSION = "v"; + public static final String LEVEL = "level"; + public static final String NAME = "name"; + public static final String HOSTNAME = "hostname"; + public static final String PID = "pid"; + public static final String TIME = "time"; public static final String MESSAGE = "msg"; private static int BUNYAN_LEVEL_TRACE = 10; @@ -55,9 +60,12 @@ protected void addCustomDataToJsonMap(Map map, ILoggingEvent eve map.put(PARENT_MESSAGE_ID, parentMessageId); } + map.put(VERSION, "0"); map.put(LEVEL, getBunyanLevel(event)); - map.put(LEVEL_STRING, event.getLevel().levelStr); + map.put(NAME, "sailor-jvm"); + map.put(PID, String.valueOf(ProcessHandle.current().pid())); map.put(BunyanJsonLayout.TIME, time); + map.put(LEVEL_STRING, event.getLevel().levelStr); map.remove(JsonLayout.TIMESTAMP_ATTR_NAME); final Object message = map.get(JsonLayout.FORMATTED_MESSAGE_ATTR_NAME); @@ -67,7 +75,8 @@ protected void addCustomDataToJsonMap(Map map, ILoggingEvent eve try { map.put(BunyanJsonLayout.HOSTNAME, InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { - // ignore + // Set a default value so bunyan can still validate the log entry + map.put(BunyanJsonLayout.HOSTNAME, "unknown-host"); } } diff --git a/src/main/java/io/elastic/sailor/impl/CryptoServiceImpl.java b/src/main/java/io/elastic/sailor/impl/CryptoServiceImpl.java index bfd10d5..857f051 100755 --- a/src/main/java/io/elastic/sailor/impl/CryptoServiceImpl.java +++ b/src/main/java/io/elastic/sailor/impl/CryptoServiceImpl.java @@ -68,6 +68,7 @@ public JsonObject decryptMessageContent(final byte[] bytes, final MessageEncodin } public byte[] encrypt(final String message, final MessageEncoding encoding) { + logger.info("Encrypting message: {}", message); try { Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, encryptionKey, encryptionIV); @@ -114,4 +115,4 @@ private Key generateKey(String encryptionKey) { throw new RuntimeException(e); } } -} \ No newline at end of file +} diff --git a/src/main/java/io/elastic/sailor/impl/GracefulShutdownHandler.java b/src/main/java/io/elastic/sailor/impl/GracefulShutdownHandler.java index c1738cb..f5690cf 100644 --- a/src/main/java/io/elastic/sailor/impl/GracefulShutdownHandler.java +++ b/src/main/java/io/elastic/sailor/impl/GracefulShutdownHandler.java @@ -41,6 +41,11 @@ public void run() { } protected void prepareGracefulShutdown() { + logger.info("Preparing graceful shutdown"); + + HttpUtils.closeHttpClients(); + logger.info("Closed all HTTP clients"); + if (this.amqp == null) { return; } diff --git a/src/main/java/io/elastic/sailor/impl/HttpUtils.java b/src/main/java/io/elastic/sailor/impl/HttpUtils.java index a13314c..e0b3d22 100644 --- a/src/main/java/io/elastic/sailor/impl/HttpUtils.java +++ b/src/main/java/io/elastic/sailor/impl/HttpUtils.java @@ -1,61 +1,55 @@ package io.elastic.sailor.impl; -import io.elastic.api.JSON; -import io.elastic.sailor.UnexpectedStatusCodeException; -import org.apache.commons.io.IOUtils; -import org.apache.http.*; - -import org.apache.http.auth.AuthenticationException; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.methods.*; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.auth.BasicScheme; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.protocol.HTTP; -import org.apache.http.util.EntityUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import jakarta.json.JsonObject; import java.io.IOException; import java.io.InterruptedIOException; import java.io.UnsupportedEncodingException; -import java.net.MalformedURLException; import java.net.SocketException; -import java.net.URL; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URLDecoder; +import java.util.ArrayList; -public class HttpUtils { - - private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class.getName()); - - public static String postJson(String url, JsonObject body, AuthorizationHandler authorizationHandler) throws IOException { - return postJson(url, body, authorizationHandler, 0); - } +import org.apache.commons.io.IOUtils; +import org.apache.hc.client5.http.HttpRequestRetryStrategy; +import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; +import org.apache.hc.client5.http.classic.methods.*; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.client5.http.utils.Base64; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.HttpHeaders; +import org.apache.hc.core5.http.HttpRequest; +import org.apache.hc.core5.http.HttpResponse; +import org.apache.hc.core5.http.ParseException; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.http.io.entity.StringEntity; +import org.apache.hc.core5.http.protocol.HttpContext; +import org.apache.hc.core5.util.TimeValue; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; - public static JsonObject getJson(String url, final AuthorizationHandler authorizationHandler) { - return getJson(url, authorizationHandler, 0); - } +import io.elastic.api.JSON; +import io.elastic.sailor.UnexpectedStatusCodeException; +import jakarta.json.JsonObject; - public static JsonObject putJson(final String url, - final JsonObject body, - final AuthorizationHandler authorizationHandler) { - return putJson(url, body, authorizationHandler, 0); - } +public class HttpUtils { - public static String postJson(final String url, - final JsonObject body, - final AuthorizationHandler authorizationHandler, - final int retryCount) { + private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class.getName()); + private static final ArrayList httpClients = new ArrayList(); - final HttpPost httpPost = new HttpPost(url); - httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json"); + public static String postJson( + final String url, + final CloseableHttpClient httpClient, + final JsonObject body, + final AuthorizationHandler authorizationHandler + ) { + final HttpPost httpPost = new HttpPost(sanitizeUrl(url)); + httpPost.addHeader(HttpHeaders.CONTENT_TYPE, "application/json"); httpPost.setEntity(createStringEntity(body)); final byte[] bytes = sendHttpRequest( - httpPost, authorizationHandler, retryCount, new ByteArrayHttpEntityCallback()); + httpPost, httpClient, authorizationHandler, new ByteArrayHttpEntityCallback()); if (bytes == null) { throw new RuntimeException("Null response received"); @@ -66,17 +60,17 @@ public static String postJson(final String url, return new String(bytes); } - public static JsonObject post(final String url, - final HttpEntity body, - final AuthorizationHandler authorizationHandler, - final int retryCount) { - - - final HttpPost httpPost = new HttpPost(url); + public static JsonObject post( + final String url, + final CloseableHttpClient httpClient, + final HttpEntity body, + final AuthorizationHandler authorizationHandler + ) { + final HttpPost httpPost = new HttpPost(sanitizeUrl(url)); httpPost.setEntity(body); final JsonObject result = sendHttpRequest( - httpPost, authorizationHandler, retryCount, new JsonObjectParseCallback()); + httpPost, httpClient, authorizationHandler, new JsonObjectParseCallback()); if (result == null) { throw new RuntimeException("Null response received"); @@ -87,15 +81,17 @@ public static JsonObject post(final String url, return result; } - public static JsonObject getJson(final String url, - final AuthorizationHandler authorizationHandler, - int retryCount) { + public static JsonObject getJson( + final String url, + final CloseableHttpClient httpClient, + final AuthorizationHandler authorizationHandler + ) { - final HttpGet httpGet = new HttpGet(url); - httpGet.addHeader(HTTP.USER_AGENT, "eio-sailor-java"); + final HttpGet httpGet = new HttpGet(sanitizeUrl(url)); + httpGet.addHeader(HttpHeaders.USER_AGENT, "eio-sailor-java"); final JsonObject content = sendHttpRequest( - httpGet, authorizationHandler, retryCount, new JsonObjectParseCallback()); + httpGet, httpClient, authorizationHandler, new JsonObjectParseCallback()); if (content == null) { throw new RuntimeException("Null response received"); @@ -104,15 +100,17 @@ public static JsonObject getJson(final String url, return content; } - public static byte[] get(final String url, - final AuthorizationHandler authorizationHandler, - int retryCount) { + public static byte[] get( + final String url, + final CloseableHttpClient httpClient, + final AuthorizationHandler authorizationHandler + ) { - final HttpGet httpGet = new HttpGet(url); - httpGet.addHeader(HTTP.USER_AGENT, "eio-sailor-java"); + final HttpGet httpGet = new HttpGet(sanitizeUrl(url)); + httpGet.addHeader(HttpHeaders.USER_AGENT, "eio-sailor-java"); final byte[] content = sendHttpRequest( - httpGet, authorizationHandler, retryCount, new ByteArrayHttpEntityCallback()); + httpGet, httpClient, authorizationHandler, new ByteArrayHttpEntityCallback()); if (content == null) { throw new RuntimeException("Null response received"); @@ -122,17 +120,18 @@ public static byte[] get(final String url, } - public static JsonObject putJson(final String url, - final JsonObject body, - final AuthorizationHandler authorizationHandler, - final int retryCount) { - - final HttpPut httpPut = new HttpPut(url); - httpPut.addHeader(HTTP.CONTENT_TYPE, "application/json"); + public static JsonObject putJson( + final String url, + final CloseableHttpClient httpClient, + final JsonObject body, + final AuthorizationHandler authorizationHandler + ) { + final HttpPut httpPut = new HttpPut(sanitizeUrl(url)); + httpPut.addHeader(HttpHeaders.CONTENT_TYPE, "application/json"); httpPut.setEntity(createStringEntity(body)); final JsonObject content = sendHttpRequest( - httpPut, authorizationHandler, retryCount, new JsonObjectParseCallback()); + httpPut, httpClient, authorizationHandler, new JsonObjectParseCallback()); if (content == null) { throw new RuntimeException("Null response received"); @@ -144,14 +143,15 @@ public static JsonObject putJson(final String url, } - public static void delete(final String url, - final AuthorizationHandler authorizationHandler, - final int retryCount) { - - final HttpDelete httpDelete = new HttpDelete(url); - httpDelete.addHeader(HTTP.CONTENT_TYPE, "application/json"); + public static void delete( + final String url, + final CloseableHttpClient httpClient, + final AuthorizationHandler authorizationHandler + ) { + final HttpDelete httpDelete = new HttpDelete(sanitizeUrl(url)); + httpDelete.addHeader(HttpHeaders.CONTENT_TYPE, "application/json"); - sendHttpRequest(httpDelete, authorizationHandler, retryCount, null); + sendHttpRequest(httpDelete, httpClient, authorizationHandler, null); logger.info("Successfully sent delete"); @@ -159,25 +159,15 @@ public static void delete(final String url, } public static StringEntity createStringEntity(final JsonObject body) { - return createStringEntity(JSON.stringify(body)); - } - - public static StringEntity createStringEntity(final String content) { - try { - return new StringEntity(content); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } + return new StringEntity(JSON.stringify(body)); } - private static T sendHttpRequest(final HttpUriRequest request, - final AuthorizationHandler authorizationHandler, - final int retryCount, - final HttpEntityCallback callback) { - - CloseableHttpClient httpClient = HttpClients.custom() - .setRetryHandler((exception, executionCount, context) -> { - if (executionCount >= retryCount) { + public static CloseableHttpClient createHttpClient(final int retryCount) { + final CloseableHttpClient httpClient = HttpClients.custom() + .setRetryStrategy(new HttpRequestRetryStrategy() { + @Override + public boolean retryRequest(HttpRequest request, IOException exception, int execCount, HttpContext context) { + if (execCount >= retryCount) { // Do not retry if over max retry count return false; } @@ -188,41 +178,83 @@ private static T sendHttpRequest(final HttpUriRequest request, if (exception instanceof SocketException) { return true; } - return false; - }) - .build(); + } + @Override + public boolean retryRequest(HttpResponse response, int execCount, HttpContext context) { + final int statusCode = response.getCode(); + final boolean shouldRetry = statusCode >= 500 && execCount <= retryCount; + if (shouldRetry) { + logger.warn(response.toString()); + logger.warn("Error {} during request, retrying ({}/{})", statusCode, execCount, retryCount); + } + return shouldRetry; + } + private static final long MAX_BACKOFF = 15000; // 15 seconds + private TimeValue getRetryInterval(int execCount) { + final double delay = Math.pow(2, execCount) * 1000; + final double randomSum = delay * 0.2 * Math.random(); // 0-20% of the delay + return TimeValue.ofMilliseconds((long) Math.min(delay + randomSum, MAX_BACKOFF)); + } + @Override + public TimeValue getRetryInterval(HttpRequest request, IOException exception, int execCount, HttpContext context) { + return getRetryInterval(execCount); + } + @Override + public TimeValue getRetryInterval(HttpResponse response, int execCount, HttpContext context) { + return getRetryInterval(execCount); + } + }) + .build(); + httpClients.add(httpClient); + return httpClient; + } - logger.info("Sending {} request to {}", request.getMethod(), request.getURI().getPath()); - try { - authorizationHandler.authorize(request); - final CloseableHttpResponse response = httpClient.execute(request); - final StatusLine statusLine = response.getStatusLine(); - final int statusCode = statusLine.getStatusCode(); - if (statusCode >= 400) { - throw new UnexpectedStatusCodeException(statusCode); + public static void closeHttpClients() { + for (CloseableHttpClient client : httpClients) { + try { + client.close(); + } catch (IOException e) { + logger.warn("Failed to close HTTP client: {}", e.getMessage()); } + } + httpClients.clear(); + } - final HttpEntity responseEntity = response.getEntity(); + private static T sendHttpRequest( + final HttpUriRequest request, + final CloseableHttpClient httpClient, + final AuthorizationHandler authorizationHandler, + final HttpEntityCallback callback + ) { + try { + logger.info("Sending {} request to {}", request.getMethod(), request.getUri().getPath()); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + authorizationHandler.authorize(request); - if (responseEntity == null || callback == null) { - return null; - } + try { + return httpClient.execute(request, response -> { + final int statusCode = response.getCode(); + if (statusCode >= 400) { + throw new UnexpectedStatusCodeException(statusCode); + } - final T result = callback.handle(responseEntity); + final HttpEntity responseEntity = response.getEntity(); - EntityUtils.consume(responseEntity); + if (responseEntity == null || callback == null) { + return null; + } - return result; + final T result = callback.handle(responseEntity); + EntityUtils.consume(responseEntity); + + return result; + }); } catch (Exception e) { throw new RuntimeException(e); - } finally { - try { - httpClient.close(); - } catch (IOException e) { - e.printStackTrace(); - } } } @@ -253,15 +285,13 @@ public byte[] handle(HttpEntity entity) { } } - - private static UsernamePasswordCredentials retrieveCredentialsFromUrl(final URL url) { - final String userInfo = url.getUserInfo(); + private static UsernamePasswordCredentials retrieveCredentialsFromUri(final URI uri) { + final String userInfo = uri.getUserInfo(); if (userInfo == null) { - throw new IllegalArgumentException("User info is missing in the given url: " + url); + throw new IllegalArgumentException("User info is missing in the given url: " + uri); } - String decodedUserInfo = urlDecode(userInfo); final String[] userAndPassword = decodedUserInfo.split(":"); @@ -270,7 +300,7 @@ private static UsernamePasswordCredentials retrieveCredentialsFromUrl(final URL throw new IllegalArgumentException("Either username or password is missing"); } - return new UsernamePasswordCredentials(userAndPassword[0], userAndPassword[1]); + return new UsernamePasswordCredentials(userAndPassword[0], userAndPassword[1].toCharArray()); } private static String urlDecode(final String input) { @@ -286,7 +316,7 @@ private static String consumeToString(final HttpEntity entity) { String result; try { result = EntityUtils.toString(entity); - } catch (IOException e) { + } catch (IOException | ParseException e) { throw new RuntimeException(e); } @@ -295,6 +325,26 @@ private static String consumeToString(final HttpEntity entity) { return result; } + private static URI sanitizeUrl(final String url) { + URI originalUri; + URI sanitizedUri; + try { + originalUri = new URI(url); + sanitizedUri = new URI( + originalUri.getScheme(), + null, // remove user info + originalUri.getHost(), + originalUri.getPort(), + originalUri.getPath(), + originalUri.getQuery(), + originalUri.getFragment() + ); + } catch (URISyntaxException e) { + throw new RuntimeException("Invalid url " + url); + } + return sanitizedUri; + } + public static void consume(final HttpEntity entity) { try { EntityUtils.consume(entity); @@ -314,14 +364,10 @@ static abstract class AbstractBasicAuthorizationHandler implements Authorization @Override public void authorize(HttpUriRequest request) { - - try { - final Header header = new BasicScheme() - .authenticate(createCredentials(request), request, null); - request.addHeader(header); - } catch (AuthenticationException e) { - throw new RuntimeException(e); - } + UsernamePasswordCredentials creds = createCredentials(request); + String auth = creds.getUserName() + ":" + String.valueOf(creds.getUserPassword()); + String encodedAuth = Base64.encodeBase64String(auth.getBytes()); + request.addHeader(HttpHeaders.AUTHORIZATION, "Basic " + encodedAuth); } } @@ -329,7 +375,14 @@ public static class BasicAuthorizationHandler extends AbstractBasicAuthorization private UsernamePasswordCredentials credentials; public BasicAuthorizationHandler(final String username, final String password) { - this.credentials = new UsernamePasswordCredentials(username, password); + this.credentials = new UsernamePasswordCredentials(username, password.toCharArray()); + } + public BasicAuthorizationHandler(final String url) { + try { + this.credentials = retrieveCredentialsFromUri(new URI(url)); + } catch (URISyntaxException e) { + throw new IllegalArgumentException("Invalid URL provided: " + url, e); + } } @Override @@ -342,23 +395,6 @@ public String getUsername() { } } - public static class BasicURLAuthorizationHandler extends AbstractBasicAuthorizationHandler { - - @Override - UsernamePasswordCredentials createCredentials(final HttpUriRequest request) { - final URL url = getRequestURL(request); - return retrieveCredentialsFromUrl(url); - } - - private URL getRequestURL(final HttpUriRequest request) { - try { - return request.getURI().toURL(); - } catch (MalformedURLException e) { - throw new RuntimeException(e); - } - } - } - public static class BearerAuthorizationHandler implements AuthorizationHandler { private String token; diff --git a/src/main/java/io/elastic/sailor/impl/MessageConsumer.java b/src/main/java/io/elastic/sailor/impl/MessageConsumer.java index e6189d2..0499b45 100755 --- a/src/main/java/io/elastic/sailor/impl/MessageConsumer.java +++ b/src/main/java/io/elastic/sailor/impl/MessageConsumer.java @@ -11,6 +11,8 @@ import jakarta.json.Json; import jakarta.json.JsonObject; + +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.slf4j.LoggerFactory; import org.slf4j.MDC; @@ -28,6 +30,7 @@ public class MessageConsumer extends DefaultConsumer { private final MessageResolver messageResolver; private final Channel channel; private final ExecutorService threadPool; + private final CloseableHttpClient httpClient; public MessageConsumer(Channel channel, CryptoServiceImpl cipher, @@ -46,6 +49,7 @@ public MessageConsumer(Channel channel, this.containerContext = containerContext; this.messageResolver = messageResolver; this.threadPool = threadPool; + this.httpClient = HttpUtils.createHttpClient(4); } @Override @@ -126,7 +130,7 @@ private static void removeFromMDC(final String key) { public JsonObject getSnapShot() { final String uri = this.step.getSnapshotUri(); final HttpUtils.AuthorizationHandler authorizationHandler = step.getAuthorizationHandler(); - final JsonObject step = HttpUtils.getJson(uri, authorizationHandler, 4); + final JsonObject step = HttpUtils.getJson(uri, this.httpClient, authorizationHandler); return getAsNullSafeObject(step, Constants.STEP_PROPERTY_SNAPSHOT); } diff --git a/src/main/java/io/elastic/sailor/impl/ObjectStorageImpl.java b/src/main/java/io/elastic/sailor/impl/ObjectStorageImpl.java index aa3d889..265e101 100644 --- a/src/main/java/io/elastic/sailor/impl/ObjectStorageImpl.java +++ b/src/main/java/io/elastic/sailor/impl/ObjectStorageImpl.java @@ -4,7 +4,12 @@ import com.google.inject.name.Named; import io.elastic.sailor.Constants; import io.elastic.sailor.ObjectStorage; -import org.apache.http.entity.ByteArrayEntity; + +import java.io.ByteArrayInputStream; + +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.io.entity.InputStreamEntity; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -17,6 +22,11 @@ public class ObjectStorageImpl implements ObjectStorage { private CryptoServiceImpl cryptoService; private String objectStorageUri; private String objectStorageToken; + private final CloseableHttpClient httpClient; + + public ObjectStorageImpl() { + this.httpClient = HttpUtils.createHttpClient(5); + } @Override public JsonObject getJsonObject(final String id) { @@ -35,9 +45,11 @@ public JsonObject getJsonObject(final String id) { final String endpoint = String.format("%s/objects/%s", this.objectStorageUri, id); - final byte[] bytes = HttpUtils.get(endpoint, - new HttpUtils.BearerAuthorizationHandler(this.objectStorageToken), - 5); + final byte[] bytes = HttpUtils.get( + endpoint, + this.httpClient, + new HttpUtils.BearerAuthorizationHandler(this.objectStorageToken) + ); return cryptoService.decryptMessageContent(bytes, MessageEncoding.UTF8); } @@ -64,11 +76,12 @@ public JsonObject post(String object) { final byte[] content = cryptoService.encrypt(object, MessageEncoding.UTF8); - - final JsonObject result = HttpUtils.post(endpoint, - new ByteArrayEntity(content), - new HttpUtils.BearerAuthorizationHandler(this.objectStorageToken), - 5); + final JsonObject result = HttpUtils.post( + endpoint, + this.httpClient, + new InputStreamEntity(new ByteArrayInputStream(content), ContentType.APPLICATION_JSON), + new HttpUtils.BearerAuthorizationHandler(this.objectStorageToken) + ); return result; } @@ -80,10 +93,10 @@ public void setCryptoService(final CryptoServiceImpl cryptoService) { @Inject(optional = true) public void setObjectStorageUri(final @Named(Constants.ENV_VAR_OBJECT_STORAGE_URI) String objectStorageUri) { - this.objectStorageUri = objectStorageUri; + this.objectStorageUri = objectStorageUri != null ? objectStorageUri.trim() : null; if (this.objectStorageUri != null && this.objectStorageUri.endsWith("/")) { - this.objectStorageUri.substring(0, this.objectStorageUri.length() - 1); + this.objectStorageUri = this.objectStorageUri.substring(0, this.objectStorageUri.length() - 1); } } diff --git a/src/test/groovy/io/elastic/sailor/impl/HttpUtilsSpec.groovy b/src/test/groovy/io/elastic/sailor/impl/HttpUtilsSpec.groovy index b182364..b9f23e4 100644 --- a/src/test/groovy/io/elastic/sailor/impl/HttpUtilsSpec.groovy +++ b/src/test/groovy/io/elastic/sailor/impl/HttpUtilsSpec.groovy @@ -6,8 +6,9 @@ import com.github.tomakehurst.wiremock.WireMockServer import com.github.tomakehurst.wiremock.http.Fault import com.github.tomakehurst.wiremock.stubbing.Scenario import io.elastic.sailor.impl.HttpUtils.BasicAuthorizationHandler -import io.elastic.sailor.impl.HttpUtils.BasicURLAuthorizationHandler +import io.elastic.sailor.UnexpectedStatusCodeException import org.junit.Rule +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient import spock.lang.Specification import jakarta.json.Json @@ -17,20 +18,22 @@ import static com.github.restdriver.clientdriver.RestClientDriver.onRequestTo import static com.github.tomakehurst.wiremock.client.WireMock.* import static org.hamcrest.Matchers.equalToIgnoringCase + class HttpUtilsSpec extends Specification { @Rule public ClientDriverRule driver = new ClientDriverRule(12345); - def basicURLAuthHandler = new HttpUtils.BasicURLAuthorizationHandler(); def basicAuthHandler = new BasicAuthorizationHandler("homer.simpson@example.org", "secret") def "should post json successfully when credentials are inside url"() { setup: + def url = "http://homer.simpson%40example.org:secret@localhost:12345/v1/exec/result/55e5eeb460a8e2070000001e" def body = Json.createObjectBuilder() .add('foo', 'barbaz') .build() + def httpClient = HttpUtils.createHttpClient(3) driver.addExpectation( onRequestTo("/v1/exec/result/55e5eeb460a8e2070000001e") @@ -42,13 +45,16 @@ class HttpUtilsSpec extends Specification { when: def result = HttpUtils.postJson( - "http://homer.simpson%40example.org:secret@localhost:12345/v1/exec/result/55e5eeb460a8e2070000001e", + url, + httpClient, body, - basicURLAuthHandler) + new HttpUtils.BasicAuthorizationHandler(url)) then: - result == '{"status":"done"}' + + cleanup: + httpClient.close() } def "should post json successfully"() { @@ -57,6 +63,7 @@ class HttpUtilsSpec extends Specification { def body = Json.createObjectBuilder() .add('foo', 'barbaz') .build() + def httpClient = HttpUtils.createHttpClient(3) driver.addExpectation( onRequestTo("/v1/exec/result/55e5eeb460a8e2070000001e") @@ -69,13 +76,15 @@ class HttpUtilsSpec extends Specification { when: def result = HttpUtils.postJson( "http://localhost:12345/v1/exec/result/55e5eeb460a8e2070000001e", + httpClient, body, - basicAuthHandler, - 0) + basicAuthHandler) then: - result == '{"status":"done"}' + + cleanup: + httpClient.close() } def "should put json successfully"() { @@ -84,6 +93,7 @@ class HttpUtilsSpec extends Specification { def body = Json.createObjectBuilder() .add('foo', 'barbaz') .build() + def httpClient = HttpUtils.createHttpClient(3) driver.addExpectation( onRequestTo("/v1/accounts/55e5eeb460a8e2070000001e") @@ -96,17 +106,21 @@ class HttpUtilsSpec extends Specification { when: def result = HttpUtils.putJson( "http://localhost:12345/v1/accounts/55e5eeb460a8e2070000001e", + httpClient, body, basicAuthHandler) then: - result.toString() == '{"id":"55e5eeb460a8e2070000001e"}' + + cleanup: + httpClient.close() } def "should get json successfully"() { setup: + def httpClient = HttpUtils.createHttpClient(3) driver.addExpectation( onRequestTo("/v1/users") .withBasicAuth("admin", "secret"), @@ -116,11 +130,14 @@ class HttpUtilsSpec extends Specification { when: def result = HttpUtils.getJson( "http://localhost:12345/v1/users", + httpClient, new BasicAuthorizationHandler("admin", "secret")) then: - result.toString() == '{"id":"1","email":"homer.simpson@example.org"}' + + cleanup: + httpClient.close() } def "should retry getting json"() { @@ -129,6 +146,7 @@ class HttpUtilsSpec extends Specification { setup: wireMockServer.start() + def httpClient = HttpUtils.createHttpClient(3) configureFor("localhost", 12346) @@ -146,18 +164,124 @@ class HttpUtilsSpec extends Specification { when: def result = HttpUtils.getJson( "http://localhost:12346/econnreset", - new BasicAuthorizationHandler("admin", "secret"), 2) + httpClient, + new BasicAuthorizationHandler("admin", "secret")) + + then: + result.toString() == '{"id":"1","email":"homer.simpson@example.org"}' + + cleanup: + httpClient.close() + wireMockServer.stop() + } + + def "should not retry 408 Request Timeout response from server"() { + def wireMockServer = new WireMockServer(12346); + + setup: + wireMockServer.start() + def httpClient = HttpUtils.createHttpClient(3) + + configureFor("localhost", 12346) + + stubFor(get(urlEqualTo("/timeout")).inScenario("timeout") + .whenScenarioStateIs(Scenario.STARTED) + .willReturn( + aResponse().withStatus(408).withBody("Request Timeout") + )) + + when: + HttpUtils.getJson( + "http://localhost:12346/timeout", + httpClient, + new BasicAuthorizationHandler("admin", "secret")) + then: + def e = thrown(RuntimeException) + e.message.contains("Got 408 response") + cleanup: + httpClient.close() + wireMockServer.stop() + } + + def "should retry 500 status code response from server"() { + def wireMockServer = new WireMockServer(12346); + + setup: + wireMockServer.start() + def httpClient = HttpUtils.createHttpClient(3) + + configureFor("localhost", 12346) + + stubFor(get(urlEqualTo("/timeout")).inScenario("timeout") + .whenScenarioStateIs(Scenario.STARTED) + .willReturn(aResponse().withStatus(500).withBody("Server Error")) + .willSetStateTo("next request 1")) + stubFor(get(urlEqualTo("/timeout")).inScenario("timeout") + .whenScenarioStateIs("next request 1") + .willReturn(aResponse().withStatus(500).withBody("Server Error")) + .willSetStateTo("next request 2")) + stubFor(get(urlEqualTo("/timeout")).inScenario("timeout") + .whenScenarioStateIs("next request 2") + .willReturn(aResponse().withStatus(500).withBody("Server Error")) + .willSetStateTo("next request 3")) + stubFor(get(urlEqualTo("/timeout")).inScenario("timeout") + .whenScenarioStateIs("next request 3") + .willReturn( + aResponse().withStatus(200).withBody('{"id":"1","email":"homer.simpson@example.org"}') + )) + when: + def result = HttpUtils.getJson( + "http://localhost:12346/timeout", + httpClient, + new BasicAuthorizationHandler("admin", "secret")) then: result.toString() == '{"id":"1","email":"homer.simpson@example.org"}' + cleanup: + httpClient.close() + wireMockServer.stop() + } + + def "should fail if retry limit is reached"() { + def wireMockServer = new WireMockServer(12346); + + setup: + wireMockServer.start() + def httpClient1 = HttpUtils.createHttpClient(1); + + configureFor("localhost", 12346) + + stubFor(get(urlEqualTo("/timeout")).inScenario("timeout") + .whenScenarioStateIs(Scenario.STARTED) + .willReturn(aResponse().withStatus(500).withBody("Server Error")) + .willSetStateTo("next request 1")) + stubFor(get(urlEqualTo("/timeout")).inScenario("timeout") + .whenScenarioStateIs("next request 1") + .willReturn(aResponse().withStatus(500).withBody("Server Error")) + .willSetStateTo("next request 2")) + stubFor(get(urlEqualTo("/timeout")).inScenario("timeout") + .whenScenarioStateIs("next request 2") + .willReturn( + aResponse().withStatus(200).withBody('{"id":"1","email":"homer.simpson@example.org"}') + )) + when: + HttpUtils.getJson( + "http://localhost:12346/timeout", + httpClient1, + new BasicAuthorizationHandler("admin", "secret")) + then: + def e = thrown(RuntimeException) + e.message.contains("Got 500 response") cleanup: + httpClient1.close() wireMockServer.stop() } def "should send delete successfully"() { setup: + def httpClient = HttpUtils.createHttpClient(3) driver.addExpectation( onRequestTo("/v1/users/1234567") .withMethod(ClientDriverRequest.Method.DELETE) @@ -168,20 +292,29 @@ class HttpUtilsSpec extends Specification { expect: HttpUtils.delete( "http://localhost:12345/v1/users/1234567", - new BasicAuthorizationHandler("admin", "secret"), - 0) + httpClient, + new BasicAuthorizationHandler("admin", "secret")) + cleanup: + httpClient.close() } def "should fail to post json if user info not present in the url"() { + setup: + def httpClient = HttpUtils.createHttpClient(3) when: + def url = "http://localhost:10000/v1/exec/result/55e5eeb460a8e2070000001e" HttpUtils.postJson( - "http://localhost:10000/v1/exec/result/55e5eeb460a8e2070000001e", + url, + httpClient, Json.createObjectBuilder().build(), - new BasicURLAuthorizationHandler()) + new BasicAuthorizationHandler(url)) then: def e = thrown(RuntimeException) e.message.contains 'User info is missing in the given url: http://localhost:10000/v1/exec/result/55e5eeb460a8e2070000001e' + + cleanup: + httpClient.close() } } diff --git a/test.sh b/test.sh index 76f86c7..ee8f4e3 100755 --- a/test.sh +++ b/test.sh @@ -1,13 +1,127 @@ -export TASK="{\"_id\":\"5559edd38968ec0736000003\",\"data\":{\"step_1\":{\"uri\":\"546456456456456\"}},\"recipe\":{\"nodes\":[{\"id\":\"step_1\",\"compId\":\"testcomponent\",\"function\":\"newContacts\"}]}}" -export STEP_ID="step_1" -export AMQP_URI="amqp://guest:guest@127.0.0.1:5672" -export LISTEN_MESSAGES_ON="javasailor:test_exec:step_1:messages" -export PUBLISH_MESSAGES_TO="javasailor_exchange" -export DATA_ROUTING_KEY="javasailor.test_exec.step_1.message" -export ERROR_ROUTING_KEY="javasailor.test_exec.step_1.error" -export SNAPSHOT_ROUTING_KEY="javasailor.test_exec.step_1.snapshot" -export REBOUND_ROUTING_KEY="javasailor.test_exec.step_1.rebound" -export SHAPSHOT_ROUTING_KEY="javasailor.test_exec.step_1.rebound" -export COMPONENT_PATH="../component1" - -java -cp ./build/classes/main:./lib/*:../component1/build/classes/main/:../component1/lib/* io.elastic.sailor.Sailor \ No newline at end of file +#!/bin/bash +# This script builds the Sailor project and the component, then runs the Sailor application with the necessary environment variables set. + +# Default value +JAVA_EXECUTABLE="java" + +# Help message +print_help() { + echo "This script builds the Sailor, places it into the component as ./lib/sailor-jvm.jar," + echo "builds the component, and runs the Sailor application with the necessary environment variables set." + echo + echo "Make sure that the component has the following in its build.gradle file:" + echo "dependencies {" + echo " implementation files(\"./lib/sailor-jvm.jar\")" + echo "}" + echo + echo "Usage: $0 [--java ]" + echo + echo "Arguments:" + echo " (required) Path to the component directory." + echo " --java (optional) Java executable to use (default: java)." + exit 1 +} + +# First, check if --help is anywhere +for arg in "$@"; do + if [[ "$arg" == "--help" || "$arg" == "-h" ]]; then + print_help + fi +done + +# Ensure at least 1 argument (component path) +if [[ $# -lt 1 ]]; then + echo "Error: Missing required argument." + print_help +fi + +# First argument is the required component path +COMPONENT_PATH="$1" +shift + +# Parse optional named arguments +while [[ $# -gt 0 ]]; do + case "$1" in + --java) + if [[ -z "$2" ]]; then + echo "Error: --java requires an argument" + exit 1 + fi + JAVA_EXECUTABLE="$2" + shift 2 + ;; + *) + echo "Unknown argument: $1" + print_help + ;; + esac +done + +"$JAVA_EXECUTABLE" -version || { + echo "Java executable not found or not working. Please check the path to your Java installation." + exit 1 +} +echo + +if [ ! -d "$COMPONENT_PATH" ]; then + echo "Component path $COMPONENT_PATH does not exist. Please check the path." + exit 1 +fi + +echo "Building Sailor..." +./gradlew shadowJar || { + echo "Failed to build Sailor. Please check the build logs." + exit 1 +} + +# Get sailor version +SAILOR_VERSION=$(./gradlew -q printVersion | grep "Sailor JVM version:" | sed -E 's/.*: //') +echo "Sailor version: $SAILOR_VERSION" + +# Get the path to the Sailor JAR Failed +SAILOR_JAR=$(find build/libs -name "sailor-jvm-$SAILOR_VERSION.jar" | head -n 1) +if [ -z "$SAILOR_JAR" ]; then + echo "Sailor JAR not found. Please check the build process." + exit 1 +fi + +# Copy the Sailor JAR to the component Path +cp "$SAILOR_JAR" "$COMPONENT_PATH/lib/sailor-jvm.jar" || { + echo "Failed to copy Sailor JAR to component path. Please check the paths." + exit 1 +} +echo "Sailor JAR copied to component path: $COMPONENT_PATH/lib/sailor-jvm.jar" + +echo "Building the component..." +"$COMPONENT_PATH/gradlew" -p "$COMPONENT_PATH" assemble || { + echo "Failed to build the component. Please check the build logs." + echo + echo "Make sure that component dependencies have \"implementation files(\"./lib/sailor-jvm.jar\")\" in the build.gradle file in order to use local Sailor." + exit 1 +} + +# export LOG_LEVEL="DEBUG" +export ELASTICIO_AMQP_URI="amqp://guest:guest@localhost:5672" +export ELASTICIO_API_URI="https://api-sparrow.elastic.io" +export ELASTICIO_LISTEN_MESSAGES_ON="59d341e9037f7200184a408b:6849ad693a3c2100124ebecb/ordinary:step_1:messages" +export ELASTICIO_PUBLISH_MESSAGES_TO="59d341e9037f7200184a408b_org" +export ELASTICIO_DATA_ROUTING_KEY="59d341e9037f7200184a408b.6849ad693a3c2100124ebecb/ordinary.step_2.message" +export ELASTICIO_ERROR_ROUTING_KEY="59d341e9037f7200184a408b.6849ad693a3c2100124ebecb/ordinary.step_2.error" +export ELASTICIO_REBOUND_ROUTING_KEY="59d341e9037f7200184a408b.6849ad693a3c2100124ebecb/ordinary.step_2.rebound" +export ELASTICIO_SNAPSHOT_ROUTING_KEY="59d341e9037f7200184a408b.6849ad693a3c2100124ebecb/ordinary.step_2.snapshot" +export ELASTICIO_MESSAGE_CRYPTO_PASSWORD="k8HO8UurPfKUNjECNAbvLRjBHWkIWz" +export ELASTICIO_MESSAGE_CRYPTO_IV="1gmpybK4iLRRyyu0" +export ELASTICIO_NO_SELF_PASSTRHOUGH="false" +export ELASTICIO_API_REQUEST_RETRY_ATTEMPTS="5" +export ELASTICIO_API_KEY="c316abf1-5016-4a20-96fa-133a45103bf6" +export ELASTICIO_API_USERNAME="task-6849ad693a3c2100124ebecb" +export ELASTICIO_COMP_ID="68494af0c53ea500123dbc7c" +export ELASTICIO_CONTAINER_ID="testcomponent_container" +export ELASTICIO_EXEC_ID="testcomponent_exec" +export ELASTICIO_FLOW_ID="6849ad693a3c2100124ebecb" +export ELASTICIO_FUNCTION="getPetsByStatus" +export ELASTICIO_STEP_ID="step_1" +export ELASTICIO_USER_ID="testcomponent_user" +export ELASTICIO_WORKSPACE_ID="testcomponent_workspace" + +"$JAVA_EXECUTABLE" -cp "$COMPONENT_PATH/lib/*:$COMPONENT_PATH/:$COMPONENT_PATH/build/classes/main" io.elastic.sailor.Sailor | bunyan