Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Commit f71a0c3

Browse files
authored
Merge pull request #229 from wallner/filter_resources
Filtering of attributes of the queried resources
2 parents eac92ca + 05c4338 commit f71a0c3

File tree

9 files changed

+239
-116
lines changed

9 files changed

+239
-116
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Please update to OSIAM 3.0 as soon as possible.
88

99
### Features
1010

11+
- It is now possible to filter the attributes of the returned `User` and `Group` resources by supplying the
12+
attributes as arguments to the the appropriate methods.
1113
- Introduce a `getMe()` method to retrieve the currently logged in user
1214
from OSIAM. This is supposed to be the new method to retrieve the logged in
1315
user and also works with OSIAM 2.x.

docs/working-with-groups.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,36 @@ To retrieve a single Group you need her UUID (for example
7676
94bbe688-4b1e-4e4e-80e7-e5ba5c4d6db4):
7777

7878
```java
79-
OsiamConnector oConnector = [Retrieving an OsiamConnector](create-osiam-connector.md)
80-
AccessToken accessToken = [Retrieving an AccessToken](login-and-getting-an-access-token.md#retrieving-an-accesstoken)
8179
Group group = oConnector.getGroup(<GROUP_UUID>, accessToken);
8280
```
8381
(please consider the possible runtimeException which are explained in the
8482
Javadoc)
8583

84+
It is possible to filter the attributes of the returned resource by specifying the attributes to return as parameters
85+
to the method:
86+
87+
```java
88+
Group group = osiamConnector.getGroup(<GROUP_ID>, accessToken, "displayName", "meta.created");
89+
```
8690
## Retrieve all Groups
8791

8892
If you want to retrieve all groups you can call the following method:
8993

9094
```java
91-
OsiamConnector oConnector = [Retrieving an OsiamConnector](create-osiam-connector.md)
92-
AccessToken accessToken = [Retrieving an AccessToken](login-and-getting-an-access-token.md#retrieving-an-accesstoken)
93-
SCIMSearchResult<Group> searchResult = oConnector.getAllGroups(accessToken);
95+
List<Group> searchResult = oConnector.getAllGroups(accessToken);
9496
int numberOfGroups = searchResult.getTotalResults();
9597
for (Group actGroup : searchResult.Resources()) {
9698
//...
9799
}
98100
//...
99101
```
100102

103+
It is possible to filter the attributes of the returned groups by supplying the attributes as parameters to the method:
104+
105+
```java
106+
List<Group> searchResult = oConnector.getAllGroups(accessToken, "displayName", "meta.created");
107+
```
108+
101109
## Search for groups
102110

103111
The [Query](query.md) class helps you
@@ -107,8 +115,6 @@ examples in the page are for users. Please adapt them for groups)
107115
A complete example how you can run a search for a user is described below:
108116

109117
```java
110-
OsiamConnector oConnector = [Retrieving an OsiamConnector](create-osiam-connector.md)
111-
AccessToken accessToken = [Retrieving an AccessToken](login-and-getting-an-access-token.md#retrieving-an-accesstoken)
112118
Query query = [Create an Query](query.md)
113119
SCIMSearchResult<Group> result = oConnector.searchGroups(query, accessToken);
114120
```

docs/working-with-user.md

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,51 +74,58 @@ To retrieve a single user you need his UUID (for example:
7474
94bbe688-4b1e-4e4e-80e7-e5ba5c4d6db4):
7575

7676
```java
77-
OsiamConnector oConnector = [Retrieving an OsiamConnector](create-osiam-connector.md)
78-
AccessToken accessToken = [Retrieving an AccessToken](login-and-getting-an-access-token.md#retrieving-an-accesstoken)
79-
User user = oConnector.getUser(<USER_UUID>, accessToken);
77+
User user = osiamConnector.getUser(<USER_UUID>, accessToken);
8078
```
8179

80+
It is possible to filter the attributes of the returned resource by specifying the attributes to return as parameters
81+
to the method:
82+
83+
```java
84+
User user = osiamConnector.getUser(<USER_ID>, accessToken, "userName", "displayName", "meta.created");
85+
```
86+
8287
(please consider the possible runtimeException which are explained in the
8388
Javadoc)
8489

85-
# Retrieve the current logged in user by his access token
90+
# Retrieve the currently logged in user by their access token
8691

87-
If you are logged in with the client scope and you try to retrieve the current
88-
logged user you will retrieve a ConflictException.
92+
To retrieve the user that the currently held access token belongs to, the `getMe()` method is avaiable:
8993

90-
```sh
91-
OsiamConnector oConnector = [Retrieving an OsiamConnector](create-osiam-connector.md)
92-
AccessToken accessToken = [Retrieving an AccessToken](login-and-getting-an-access-token.md#retrieving-an-accesstoken)
94+
```java
9395
// retrieves the complete currently logged in User.
94-
User user = oConnector.getMe(accessToken);
96+
User user = osiamConnector.getMe(accessToken);
97+
```
98+
It is possible to filter the attributes of the returned user by supplying the wanted attributes as parameters
99+
to the method:
100+
101+
```java
102+
User user = osiamConnector.getUser(<USER_ID>, accessToken, "userName", "displayName", "meta.created");
95103
```
96104

97105
# Retrieve all Users
98106

99107
If you want to retrieve all users you can call the following method:
100108

101109
```java
102-
OsiamConnector oConnector = [Retrieving an OsiamConnector](create-osiam-connector.md)
103-
AccessToken accessToken = [Retrieving an AccessToken](login-and-getting-an-access-token.md#retrieving-an-accesstoken)
104-
SCIMSearchResult<User> searchResult = oConnector.getAllUsers(accessToken);
110+
List<User> users = oConnector.getAllUsers(accessToken);
105111
int numberOfUsers = searchResult.getTotalResults();
106112
for (User actUser : searchResult.Resources()) {
107113
//...
108114
}
109-
//...
115+
```
116+
It is possible to filter the attributes of the returned users by supplying the attributes as parameters to the method:
117+
118+
```java
119+
List<User> users = oConnector.getAllUsers(accessToken, "userName", "meta.created");
110120
```
111121

112122
# Search for User
113123

114124
The [Query](query.md) class helps you
115125
to create an Query based on the needed filter and other attributes.
116126

117-
A complete example how you can run a search for a user is described below:
127+
An example how to run a search for a user is shown below:
118128

119129
```java
120-
OsiamConnector oConnector = [Retrieving an OsiamConnector](create-osiam-connector.md)
121-
AccessToken accessToken = [Retrieving an AccessToken](login-and-getting-an-access-token.md#retrieving-an-accesstoken)
122-
Query query = [Create an Query](query.md)
123130
SCIMSearchResult<User> result = oConnector.searchUsers(query, accessToken);
124131
```

src/main/java/org/osiam/client/AbstractOsiamService.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,15 @@
3030
import com.fasterxml.jackson.databind.node.ArrayNode;
3131
import com.fasterxml.jackson.databind.node.ObjectNode;
3232
import com.fasterxml.jackson.databind.type.TypeFactory;
33+
import com.google.common.base.Joiner;
3334
import com.google.common.base.Strings;
3435
import org.glassfish.jersey.client.ClientProperties;
35-
import org.osiam.client.exception.BadRequestException;
36-
import org.osiam.client.exception.ConflictException;
37-
import org.osiam.client.exception.ConnectionInitializationException;
38-
import org.osiam.client.exception.ForbiddenException;
39-
import org.osiam.client.exception.NoResultException;
40-
import org.osiam.client.exception.OAuthErrorMessage;
41-
import org.osiam.client.exception.OsiamClientException;
42-
import org.osiam.client.exception.OsiamRequestException;
43-
import org.osiam.client.exception.UnauthorizedException;
36+
import org.osiam.client.exception.*;
4437
import org.osiam.client.oauth.AccessToken;
4538
import org.osiam.client.query.Query;
4639
import org.osiam.client.query.QueryBuilder;
4740
import org.osiam.resources.helper.UserDeserializer;
48-
import org.osiam.resources.scim.ErrorResponse;
49-
import org.osiam.resources.scim.Group;
50-
import org.osiam.resources.scim.Resource;
51-
import org.osiam.resources.scim.SCIMSearchResult;
52-
import org.osiam.resources.scim.User;
41+
import org.osiam.resources.scim.*;
5342

5443
import javax.ws.rs.ProcessingException;
5544
import javax.ws.rs.client.Entity;
@@ -60,7 +49,6 @@
6049
import javax.ws.rs.core.Response.Status.Family;
6150
import javax.ws.rs.core.Response.StatusType;
6251
import java.io.IOException;
63-
import java.lang.reflect.ParameterizedType;
6452
import java.util.List;
6553
import java.util.Map;
6654

@@ -104,14 +92,21 @@ static void checkAccessTokenIsNotNull(AccessToken accessToken) {
10492
checkNotNull(accessToken, "The given accessToken must not be null.");
10593
}
10694

107-
T getResource(String id, AccessToken accessToken) {
95+
T getResource(String id, AccessToken accessToken, String... attributes) {
10896
checkArgument(!Strings.isNullOrEmpty(id), "The given id must not be null nor empty.");
10997
checkAccessTokenIsNotNull(accessToken);
11098

11199
StatusType status;
112100
String content;
101+
102+
WebTarget target;
103+
if (attributes == null || attributes.length == 0) {
104+
target = targetEndpoint;
105+
} else {
106+
target = targetEndpoint.queryParam("attributes", Joiner.on(",").join(attributes));
107+
}
113108
try {
114-
Response response = targetEndpoint.path(typeName + "s").path(id).request(MediaType.APPLICATION_JSON)
109+
Response response = target.path(typeName + "s").path(id).request(MediaType.APPLICATION_JSON)
115110
.header(AUTHORIZATION, BEARER + accessToken.getToken())
116111
.property(ClientProperties.CONNECT_TIMEOUT, connectTimeout)
117112
.property(ClientProperties.READ_TIMEOUT, readTimeout)
@@ -128,8 +123,12 @@ T getResource(String id, AccessToken accessToken) {
128123
return mapToResource(content);
129124
}
130125

131-
List<T> getAllResources(AccessToken accessToken) {
132-
Query query = new QueryBuilder().count(Integer.MAX_VALUE).build();
126+
List<T> getAllResources(AccessToken accessToken, String... attributes) {
127+
QueryBuilder qBuilder = new QueryBuilder().count(Integer.MAX_VALUE);
128+
if (attributes != null && attributes.length > 0) {
129+
qBuilder.attributes(Joiner.on(',').join(attributes));
130+
}
131+
Query query = qBuilder.build();
133132
return searchResources(query, accessToken).getResources();
134133
}
135134

0 commit comments

Comments
 (0)