Skip to content

Commit

Permalink
[Refactor] Adopt request builder patterns for SecurityRestApiActions …
Browse files Browse the repository at this point in the history
…for consistency and clarity (opensearch-project#3123)

Main differences are: 
- Using functional approach to handle requests instead of inheritance
which (IMHO) simplify code support and reading
- All checks and verification stay the same, I only changed names. 
- PATCH uses the same validation rules as PUT and DELETE methods 

Signed-off-by: Andrey Pleskach <ples@aiven.io>
  • Loading branch information
willyborankin authored Aug 29, 2023
1 parent 4c095d2 commit 0338cdd
Show file tree
Hide file tree
Showing 52 changed files with 3,920 additions and 3,158 deletions.
41 changes: 10 additions & 31 deletions src/main/java/org/opensearch/security/DefaultObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ public static boolean getOrDefault(Map<String, Object> properties, String key, b
);
}

@SuppressWarnings("unchecked")
public static <T> T getOrDefault(Map<String, Object> properties, String key, T defaultValue) {
T value = (T) properties.get(key);
return value != null ? value : defaultValue;
}

@SuppressWarnings("removal")
public static <T> T readTree(JsonNode node, Class<T> clazz) throws IOException {

final SecurityManager sm = System.getSecurityManager();
Expand All @@ -117,12 +117,7 @@ public static <T> T readTree(JsonNode node, Class<T> clazz) throws IOException {
}

try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<T>() {
@Override
public T run() throws Exception {
return objectMapper.treeToValue(node, clazz);
}
});
return AccessController.doPrivileged((PrivilegedExceptionAction<T>) () -> objectMapper.treeToValue(node, clazz));
} catch (final PrivilegedActionException e) {
throw (IOException) e.getCause();
}
Expand All @@ -138,12 +133,7 @@ public static <T> T readValue(String string, Class<T> clazz) throws IOException
}

try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<T>() {
@Override
public T run() throws Exception {
return objectMapper.readValue(string, clazz);
}
});
return AccessController.doPrivileged((PrivilegedExceptionAction<T>) () -> objectMapper.readValue(string, clazz));
} catch (final PrivilegedActionException e) {
throw (IOException) e.getCause();
}
Expand All @@ -159,12 +149,7 @@ public static JsonNode readTree(String string) throws IOException {
}

try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<JsonNode>() {
@Override
public JsonNode run() throws Exception {
return objectMapper.readTree(string);
}
});
return AccessController.doPrivileged((PrivilegedExceptionAction<JsonNode>) () -> objectMapper.readTree(string));
} catch (final PrivilegedActionException e) {
throw (IOException) e.getCause();
}
Expand All @@ -180,12 +165,11 @@ public static String writeValueAsString(Object value, boolean omitDefaults) thro
}

try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<String>() {
@Override
public String run() throws Exception {
return (omitDefaults ? defaulOmittingObjectMapper : objectMapper).writeValueAsString(value);
}
});
return AccessController.doPrivileged(
(PrivilegedExceptionAction<String>) () -> (omitDefaults ? defaulOmittingObjectMapper : objectMapper).writeValueAsString(
value
)
);
} catch (final PrivilegedActionException e) {
throw (JsonProcessingException) e.getCause();
}
Expand Down Expand Up @@ -224,12 +208,7 @@ public static <T> T readValue(String string, JavaType jt) throws IOException {
}

try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<T>() {
@Override
public T run() throws Exception {
return objectMapper.readValue(string, jt);
}
});
return AccessController.doPrivileged((PrivilegedExceptionAction<T>) () -> objectMapper.readValue(string, jt));
} catch (final PrivilegedActionException e) {
throw (IOException) e.getCause();
}
Expand Down

Large diffs are not rendered by default.

Loading

0 comments on commit 0338cdd

Please sign in to comment.