diff options
author | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2020-12-01 17:06:42 +0100 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2020-12-01 17:49:22 +0100 |
commit | 0b8d21fe0c915057e6c88cab1b2e988d60c5a16f (patch) | |
tree | 3f03f81c21ba25065b1b59e501b2520d65bab98e | |
parent | f2599280846073b144c4065bf25478138cc38b67 (diff) |
Add helper method to rethrow checked as unchecked
7 files changed, 34 insertions, 40 deletions
diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/rpc/security/MultiTenantRpcAuthorizer.java b/configserver/src/main/java/com/yahoo/vespa/config/server/rpc/security/MultiTenantRpcAuthorizer.java index 54da80a0299..49a8df3d0e4 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/rpc/security/MultiTenantRpcAuthorizer.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/rpc/security/MultiTenantRpcAuthorizer.java @@ -11,7 +11,6 @@ import com.yahoo.config.provision.security.NodeIdentifierException; import com.yahoo.config.provision.security.NodeIdentity; import com.yahoo.jrt.Request; import com.yahoo.jrt.SecurityContext; -import java.util.logging.Level; import com.yahoo.security.tls.MixedMode; import com.yahoo.security.tls.TransportSecurityUtils; import com.yahoo.vespa.config.ConfigKey; @@ -29,9 +28,11 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.function.BiConsumer; +import java.util.logging.Level; import java.util.logging.Logger; -import static com.yahoo.vespa.config.server.rpc.security.AuthorizationException.*; +import static com.yahoo.vespa.config.server.rpc.security.AuthorizationException.Type; +import static com.yahoo.yolean.Exceptions.throwUnchecked; /** @@ -206,11 +207,6 @@ public class MultiTenantRpcAuthorizer implements RpcAuthorizer { .orElseThrow(() -> new AuthorizationException(String.format("No handler exists for tenant '%s'", tenantName.value()))); } - @SuppressWarnings("unchecked") - private static <T extends Throwable> void throwUnchecked(Throwable t) throws T { - throw (T)t; - } - private enum JrtErrorCode { UNAUTHORIZED(1), AUTHORIZATION_FAILED(2); diff --git a/jdisc_http_service/pom.xml b/jdisc_http_service/pom.xml index 7333db96b91..094ca7baa25 100644 --- a/jdisc_http_service/pom.xml +++ b/jdisc_http_service/pom.xml @@ -78,6 +78,12 @@ <version>${project.version}</version> <scope>provided</scope> </dependency> + <dependency> + <groupId>com.yahoo.vespa</groupId> + <artifactId>yolean</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> <!-- TEST SCOPE --> <dependency> diff --git a/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/server/jetty/Exceptions.java b/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/server/jetty/Exceptions.java deleted file mode 100644 index 0806f352ae9..00000000000 --- a/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/server/jetty/Exceptions.java +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.jdisc.http.server.jetty; - -/** - * Utility methods for exceptions - * - * @author Tony Vaagenes - */ -public class Exceptions { - - /** - * Allows treating checked exceptions as unchecked. - * Usage: - * throw throwUnchecked(e); - * The reason for the return type is to allow writing throw at the call site - * instead of just calling throwUnchecked. Just calling throwUnchecked - * means that the java compiler won't know that the statement will throw an exception, - * and will therefore complain on things such e.g. missing return value. - */ - public static RuntimeException throwUnchecked(Throwable e) { - throwUncheckedImpl(e); - return null; - } - - @SuppressWarnings("unchecked") - private static <T extends Throwable> void throwUncheckedImpl(Throwable t) throws T { - throw (T)t; - } - -} diff --git a/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/server/jetty/HttpRequestDispatch.java b/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/server/jetty/HttpRequestDispatch.java index 940009e7520..84c47f5a342 100644 --- a/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/server/jetty/HttpRequestDispatch.java +++ b/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/server/jetty/HttpRequestDispatch.java @@ -35,8 +35,8 @@ import java.util.logging.Logger; import static com.yahoo.jdisc.http.HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED; import static com.yahoo.jdisc.http.core.HttpServletRequestUtils.getConnection; -import static com.yahoo.jdisc.http.server.jetty.Exceptions.throwUnchecked; import static com.yahoo.jdisc.http.server.jetty.JDiscHttpServlet.getConnector; +import static com.yahoo.yolean.Exceptions.throwUnchecked; /** * @author Simon Thoresen Hult diff --git a/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/server/jetty/JDiscFilterInvokerFilter.java b/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/server/jetty/JDiscFilterInvokerFilter.java index e4dbccf1bcb..a89c115a1c2 100644 --- a/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/server/jetty/JDiscFilterInvokerFilter.java +++ b/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/server/jetty/JDiscFilterInvokerFilter.java @@ -26,8 +26,8 @@ import java.util.Map; import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; -import static com.yahoo.jdisc.http.server.jetty.Exceptions.throwUnchecked; import static com.yahoo.jdisc.http.server.jetty.JDiscHttpServlet.getConnector; +import static com.yahoo.yolean.Exceptions.throwUnchecked; /** * Runs JDisc security filters for Servlets diff --git a/yolean/abi-spec.json b/yolean/abi-spec.json index 4b68b2527b8..82bf59ebf87 100644 --- a/yolean/abi-spec.json +++ b/yolean/abi-spec.json @@ -40,7 +40,8 @@ "public static void uncheckAndIgnore(com.yahoo.yolean.Exceptions$RunnableThrowingIOException, java.lang.Class)", "public static java.lang.Object uncheck(com.yahoo.yolean.Exceptions$SupplierThrowingIOException)", "public static varargs java.lang.Object uncheck(com.yahoo.yolean.Exceptions$SupplierThrowingIOException, java.lang.String, java.lang.String[])", - "public static java.lang.Object uncheckAndIgnore(com.yahoo.yolean.Exceptions$SupplierThrowingIOException, java.lang.Class)" + "public static java.lang.Object uncheckAndIgnore(com.yahoo.yolean.Exceptions$SupplierThrowingIOException, java.lang.Class)", + "public static java.lang.RuntimeException throwUnchecked(java.lang.Throwable)" ], "fields": [] }, diff --git a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java index 063ba70c75d..c377ee3ac37 100644 --- a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java +++ b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java @@ -160,4 +160,25 @@ public class Exceptions { public interface SupplierThrowingIOException<T> { T get() throws IOException; } + + /** + * Allows treating checked exceptions as unchecked. + * Usage: + * throw throwUnchecked(e); + * The reason for the return type is to allow writing throw at the call site + * instead of just calling throwUnchecked. Just calling throwUnchecked + * means that the java compiler won't know that the statement will throw an exception, + * and will therefore complain on things such e.g. missing return value. + */ + public static RuntimeException throwUnchecked(Throwable e) { + throwUncheckedImpl(e); + return new RuntimeException(); // Non-null return value to stop tooling from complaining about potential NPE + } + + @SuppressWarnings("unchecked") + private static <T extends Throwable> void throwUncheckedImpl(Throwable t) throws T { + throw (T)t; + } + + } |