Skip to content

Instantly share code, notes, and snippets.

intent

produce a docker image with 3 main layers:

  • OS / java - rarely changes, good caching and sharing among apps in a kubernetes cluster
  • dependencies - changes are not too frequent, good caching among app versions
  • app jar - changes with every version, but should be quite compact

how to

in build.gradle.kts:

@xhanin
xhanin / MailhogContainer.kt
Created April 1, 2021 05:29
A simple Kotlin Mailhog Test Container - allow to use mailhog to test sending emails with a real smtp server
package mailhog
/*
dependencies:
testImplementation("org.testcontainers:testcontainers:1.15.2")
testImplementation(platform("org.http4k:http4k-bom:4.3.5.4"))
testImplementation("org.http4k:http4k-core")
testImplementation("org.http4k:http4k-client-apache") {
because("mailhog http api messages access")
public class SafeCachingFactorizer implements Servlet {
private static class FactorResult { BigInteger number; BigInteger[] factors; }
private volatile FactorResult lastResult = null;
public void service(ServletRequest req, ServletResponse resp) {
BigInteger i = extractFromRequest(req);
FactorResult result = this.lastResult;
if (result == null || !i.equals(result.number)) {
result = new FactorResult() { { number = i; factors = factor(i); } };
@xhanin
xhanin / JacksonDeserializerTest.java
Created October 23, 2015 15:42
jackson deserializer unwrapping a data field
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
@xhanin
xhanin / BsonJSR310Module.java
Created June 10, 2015 09:59
RESTX issue 204 workaround
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import de.undercouch.bson4jackson.BsonGenerator;
import de.undercouch.bson4jackson.serializers.BsonSerializer;
@xhanin
xhanin / RestxSpringContextLoaderListener.java
Created February 12, 2015 12:55
restx spring integration
package restx.spring;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContextEvent;
/**
* User: xavierhanin
@xhanin
xhanin / JongoModule.java
Created May 20, 2014 14:00
Secured Jongo
@Module(priority = -100)
public class JongoModule {
JacksonMapper.Builder getJacksonMapperBuilder() {
return new JacksonMapper.Builder()
.registerModule(new BsonJodaTimeModule())
.withView(Views.Private.class)
;
}
@xhanin
xhanin / AppModule.java
Last active August 29, 2015 14:00
RESTX JDBC support with BoneCP for connexion pool and Flyway for migration
// In AppModule class, or could be in a dedicated module.
@Provides
public DBI dbi(@Named("datasource") DataSource ds) {
return new DBI(ds);
}
@xhanin
xhanin / README.md
Created April 7, 2014 19:42
Java Annotation Processing Notes

A few notes on java annotation processing, collected when working on RESTX annotation processor.

Don't register multiple annotation processor on same annotation.

It fails silently.

Disable annotation processing in module defining an annotation processor.

Use -proc:none javac option. In restx build you can use this fragment: classpath:///restx/build/fragments/maven/annotation-processing-disable.xml

@xhanin
xhanin / endpoint-fragment.java
Created April 6, 2014 19:57
RESTX java 8 endpoint with lambda capturing local variable
@GET("/helloConcat")
@PermitAll
public Iterable<String> concatMessages(String q) {
String c = q + "test";
return asList(NAMES)
.stream()
.map((s) -> (s + c))
.collect(Collectors.toList());
}