Skip to content

Instantly share code, notes, and snippets.

View jvorhauer's full-sized avatar
🏠
Working from home

Jurjen Vorhauer jvorhauer

🏠
Working from home
View GitHub Profile
@jvorhauer
jvorhauer / GitCommitEmoji.md
Created August 10, 2022 11:30 — forked from parmentf/GitCommitEmoji.md
Git Commit message Emoji
@jvorhauer
jvorhauer / to-hex-string.java
Last active November 4, 2021 12:59
[convert bytes to hex string] Convert any byte array to a hex string #java
String bytesToHex(byte[] bytes) {
char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
lazy val foo = project.in(file("foo"))
.settings(
scalaVersion := "3.0.0",
libraryDependencies +=
("com.typesafe.akka" %% "akka-actor-typed" % "2.6.15")
.cross(CrossVersion.for3Use2_13)
)
@jvorhauer
jvorhauer / config-webclient-ssl-basicauth.java
Created September 18, 2020 06:41
WebClient with SSL and basic authentication
// https://www.viralpatel.net/basic-authentication-spring-webclient/
@Bean
public WebClient webClient() {
var httpClient = trustStore == null || trustStore.isEmpty() ?
reactor.netty.http.client.HttpClient.create().compress(true) :
reactor.netty.http.client.HttpClient.create().compress(true)
.secure(sslCtxSpec -> sslCtxSpec.sslContext(createSslContext()));
return WebClient.builder()
.baseUrl(baseUrl)
.defaultHeaders(header -> header.setBasicAuth(username, password))
@jvorhauer
jvorhauer / poll-webclient-repeat.java
Last active November 4, 2021 13:00
[mono-with-retry] Repeat a GET if the Mono is empty #java
// https://stackoverflow.com/questions/55923326/conditional-repeat-or-retry-on-mono-with-webclient-from-spring-webflux
private Either<Fault, String> poll(final String id) {
JobResponse jr = webClient
.get()
.uri(baseUrl + "/jobs/" + id + "?f=json")
.retrieve()
.bodyToMono(JobResponse.class)
.filter(JobResponse::done)
.repeatWhenEmpty(
Repeat.onlyIf(ctx -> true).timeout(Duration.ofSeconds(10L))
@jvorhauer
jvorhauer / read-resource.java
Last active September 17, 2020 06:11
Java 11 - Read a Resource
// No dependencies, NIO 2.0
public String read(final String resource) {
var in = getClass().getClassLoader().getResource(resource);
return new String(Files.readAllBytes(Paths.get(in.toURI())));
}
// IOUtils + Vavr, Apache Commons
public String read() {
return Option.of(getClass().getClassLoader().getResourceAsStream(fn))
.flatMap(in -> Try.of(() -> IOUtils.toString(in, StandardCharsets.UTF_8)).toOption())

Keybase proof

I hereby claim:

  • I am jvorhauer on github.
  • I am juvor (https://keybase.io/juvor) on keybase.
  • I have a public key ASBlcZ3ewVFYqU-MARd3_MqJluuHL2T-DAo0wiPAFj9zpgo

To claim this, I am signing this object:

@jvorhauer
jvorhauer / gist-notes.md
Last active October 15, 2021 13:49
Gists as Notes

Gist Notes

Use Github gists to store notes.

API

Use the available API as provided by Github.

Clients

@jvorhauer
jvorhauer / auth.scala
Created December 7, 2016 07:26 — forked from vkostyukov/auth.scala
Simple Auth with Finch
scala> case class Auth(u: String)
defined class Auth
scala> val auth = headerOption("Auth").withDefault("anon").as[Auth].mapOutput { a =>
| if (a.u == "foo") Ok(a) else Unauthorized(new Exception("wrong credentials"))
| }
scala> val e = get("foo" :: auth).map(_.u)
e: io.finch.Endpoint[String] = GET /foo/header(Auth)
@jvorhauer
jvorhauer / MyScalatraServlet.scala
Created October 24, 2016 11:05 — forked from tjdett/MyScalatraServlet.scala
Scala + Apache POI to read uploaded Excel doc
package com.example.app
import org.scalatra._
import servlet.{SizeConstraintExceededException, FileUploadSupport}
import scalate.ScalateSupport
import java.io.ByteArrayInputStream
import org.apache.poi.ss.usermodel.WorkbookFactory
import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.usermodel.DataFormatter
import org.apache.poi.ss.usermodel.DateUtil