Skip to content

Commit 48ad2d4

Browse files
author
eugenp
committed
naming cleanup, security work for apis, template improvements, etc
1 parent 2d42832 commit 48ad2d4

61 files changed

Lines changed: 517 additions & 523 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.

‎api/src/main/java/org/rest/sec/client/template/PrivilegeClientRestTemplate.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public final Pair<String, String> getDefaultCredentials() {
3737

3838
@Override
3939
protected void beforeReadOperation() {
40-
givenAuthenticated();
4140
super.beforeReadOperation();
4241
}
4342

‎api/src/main/java/org/rest/sec/client/template/UserClientRestTemplate.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public final Pair<String, String> getDefaultCredentials() {
3737

3838
@Override
3939
protected void beforeReadOperation() {
40-
givenAuthenticated();
4140
super.beforeReadOperation();
4241
}
4342

‎common/src/main/java/org/apache/commons/lang3/tuple/ImmutableTriple.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,3 @@ public R getRight() {
8585
return right;
8686
}
8787
}
88-

‎common/src/main/java/org/rest/common/client/marshall/IMarshaller.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
public interface IMarshaller {
66

7-
<T> String encode(final T resource);
7+
<T> String encode(final T entity);
88

9-
<T> T decode(final String resourceAsString, final Class<T> clazz);
9+
<T> T decode(final String entityAsString, final Class<T> clazz);
1010

11-
<T> List<T> decodeList(final String resourcesAsString, final Class<T> clazz);
11+
<T> List<T> decodeList(final String entitiesAsString, final Class<T> clazz);
1212

1313
String getMime();
1414

‎common/src/main/java/org/rest/common/client/security/ClientAuthenticator.java‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.apache.http.auth.UsernamePasswordCredentials;
44
import org.apache.http.impl.client.DefaultHttpClient;
55
import org.rest.common.security.PreemptiveAuthHttpRequestFactory;
6-
import org.rest.common.spring.CommonSpringProfileUtil;
6+
import org.rest.common.spring.util.CommonSpringProfileUtil;
77
import org.springframework.context.annotation.Profile;
88
import org.springframework.stereotype.Component;
99
import org.springframework.web.client.RestTemplate;
@@ -21,15 +21,7 @@ public ClientAuthenticator() {
2121
// API
2222

2323
public final void givenAuthenticated(final RestTemplate restTemplate, final String username, final String password) {
24-
if (isBasicAuth()) {
25-
basicAuth(restTemplate, username, password);
26-
} else {
27-
digestAuth(username, password);
28-
}
29-
}
30-
31-
protected final boolean isBasicAuth() {
32-
return true;
24+
basicAuth(restTemplate, username, password);
3325
}
3426

3527
final void basicAuth(final RestTemplate restTemplate, final String username, final String password) {
@@ -40,9 +32,4 @@ final void basicAuth(final RestTemplate restTemplate, final String username, fin
4032
((DefaultHttpClient) requestFactory.getHttpClient()).getCredentialsProvider().setCredentials(requestFactory.getAuthScope(), new UsernamePasswordCredentials(username, password));
4133
}
4234

43-
@SuppressWarnings("unused")
44-
protected final void digestAuth(final String username, final String password) {
45-
throw new UnsupportedOperationException();
46-
}
47-
4835
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.rest.common.client.template;
2+
3+
import org.apache.commons.lang3.tuple.Pair;
4+
import org.rest.common.client.marshall.IMarshaller;
5+
import org.rest.common.client.web.HeaderUtil;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.http.HttpHeaders;
8+
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> getReadDefaultCredentials() {
29+
return getDefaultCredentials();
30+
}
31+
32+
protected Pair<String, String> getWriteDefaultCredentials() {
33+
return getDefaultCredentials();
34+
}
35+
36+
/**
37+
* - 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
38+
*/
39+
protected void beforeReadOperation() {
40+
//
41+
}
42+
43+
/**
44+
* - note: hook to be able to customize the find headers if needed
45+
*/
46+
protected HttpHeaders findHeaders() {
47+
return HeaderUtil.createAcceptHeaders(marshaller);
48+
}
49+
50+
protected HttpHeaders findHeadersWithAuth() {
51+
final Pair<String, String> defaultCredentials = getReadDefaultCredentials();
52+
return findHeadersWithAuth(defaultCredentials.getLeft(), defaultCredentials.getRight());
53+
}
54+
55+
protected HttpHeaders findHeadersWithAuth(final Pair<String, String> credentials) {
56+
if (credentials == null) {
57+
return findHeadersWithAuth(null, null);
58+
}
59+
return findHeadersWithAuth(credentials.getLeft(), credentials.getRight());
60+
}
61+
62+
/**
63+
* - note: hook to be able to customize the find headers if needed
64+
*/
65+
protected HttpHeaders findHeadersWithAuth(final String username, final String password) {
66+
if (username == null || password == null) {
67+
Preconditions.checkState(username == null && password == null);
68+
final Pair<String, String> defaultCredentials = getReadDefaultCredentials();
69+
return HeaderUtil.createAcceptAndBasicAuthHeaders(marshaller, defaultCredentials.getLeft(), defaultCredentials.getRight());
70+
}
71+
return HeaderUtil.createAcceptAndBasicAuthHeaders(marshaller, username, password);
72+
}
73+
74+
// write
75+
76+
protected HttpHeaders writeHeadersWithAuth() {
77+
final Pair<String, String> defaultCredentials = getWriteDefaultCredentials();
78+
return writeHeadersWithAuth(defaultCredentials.getLeft(), defaultCredentials.getRight());
79+
}
80+
81+
protected HttpHeaders writeHeadersWithAuth(final Pair<String, String> credentials) {
82+
if (credentials == null) {
83+
return writeHeadersWithAuth(null, null);
84+
}
85+
return writeHeadersWithAuth(credentials.getLeft(), credentials.getRight());
86+
}
87+
88+
private HttpHeaders writeHeadersWithAuth(final String username, final String password) {
89+
if (username == null || password == null) {
90+
Preconditions.checkState(username == null && password == null);
91+
final Pair<String, String> defaultCredentials = getWriteDefaultCredentials();
92+
return HeaderUtil.createContentTypeAndBasicAuthHeaders(marshaller, defaultCredentials.getLeft(), defaultCredentials.getRight());
93+
}
94+
return HeaderUtil.createContentTypeAndBasicAuthHeaders(marshaller, username, password);
95+
}
96+
97+
}
Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package org.rest.common.client.template;
22

33
import org.apache.commons.lang3.tuple.Pair;
4-
import org.rest.common.client.web.HeaderUtil;
54
import org.rest.common.persistence.model.IEntity;
65
import org.rest.common.web.WebConstants;
76
import org.springframework.http.HttpEntity;
8-
import org.springframework.http.HttpHeaders;
97
import org.springframework.http.HttpMethod;
108
import org.springframework.http.ResponseEntity;
119

@@ -19,6 +17,11 @@ public AbstractClientRestTemplate(final Class<T> clazzToSet) {
1917

2018
// create
2119

20+
@Override
21+
public final T create(final T resource) {
22+
return create(resource, null);
23+
}
24+
2225
@Override
2326
public final T create(final T resource, final Pair<String, String> credentials) {
2427
final String locationOfCreatedResource = createAsUri(resource, credentials);
@@ -27,20 +30,13 @@ public final T create(final T resource, final Pair<String, String> credentials)
2730
}
2831

2932
@Override
30-
public final T create(final T resource) {
31-
final String locationOfCreatedResource = createAsUri(resource, null);
32-
33-
return findOneByUri(locationOfCreatedResource, null);
33+
public final String createAsUri(final T resource) {
34+
return createAsUri(resource, null);
3435
}
3536

3637
@Override
3738
public final String createAsUri(final T resource, final Pair<String, String> credentials) {
38-
if (credentials != null) {
39-
auth.givenAuthenticated(restTemplate, credentials.getLeft(), credentials.getRight());
40-
} else {
41-
givenAuthenticated();
42-
}
43-
final ResponseEntity<Void> responseEntity = restTemplate.exchange(getUri(), HttpMethod.POST, new HttpEntity<T>(resource, writeHeaders()), Void.class);
39+
final ResponseEntity<Void> responseEntity = restTemplate.exchange(getUri(), HttpMethod.POST, new HttpEntity<T>(resource, writeHeadersWithAuth(credentials)), Void.class);
4440

4541
final String locationOfCreatedResource = responseEntity.getHeaders().getLocation().toString();
4642
Preconditions.checkNotNull(locationOfCreatedResource);
@@ -51,18 +47,20 @@ public final String createAsUri(final T resource, final Pair<String, String> cre
5147
// update
5248

5349
@Override
54-
public final void update(final T resource) {
55-
givenAuthenticated();
56-
final ResponseEntity<T> responseEntity = restTemplate.exchange(getUri() + "/" + resource.getId(), HttpMethod.PUT, new HttpEntity<T>(resource, writeHeaders()), clazz);
50+
public void update(final T resource) {
51+
updateInternal(resource, getWriteDefaultCredentials());
52+
}
53+
54+
protected final void updateInternal(final T resource, final Pair<String, String> credentials) {
55+
final ResponseEntity<T> responseEntity = restTemplate.exchange(getUri() + "/" + resource.getId(), HttpMethod.PUT, new HttpEntity<T>(resource, writeHeadersWithAuth(credentials)), clazz);
5756
Preconditions.checkState(responseEntity.getStatusCode().value() == 200);
5857
}
5958

6059
// delete
6160

6261
@Override
6362
public final void delete(final long id) {
64-
// final ResponseEntity<Object> deleteResourceResponse = restTemplate.exchange(getUri() + WebConstants.PATH_SEP + id, HttpMethod.DELETE, new HttpEntity<T>(writeHeaders()), null);
65-
final ResponseEntity<Void> deleteResourceResponse = restTemplate.exchange(getUri() + WebConstants.PATH_SEP + id, HttpMethod.DELETE, new HttpEntity<Void>(writeHeaders()), Void.class);
63+
final ResponseEntity<Void> deleteResourceResponse = restTemplate.exchange(getUri() + WebConstants.PATH_SEP + id, HttpMethod.DELETE, new HttpEntity<Void>(writeHeadersWithAuth()), Void.class);
6664

6765
Preconditions.checkState(deleteResourceResponse.getStatusCode().value() == 204);
6866
}
@@ -74,9 +72,6 @@ public final void deleteAll() {
7472

7573
// template method
7674

77-
@Override
78-
public abstract Pair<String, String> getDefaultCredentials();
79-
8075
/**
8176
* - 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
8277
*/
@@ -85,19 +80,4 @@ protected void beforeReadOperation() {
8580
//
8681
}
8782

88-
/**
89-
* - note: hook to be able to customize the find headers if needed
90-
*/
91-
@Override
92-
protected HttpHeaders findHeaders() {
93-
return HeaderUtil.createAcceptHeaders(marshaller);
94-
}
95-
96-
/**
97-
* - note: hook to be able to customize the write headers if needed
98-
*/
99-
protected HttpHeaders writeHeaders() {
100-
return HeaderUtil.createContentTypeHeaders(marshaller);
101-
}
102-
10383
}

‎common/src/main/java/org/rest/common/client/template/AbstractNamedClientRestTemplate.java‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static org.rest.common.search.ClientOperation.EQ;
44

55
import org.apache.commons.lang3.tuple.ImmutableTriple;
6-
import org.apache.commons.lang3.tuple.Pair;
76
import org.rest.common.persistence.model.INameableEntity;
87
import org.rest.common.search.ClientOperation;
98
import org.rest.common.util.SearchField;
@@ -23,14 +22,4 @@ public T findByName(final String name) {
2322
return searchOne(new ImmutableTriple<String, ClientOperation, String>(SearchField.name.name(), EQ, name));
2423
}
2524

26-
// util
27-
28-
protected void auth(final Pair<String, String> credentials) {
29-
if (credentials != null) {
30-
auth.givenAuthenticated(restTemplate, credentials.getLeft(), credentials.getRight());
31-
} else {
32-
givenAuthenticated();
33-
}
34-
}
35-
3625
}

0 commit comments

Comments
 (0)