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.jetty;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021import org.apache.juneau.rest.client.*;
022
023/**
024 * {@link HttpTransportProvider} implementation that supplies a {@link JettyHttpTransport}.
025 *
026 * <p>
027 * Registered via {@code META-INF/services/HttpTransportProvider} so that
028 * {@code RestClient} can auto-discover Jetty 12 when this module is on the classpath.
029 *
030 * <h5 class='section'>See Also:</h5><ul>
031 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/NextGenRestClient">Next-Generation REST Client</a>
032 * </ul>
033 *
034 * @since 9.2.1
035 */
036@SuppressWarnings({
037   "resource" // Provider methods return Closeables owned by the caller; Eclipse JDT @Owning warning is by design.
038})
039public final class JettyHttpTransportProvider implements HttpTransportProvider {
040
041   @Override /* HttpTransportProvider */
042   public int getPriority() {
043      return 75;
044   }
045
046   @Override /* HttpTransportProvider */
047   public boolean isAvailable() {
048      try {
049         Class.forName("org.eclipse.jetty.client.HttpClient");
050         return true;
051      } catch (ClassNotFoundException e) {
052         return false;
053      }
054   }
055
056   @Override /* HttpTransportProvider */
057   @SuppressWarnings({
058      "java:S112" // Re-wrap as RuntimeException - JettyHttpTransport.create() throws generic Exception from Jetty HttpClient startup; no specific type fits and the HttpTransportProvider.create() SPI method cannot declare checked exceptions.
059   })
060   public HttpTransport create() {
061      try {
062         return JettyHttpTransport.create();
063      } catch (Exception e) { // HTT: JettyHttpTransport.create() only fails if Jetty's HttpClient cannot start (thread/selector exhaustion); a seam (injectable factory) would make this testable
064         throw rex(e, "Failed to create JettyHttpTransport: %s", e.getMessage()); // HTT: see the catch above
065      }
066   }
067}