chamikaramj commented on code in PR #22833: URL: https://github.com/apache/beam/pull/22833#discussion_r959108560
########## sdks/java/extensions/google-cloud-platform-core/src/test/java/org/apache/beam/sdk/extensions/gcp/util/TransportTest.java: ########## @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.sdk.extensions.gcp.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import com.google.api.client.http.HttpTransport; +import com.google.api.client.http.LowLevelHttpRequest; +import java.io.IOException; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Test case for {@link Transport}. */ +@RunWith(JUnit4.class) +public class TransportTest { + + private static class MockHttpTransport extends HttpTransport { + + @Override + protected LowLevelHttpRequest buildRequest(String method, String url) throws IOException { + return null; + } + } + + @Test + public void testGetSetTransport() { + HttpTransport defaultTransport = Transport.getTransport(); + try { + Transport.setHttpTransport(new MockHttpTransport()); Review Comment: Just noting that this changes the transport globally so probably good to monitor pre-commit test suite for any related flakes for other tests that are run parallelly (I believe Gradle does this). ########## sdks/java/extensions/google-cloud-platform-core/src/main/java/org/apache/beam/sdk/extensions/gcp/util/Transport.java: ########## @@ -34,29 +34,64 @@ import org.apache.beam.sdk.extensions.gcp.auth.NullCredentialInitializer; import org.apache.beam.sdk.extensions.gcp.options.GcsOptions; import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList; +import org.apache.http.annotation.Experimental; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** Helpers for cloud communication. */ public class Transport { + private static final Logger LOG = LoggerFactory.getLogger(Transport.class); + + private static @Nullable HttpTransport httpTransport; + private static class SingletonHelper { /** Global instance of the JSON factory. */ private static final JsonFactory JSON_FACTORY; /** Global instance of the HTTP transport. */ - private static final HttpTransport HTTP_TRANSPORT; + private static final HttpTransport DEFAULT_HTTP_TRANSPORT; static { try { JSON_FACTORY = JacksonFactory.getDefaultInstance(); - HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + DEFAULT_HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); } catch (GeneralSecurityException | IOException e) { throw new RuntimeException(e); } } } - public static HttpTransport getTransport() { - return SingletonHelper.HTTP_TRANSPORT; + /** + * Get previously set instance of {@link HttpTransport}. If not set, returns the default global + * instance constructed from {@link GoogleNetHttpTransport#newTrustedTransport()}. + */ + public static synchronized HttpTransport getTransport() { + if (httpTransport == null) { + httpTransport = SingletonHelper.DEFAULT_HTTP_TRANSPORT; + } + return httpTransport; + } + + /** + * Set a customized instance of {@link HttpTransport} with trust store and mTLS settings. Refer to + * https://github.com/googleapis/google-auth-library-java#configuring-a-proxy for the construction + * of customized instance of HttpTransport. Reset to default HttpTransport by passing null to this + * method. + */ + @Experimental + public static synchronized void setHttpTransport(HttpTransport transport) { + // TDOO(https://github.com/apache/beam/issues/22504) Support customized HttpTransport through + // pipeline options then remove this experimental method. Review Comment: Is it possible to introduce a single pipeline option that sets the full transport object (instead of getting into details about customizing the transport) ? -- 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]
