001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.rest.client;
018
019/**
020 * {@link java.util.ServiceLoader} SPI for auto-discovering {@link HttpTransport} implementations on the classpath.
021 *
022 * <p>
023 * Transport adapter modules (e.g. {@code juneau-rest-client-apache-httpclient-45}) register their provider
024 * via {@code META-INF/services/HttpTransportProvider}.
025 *
026 * <p>
027 * When no transport is explicitly set, {@code RestClient} uses {@link java.util.ServiceLoader} to load all
028 * available providers and selects the first one whose {@link #isAvailable()} returns {@code true}.
029 *
030 * @since 9.2.1
031 */
032public interface HttpTransportProvider {
033
034   /**
035    * Returns the priority of this provider.
036    *
037    * <p>
038    * When multiple providers are available, the one with the <b>lowest</b> priority value is used first.
039    * This allows applications to install a high-priority mock or test transport that takes precedence
040    * over real transports.
041    *
042    * @return The priority. Defaults to {@code 100}.
043    */
044   default int getPriority() {
045      return 100;
046   }
047
048   /**
049    * Returns {@code true} if the required runtime dependencies of this transport are present on the classpath.
050    *
051    * <p>
052    * Providers must check for their dependencies here (e.g. by attempting to load the underlying client class)
053    * so that {@code RestClient} can safely skip providers whose jars are absent.
054    *
055    * @return {@code true} if available.
056    */
057   boolean isAvailable();
058
059   /**
060    * Creates and returns a new {@link HttpTransport} instance using default configuration.
061    *
062    * @return A new transport. Never <jk>null</jk>.
063    */
064   HttpTransport create();
065}