Skip to content

Instantly share code, notes, and snippets.

View jkuipers's full-sized avatar

Joris Kuipers jkuipers

  • Trifork
  • The Netherlands, near Amsterdam
View GitHub Profile
@jkuipers
jkuipers / ResolvedErrorAttributes.java
Created April 8, 2018 13:42
Example of custom Spring Boot ErrorAttributes which resolves ObjectErrors (incl. FieldErrors) rather than serializing them fully
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.context.MessageSource;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.context.request.WebRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@jkuipers
jkuipers / spring-boot-snippet.gradle
Created June 23, 2020 21:18
Sample of how to configure Spring Boot's layered jar support for multi-module build that includes your own libs
subprojects { subproject ->
// ...
plugins.withId('org.springframework.boot') {
springBoot {
bootJar {
layered {
// application follows Boot's defaults
application {
intoLayer("spring-boot-loader") {
@jkuipers
jkuipers / HttpConnectionPoolHealthIndicator.java
Last active June 16, 2020 17:43
Ensures unhealthy status in case a PoolingHttpClientConnectionManager is shut down, e.g. as result of OOM
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.Status;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
@jkuipers
jkuipers / HttpClientProperties.java
Created May 29, 2018 20:23
Type-safe configuration class for the HttpClientAutoConfiguration sample
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("http.client")
public class HttpClientProperties {
private int connectionTimeoutInMillis = 1_000;
private int readTimeoutInMillis = 10_000;
/** Used to configure both the default max per route as well as the maximum total connections. */
private int maxConnections = 200;
/**
* Configures a LoggingClientHttpRequestInterceptor and a BufferingClientHttpRequestFactory,

Keybase proof

I hereby claim:

  • I am jkuipers on github.
  • I am jkuipers (https://keybase.io/jkuipers) on keybase.
  • I have a public key ASCj78lyxPW8_W1t735luDbfcfDboljNT1nxLwkDZvK7rQo

To claim this, I am signing this object:

@jkuipers
jkuipers / InstrumentedQueueMessagingTemplate.java
Last active May 2, 2019 12:18
Spring Cloud AWS QueueMessagingTemplate subtype which adds Brave and Micrometer.io integration for sending messages
import brave.Span;
import brave.Tracer;
import brave.Tracing;
import brave.propagation.Propagation;
import brave.propagation.TraceContext;
import com.amazonaws.services.sqs.AmazonSQSAsync;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import org.springframework.cloud.aws.messaging.core.QueueMessageChannel;
import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate;
import com.netflix.hystrix.HystrixThreadPoolKey;
import com.netflix.hystrix.HystrixThreadPoolProperties;
import com.netflix.hystrix.strategy.HystrixPlugins;
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariable;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableLifecycle;
import com.netflix.hystrix.strategy.eventnotifier.HystrixEventNotifier;
import com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook;
import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisher;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_DECORATION_FILTER_ORDER;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PROXY_KEY;
/**
* Adds an API key in the form of a request header to proxied requests.
@jkuipers
jkuipers / CommonsHttpClientSniSupport.java
Created September 7, 2015 20:03
SNI (Server Name Identification) support for old Apache Commons HTTP Client (for use with Spring Security SAML)
import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.apache.commons.httpclient.protocol.ReflectionSocketFactory;
import org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.security.ssl.SSLSocketImpl;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.Security;
import java.util.ArrayList;
import java.util.List;