rest template work for digest auth and early work on gson

This commit is contained in:
eugenp 2014-08-19 17:34:40 +03:00
parent 1222077785
commit 7bee5f637e
8 changed files with 264 additions and 139 deletions

View File

@ -1,150 +1,157 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId>
<artifactId>jackson</artifactId>
<version>0.1-SNAPSHOT</version>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId>
<artifactId>jackson</artifactId>
<version>0.1-SNAPSHOT</version>
<name>jackson</name>
<name>jackson</name>
<dependencies>
<dependencies>
<!-- utils -->
<!-- utils -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>16.0.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>17.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<!-- web -->
<!-- web -->
<!-- marshalling -->
<!-- marshalling -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<build>
<finalName>jackson</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</dependencies>
<plugins>
<build>
<finalName>jackson</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
</build>
</plugins>
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
</build>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<!-- marshalling -->
<jackson.version>2.4.1</jackson.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
<logback.version>1.1.2</logback.version>
<!-- marshalling -->
<jackson.version>2.4.1</jackson.version>
<!-- various -->
<hibernate-validator.version>5.1.2.Final</hibernate-validator.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
<logback.version>1.1.2</logback.version>
<!-- util -->
<guava.version>17.0</guava.version>
<commons-lang3.version>3.3.2</commons-lang3.version>
<!-- various -->
<hibernate-validator.version>5.1.2.Final</hibernate-validator.version>
<!-- testing -->
<org.hamcrest.version>1.3</org.hamcrest.version>
<junit.version>4.11</junit.version>
<mockito.version>1.9.5</mockito.version>
<!-- util -->
<guava.version>17.0</guava.version>
<commons-lang3.version>3.3.2</commons-lang3.version>
<httpcore.version>4.3.2</httpcore.version>
<httpclient.version>4.3.5</httpclient.version>
<!-- testing -->
<org.hamcrest.version>1.3</org.hamcrest.version>
<junit.version>4.11</junit.version>
<mockito.version>1.9.5</mockito.version>
<rest-assured.version>2.3.2</rest-assured.version>
<httpcore.version>4.3.2</httpcore.version>
<httpclient.version>4.3.5</httpclient.version>
<!-- maven plugins -->
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<rest-assured.version>2.3.2</rest-assured.version>
</properties>
<!-- maven plugins -->
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
</properties>
</project>

View File

@ -0,0 +1,10 @@
package org.baeldung.gson;
public class GenericSourceClass {
int intField;
public GenericSourceClass(final int i) {
intField = i;
}
}

View File

@ -0,0 +1,32 @@
package org.baeldung.gson;
import org.junit.Before;
import org.junit.Test;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class GsonDeserializationUnitTest {
private Gson gson;
@Before
public final void before() {
gson = new Gson();
}
// tests
@Test
public void givenUsingGson_whenDeserializingGeneric_thenCorrect() {
final java.lang.reflect.Type genericSourceClassType = new TypeToken<GenericSourceClass>() {
}.getType();
final GenericSourceClass sourceObject = new GenericSourceClass(1);
final String serializedSourceObject = gson.toJson(sourceObject, genericSourceClassType);
final GenericSourceClass targetObject = gson.fromJson(serializedSourceObject, genericSourceClassType);
System.out.println(targetObject);
}
}

View File

@ -89,7 +89,7 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.2</version>
<version>${jackson.version}</version>
</dependency>
<!-- http -->
@ -227,13 +227,13 @@
<debuglevel>source</debuglevel>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
@ -290,6 +290,9 @@
<org.slf4j.version>1.7.7</org.slf4j.version>
<logback.version>1.1.2</logback.version> <!-- do not upgrade - see http://jira.qos.ch/browse/LOGBACK-851 -->
<!-- marshalling -->
<jackson.version>2.4.2</jackson.version>
<!-- various -->
<hibernate-validator.version>5.1.2.Final</hibernate-validator.version>
@ -308,7 +311,7 @@
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -4,6 +4,7 @@ import java.net.URI;
import org.apache.http.HttpHost;
import org.apache.http.client.AuthCache;
import org.apache.http.client.HttpClient;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.DigestScheme;
import org.apache.http.impl.client.BasicAuthCache;
@ -15,8 +16,8 @@ import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
public class HttpComponentsClientHttpRequestFactoryDigestAuth extends HttpComponentsClientHttpRequestFactory {
HttpHost host;
public HttpComponentsClientHttpRequestFactoryDigestAuth(final HttpHost host) {
super();
public HttpComponentsClientHttpRequestFactoryDigestAuth(final HttpHost host, final HttpClient httpClient) {
super(httpClient);
this.host = host;
}

View File

@ -1,9 +1,13 @@
package org.baeldung.client.spring;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.baeldung.client.HttpComponentsClientHttpRequestFactoryDigestAuth;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -12,6 +16,8 @@ import org.springframework.web.client.RestTemplate;
@Configuration
public class ClientConfig {
private static final String DEFAULT_USER = "user1";
private static final String DEFAULT_PASS = "user1Pass";
public ClientConfig() {
super();
@ -22,7 +28,9 @@ public class ClientConfig {
@Bean
public RestTemplate restTemplate() {
final HttpHost host = new HttpHost("localhost", 8080, "http");
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host);
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider()).useSystemProperties().build();
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host, client);
final RestTemplate restTemplate = new RestTemplate(requestFactory);
final int timeout = 5;
@ -43,9 +51,16 @@ public class ClientConfig {
// httpClient.getParams().setParameter("http.protocol.head-body-timeout", timeout * 1000);
// - note: timeout via the API
final HttpParams httpParams = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout
HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout
// final HttpParams httpParams = httpClient.getParams();
// HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout
// HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout
}
private final CredentialsProvider provider() {
final CredentialsProvider provider = new BasicCredentialsProvider();
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS);
provider.setCredentials(AuthScope.ANY, credentials);
return provider;
}
}

View File

@ -0,0 +1,64 @@
package org.baeldung.client;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.baeldung.web.dto.Foo;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
public class ClientNoSpringLiveTest {
private static final String DEFAULT_USER = "user1";
private static final String DEFAULT_PASS = "user1Pass";
// tests - no Spring
@Test
public final void givenUsingCustomHttpRequestFactory_whenSecuredRestApiIsConsumed_then200OK() {
final HttpHost host = new HttpHost("localhost", 8080, "http");
final CredentialsProvider credentialsProvider = provider();
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).useSystemProperties().build();
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host, client);
final RestTemplate restTemplate = new RestTemplate(requestFactory);
// credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass"));
final String uri = "http://localhost:8080/spring-security-rest-digest-auth/api/foos/1";
final ResponseEntity<Foo> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class);
System.out.println(responseEntity.getStatusCode());
}
@Test
public final void givenUsingStandardRequestFactory_whenSecuredRestApiIsConsumed_then200OK() {
final CredentialsProvider credentialsProvider = provider();
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).useSystemProperties().build();
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(client);
final RestTemplate restTemplate = new RestTemplate(requestFactory);
// credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass"));
final String uri = "http://localhost:8080/spring-security-rest-digest-auth/api/foos/1";
final ResponseEntity<Foo> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class);
System.out.println(responseEntity.getStatusCode());
}
// UTIL
private final CredentialsProvider provider() {
final CredentialsProvider provider = new BasicCredentialsProvider();
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS);
provider.setCredentials(AuthScope.ANY, credentials);
return provider;
}
}

View File

@ -1,8 +1,5 @@
package org.baeldung.client;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.DefaultHttpClient;
import org.baeldung.client.spring.ClientConfig;
import org.baeldung.web.dto.Foo;
import org.junit.Test;
@ -10,7 +7,6 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@ -18,21 +14,18 @@ import org.springframework.web.client.RestTemplate;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ClientConfig.class }, loader = AnnotationConfigContextLoader.class)
public class ClientLiveTest {
public class ClientWithSpringLiveTest {
@Autowired
private RestTemplate restTemplate;
// tests
// tests - no Spring
@Test
public final void whenSecuredRestApiIsConsumed_then200OK() {
final HttpComponentsClientHttpRequestFactory requestFactory = (HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory();
final DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient();
httpClient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass"));
final String uri = "http://localhost:8080/spring-security-rest-digest-auth/api/foos/1";
final ResponseEntity<Foo> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class);
System.out.println(responseEntity.getStatusCode());
}