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

Commit ff991c6

Browse files
committed
Restore Compatibility with OSIAM <= 2.x
The new SCIM error message construct is not backwards compatible with OSIAM <= 2.x. This commit restores the compatibility by trying to first read the new construct and then the old one, if the first read fails. Any further processing is the same as before.
1 parent 419d62c commit ff991c6

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

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

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,33 @@
2323

2424
package org.osiam.client;
2525

26-
import static com.google.common.base.Preconditions.*;
26+
import static com.google.common.base.Preconditions.checkArgument;
27+
import static com.google.common.base.Preconditions.checkNotNull;
2728

28-
import java.io.IOException;
29-
import java.lang.reflect.ParameterizedType;
30-
import java.util.List;
29+
import com.fasterxml.jackson.core.JsonProcessingException;
30+
import com.fasterxml.jackson.core.Version;
31+
import com.fasterxml.jackson.core.type.TypeReference;
32+
import com.fasterxml.jackson.databind.JavaType;
33+
import com.fasterxml.jackson.databind.ObjectMapper;
34+
import com.fasterxml.jackson.databind.module.SimpleModule;
35+
import com.fasterxml.jackson.databind.type.TypeFactory;
36+
import com.google.common.base.Strings;
37+
import org.osiam.client.exception.ConflictException;
38+
import org.osiam.client.exception.ConnectionInitializationException;
39+
import org.osiam.client.exception.ForbiddenException;
40+
import org.osiam.client.exception.NoResultException;
41+
import org.osiam.client.exception.OAuthErrorMessage;
42+
import org.osiam.client.exception.OsiamClientException;
43+
import org.osiam.client.exception.OsiamRequestException;
44+
import org.osiam.client.exception.ScimErrorMessage;
45+
import org.osiam.client.exception.UnauthorizedException;
46+
import org.osiam.client.oauth.AccessToken;
47+
import org.osiam.client.query.Query;
48+
import org.osiam.client.query.QueryBuilder;
49+
import org.osiam.resources.helper.UserDeserializer;
50+
import org.osiam.resources.scim.Resource;
51+
import org.osiam.resources.scim.SCIMSearchResult;
52+
import org.osiam.resources.scim.User;
3153

3254
import javax.ws.rs.ProcessingException;
3355
import javax.ws.rs.client.Entity;
@@ -37,23 +59,10 @@
3759
import javax.ws.rs.core.Response.Status;
3860
import javax.ws.rs.core.Response.Status.Family;
3961
import javax.ws.rs.core.Response.StatusType;
40-
41-
import org.osiam.client.exception.*;
42-
import org.osiam.client.oauth.AccessToken;
43-
import org.osiam.client.query.Query;
44-
import org.osiam.client.query.QueryBuilder;
45-
import org.osiam.resources.helper.UserDeserializer;
46-
import org.osiam.resources.scim.Resource;
47-
import org.osiam.resources.scim.SCIMSearchResult;
48-
import org.osiam.resources.scim.User;
49-
50-
import com.fasterxml.jackson.core.JsonProcessingException;
51-
import com.fasterxml.jackson.core.Version;
52-
import com.fasterxml.jackson.databind.JavaType;
53-
import com.fasterxml.jackson.databind.ObjectMapper;
54-
import com.fasterxml.jackson.databind.module.SimpleModule;
55-
import com.fasterxml.jackson.databind.type.TypeFactory;
56-
import com.google.common.base.Strings;
62+
import java.io.IOException;
63+
import java.lang.reflect.ParameterizedType;
64+
import java.util.List;
65+
import java.util.Map;
5766

5867
/**
5968
* AbstractOsiamService provides all basic methods necessary to manipulate the Entities registered in the given OSIAM
@@ -289,7 +298,10 @@ protected String extractErrorMessageDefault(String content, StatusType status) {
289298

290299
protected String extractErrorMessage(String content, StatusType status) {
291300

292-
String message = getScimErrorMessage(content);
301+
String message = getScimErrorMessageSinceOsiam3(content);
302+
if (message == null) {
303+
message = getScimErrorMessageUpToOsiam2(content);
304+
}
293305
if (message == null) {
294306
message = getOAuthErrorMessage(content);
295307
}
@@ -303,18 +315,27 @@ protected String extractErrorMessage(String content, StatusType status) {
303315
return message;
304316
}
305317

306-
private String getScimErrorMessage(String content) {
318+
private String getScimErrorMessageSinceOsiam3(String content) {
307319
try {
308-
ScimErrorMessage error = new ObjectMapper().readValue(content, ScimErrorMessage.class);
320+
ScimErrorMessage error = mapper.readValue(content, ScimErrorMessage.class);
309321
return error.getDetail();
310322
} catch (ProcessingException | IOException e) {
311323
return null;
312324
}
313325
}
314326

327+
private String getScimErrorMessageUpToOsiam2(String content) {
328+
try {
329+
Map<String, String> error = mapper.readValue(content, new TypeReference<Map<String, String>>() {});
330+
return error.get("description");
331+
} catch (ProcessingException | IOException e) {
332+
return null;
333+
}
334+
}
335+
315336
private String getOAuthErrorMessage(String content) {
316337
try {
317-
OAuthErrorMessage error = new ObjectMapper().readValue(content, OAuthErrorMessage.class);
338+
OAuthErrorMessage error = mapper.readValue(content, OAuthErrorMessage.class);
318339
return error.getDescription();
319340
} catch (ProcessingException | IOException e) {
320341
return null;
@@ -326,6 +347,7 @@ protected static void checkAccessTokenIsNotNull(AccessToken accessToken) {
326347
}
327348

328349
protected static class Builder<T> {
350+
329351
private String endpoint;
330352
private Class<T> type;
331353
private String typeName;

0 commit comments

Comments
 (0)