| /* |
| * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0, which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * This Source Code may also be made available under the following Secondary |
| * Licenses when the conditions for such availability set forth in the |
| * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| * version 2 with the GNU Classpath Exception, which is available at |
| * https://www.gnu.org/software/classpath/license.html. |
| * |
| * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| */ |
| |
| package org.glassfish.jersey.jetty.connector; |
| |
| import javax.ws.rs.client.Client; |
| import javax.ws.rs.core.Configurable; |
| import javax.ws.rs.core.Configuration; |
| |
| import org.glassfish.jersey.client.Initializable; |
| import org.glassfish.jersey.client.spi.Connector; |
| import org.glassfish.jersey.client.spi.ConnectorProvider; |
| |
| import org.eclipse.jetty.client.HttpClient; |
| |
| /** |
| * A {@link ConnectorProvider} for Jersey {@link Connector connector} |
| * instances that utilize the Jetty HTTP Client to send and receive |
| * HTTP request and responses. |
| * <p> |
| * The following connector configuration properties are supported: |
| * <ul> |
| * <li>{@link org.glassfish.jersey.client.ClientProperties#ASYNC_THREADPOOL_SIZE}</li> |
| * <li>{@link org.glassfish.jersey.client.ClientProperties#CONNECT_TIMEOUT}</li> |
| * <li>{@link org.glassfish.jersey.client.ClientProperties#FOLLOW_REDIRECTS}</li> |
| * <li>{@link org.glassfish.jersey.client.ClientProperties#PROXY_URI}</li> |
| * <li>{@link org.glassfish.jersey.client.ClientProperties#PROXY_USERNAME}</li> |
| * <li>{@link org.glassfish.jersey.client.ClientProperties#PROXY_PASSWORD}</li> |
| * <li>{@link org.glassfish.jersey.client.ClientProperties#PROXY_PASSWORD}</li> |
| * <li>{@link JettyClientProperties#PREEMPTIVE_BASIC_AUTHENTICATION}</li> |
| * <li>{@link JettyClientProperties#DISABLE_COOKIES}</li> |
| * </ul> |
| * </p> |
| * <p> |
| * This transport supports both synchronous and asynchronous processing of client requests. |
| * The following methods are supported: GET, POST, PUT, DELETE, HEAD, OPTIONS, TRACE, CONNECT and MOVE. |
| * </p> |
| * <p> |
| * Typical usage: |
| * </p> |
| * <pre> |
| * {@code |
| * ClientConfig config = new ClientConfig(); |
| * config.connectorProvider(new JettyConnectorProvider()); |
| * Client client = ClientBuilder.newClient(config); |
| * |
| * // async request |
| * WebTarget target = client.target("http://localhost:8080"); |
| * Future<Response> future = target.path("resource").request().async().get(); |
| * |
| * // wait for 3 seconds |
| * Response response = future.get(3, TimeUnit.SECONDS); |
| * String entity = response.readEntity(String.class); |
| * client.close(); |
| * } |
| * </pre> |
| * <p> |
| * Connector instances created via Jetty HTTP Client-based connector provider support only |
| * {@link org.glassfish.jersey.client.RequestEntityProcessing#BUFFERED entity buffering}. |
| * Defining the property {@link org.glassfish.jersey.client.ClientProperties#REQUEST_ENTITY_PROCESSING} has no |
| * effect on Jetty HTTP Client-based connectors. |
| * </p> |
| * |
| * @author Arul Dhesiaseelan (aruld at acm.org) |
| * @author Marek Potociar |
| * @since 2.5 |
| */ |
| public class JettyConnectorProvider implements ConnectorProvider { |
| |
| @Override |
| public Connector getConnector(Client client, Configuration runtimeConfig) { |
| return new JettyConnector(client, runtimeConfig); |
| } |
| |
| /** |
| * Retrieve the underlying Jetty {@link org.eclipse.jetty.client.HttpClient} instance from |
| * {@link org.glassfish.jersey.client.JerseyClient} or {@link org.glassfish.jersey.client.JerseyWebTarget} |
| * configured to use {@code JettyConnectorProvider}. |
| * |
| * @param component {@code JerseyClient} or {@code JerseyWebTarget} instance that is configured to use |
| * {@code JettyConnectorProvider}. |
| * @return underlying Jetty {@code HttpClient} instance. |
| * |
| * @throws java.lang.IllegalArgumentException in case the {@code component} is neither {@code JerseyClient} |
| * nor {@code JerseyWebTarget} instance or in case the component |
| * is not configured to use a {@code JettyConnectorProvider}. |
| * @since 2.8 |
| */ |
| public static HttpClient getHttpClient(Configurable<?> component) { |
| if (!(component instanceof Initializable)) { |
| throw new IllegalArgumentException( |
| LocalizationMessages.INVALID_CONFIGURABLE_COMPONENT_TYPE(component.getClass().getName())); |
| } |
| |
| final Initializable<?> initializable = (Initializable<?>) component; |
| Connector connector = initializable.getConfiguration().getConnector(); |
| if (connector == null) { |
| initializable.preInitialize(); |
| connector = initializable.getConfiguration().getConnector(); |
| } |
| |
| if (connector instanceof JettyConnector) { |
| return ((JettyConnector) connector).getHttpClient(); |
| } |
| |
| throw new IllegalArgumentException(LocalizationMessages.EXPECTED_CONNECTOR_PROVIDER_NOT_USED()); |
| } |
| } |