kenhuuu commented on code in PR #2799:
URL: https://github.com/apache/tinkerpop/pull/2799#discussion_r1792771987
##########
gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Cluster.java:
##########
@@ -732,16 +739,82 @@ public Builder loadBalancingStrategy(final
LoadBalancingStrategy loadBalancingSt
}
/**
- * Specifies an {@link RequestInterceptor} that will allow
manipulation of the {@code FullHttpRequest} prior
- * to its being sent to the server. For websockets the interceptor is
only called on the handshake.
+ * Adds a {@link RequestInterceptor} after another one that will allow
manipulation of the {@code HttpRequest}
+ * prior to its being sent to the server.
*/
- public Builder requestInterceptor(final RequestInterceptor
interceptor) {
- interceptors.add(interceptor);
+ public Builder addInterceptorAfter(final String priorInterceptorName,
final String nameOfInterceptor,
+ final RequestInterceptor
interceptor) {
+ final int index = getInterceptorIndex(priorInterceptorName);
+ if (INTERCEPTOR_NOT_FOUND == index) {
+ throw new IllegalArgumentException(priorInterceptorName + "
interceptor not found");
+ } else if (getInterceptorIndex(nameOfInterceptor) !=
INTERCEPTOR_NOT_FOUND) {
+ throw new IllegalArgumentException(nameOfInterceptor + "
interceptor already exists");
+ }
+ interceptors.add(index + 1, Pair.of(nameOfInterceptor,
interceptor));
+
return this;
}
+ /**
+ * Adds a {@link RequestInterceptor} before another one that will
allow manipulation of the {@code HttpRequest}
+ * prior to its being sent to the server.
+ */
+ public Builder addInterceptorBefore(final String
subsequentInterceptorName, final String nameOfInterceptor,
+ final RequestInterceptor
interceptor) {
+ final int index = getInterceptorIndex(subsequentInterceptorName);
+ if (INTERCEPTOR_NOT_FOUND == index) {
+ throw new IllegalArgumentException(subsequentInterceptorName +
" interceptor not found");
+ } else if (getInterceptorIndex(nameOfInterceptor) !=
INTERCEPTOR_NOT_FOUND) {
+ throw new IllegalArgumentException(nameOfInterceptor + "
interceptor already exists");
+ } else if (index == 0) {
+ interceptors.addFirst(Pair.of(nameOfInterceptor, interceptor));
+ } else {
+ interceptors.add(index - 1, Pair.of(nameOfInterceptor,
interceptor));
+ }
+
+ return this;
+ }
+
+ /**
+ * Adds a {@link RequestInterceptor} to the end of the list that will
allow manipulation of the
+ * {@code HttpRequest} prior to its being sent to the server.
+ */
+ public Builder addInterceptor(final String name, final
RequestInterceptor interceptor) {
+ if (getInterceptorIndex(name) != INTERCEPTOR_NOT_FOUND) {
+ throw new IllegalArgumentException(name + " interceptor
already exists");
+ }
+ interceptors.add(Pair.of(name, interceptor));
+ return this;
+ }
+
+ /**
+ * Removes a {@link RequestInterceptor} from the list. This can be
used to remove the default interceptors that
+ * aren't needed.
+ */
+ public Builder removeInterceptor(final String name) {
+ final int index = getInterceptorIndex(name);
+ if (index == INTERCEPTOR_NOT_FOUND) {
+ throw new IllegalArgumentException(name + " interceptor not
found");
+ }
+ interceptors.remove(index);
+ return this;
+ }
+
+ private int getInterceptorIndex(final String name) {
+ for (int i = 0; i < interceptors.size(); i++) {
+ if (interceptors.get(i).getLeft().equals(name)) {
+ return i;
+ }
+ }
+
+ return INTERCEPTOR_NOT_FOUND;
+ }
+
+ /**
+ * Adds an Auth {@link RequestInterceptor} to the end of list of
interceptors.
+ */
public Builder auth(final Auth auth) {
- interceptors.add(auth);
+
interceptors.add(Pair.of(auth.getClass().getSimpleName().toLowerCase() +
"-auth", auth));
Review Comment:
Yes, that is the intended usage. Essentially, if you are going to use the
interceptors, you'll have to become intimately aware of exactly what other
interceptors are used and how they interact with one another.
The driver's default interceptors, however, should be able to be used
without the user understanding the ordering of interceptors. For example, if
you are using basic auth, it should just work as the auth() method should know
where to place that interceptor in relation to the default "serializer"
interceptor.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]