Skip to content

Instantly share code, notes, and snippets.

Avatar

Joris Kuipers jkuipers

  • Trifork
  • The Netherlands, near Amsterdam
View GitHub Profile
@jkuipers
jkuipers / HttpClientProperties.java
Created May 29, 2018
Type-safe configuration class for the HttpClientAutoConfiguration sample
View HttpClientProperties.java
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,
@jkuipers
jkuipers / HttpClientAutoConfiguration.java
Last active Jun 28, 2022
Spring Boot auto-configuration example for an Apache Components HTTP client and its usage in all RestTemplates created by the RestTemplateBuilder, plus trace logging support
View HttpClientAutoConfiguration.java
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateCustomizer;
@jkuipers
jkuipers / ResolvedErrorAttributes.java
Created Apr 8, 2018
Example of custom Spring Boot ErrorAttributes which resolves ObjectErrors (incl. FieldErrors) rather than serializing them fully
View ResolvedErrorAttributes.java
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 / LoggingClientHttpRequestInterceptor.java
Last active Aug 7, 2022
RestTemplate-interceptor that logs outgoing requests and resulting responses
View LoggingClientHttpRequestInterceptor.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.StreamUtils;
@jkuipers
jkuipers / CommonsHttpClientSniSupport.java
Created Sep 7, 2015
SNI (Server Name Identification) support for old Apache Commons HTTP Client (for use with Spring Security SAML)
View CommonsHttpClientSniSupport.java
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;
@jkuipers
jkuipers / mvc-config-fragment.xml
Created Sep 4, 2013
Spring XML configuration example of setting up a Spring Data DomainClassConverter
View mvc-config-fragment.xml
<mvc:annotation-driven conversion-service="conversionService" ignoreDefaultModelOnRedirect="true">
<mvc:argument-resolvers>
<bean class="org.springframework.data.web.PageableArgumentResolver"/>
</mvc:argument-resolvers>
</mvc:annotation-driven>
<bean class="org.springframework.data.repository.support.DomainClassConverter">
<constructor-arg ref="conversionService"/>
</bean>
@jkuipers
jkuipers / formMacros.ftl
Last active Jun 21, 2022
FreeMarker macros for rendering Bootstrap horizontal form inputs in Spring-MVC applications
View formMacros.ftl
<#ftl strip_whitespace=true>
<#import "spring.ftl" as spring />
<#-- This file contains form-related macros for use in the other Freemarker template files.
The generated HTML is intended for use with Twitter Bootstrap based forms. -->
<#--
* radioButtons
*
* @param path the name of the field to bind to
@jkuipers
jkuipers / SmartCookieLocaleResolver.java
Created Aug 30, 2012
Spring LocaleResolver that uses cookies but falls back to the HTTP Session when cookies are disabled
View SmartCookieLocaleResolver.java
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
/**
* {@link LocaleResolver} implementation based on Spring's built-in
@jkuipers
jkuipers / RestTemplateConfigSample.java
Created May 21, 2012
RestTemplate with basic or digest authentication configured
View RestTemplateConfigSample.java
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
DefaultHttpClient client = (DefaultHttpClient) factory.getHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(hostName, portNumber),
new UsernamePasswordCredentials(username, password));
RestTemplate restTemplate = new RestTemplate(factory);
@jkuipers
jkuipers / GPathResultMethodArgumentResolver.java
Created Apr 16, 2012
Spring-WS MethodArgumentResolver for Groovy-based endpoints supporting GPathResult params
View GPathResultMethodArgumentResolver.java
import groovy.util.XmlSlurper;
import groovy.util.slurpersupport.GPathResult;
import java.util.List;
import java.util.Properties;
import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXResult;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;