Skip to content

Commit 8d81d42

Browse files
committed
new upgraded lesson - as reference
1 parent e3c8633 commit 8d81d42

193 files changed

Lines changed: 9911 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<artifactId>common-client-m8-l2-upgraded</artifactId>
4+
<name>common-client</name>
5+
6+
<parent>
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>user-management-parent-m8-l2-upgraded</artifactId>
9+
<version>0.8.1-SNAPSHOT</version>
10+
<relativePath>..</relativePath>
11+
</parent>
12+
13+
<dependencies>
14+
15+
<!-- internal -->
16+
17+
<dependency>
18+
<groupId>com.baeldung</groupId>
19+
<artifactId>common-m8-l2-upgraded</artifactId>
20+
<version>0.8.1-SNAPSHOT</version>
21+
</dependency>
22+
23+
<!-- security -->
24+
25+
<dependency>
26+
<groupId>org.springframework.security</groupId>
27+
<artifactId>spring-security-config</artifactId>
28+
</dependency>
29+
30+
</dependencies>
31+
32+
<build>
33+
<resources>
34+
<resource>
35+
<directory>src/main/resources</directory>
36+
<filtering>true</filtering>
37+
</resource>
38+
</resources>
39+
40+
<plugins>
41+
42+
<!-- this create jar file of code from src/test/java so modules with tests can share code -->
43+
<plugin>
44+
<groupId>org.apache.maven.plugins</groupId>
45+
<artifactId>maven-jar-plugin</artifactId>
46+
<executions>
47+
<execution>
48+
<goals>
49+
<goal>test-jar</goal>
50+
</goals>
51+
</execution>
52+
</executions>
53+
</plugin>
54+
55+
</plugins>
56+
57+
</build>
58+
59+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.client;
2+
3+
import com.baeldung.common.interfaces.IDto;
4+
5+
public interface IDtoOperations<T extends IDto> {
6+
7+
T createNewResource();
8+
9+
void invalidate(final T entity);
10+
11+
void change(final T resource);
12+
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.client.marshall;
2+
3+
import java.util.List;
4+
5+
public interface IMarshaller {
6+
7+
<T> String encode(final T entity);
8+
9+
<T> T decode(final String entityAsString, final Class<T> clazz);
10+
11+
<T> List<T> decodeList(final String entitiesAsString, final Class<T> clazz);
12+
13+
String getMime();
14+
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.client.spring;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ComponentScan({ "com.baeldung.common.client", "com.baeldung.client" })
8+
public class CommonClientConfig {
9+
10+
public CommonClientConfig() {
11+
super();
12+
}
13+
14+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.baeldung.client.template;
2+
3+
import org.apache.commons.lang3.tuple.Pair;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.http.HttpHeaders;
6+
7+
import com.baeldung.client.marshall.IMarshaller;
8+
import com.baeldung.client.util.HeaderUtil;
9+
import com.google.common.base.Preconditions;
10+
11+
public abstract class AbstractBaseClientRestTemplate {
12+
13+
@Autowired
14+
protected IMarshaller marshaller;
15+
16+
public AbstractBaseClientRestTemplate() {
17+
super();
18+
}
19+
20+
// find - one
21+
22+
// util
23+
24+
// template method
25+
26+
protected abstract Pair<String, String> getDefaultCredentials();
27+
28+
protected Pair<String, String> getReadCredentials() {
29+
return getDefaultCredentials();
30+
}
31+
32+
protected Pair<String, String> getReadExtendedCredentials() {
33+
return getReadCredentials();
34+
}
35+
36+
protected Pair<String, String> getWriteCredentials() {
37+
return getDefaultCredentials();
38+
}
39+
40+
/**
41+
* - this is a hook that executes before read operations, in order to allow custom security work to happen for read operations; similar to: AbstractRestTemplate.findRequest
42+
*/
43+
protected void beforeReadOperation() {
44+
//
45+
}
46+
47+
// read
48+
49+
/**
50+
* - note: hook to be able to customize the find headers if needed
51+
*/
52+
protected HttpHeaders readHeaders() {
53+
return HeaderUtil.createAcceptHeaders(marshaller);
54+
}
55+
56+
protected HttpHeaders readHeadersWithAuth() {
57+
final Pair<String, String> defaultCredentials = getReadCredentials();
58+
return readHeadersWithAuth(defaultCredentials.getLeft(), defaultCredentials.getRight());
59+
}
60+
61+
protected HttpHeaders readHeadersWithAuth(final Pair<String, String> credentials) {
62+
if (credentials == null) {
63+
final Pair<String, String> readCredentials = getReadCredentials();
64+
return readHeadersWithAuth(readCredentials.getLeft(), readCredentials.getRight());
65+
}
66+
return readHeadersWithAuth(credentials.getLeft(), credentials.getRight());
67+
}
68+
69+
private final HttpHeaders readHeadersWithAuth(final String username, final String password) {
70+
Preconditions.checkNotNull(username);
71+
Preconditions.checkNotNull(password);
72+
return HeaderUtil.createAcceptAndBasicAuthHeaders(marshaller, username, password);
73+
}
74+
75+
// write
76+
77+
protected HttpHeaders writeHeadersWithAuth() {
78+
final Pair<String, String> defaultCredentials = getWriteCredentials();
79+
return writeHeadersWithAuth(defaultCredentials.getLeft(), defaultCredentials.getRight());
80+
}
81+
82+
protected HttpHeaders writeHeadersWithAuth(final Pair<String, String> credentials) {
83+
if (credentials == null) {
84+
final Pair<String, String> writeCredentials = getWriteCredentials();
85+
return writeHeadersWithAuth(writeCredentials.getLeft(), writeCredentials.getRight());
86+
}
87+
return writeHeadersWithAuth(credentials.getLeft(), credentials.getRight());
88+
}
89+
90+
private final HttpHeaders writeHeadersWithAuth(final String username, final String password) {
91+
Preconditions.checkNotNull(username);
92+
Preconditions.checkNotNull(password);
93+
return HeaderUtil.createContentTypeAndBasicAuthHeaders(marshaller, username, password);
94+
}
95+
96+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.client.template;
2+
3+
import java.util.List;
4+
5+
import com.baeldung.common.interfaces.IDto;
6+
7+
public interface IReadOnlyTemplateWithUri<T extends IDto> {
8+
9+
// find - one
10+
11+
T findOneByUri(final String uri);
12+
13+
// find - all
14+
15+
List<T> findAllByUri(final String uri);
16+
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.client.template;
2+
3+
import com.baeldung.common.interfaces.IDto;
4+
5+
public interface IRestClientWithUri<T extends IDto> extends IReadOnlyTemplateWithUri<T> {
6+
7+
// create
8+
9+
String createAsUri(final T resource);
10+
11+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.client.util;
2+
3+
import org.springframework.http.HttpHeaders;
4+
5+
import com.baeldung.client.marshall.IMarshaller;
6+
import com.baeldung.common.security.SpringSecurityUtil;
7+
8+
public final class HeaderUtil {
9+
10+
private HeaderUtil() {
11+
throw new AssertionError();
12+
}
13+
14+
// API
15+
16+
public static HttpHeaders createContentTypeHeaders(final IMarshaller marshaller) {
17+
final HttpHeaders headers = new HttpHeaders() {
18+
{
19+
set(com.google.common.net.HttpHeaders.CONTENT_TYPE, marshaller.getMime());
20+
}
21+
};
22+
return headers;
23+
}
24+
25+
public static HttpHeaders createAcceptHeaders(final IMarshaller marshaller) {
26+
final HttpHeaders headers = new HttpHeaders() {
27+
{
28+
set(com.google.common.net.HttpHeaders.ACCEPT, marshaller.getMime());
29+
}
30+
};
31+
return headers;
32+
}
33+
34+
public static HttpHeaders createAcceptAndBasicAuthHeaders(final IMarshaller marshaller, final String username, final String password) {
35+
final HttpHeaders headers = HeaderUtil.createAcceptHeaders(marshaller);
36+
final String basicAuthorizationHeader = HeaderUtil.createBasicAuthenticationAuthorizationHeader(username, password);
37+
headers.set(com.google.common.net.HttpHeaders.AUTHORIZATION, basicAuthorizationHeader);
38+
39+
return headers;
40+
}
41+
42+
public static HttpHeaders createContentTypeAndBasicAuthHeaders(final IMarshaller marshaller, final String username, final String password) {
43+
final String basicAuthorizationHeader = HeaderUtil.createBasicAuthenticationAuthorizationHeader(username, password);
44+
return createContentTypeAndBasicAuthHeaders(marshaller, basicAuthorizationHeader);
45+
}
46+
47+
public static HttpHeaders createContentTypeAndBasicAuthHeaders(final IMarshaller marshaller, final String basicAuthorizationHeader) {
48+
final HttpHeaders headers = HeaderUtil.createContentTypeHeaders(marshaller);
49+
headers.set(com.google.common.net.HttpHeaders.AUTHORIZATION, basicAuthorizationHeader);
50+
return headers;
51+
}
52+
53+
public static String createBasicAuthenticationAuthorizationHeader(final String username, final String password) {
54+
return "Basic " + SpringSecurityUtil.encodeAuthorizationKey(username, password);
55+
}
56+
57+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.client.util;
2+
3+
import java.net.MalformedURLException;
4+
import java.net.URI;
5+
import java.net.URISyntaxException;
6+
import java.net.URL;
7+
8+
import org.springframework.web.util.UriTemplate;
9+
10+
public final class UriUtil {
11+
12+
private UriUtil() {
13+
throw new AssertionError();
14+
}
15+
16+
// API
17+
18+
public static URI createSearchUri(final String uriBase, final String paramToExpand) {
19+
URL url = null;
20+
try {
21+
url = new UriTemplate(uriBase).expand(paramToExpand).toURL();
22+
} catch (final MalformedURLException ex) {
23+
throw new IllegalArgumentException(ex);
24+
}
25+
26+
try {
27+
return url.toURI();
28+
} catch (final URISyntaxException ex) {
29+
throw new IllegalArgumentException(ex);
30+
}
31+
}
32+
33+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Overview

0 commit comments

Comments
 (0)