Friday, August 28, 2020

Encoding as Base64 in Kotlin

 I share three examples Encoding as Base64 in Kotlin.


Example 1: from path.

import org.apache.commons.codec.binary.Base64
import org.apache.commons.io.FileUtils
import java.io.File
import java.nio.charset.StandardCharsets

fun main(args: Array<String>) {
val file = File("PATH_YOUR_FILE")
val encoded: ByteArray = Base64.encodeBase64(FileUtils.readFileToByteArray(file))
println("*****************************start ****************")
println(String(encoded, StandardCharsets.US_ASCII))
println("*****************************end *****************")

}

Example 2:  from String

import org.apache.commons.codec.binary.Base64
import java.nio.charset.StandardCharsets

fun main(args: Array<String>) {
val encoded = Base64.encodeBase64("Test".toByteArray())
println("*****************************start ****************")
println(String(encoded, StandardCharsets.US_ASCII))
println("*****************************end *****************")
}

Example 3: from WS -> file: MultipartFile

import org.apache.commons.codec.binary.Base64
import org.springframework.web.multipart.MultipartFile
import java.nio.charset.StandardCharsets

fun encodeBase64(file: MultipartFile) {
val encoded: ByteArray = Base64.encodeBase64(file.bytes)
println("*****************************start ****************")
println(String(encoded, StandardCharsets.US_ASCII))
println("*****************************end *****************")
}


Wednesday, August 5, 2020

Start, enable, stop and disable firewalld on Centos


Start firewalld  service run nex commands:

systemctl unmask --now firewalld

systemctl enable firewalld

systemctl start firewalld

Stop firewalld  service run nex commands:

systemctl stop firewalld

sudo systemctl disable firewalld

sudo systemctl mask --now firewalld









References:
https://linuxize.com/post/how-to-stop-and-disable-firewalld-on-centos-7/
https://bobcares.com/blog/failed-to-start-firewalld-service-unit-is-masked/


Tuesday, August 4, 2020

Determine SSL cert expire date from the cert file (.p12)

1. Use openssl to extract the certificate from the .p12 file to a .pem file
openssl pkcs12 -in certificate.p12 -out certificate.pem -nodes

2.  You can extract the expiration date from the certifcate in the .pem file 
cat certificate.pem | openssl x509 -noout -enddate




References:https://stackoverflow.com/questions/28373771/how-to-determine-ssl-cert-expire-date-from-the-cert-file-itself-p12


Saturday, August 1, 2020

How to detect OS in kotlin

In kotlin also you can use System.getProperty("os.name") for detect type OS.

Check example class test.kt

fun main(args: Array<String>) {

val OS = System.getProperty("os.name").toLowerCase()

when {
OS.indexOf("win") >= 0 -> {
print("Windows")
}
OS.indexOf("mac") >= 0 -> {
print("Mac")
}
(OS.indexOf("nix") >= 0
|| OS.indexOf("nux") >= 0
|| OS.indexOf("aix") > 0) -> {
print("Unix or Linux")
}
OS.indexOf("sunos") >= 0 -> {
print("Solaris")
}
else -> print("Others")
}


}






References:
http://lopica.sourceforge.net/os.html
https://mkyong.com/java/how-to-detect-os-in-java-systemgetpropertyosname

How to execute shell command from Kotlin

In Kotlin, also we can use ProcessBuilder or Runtime.getRuntime().exec to execute external shell command :

ProcessBuilder

This class is used to create operating system processes. Each ProcessBuilder instance manages a collection of process attributes. The start() method creates a new Process instance with those attributes. The start() method can be invoked repeatedly from the same instance to create new subprocesses with identical or related attributes.

fun processBuilder() {
val processBuilder = ProcessBuilder()
// -- Linux --

// Run a shell command
//processBuilder.command("bash", "-c", "ls /home/your_user/")

// Run a shell script
//processBuilder.command("path/to/hello.sh");

// -- Windows --
// Run a command
processBuilder.command("cmd.exe", "/c", "dir C:/Users/your_user");

// Run a bat file
//processBuilder.command("C:/Users/your_user/file.bat");

try {

val process: Process = processBuilder.start()
val reader = BufferedReader(InputStreamReader(process.inputStream))
val line = reader.readText();
println(line)

val exitVal = process.waitFor()
if (exitVal == 0) {
println("Success!")
//exitProcess(0)
} else {
//else...
}
} catch (e: IOException) {
e.printStackTrace()
} catch (e: InterruptedException) {
e.printStackTrace()
}
}

Runtime.getRuntime().exec()

Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method.

fun runtimeExec() {

try {
// -- Linux --

// Run a shell command
//val process = Runtime.getRuntime().exec("ls /home/your_user/")

// Run a shell script
//val process = Runtime.getRuntime().exec("path/to/hello.sh")

// -- Windows --

// Run a command
val process = Runtime.getRuntime().exec("cmd /c dir C:/Users/your_user")

//Run a bat file
//val process = Runtime.getRuntime().exec(
// "cmd /c hello.bat", null, File("C:/Users/your_user/"))

val reader = BufferedReader(
InputStreamReader(process.inputStream))

val line = reader.readText();
println(line)

val exitVal = process.waitFor()
if (exitVal == 0) {
println("Success!")
// exitProcess(0)
} else {
//else...
}

} catch (e: IOException) {
e.printStackTrace()
} catch (e: InterruptedException) {
e.printStackTrace()
}

}

Run & Test


hello.sh
#!/bin/sh
echo Hello World

fun main(args: Array<String>) {
val processBuilder = ProcessBuilder()

// -- Linux --
// Run a shell command
processBuilder.command("bash", "-c", "sh /home/hello.sh")

try {

val process: Process = processBuilder.start()
val reader = BufferedReader(InputStreamReader(process.inputStream))
val line = reader.readText();
println(line)

val exitVal = process.waitFor()
if (exitVal == 0) {
println("Success!")
//exitProcess(0)
} else {
//else...
}
} catch (e: IOException) {
e.printStackTrace()
} catch (e: InterruptedException) {
e.printStackTrace()
}

}

Output






















References:

https://mkyong.com/java/how-to-execute-shell-command-from-java/
https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html

Multiple Data Sources in Spring Boot 3 with Java 21

  In this blog post, we'll explore the configuration and setup for a Spring Boot 3 application with Java 21 that uses multiple data sour...